-
Notifications
You must be signed in to change notification settings - Fork 74
/
context.go
458 lines (378 loc) · 11.9 KB
/
context.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 types
import (
"context"
"fmt"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"math/big"
"sync"
"time"
evmtypes "github.com/artela-network/artela/x/evm/types"
artelatypes "github.com/artela-network/aspect-core/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
cosmos "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/artela-network/artela-evm/vm"
ethtypes "github.com/ethereum/go-ethereum/core/types"
statedb "github.com/artela-network/artela/x/evm/states"
inherent "github.com/artela-network/aspect-core/chaincoreext/jit_inherent"
)
const (
AspectContextKey cosmos.ContextKey = "aspect-ctx"
AspectModuleName = "aspect"
)
var cachedStoreKey storetypes.StoreKey
type (
HistoryStoreBuilder func(height int64, keyPrefix string) (prefix.Store, error)
ContextBuilder func(height int64, prove bool) (cosmos.Context, error)
GetLastBlockHeight func() int64
)
// AspectRuntimeContext is the contextual object required for Aspect execution,
// containing information related to transactions (tx) and blocks. Aspects at different
// join points can access this context, and consequently, the context dynamically
// adjusts its content based on the actual execution of blocks and transactions.
// Here is the execution scenario of this context in the lifecycle of a tx process,
// listed in the order of tx execution:
// 1. initialization: Before each transaction execution, create the AspectRuntimeContext
// and establish a bidirectional connection with the sdk context.
// 2. withBlockConfig: Write information before the start of each block and destroy it
// at the end of each block. Transfer it to the AspectRuntimeContext before the execution
// of tx in the deliver state through WithExtBlock.
// 3. withEVM: Before Pre-tx-execute, incorporate the EVM context, including evm, stateDB,
// evm tracer, message, message from, etc., and pass it to the AspectRuntimeContext through
// WithTxContext.
// 4. withReceipt: After the execution of the EVM, store the result in TxContext, enabling
// subsequent JoinPoints to access the execution details of the tx.
// 5. commit: Decide whether to commit at the end of each transaction. If committing is
// necessary, write the result to the sdk context.
// 6. destory: After each transaction execution, destroy the AspectRuntimeContext.
type AspectRuntimeContext struct {
baseCtx context.Context
ethTxContext *EthTxContext
aspectContext *AspectContext
ethBlockContext *EthBlockContext
aspectState *AspectState
cosmosCtx *cosmos.Context
storeKey storetypes.StoreKey
logger log.Logger
jitManager *inherent.Manager
}
func NewAspectRuntimeContext() *AspectRuntimeContext {
return &AspectRuntimeContext{
aspectContext: NewAspectContext(),
storeKey: cachedStoreKey,
logger: log.NewNopLogger(),
}
}
func (c *AspectRuntimeContext) Init(storeKey storetypes.StoreKey) {
cachedStoreKey = storeKey
c.storeKey = storeKey
}
func (c *AspectRuntimeContext) WithCosmosContext(newTxCtx cosmos.Context) {
c.cosmosCtx = &newTxCtx
c.logger = newTxCtx.Logger().With("module", fmt.Sprintf("x/%s", AspectModuleName))
}
func (c *AspectRuntimeContext) Debug(msg string, keyvals ...interface{}) {
if c.ethTxContext != nil {
keyvals = append(keyvals, "tx-from", fmt.Sprintf("%s", c.ethTxContext.TxFrom().Hex()))
if c.ethTxContext.TxContent() != nil {
keyvals = append(keyvals, "tx-hash", fmt.Sprintf("%s", c.ethTxContext.TxContent().Hash().Hex()))
}
}
c.logger.Debug(msg, keyvals...)
}
func (c *AspectRuntimeContext) Logger() log.Logger {
return c.logger
}
func (c *AspectRuntimeContext) CosmosContext() cosmos.Context {
return *c.cosmosCtx
}
func (c *AspectRuntimeContext) StoreKey() storetypes.StoreKey {
return c.storeKey
}
func (c *AspectRuntimeContext) SetEthTxContext(newTxCtx *EthTxContext, jitManager *inherent.Manager) {
c.ethTxContext = newTxCtx
c.aspectContext = NewAspectContext()
c.jitManager = jitManager
}
func (c *AspectRuntimeContext) SetEthBlockContext(newBlockCtx *EthBlockContext) {
c.ethBlockContext = newBlockCtx
}
func (c *AspectRuntimeContext) EthBlockContext() *EthBlockContext {
return c.ethBlockContext
}
func (c *AspectRuntimeContext) EthTxContext() *EthTxContext {
return c.ethTxContext
}
func (c *AspectRuntimeContext) AspectContext() *AspectContext {
return c.aspectContext
}
func (c *AspectRuntimeContext) AspectState() *AspectState {
return c.aspectState
}
func (c *AspectRuntimeContext) JITManager() *inherent.Manager {
return c.jitManager
}
func (c *AspectRuntimeContext) StateDb() vm.StateDB {
if c.EthTxContext() == nil {
return nil
}
return c.EthTxContext().stateDb
}
func (c *AspectRuntimeContext) ClearBlockContext() {
if c.ethBlockContext != nil {
c.ethBlockContext = nil
}
}
func (c *AspectRuntimeContext) CreateStateObject() {
c.aspectState = NewAspectState(*c.cosmosCtx, c.storeKey, AspectStateKeyPrefix, c.logger)
}
func (c *AspectRuntimeContext) GetAspectState(ctx *artelatypes.RunnerContext, key string) []byte {
stateKey := AspectArrayKey(
ctx.AspectId.Bytes(),
[]byte(key),
)
return c.aspectState.Get(stateKey)
}
func (c *AspectRuntimeContext) SetAspectState(ctx *artelatypes.RunnerContext, key string, value []byte) {
stateKey := AspectArrayKey(
ctx.AspectId.Bytes(),
[]byte(key),
)
c.aspectState.Set(stateKey, value)
return
}
func (c *AspectRuntimeContext) Destroy() {
if c.ethTxContext != nil {
c.ethTxContext.ClearEvmObject()
}
if c.aspectContext != nil {
c.aspectContext.Clear()
}
c.ethTxContext = nil
c.jitManager = nil
c.aspectContext = nil
c.cosmosCtx = nil
c.aspectState = nil
c.ethBlockContext = nil
}
func (c *AspectRuntimeContext) Deadline() (deadline time.Time, ok bool) {
return c.baseCtx.Deadline()
}
func (c *AspectRuntimeContext) Done() <-chan struct{} {
return c.baseCtx.Done()
}
func (c *AspectRuntimeContext) Err() error {
return c.baseCtx.Err()
}
func (c *AspectRuntimeContext) Value(key interface{}) interface{} {
return c.baseCtx.Value(key)
}
type EthTxContext struct {
// eth Transaction,it is set in
txContent *ethtypes.Transaction
msg *core.Message
vmTracer *vm.Tracer
receipt *ethtypes.Receipt
stateDb vm.StateDB
evmCfg *statedb.EVMConfig
lastEvm *vm.EVM
from common.Address
index uint64
commit bool
}
func NewEthTxContext(ethTx *ethtypes.Transaction) *EthTxContext {
return &EthTxContext{
txContent: ethTx,
vmTracer: nil,
receipt: nil,
stateDb: nil,
}
}
func (c *EthTxContext) TxTo() string {
if c.txContent == nil {
return ""
}
if c.txContent.To() == nil {
return ""
}
return c.txContent.To().String()
}
func (c *EthTxContext) TxFrom() common.Address {
return c.from
}
func (c *EthTxContext) TxIndex() uint64 {
return c.index
}
func (c *EthTxContext) EvmCfg() *statedb.EVMConfig { return c.evmCfg }
func (c *EthTxContext) TxContent() *ethtypes.Transaction { return c.txContent }
func (c *EthTxContext) VmTracer() *vm.Tracer { return c.vmTracer }
func (c *EthTxContext) Receipt() *ethtypes.Receipt { return c.receipt }
func (c *EthTxContext) VmStateDB() vm.StateDB { return c.stateDb }
func (c *EthTxContext) LastEvm() *vm.EVM { return c.lastEvm }
func (c *EthTxContext) Message() *core.Message { return c.msg }
func (c *EthTxContext) Commit() bool { return c.commit }
func (c *EthTxContext) WithEVM(
from common.Address,
msg *core.Message,
lastEvm *vm.EVM,
monitor *vm.Tracer,
db vm.StateDB,
) *EthTxContext {
c.from = from
c.msg = msg
c.lastEvm = lastEvm
c.vmTracer = monitor
c.stateDb = db
return c
}
func (c *EthTxContext) WithEVMConfig(cfg *statedb.EVMConfig) *EthTxContext {
c.evmCfg = cfg
return c
}
func (c *EthTxContext) WithTxIndex(index uint64) *EthTxContext {
c.index = index
return c
}
func (c *EthTxContext) WithReceipt(receipt *ethtypes.Receipt) *EthTxContext {
c.receipt = receipt
return c
}
func (c *EthTxContext) WithCommit(commit bool) *EthTxContext {
c.commit = commit
return c
}
func (c *EthTxContext) WithStateDB(stateDb vm.StateDB) *EthTxContext {
c.stateDb = stateDb
return c
}
func (c *EthTxContext) ClearEvmObject() *EthTxContext {
c.stateDb = nil
c.vmTracer = nil
c.lastEvm = nil
c.evmCfg = nil
return c
}
type EthBlockContext struct {
blockHeader *ethtypes.Header
}
func NewEthBlockContextFromHeight(height int64) *EthBlockContext {
return &EthBlockContext{ðtypes.Header{Number: big.NewInt(height)}}
}
func NewEthBlockContextFromABCIBeginBlockReq(req abci.RequestBeginBlock) *EthBlockContext {
txHash := ethtypes.EmptyTxsHash
if len(req.Header.DataHash) != 0 {
txHash = common.BytesToHash(req.Header.DataHash)
}
blockHeader := ðtypes.Header{
ParentHash: common.BytesToHash(req.Header.LastBlockId.Hash),
Coinbase: common.BytesToAddress(req.Header.ProposerAddress),
TxHash: txHash,
Number: big.NewInt(req.Header.Height),
Time: uint64(req.Header.Time.UTC().Unix()),
}
return &EthBlockContext{
blockHeader: blockHeader,
}
}
func NewEthBlockContextFromQuery(sdkCtx cosmos.Context, queryCtx client.Context) *EthBlockContext {
blockHeight := sdkCtx.BlockHeight()
resBlock, err := queryCtx.Client.Block(sdkCtx, &blockHeight)
if err != nil || resBlock == nil || resBlock.Block == nil {
return nil
}
resBlockHeader := resBlock.Block.Header
txHash := ethtypes.EmptyTxsHash
if len(resBlockHeader.DataHash) != 0 {
txHash = common.BytesToHash(resBlockHeader.DataHash)
}
blockHeader := ðtypes.Header{
ParentHash: common.BytesToHash(resBlockHeader.LastBlockID.Hash),
Coinbase: common.BytesToAddress(resBlockHeader.ProposerAddress),
TxHash: txHash,
Number: big.NewInt(resBlockHeader.Height),
Time: uint64(resBlockHeader.Time.UTC().Unix()),
}
return &EthBlockContext{
blockHeader: blockHeader,
}
}
func (c *EthBlockContext) BlockHeader() *ethtypes.Header {
return c.blockHeader
}
type AspectContext struct {
// 1.string=namespace Default
// 2.string=key
// 3.string=value
context map[common.Address]map[string][]byte
mutex sync.RWMutex
}
func NewAspectContext() *AspectContext {
return &AspectContext{
context: make(map[common.Address]map[string][]byte),
}
}
func (c *AspectContext) Add(address common.Address, key string, value []byte) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.context[address] == nil {
c.context[address] = make(map[string][]byte, 1)
}
c.context[address][key] = value
}
func (c *AspectContext) Get(address common.Address, key string) []byte {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.context[address] == nil {
return []byte{}
}
return c.context[address][key]
}
func (c *AspectContext) Clear() {
for addr := range c.context {
for k := range c.context[addr] {
delete(c.context[addr], k)
}
delete(c.context, addr)
}
}
type AspectState struct {
state prefix.Store
storeKey storetypes.StoreKey
logger log.Logger
}
func NewAspectState(ctx cosmos.Context, storeKey storetypes.StoreKey, fixKey string, logger log.Logger) *AspectState {
cacheStore := prefix.NewStore(ctx.KVStore(storeKey), evmtypes.KeyPrefix(fixKey))
stateObj := &AspectState{
state: cacheStore,
storeKey: storeKey,
logger: logger,
}
return stateObj
}
func (k *AspectState) Set(key, value []byte) {
action := "updated"
if len(value) == 0 {
k.state.Delete(key)
action = "deleted"
} else {
k.state.Set(key, value)
}
if value == nil {
k.logger.Debug("setState:", "action", action, "key", string(key), "value", "nil")
} else {
k.logger.Debug("setState:", "action", action, "key", string(key), "value", string(value))
}
}
func (k *AspectState) Get(key []byte) []byte {
val := k.state.Get(key)
if val == nil {
k.logger.Debug("getState:", "key", string(key), "value", "nil")
} else {
k.logger.Debug("getState:", "key", string(key), "value", string(val))
}
return val
}