Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit d9d45b4

Browse files
author
David Ansermino
authored
CSDB Keeper (#76)
- Implements Keeper. This is a wrapper around the "real" keeper (CSDB). Since we need to pass the context into the keeper we need to abstract the CSDB from the - Adds WithContext() to CSDB to support the above requirement - Adds Keeper to app
1 parent 284c2a0 commit d9d45b4

File tree

5 files changed

+278
-21
lines changed

5 files changed

+278
-21
lines changed

app/ethermint.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package app
22

33
import (
44
"encoding/json"
5+
"github.com/cosmos/ethermint/x/evm"
56
"os"
67

78
bam "github.com/cosmos/cosmos-sdk/baseapp"
@@ -58,6 +59,8 @@ var (
5859
crisis.AppModuleBasic{},
5960
slashing.AppModuleBasic{},
6061
supply.AppModuleBasic{},
62+
// TODO: Enable EVM AppModuleBasic
63+
//evm.AppModuleBasic{},
6164
)
6265
)
6366

@@ -96,7 +99,8 @@ type EthermintApp struct {
9699
keyGov *sdk.KVStoreKey
97100
keyParams *sdk.KVStoreKey
98101
tkeyParams *sdk.TransientStoreKey
99-
// TODO: Add evm module key
102+
evmStoreKey *sdk.KVStoreKey
103+
evmCodeKey *sdk.KVStoreKey
100104

101105
// keepers
102106
accountKeeper auth.AccountKeeper
@@ -109,7 +113,7 @@ type EthermintApp struct {
109113
govKeeper gov.Keeper
110114
crisisKeeper crisis.Keeper
111115
paramsKeeper params.Keeper
112-
// TODO: Include evm Keeper
116+
evmKeeper evm.Keeper
113117

114118
// the module manager
115119
mm *module.Manager
@@ -144,7 +148,8 @@ func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
144148
keyGov: sdk.NewKVStoreKey(gov.StoreKey),
145149
keyParams: sdk.NewKVStoreKey(params.StoreKey),
146150
tkeyParams: sdk.NewTransientStoreKey(params.TStoreKey),
147-
// TODO: Initialize evm module key
151+
evmStoreKey: sdk.NewKVStoreKey(evmtypes.EvmStoreKey),
152+
evmCodeKey: sdk.NewKVStoreKey(evmtypes.EvmCodeKey),
148153
}
149154

150155
// init params keeper and subspaces
@@ -180,7 +185,7 @@ func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
180185
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, &stakingKeeper,
181186
slashingSubspace, slashing.DefaultCodespace)
182187
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
183-
// TODO: Instantiate evm Keeper
188+
app.evmKeeper = evm.NewKeeper(app.accountKeeper, app.evmStoreKey, app.evmCodeKey)
184189

185190
// register the proposal types
186191
govRouter := gov.NewRouter()
@@ -220,15 +225,15 @@ func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
220225
// initialized with tokens from genesis accounts.
221226
app.mm.SetOrderInitGenesis(genaccounts.ModuleName, supply.ModuleName, distr.ModuleName,
222227
staking.ModuleName, auth.ModuleName, bank.ModuleName, slashing.ModuleName,
223-
gov.ModuleName, mint.ModuleName, crisis.ModuleName, genutil.ModuleName)
228+
gov.ModuleName, mint.ModuleName, crisis.ModuleName, genutil.ModuleName, evmtypes.ModuleName)
224229

225230
app.mm.RegisterInvariants(&app.crisisKeeper)
226231
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
227232

228233
// initialize stores
229234
app.MountStores(app.keyMain, app.keyAccount, app.keySupply, app.keyStaking,
230235
app.keyMint, app.keyDistr, app.keySlashing, app.keyGov, app.keyParams,
231-
app.tkeyParams, app.tkeyStaking, app.tkeyDistr)
236+
app.tkeyParams, app.tkeyStaking, app.tkeyDistr, app.evmStoreKey, app.evmCodeKey)
232237

