-
Notifications
You must be signed in to change notification settings - Fork 75
/
keeper.go
458 lines (378 loc) · 14.8 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package keeper
import (
"fmt"
"math/big"
"github.com/artela-network/aspect-core/djpm"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/artela-network/artela/x/evm/artela/api"
"github.com/artela-network/artela/x/evm/artela/provider"
"github.com/artela-network/artela-evm/vm"
artela "github.com/artela-network/artela/ethereum/types"
"github.com/artela-network/artela/x/evm/states"
"github.com/artela-network/artela/x/evm/txs"
"github.com/artela-network/artela/x/evm/txs/support"
errorsmod "cosmossdk.io/errors"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
cosmos "github.com/cosmos/cosmos-sdk/types"
paramsmodule "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethereum "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
artelaType "github.com/artela-network/aspect-core/types"
artvmtype "github.com/artela-network/artela/x/evm/artela/types"
"github.com/artela-network/artela/x/evm/types"
)
// Keeper grants access to the EVM module states and implements the go-ethereum StateDB interface.
type Keeper struct {
// logger saves the logger instance of evm module
logger log.Logger
// protobuf codec
cdc codec.BinaryCodec
// store key required for the EVM Prefix KVStore. It is required by:
// - storing account's Storage State
// - storing account's Code
// - storing txs Logs
// - storing Bloom filters by block height. Needed for the Web3 API.
storeKey storetypes.StoreKey
// key to access the transient store, which is reset on every block during Commit
transientKey storetypes.StoreKey
// the address capable of executing a MsgUpdateParams message. Typically, this should be the x/gov module account.
authority cosmos.AccAddress
// access to account states
accountKeeper types.AccountKeeper
// update balance and accounting operations with coins
bankKeeper types.BankKeeper
// access historical headers for EVM states transition execution
stakingKeeper types.StakingKeeper
// fetch EIP1559 base fee and parameters
feeKeeper types.FeeKeeper
// chain ID number obtained from the context's chain id
eip155ChainID *big.Int
// tracer used to collect execution traces from the EVM txs execution
tracer string
// legacy subspace
ss paramsmodule.Subspace
// keep the evm and matched stateDB instance just finished running
aspectRuntimeContext *artvmtype.AspectRuntimeContext
aspect *provider.ArtelaProvider
clientContext client.Context
// store the block context, this will be fresh every block.
BlockContext *artvmtype.EthBlockContext
}
// NewKeeper generates new evm module keeper
func NewKeeper(
cdc codec.BinaryCodec,
storeKey, transientKey storetypes.StoreKey,
authority cosmos.AccAddress,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
stakingKeeper types.StakingKeeper,
feeKeeper types.FeeKeeper,
tracer string,
subSpace paramsmodule.Subspace,
app *baseapp.BaseApp,
logger log.Logger,
) *Keeper {
// ensure evm module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic("the EVM module account has not been set")
}
// ensure the authority account is correct
if err := cosmos.VerifyAddressFormat(authority); err != nil {
panic(err)
}
// init aspect
aspect := provider.NewArtelaProvider(storeKey, app.LastBlockHeight, logger)
// new Aspect Runtime Context
aspectRuntimeContext := artvmtype.NewAspectRuntimeContext()
aspectRuntimeContext.Init(storeKey)
// pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations
k := &Keeper{
logger: logger.With("module", fmt.Sprintf("x/%s", types.ModuleName)),
cdc: cdc,
authority: authority,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
feeKeeper: feeKeeper,
storeKey: storeKey,
transientKey: transientKey,
tracer: tracer,
ss: subSpace,
aspectRuntimeContext: aspectRuntimeContext,
aspect: aspect,
}
k.WithChainID(app.ChainId())
djpm.NewAspect(aspect, k.logger)
api.InitAspectGlobals(k)
// init aspect host api factory
artelaType.GetEvmHostHook = api.GetEvmHostInstance
artelaType.GetStateDbHook = api.GetStateDBHostInstance
artelaType.GetAspectRuntimeContextHostHook = api.GetAspectRuntimeContextHostInstance
artelaType.GetAspectStateHostHook = api.GetAspectStateHostInstance
artelaType.GetAspectPropertyHostHook = api.GetAspectPropertyHostInstance
artelaType.GetAspectTransientStorageHostHook = api.GetAspectTransientStorageHostInstance
artelaType.GetAspectTraceHostHook = api.GetAspectTraceHostInstance
artelaType.GetAspectContext = k.GetAspectContext
artelaType.SetAspectContext = k.SetAspectContext
artelaType.GetAspectPaymaster = aspect.GetAspectAccount
artelaType.JITSenderAspectByContext = k.JITSenderAspectByContext
artelaType.IsCommit = k.IsCommit
return k
}
func (k *Keeper) SetClientContext(ctx client.Context) {
k.clientContext = ctx
// k.aspectRuntimeContext.ExtBlockContext().WithBlockConfig(nil, nil, &ctx)
}
func (k Keeper) GetClientContext() client.Context {
return k.clientContext
}
// ----------------------------------------------------------------------------
// Config
// ----------------------------------------------------------------------------
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx cosmos.Context) log.Logger {
return ctx.Logger().With("module", types.ModuleName)
}
// WithChainID sets the chain id to the local variable in the keeper
func (k *Keeper) WithChainID(chainId string) {
if k.eip155ChainID != nil {
return
}
chainID, err := artela.ParseChainID(chainId)
if err != nil {
panic(err)
}
if k.eip155ChainID != nil && k.eip155ChainID.Cmp(chainID) != 0 {
panic("chain id already set")
}
k.eip155ChainID = chainID
}
// ChainID returns the EIP155 chain ID for the EVM context
func (k Keeper) ChainID() *big.Int {
return k.eip155ChainID
}
// GetAuthority returns the x/evm module authority address
func (k Keeper) GetAuthority() cosmos.AccAddress {
return k.authority
}
// ----------------------------------------------------------------------------
// Block Bloom
// Required by Web3 API
// ----------------------------------------------------------------------------
// EmitBlockBloomEvent emit block bloom events
func (k Keeper) EmitBlockBloomEvent(ctx cosmos.Context, bloom ethereum.Bloom) {
ctx.EventManager().EmitEvent(
cosmos.NewEvent(
types.EventTypeBlockBloom,
cosmos.NewAttribute(types.AttributeKeyEthereumBloom, string(bloom.Bytes())),
),
)
}
// GetBlockBloomTransient returns bloom bytes for the current block height
func (k Keeper) GetBlockBloomTransient(ctx cosmos.Context) *big.Int {
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom)
heightBz := cosmos.Uint64ToBigEndian(uint64(ctx.BlockHeight()))
bz := store.Get(heightBz)
if len(bz) == 0 {
return big.NewInt(0)
}
return new(big.Int).SetBytes(bz)
}
// SetBlockBloomTransient sets the given bloom bytes to the transient store. This value is reset on
// every block.
func (k Keeper) SetBlockBloomTransient(ctx cosmos.Context, bloom *big.Int) {
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientBloom)
heightBz := cosmos.Uint64ToBigEndian(uint64(ctx.BlockHeight()))
store.Set(heightBz, bloom.Bytes())
k.Logger(ctx).Debug(
fmt.Sprintf("setState: SetBlockBloomTransient"),
"block-height", fmt.Sprintf("%d", ctx.BlockHeight()),
"bloom", bloom.String(),
)
}
// ----------------------------------------------------------------------------
// Tx Index
// ----------------------------------------------------------------------------
// SetTxIndexTransient set the index of processing txs
func (k Keeper) SetTxIndexTransient(ctx cosmos.Context, index uint64) {
store := ctx.TransientStore(k.transientKey)
store.Set(types.KeyPrefixTransientTxIndex, cosmos.Uint64ToBigEndian(index))
k.Logger(ctx).Debug(
fmt.Sprintf("setState: SetTxIndexTransient"),
"key", "KeyPrefixTransientTxIndex",
"index", fmt.Sprintf("%d", index),
)
}
// GetTxIndexTransient returns EVM txs index on the current block.
func (k Keeper) GetTxIndexTransient(ctx cosmos.Context) uint64 {
store := ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientTxIndex)
if len(bz) == 0 {
return 0
}
return cosmos.BigEndianToUint64(bz)
}
// ----------------------------------------------------------------------------
// Log
// ----------------------------------------------------------------------------
// GetLogSizeTransient returns EVM log index on the current block.
func (k Keeper) GetLogSizeTransient(ctx cosmos.Context) uint64 {
store := ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientLogSize)
if len(bz) == 0 {
return 0
}
return cosmos.BigEndianToUint64(bz)
}
// SetLogSizeTransient fetches the current EVM log index from the transient store, increases its
// value by one and then sets the new index back to the transient store.
func (k Keeper) SetLogSizeTransient(ctx cosmos.Context, logSize uint64) {
store := ctx.TransientStore(k.transientKey)
store.Set(types.KeyPrefixTransientLogSize, cosmos.Uint64ToBigEndian(logSize))
k.Logger(ctx).Debug(
fmt.Sprintf("setState: SetLogSizeTransient"),
"key", "KeyPrefixTransientLogSize",
"logSize", fmt.Sprintf("%d", logSize),
)
}
// ----------------------------------------------------------------------------
// Storage
// ----------------------------------------------------------------------------
// GetAccountStorage return states storage associated with an account
func (k Keeper) GetAccountStorage(ctx cosmos.Context, address common.Address) support.Storage {
storage := support.Storage{}
k.ForEachStorage(ctx, address, func(key, value common.Hash) bool {
storage = append(storage, support.NewState(key, value))
return true
})
return storage
}
// ----------------------------------------------------------------------------
// Account
// ----------------------------------------------------------------------------
// GetAccountWithoutBalance load nonce and codeHash without balance,
// more efficient in cases where balance is not needed.
func (k *Keeper) GetAccountWithoutBalance(ctx cosmos.Context, addr common.Address) *states.StateAccount {
cosmosAddr := cosmos.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
if acct == nil {
return nil
}
codeHash := txs.EmptyCodeHash
ethAcct, ok := acct.(artela.EthAccountI)
if ok {
codeHash = ethAcct.GetCodeHash().Bytes()
}
return &states.StateAccount{
Nonce: acct.GetSequence(),
CodeHash: codeHash,
}
}
// GetAccountOrEmpty returns empty account if not exist, returns error if it's not `EthAccount`
func (k *Keeper) GetAccountOrEmpty(ctx cosmos.Context, addr common.Address) states.StateAccount {
acct := k.GetAccount(ctx, addr)
if acct != nil {
return *acct
}
// empty account
return states.StateAccount{
Balance: new(big.Int),
CodeHash: txs.EmptyCodeHash,
}
}
// GetNonce returns the sequence number of an account, returns 0 if not exists.
func (k *Keeper) GetNonce(ctx cosmos.Context, addr common.Address) uint64 {
cosmosAddr := cosmos.AccAddress(addr.Bytes())
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
if acct == nil {
return 0
}
return acct.GetSequence()
}
// GetBalance load account's balance of gas token
func (k *Keeper) GetBalance(ctx cosmos.Context, addr common.Address) *big.Int {
cosmosAddr := cosmos.AccAddress(addr.Bytes())
evmParams := k.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()
// if node is pruned, params is empty. Return invalid value
if evmDenom == "" {
return big.NewInt(-1)
}
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, evmDenom)
return coin.Amount.BigInt()
}
// ----------------------------------------------------------------------------
// Gas and Fee
// ----------------------------------------------------------------------------
// Tracer return a default vm.Tracer based on current keeper states
func (k Keeper) Tracer(ctx cosmos.Context, msg *core.Message, ethCfg *params.ChainConfig) vm.EVMLogger {
return txs.NewTracer(k.tracer, msg, ethCfg, ctx.BlockHeight())
}
// GetBaseFee returns current base fee, return values:
// - `nil`: london hardfork not enabled.
// - `0`: london hardfork enabled but fee is not enabled.
// - `n`: both london hardfork and fee are enabled.
func (k Keeper) GetBaseFee(ctx cosmos.Context, ethCfg *params.ChainConfig) *big.Int {
return k.getBaseFee(ctx, support.IsLondon(ethCfg, ctx.BlockHeight()))
}
func (k Keeper) getBaseFee(ctx cosmos.Context, london bool) *big.Int {
if !london {
return nil
}
baseFee := k.feeKeeper.GetBaseFee(ctx)
if baseFee == nil {
// return 0 if fee not enabled.
baseFee = big.NewInt(0)
}
return baseFee
}
// GetMinGasMultiplier returns the MinGasMultiplier param from the fee market module
func (k Keeper) GetMinGasMultiplier(ctx cosmos.Context) cosmos.Dec {
feeParams := k.feeKeeper.GetParams(ctx)
if feeParams.MinGasMultiplier.IsNil() {
// in case we are executing eth_call on a legacy block, returns a zero value.
return cosmos.ZeroDec()
}
return feeParams.MinGasMultiplier
}
// ResetTransientGasUsed reset gas used to prepare for execution of current cosmos txs, called in ante handler.
func (k Keeper) ResetTransientGasUsed(ctx cosmos.Context) {
store := ctx.TransientStore(k.transientKey)
store.Delete(types.KeyPrefixTransientGasUsed)
k.Logger(ctx).Debug("setState: ResetTransientGasUsed, delete", "key", "KeyPrefixTransientGasUsed")
}
// GetTransientGasUsed returns the gas used by current cosmos txs.
func (k Keeper) GetTransientGasUsed(ctx cosmos.Context) uint64 {
store := ctx.TransientStore(k.transientKey)
bz := store.Get(types.KeyPrefixTransientGasUsed)
if len(bz) == 0 {
return 0
}
return cosmos.BigEndianToUint64(bz)
}
// SetTransientGasUsed sets the gas used by current cosmos txs.
func (k Keeper) SetTransientGasUsed(ctx cosmos.Context, gasUsed uint64) {
store := ctx.TransientStore(k.transientKey)
bz := cosmos.Uint64ToBigEndian(gasUsed)
store.Set(types.KeyPrefixTransientGasUsed, bz)
k.Logger(ctx).Debug(
fmt.Sprintf("setState: SetTransientGasUsed, set"),
"key", "KeyPrefixTransientGasUsed",
"gasUsed", fmt.Sprintf("%d", gasUsed),
)
}
// AddTransientGasUsed accumulate gas used by each eth msg included in current cosmos txs.
func (k Keeper) AddTransientGasUsed(ctx cosmos.Context, gasUsed uint64) (uint64, error) {
result := k.GetTransientGasUsed(ctx) + gasUsed
if result < gasUsed {
return 0, errorsmod.Wrap(types.ErrGasOverflow, "transient gas used")
}
k.SetTransientGasUsed(ctx, result)
return result, nil
}