-
Notifications
You must be signed in to change notification settings - Fork 73
/
receipt.go
82 lines (74 loc) · 2.82 KB
/
receipt.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
package datactx
import (
"errors"
aspctx "github.com/artela-network/aspect-core/context"
artelatypes "github.com/artela-network/aspect-core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethereum "github.com/ethereum/go-ethereum/core/types"
"google.golang.org/protobuf/proto"
"github.com/artela-network/artela/x/evm/artela/types"
)
type ReceiptContextFieldLoader func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message
type ReceiptContext struct {
getSdkCtx func() sdk.Context
getEthTxContext func() *types.EthTxContext
receiptContentLoaders map[string]ReceiptContextFieldLoader
}
func NewReceiptContext(getEthTxContext func() *types.EthTxContext,
getSdkCtx func() sdk.Context) *ReceiptContext {
receiptCtx := &ReceiptContext{
receiptContentLoaders: make(map[string]ReceiptContextFieldLoader),
getEthTxContext: getEthTxContext,
getSdkCtx: getSdkCtx,
}
receiptCtx.registerLoaders()
return receiptCtx
}
func (c *ReceiptContext) registerLoaders() {
loaders := c.receiptContentLoaders
loaders[aspctx.ReceiptStatus] = func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message {
return &artelatypes.UintData{Data: &receipt.Status}
}
loaders[aspctx.ReceiptLogs] = func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message {
logs := make([]*artelatypes.EthLog, 0, len(receipt.Logs))
for _, log := range receipt.Logs {
topics := make([][]byte, 0, len(log.Topics))
for _, topic := range log.Topics {
topics = append(topics, topic.Bytes())
}
index := uint64(log.Index)
logs = append(logs, &artelatypes.EthLog{
Address: log.Address.Bytes(),
Topics: topics,
Data: log.Data,
Index: &index,
})
}
return &artelatypes.EthLogs{Logs: logs}
}
loaders[aspctx.ReceiptGasUsed] = func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message {
return &artelatypes.UintData{Data: &receipt.GasUsed}
}
loaders[aspctx.ReceiptCumulativeGasUsed] = func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message {
return &artelatypes.UintData{Data: &receipt.CumulativeGasUsed}
}
loaders[aspctx.ReceiptBloom] = func(ethTxCtx *types.EthTxContext, receipt *ethereum.Receipt) proto.Message {
return &artelatypes.BytesData{Data: receipt.Bloom.Bytes()}
}
}
func (c *ReceiptContext) ValueLoader(key string) ContextLoader {
return func(ctx *artelatypes.RunnerContext) ([]byte, error) {
if ctx == nil {
return nil, errors.New("aspect context error, missing important information")
}
txContext := c.getEthTxContext()
if txContext == nil {
return nil, errors.New("tx context error, failed to load")
}
receipt := txContext.Receipt()
if receipt == nil {
return nil, errors.New("receipt error, failed to load")
}
return proto.Marshal(c.receiptContentLoaders[key](txContext, receipt))
}
}