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
package logs
import (
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
)
// searchCheckpoint is both a checkpoint for searching, as well as a checkpoint for sealing blocks.
type searchCheckpoint struct {
blockNum uint64
// seen logs *after* the seal of the mentioned block, i.e. not part of this block, but building on top of it.
// There is at least one checkpoint per L2 block with logsSince == 0, i.e. the exact block boundary.
logsSince uint32
timestamp uint64
}
func newSearchCheckpoint(blockNum uint64, logsSince uint32, timestamp uint64) searchCheckpoint {
return searchCheckpoint{
blockNum: blockNum,
logsSince: logsSince,
timestamp: timestamp,
}
}
func newSearchCheckpointFromEntry(data Entry) (searchCheckpoint, error) {
if data.Type() != TypeSearchCheckpoint {
return searchCheckpoint{}, fmt.Errorf("%w: attempting to decode search checkpoint but was type %s", types.ErrDataCorruption, data.Type())
}
return searchCheckpoint{
blockNum: binary.LittleEndian.Uint64(data[1:9]),
logsSince: binary.LittleEndian.Uint32(data[9:13]),
timestamp: binary.LittleEndian.Uint64(data[13:21]),
}, nil
}
// encode creates a checkpoint entry
// type 0: "search checkpoint" <type><uint64 block number: 8 bytes><uint32 logsSince count: 4 bytes><uint64 timestamp: 8 bytes> = 21 bytes
func (s searchCheckpoint) encode() Entry {
var data Entry
data[0] = uint8(TypeSearchCheckpoint)
binary.LittleEndian.PutUint64(data[1:9], s.blockNum)
binary.LittleEndian.PutUint32(data[9:13], s.logsSince)
binary.LittleEndian.PutUint64(data[13:21], s.timestamp)
return data
}
type canonicalHash struct {
hash common.Hash
}
func newCanonicalHash(hash common.Hash) canonicalHash {
return canonicalHash{hash: hash}
}
func newCanonicalHashFromEntry(data Entry) (canonicalHash, error) {
if data.Type() != TypeCanonicalHash {
return canonicalHash{}, fmt.Errorf("%w: attempting to decode canonical hash but was type %s", types.ErrDataCorruption, data.Type())
}
return newCanonicalHash(common.Hash(data[1:33])), nil
}
func (c canonicalHash) encode() Entry {
var entry Entry
entry[0] = uint8(TypeCanonicalHash)
copy(entry[1:33], c.hash[:])
return entry
}
type initiatingEvent struct {
hasExecMsg bool
logHash common.Hash
}
func newInitiatingEventFromEntry(data Entry) (initiatingEvent, error) {
if data.Type() != TypeInitiatingEvent {
return initiatingEvent{}, fmt.Errorf("%w: attempting to decode initiating event but was type %s", types.ErrDataCorruption, data.Type())
}
flags := data[1]
return initiatingEvent{
hasExecMsg: flags&eventFlagHasExecutingMessage != 0,
logHash: common.Hash(data[2:34]),
}, nil
}
func newInitiatingEvent(logHash common.Hash, hasExecMsg bool) initiatingEvent {
return initiatingEvent{
hasExecMsg: hasExecMsg,
logHash: logHash,
}
}
// encode creates an initiating event entry
// type 2: "initiating event" <type><flags><event-hash: 20 bytes> = 22 bytes
func (i initiatingEvent) encode() Entry {
var data Entry
data[0] = uint8(TypeInitiatingEvent)
flags := byte(0)
if i.hasExecMsg {
flags = flags | eventFlagHasExecutingMessage
}
data[1] = flags
copy(data[2:34], i.logHash[:])
return data
}
type executingLink struct {
chain uint32 // chain index, not a chain ID
blockNum uint64
logIdx uint32
timestamp uint64
}
func newExecutingLink(msg types.ExecutingMessage) (executingLink, error) {
if msg.LogIdx > 1<<24 {
return executingLink{}, fmt.Errorf("log idx is too large (%v)", msg.LogIdx)
}
return executingLink{
chain: uint32(msg.Chain),
blockNum: msg.BlockNum,
logIdx: msg.LogIdx,
timestamp: msg.Timestamp,
}, nil
}
func newExecutingLinkFromEntry(data Entry) (executingLink, error) {
if data.Type() != TypeExecutingLink {
return executingLink{}, fmt.Errorf("%w: attempting to decode executing link but was type %s", types.ErrDataCorruption, data.Type())
}
timestamp := binary.LittleEndian.Uint64(data[16:24])
return executingLink{
chain: binary.LittleEndian.Uint32(data[1:5]),
blockNum: binary.LittleEndian.Uint64(data[5:13]),
logIdx: uint32(data[13]) | uint32(data[14])<<8 | uint32(data[15])<<16,
timestamp: timestamp,
}, nil
}
// encode creates an executing link entry
// type 3: "executing link" <type><chain: 4 bytes><blocknum: 8 bytes><event index: 3 bytes><uint64 timestamp: 8 bytes> = 24 bytes
func (e executingLink) encode() Entry {
var entry Entry
entry[0] = uint8(TypeExecutingLink)
binary.LittleEndian.PutUint32(entry[1:5], e.chain)
binary.LittleEndian.PutUint64(entry[5:13], e.blockNum)
entry[13] = byte(e.logIdx)
entry[14] = byte(e.logIdx >> 8)
entry[15] = byte(e.logIdx >> 16)
binary.LittleEndian.PutUint64(entry[16:24], e.timestamp)
return entry
}
type executingCheck struct {
hash common.Hash
}
func newExecutingCheck(hash common.Hash) executingCheck {
return executingCheck{hash: hash}
}
func newExecutingCheckFromEntry(data Entry) (executingCheck, error) {
if data.Type() != TypeExecutingCheck {
return executingCheck{}, fmt.Errorf("%w: attempting to decode executing check but was type %s", types.ErrDataCorruption, data.Type())
}
return newExecutingCheck(common.Hash(data[1:33])), nil
}
// encode creates an executing check entry
// type 4: "executing check" <type><event-hash: 32 bytes> = 33 bytes
func (e executingCheck) encode() Entry {
var entry Entry
entry[0] = uint8(TypeExecutingCheck)
copy(entry[1:33], e.hash[:])
return entry
}
type paddingEntry struct{}
// encoding of the padding entry
// type 5: "padding" <type><padding: 33 bytes> = 34 bytes
func (e paddingEntry) encode() Entry {
var entry Entry
entry[0] = uint8(TypePadding)
return entry
}