/
block.go
57 lines (49 loc) · 1.95 KB
/
block.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
package datactx
import (
"errors"
"github.com/artela-network/aspect-core/context"
artelatypes "github.com/artela-network/aspect-core/types"
"google.golang.org/protobuf/proto"
"github.com/artela-network/artela/x/evm/artela/types"
)
type BlockContextFieldLoader func(blockCtx *types.EthBlockContext) proto.Message
type BlockContext struct {
blockContentLoaders map[string]BlockContextFieldLoader
ctx *types.AspectRuntimeContext
}
func NewBlockContext(ctx *types.AspectRuntimeContext) *BlockContext {
blockCtx := &BlockContext{
blockContentLoaders: make(map[string]BlockContextFieldLoader),
ctx: ctx,
}
blockCtx.registerLoaders()
return blockCtx
}
func (c *BlockContext) registerLoaders() {
loaders := c.blockContentLoaders
loaders[context.BlockHeaderParentHash] = func(blockCtx *types.EthBlockContext) proto.Message {
return &artelatypes.BytesData{Data: blockCtx.BlockHeader().ParentHash.Bytes()}
}
loaders[context.BlockHeaderMiner] = func(blockCtx *types.EthBlockContext) proto.Message {
return &artelatypes.BytesData{Data: blockCtx.BlockHeader().Coinbase.Bytes()}
}
loaders[context.BlockHeaderTransactionsRoot] = func(blockCtx *types.EthBlockContext) proto.Message {
return &artelatypes.BytesData{Data: blockCtx.BlockHeader().TxHash.Bytes()}
}
loaders[context.BlockHeaderNumber] = func(blockCtx *types.EthBlockContext) proto.Message {
number := blockCtx.BlockHeader().Number.Uint64()
return &artelatypes.UintData{Data: &number}
}
loaders[context.BlockHeaderTimestamp] = func(blockCtx *types.EthBlockContext) proto.Message {
time := blockCtx.BlockHeader().Time
return &artelatypes.UintData{Data: &time}
}
}
func (c *BlockContext) ValueLoader(key string) ContextLoader {
return func(ctx *artelatypes.RunnerContext) ([]byte, error) {
if ctx == nil {
return nil, errors.New("aspect context error, missing important information")
}
return proto.Marshal(c.blockContentLoaders[key](c.ctx.EthBlockContext()))
}
}