-
Notifications
You must be signed in to change notification settings - Fork 175
/
events.go
144 lines (125 loc) · 4.04 KB
/
events.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
package types
import (
"encoding/hex"
gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/flow-go/model/flow"
)
const (
EventTypeBlockExecuted flow.EventType = "evm.BlockExecuted"
EventTypeTransactionExecuted flow.EventType = "evm.TransactionExecuted"
)
type EventPayload interface {
// CadenceEvent creates a Cadence event type
CadenceEvent() (cadence.Event, error)
}
type Event struct {
Etype flow.EventType
Payload EventPayload
}
// we might break this event into two (tx included /tx executed) if size becomes an issue
type TransactionExecutedPayload struct {
BlockHeight uint64
TxEncoded []byte
TxHash gethCommon.Hash
Result *Result
}
var transactionExecutedEventCadenceType = &cadence.EventType{
Location: stdlib.FlowLocation{},
QualifiedIdentifier: string(EventTypeTransactionExecuted),
Fields: []cadence.Field{
cadence.NewField("blockHeight", cadence.UInt64Type{}),
cadence.NewField("transactionHash", cadence.StringType{}),
cadence.NewField("transaction", cadence.StringType{}),
cadence.NewField("failed", cadence.BoolType{}),
cadence.NewField("transactionType", cadence.UInt8Type{}),
cadence.NewField("gasConsumed", cadence.UInt64Type{}),
cadence.NewField("deployedContractAddress", cadence.StringType{}),
cadence.NewField("returnedValue", cadence.StringType{}),
cadence.NewField("logs", cadence.StringType{}),
},
}
// todo add decoder for events from cadence to evm payload
func (p *TransactionExecutedPayload) CadenceEvent() (cadence.Event, error) {
var encodedLogs []byte
var err error
if len(p.Result.Logs) > 0 {
encodedLogs, err = rlp.EncodeToBytes(p.Result.Logs)
if err != nil {
return cadence.Event{}, err
}
}
fields := []cadence.Value{
cadence.NewUInt64(p.BlockHeight),
cadence.String(p.TxHash.String()),
cadence.String(hex.EncodeToString(p.TxEncoded)),
cadence.NewBool(p.Result.Failed),
cadence.NewUInt8(p.Result.TxType),
cadence.NewUInt64(p.Result.GasConsumed),
cadence.String(hex.EncodeToString(p.Result.DeployedContractAddress.Bytes())),
cadence.String(hex.EncodeToString(p.Result.ReturnedValue)),
cadence.String(hex.EncodeToString(encodedLogs)),
}
return cadence.
NewEvent(fields).
WithType(transactionExecutedEventCadenceType), nil
}
func NewTransactionExecutedEvent(
height uint64,
txEncoded []byte,
txHash gethCommon.Hash,
result *Result,
) *Event {
return &Event{
Etype: EventTypeTransactionExecuted,
Payload: &TransactionExecutedPayload{
BlockHeight: height,
TxEncoded: txEncoded,
TxHash: txHash,
Result: result,
},
}
}
var blockExecutedEventCadenceType = &cadence.EventType{
Location: stdlib.FlowLocation{}, // todo create evm custom location
QualifiedIdentifier: string(EventTypeBlockExecuted),
Fields: []cadence.Field{
cadence.NewField("height", cadence.UInt64Type{}),
cadence.NewField("totalSupply", cadence.UInt64Type{}),
cadence.NewField("parentHash", cadence.StringType{}),
cadence.NewField("receiptRoot", cadence.StringType{}),
cadence.NewField(
"transactionHashes",
cadence.NewVariableSizedArrayType(cadence.StringType{}),
),
},
}
type BlockExecutedEventPayload struct {
Block *Block
}
func (p *BlockExecutedEventPayload) CadenceEvent() (cadence.Event, error) {
hashes := make([]cadence.Value, len(p.Block.TransactionHashes))
for i, hash := range p.Block.TransactionHashes {
hashes[i] = cadence.String(hash.String())
}
fields := []cadence.Value{
cadence.NewUInt64(p.Block.Height),
cadence.NewUInt64(p.Block.TotalSupply),
cadence.String(p.Block.ReceiptRoot.String()),
cadence.String(p.Block.ParentBlockHash.String()),
cadence.NewArray(hashes).WithType(cadence.NewVariableSizedArrayType(cadence.StringType{})),
}
return cadence.
NewEvent(fields).
WithType(blockExecutedEventCadenceType), nil
}
func NewBlockExecutedEvent(block *Block) *Event {
return &Event{
Etype: EventTypeBlockExecuted,
Payload: &BlockExecutedEventPayload{
Block: block,
},
}
}