|
| 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 | +} |
0 commit comments