233238
// initialize BaseApp
234239
app.SetInitChainer(app.InitChainer)

importer/importer_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ var (
5050
// paramsKey = sdk.NewKVStoreKey("params")
5151
// tParamsKey = sdk.NewTransientStoreKey("transient_params")
5252
accKey = sdk.NewKVStoreKey("acc")
53-
storageKey = sdk.NewKVStoreKey("storage")
54-
codeKey = sdk.NewKVStoreKey("code")
53+
storageKey = sdk.NewKVStoreKey(evmtypes.EvmStoreKey)
54+
codeKey = sdk.NewKVStoreKey(evmtypes.EvmCodeKey)
5555

5656
logger = tmlog.NewNopLogger()
5757

@@ -103,8 +103,7 @@ func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak auth.Accoun
103103
ms := cms.CacheMultiStore()
104104
ctx := sdk.NewContext(ms, abci.Header{}, false, logger)
105105

106-
stateDB, err := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
107-
require.NoError(t, err, "failed to create a StateDB instance")
106+
stateDB := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
108107

109108
// sort the addresses and insertion of key/value pairs matters
110109
genAddrs := make([]string, len(genBlock.Alloc))
@@ -136,7 +135,7 @@ func createAndTestGenesis(t *testing.T, cms sdk.CommitMultiStore, ak auth.Accoun
136135
// commit the stateDB with 'false' to delete empty objects
137136
//
138137
// NOTE: Commit does not yet return the intra merkle root (version)
139-
_, err = stateDB.Commit(false)
138+
_, err := stateDB.Commit(false)
140139
require.NoError(t, err)
141140

142141
// persist multi-store cache state
@@ -269,9 +268,7 @@ func TestImportBlocks(t *testing.T) {
269268
}
270269

271270
func createStateDB(t *testing.T, ctx sdk.Context, ak auth.AccountKeeper) *evmtypes.CommitStateDB {
272-
stateDB, err := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
273-
require.NoError(t, err, "failed to create a StateDB instance")
274-
271+
stateDB := evmtypes.NewCommitStateDB(ctx, ak, storageKey, codeKey)
275272
return stateDB
276273
}
277274

@@ -309,7 +306,7 @@ func accumulateRewards(
309306
// ApplyDAOHardFork modifies the state database according to the DAO hard-fork
310307
// rules, transferring all balances of a set of DAO accounts to a single refund
311308
// contract.
312-
// Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
309+
// Code is pulled from go-ethereum 1.9 because the StateDB interface does not include the
313310
// SetBalance function implementation
314311
// Ref: https://github.com/ethereum/go-ethereum/blob/52f2461774bcb8cdd310f86b4bc501df5b783852/consensus/misc/dao.go#L74
315312
func applyDAOHardFork(statedb *evmtypes.CommitStateDB) {

x/evm/keeper.go

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
package evm
2+
3+
import (
4+
ethcmn "github.com/ethereum/go-ethereum/common"
5+
ethvm "github.com/ethereum/go-ethereum/core/vm"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/cosmos/cosmos-sdk/x/auth"
9+
types "github.com/cosmos/ethermint/x/evm/types"
10+
ethstate "github.com/ethereum/go-ethereum/core/state"
11+
ethtypes "github.com/ethereum/go-ethereum/core/types"
12+
13+
"math/big"
14+
)
15+
16+
// Keeper wraps the CommitStateDB, allowing us to pass in SDK context while adhering
17+
// to the StateDB interface
18+
type Keeper struct {
19+
csdb *types.CommitStateDB
20+
}
21+
22+
func NewKeeper(ak auth.AccountKeeper, storageKey, codeKey sdk.StoreKey) Keeper {
23+
return Keeper{
24+
csdb: types.NewCommitStateDB(sdk.Context{}, ak, storageKey, codeKey),
25+
}
26+
}
27+
28+
// ----------------------------------------------------------------------------
29+
// Setters
30+
// ----------------------------------------------------------------------------
31+
32+
// Calls CommitStateDB.SetBalance using the passed in context
33+
func (k *Keeper) SetBalance(ctx sdk.Context, addr ethcmn.Address, amount *big.Int) {
34+
k.csdb.WithContext(ctx).SetBalance(addr, amount)
35+
}
36+
37+
// Calls CommitStateDB.AddBalance using the passed in context
38+
func (k *Keeper) AddBalance(ctx sdk.Context, addr ethcmn.Address, amount *big.Int) {
39+
k.csdb.WithContext(ctx).AddBalance(addr, amount)
40+
}
41+
42+
// Calls CommitStateDB.SubBalance using the passed in context
43+
func (k *Keeper) SubBalance(ctx sdk.Context, addr ethcmn.Address, amount *big.Int) {
44+
k.csdb.WithContext(ctx).SubBalance(addr, amount)
45+
}
46+
47+
// Calls CommitStateDB.SetNonce using the passed in context
48+
func (k *Keeper) SetNonce(ctx sdk.Context, addr ethcmn.Address, nonce uint64) {
49+
k.csdb.WithContext(ctx).SetNonce(addr, nonce)
50+
}
51+
52+
// Calls CommitStateDB.SetState using the passed in context
53+
func (k *Keeper) SetState(ctx sdk.Context, addr ethcmn.Address, key, value ethcmn.Hash) {
54+
k.csdb.WithContext(ctx).SetState(addr, key, value)
55+
}
56+
57+
// Calls CommitStateDB.SetCode using the passed in context
58+
func (k *Keeper) SetCode(ctx sdk.Context, addr ethcmn.Address, code []byte) {
59+
k.csdb.WithContext(ctx).SetCode(addr, code)
60+
}
61+
62+
// Calls CommitStateDB.AddLog using the passed in context
63+
func (k *Keeper) AddLog(ctx sdk.Context, log *ethtypes.Log) {
64+
k.csdb.WithContext(ctx).AddLog(log)
65+
}
66+
67+
// Calls CommitStateDB.AddPreimage using the passed in context
68+
func (k *Keeper) AddPreimage(ctx sdk.Context, hash ethcmn.Hash, preimage []byte) {
69+
k.csdb.WithContext(ctx).AddPreimage(hash, preimage)
70+
}
71+
72+
// Calls CommitStateDB.AddRefund using the passed in context
73+
func (k *Keeper) AddRefund(ctx sdk.Context, gas uint64) {
74+
k.csdb.WithContext(ctx).AddRefund(gas)
75+
}
76+
77+
// Calls CommitStateDB.SubRefund using the passed in context
78+
func (k *Keeper) SubRefund(ctx sdk.Context, gas uint64) {
79+
k.csdb.WithContext(ctx).SubRefund(gas)
80+
}
81+
82+
// ----------------------------------------------------------------------------
83+
// Getters
84+
// ----------------------------------------------------------------------------
85+
86+
// Calls CommitStateDB.GetBalance using the passed in context
87+
func (k *Keeper) GetBalance(ctx sdk.Context, addr ethcmn.Address) *big.Int {
88+
return k.csdb.WithContext(ctx).GetBalance(addr)
89+
}
90+
91+
// Calls CommitStateDB.GetNonce using the passed in context
92+
func (k *Keeper) GetNonce(ctx sdk.Context, addr ethcmn.Address) uint64 {
93+
return k.csdb.WithContext(ctx).GetNonce(addr)
94+
}
95+
96+
// Calls CommitStateDB.TxIndex using the passed in context
97+
func (k *Keeper) TxIndex(ctx sdk.Context) int {
98+
return k.csdb.WithContext(ctx).TxIndex()
99+
}
100+
101+
// Calls CommitStateDB.BlockHash using the passed in context
102+
func (k *Keeper) BlockHash(ctx sdk.Context) ethcmn.Hash {
103+
return k.csdb.WithContext(ctx).BlockHash()
104+
}
105+
106+
// Calls CommitStateDB.GetCode using the passed in context
107+
func (k *Keeper) GetCode(ctx sdk.Context, addr ethcmn.Address) []byte {
108+
return k.csdb.WithContext(ctx).GetCode(addr)
109+
}
110+
111+
// Calls CommitStateDB.GetCodeSize using the passed in context
112+
func (k *Keeper) GetCodeSize(ctx sdk.Context, addr ethcmn.Address) int {
113+
return k.csdb.WithContext(ctx).GetCodeSize(addr)
114+
}
115+
116+
// Calls CommitStateDB.GetCodeHash using the passed in context
117+
func (k *Keeper) GetCodeHash(ctx sdk.Context, addr ethcmn.Address) ethcmn.Hash {
118+
return k.csdb.WithContext(ctx).GetCodeHash(addr)
119+
}
120+
121+
// Calls CommitStateDB.GetState using the passed in context
122+
func (k *Keeper) GetState(ctx sdk.Context, addr ethcmn.Address, hash ethcmn.Hash) ethcmn.Hash {
123+
return k.csdb.WithContext(ctx).GetState(addr, hash)
124+
}
125+
126+
// Calls CommitStateDB.GetCommittedState using the passed in context
127+
func (k *Keeper) GetCommittedState(ctx sdk.Context, addr ethcmn.Address, hash ethcmn.Hash) ethcmn.Hash {
128+
return k.csdb.WithContext(ctx).GetCommittedState(addr, hash)
129+
}
130+
131+
// Calls CommitStateDB.GetLogs using the passed in context
132+
func (k *Keeper) GetLogs(ctx sdk.Context, hash ethcmn.Hash) []*ethtypes.Log {
133+
return k.csdb.WithContext(ctx).GetLogs(hash)
134+
}
135+
136+
// Calls CommitStateDB.Logs using the passed in context
137+
func (k *Keeper) Logs(ctx sdk.Context) []*ethtypes.Log {
138+
return k.csdb.WithContext(ctx).Logs()
139+
}
140+
141+
// Calls CommitStateDB.GetRefund using the passed in context
142+
func (k *Keeper) GetRefund(ctx sdk.Context) uint64 {
143+
return k.csdb.WithContext(ctx).GetRefund()
144+
}
145+
146+
// Calls CommitStateDB.Preimages using the passed in context
147+
func (k *Keeper) Preimages(ctx sdk.Context) map[ethcmn.Hash][]byte {
148+
return k.csdb.WithContext(ctx).Preimages()
149+
}
150+
151+
// Calls CommitStateDB.HasSuicided using the passed in context
152+
func (k *Keeper) HasSuicided(ctx sdk.Context, addr ethcmn.Address) bool {
153+
return k.csdb.WithContext(ctx).HasSuicided(addr)
154+
}
155+
156+
// Calls CommitStateDB.StorageTrie using the passed in context
157+
func (k *Keeper) StorageTrie(ctx sdk.Context, addr ethcmn.Address) ethstate.Trie {
158+
return k.csdb.WithContext(ctx).StorageTrie(addr)
159+
}
160+
161+
// ----------------------------------------------------------------------------
162+
// Persistence
163+
// ----------------------------------------------------------------------------
164+
165+
// Calls CommitStateDB.Commit using the passed in context
166+
func (k *Keeper) Commit(ctx sdk.Context, deleteEmptyObjects bool) (root ethcmn.Hash, err error) {
167+
return k.csdb.WithContext(ctx).Commit(deleteEmptyObjects)
168+
}
169+
170+
// Calls CommitStateDB.Finalise using the passed in context
171+
func (k *Keeper) Finalise(ctx sdk.Context, deleteEmptyObjects bool) {
172+
k.csdb.WithContext(ctx).Finalise(deleteEmptyObjects)
173+
}
174+
175+
// Calls CommitStateDB.IntermediateRoot using the passed in context
176+
func (k *Keeper) IntermediateRoot(ctx sdk.Context, deleteEmptyObjects bool) {
177+
k.csdb.WithContext(ctx).IntermediateRoot(deleteEmptyObjects)
178+
}
179+
180+
// ----------------------------------------------------------------------------
181+
// Snapshotting
182+
// ----------------------------------------------------------------------------
183+
184+
// Calls CommitStateDB.Snapshot using the passed in context
185+
func (k *Keeper) Snapshot(ctx sdk.Context) int {
186+
return k.csdb.WithContext(ctx).Snapshot()
187+
}
188+
189+
// Calls CommitStateDB.RevertToSnapshot using the passed in context
190+
func (k *Keeper) RevertToSnapshot(ctx sdk.Context, revID int) {
191+
k.csdb.WithContext(ctx).RevertToSnapshot(revID)
192+
}
193+
194+
// ----------------------------------------------------------------------------
195+
// Auxiliary
196+
// ----------------------------------------------------------------------------
197+
198+
// Calls CommitStateDB.Database using the passed in context
199+
func (k *Keeper) Database(ctx sdk.Context) ethstate.Database {
200+
return k.csdb.WithContext(ctx).Database()
201+
}
202+
203+
// Calls CommitStateDB.Empty using the passed in context
204+
func (k *Keeper) Empty(ctx sdk.Context, addr ethcmn.Address) bool {
205+
return k.csdb.WithContext(ctx).Empty(addr)
206+
}
207+
208+
// Calls CommitStateDB.Exist using the passed in context
209+
func (k *Keeper) Exist(ctx sdk.Context, addr ethcmn.Address) bool {
210+
return k.csdb.WithContext(ctx).Exist(addr)
211+
}
212+
213+
// Calls CommitStateDB.Error using the passed in context
214+
func (k *Keeper) Error(ctx sdk.Context) error {
215+
return k.csdb.WithContext(ctx).Error()
216+
}
217+
218+
// Calls CommitStateDB.Suicide using the passed in context
219+
func (k *Keeper) Suicide(ctx sdk.Context, addr ethcmn.Address) bool {
220+
return k.csdb.WithContext(ctx).Suicide(addr)
221+
}
222+
223+
// Calls CommitStateDB.Reset using the passed in context
224+
func (k *Keeper) Reset(ctx sdk.Context, root ethcmn.Hash) error {
225+
return k.csdb.WithContext(ctx).Reset(root)
226+
}
227+
228+
// Calls CommitStateDB.Prepare using the passed in context
229+
func (k *Keeper) Prepare(ctx sdk.Context, thash, bhash ethcmn.Hash, txi int) {
230+
k.csdb.WithContext(ctx).Prepare(thash, bhash, txi)
231+
}
232+
233+
// Calls CommitStateDB.CreateAccount using the passed in context
234+
func (k *Keeper) CreateAccount(ctx sdk.Context, addr ethcmn.Address) {
235+
k.csdb.WithContext(ctx).CreateAccount(addr)
236+
}
237+
238+
// Calls CommitStateDB.Copy using the passed in context
239+
func (k *Keeper) Copy(ctx sdk.Context) ethvm.StateDB {
240+
return k.csdb.WithContext(ctx).Copy()
241+
}
242+
243+
// Calls CommitStateDB.ForEachStorage using passed in context
244+
func (k *Keeper) ForEachStorage(ctx sdk.Context, addr ethcmn.Address, cb func(key, value ethcmn.Hash) bool) error {
245+
return k.csdb.WithContext(ctx).ForEachStorage(addr, cb)
246+
}
247+
248+
// Calls CommitStateDB.GetOrNetStateObject using the passed in context
249+
func (k *Keeper) GetOrNewStateObject(ctx sdk.Context, addr ethcmn.Address) types.StateObject {
250+
return k.csdb.WithContext(ctx).GetOrNewStateObject(addr)
251+
}

x/evm/types/key.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const (
44
// module name
55
ModuleName = "ethermint"
66

7-
// TODO: Use this
8-
// StoreKey to be used when creating the KVStore
9-
StoreKey = ModuleName
10-
)
7+
EvmStoreKey = "evmstore"
8+
EvmCodeKey = "evmcode"
9+
)

0 commit comments

Comments
 (0)