-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathbatch_info.go
More file actions
288 lines (242 loc) · 7.38 KB
/
batch_info.go
File metadata and controls
288 lines (242 loc) · 7.38 KB
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
package derivation
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/big"
"github.com/morph-l2/go-ethereum/common"
"github.com/morph-l2/go-ethereum/common/hexutil"
eth "github.com/morph-l2/go-ethereum/core/types"
geth "github.com/morph-l2/go-ethereum/eth"
"github.com/morph-l2/go-ethereum/eth/catalyst"
"morph-l2/node/types"
"morph-l2/node/zstd"
)
const (
// BlockContextLegacyLength is the length of a legacy block context without coinbase
BlockContextLegacyLength = 60
)
type BlockContext struct {
Number uint64 `json:"number"`
Timestamp uint64 `json:"timestamp"`
BaseFee *big.Int
GasLimit uint64
txsNum uint16
l1MsgNum uint16
coinbase common.Address
SafeL2Data *catalyst.SafeL2Data
L2TxHashes []byte
TxHashes []byte
}
func (b *BlockContext) Decode(bc []byte) error {
wb := new(types.WrappedBlock)
txsNum, l1MsgNum, err := wb.DecodeBlockContext(bc)
if err != nil {
return err
}
b.BaseFee = wb.BaseFee
b.GasLimit = wb.GasLimit
b.txsNum = txsNum
b.l1MsgNum = l1MsgNum
b.coinbase = wb.Miner
return nil
}
func decodeCoinbase(bc []byte) (common.Address, error) {
return types.DecodeCoinbase(bc)
}
type BatchInfo struct {
batchIndex uint64
blockNum uint64
txNum uint64
version uint64
blockContexts []*BlockContext
l1BlockNumber uint64
txHash common.Hash
nonce uint64
lastBlockNumber uint64
firstBlockNumber uint64
root common.Hash
withdrawalRoot common.Hash
parentTotalL1MessagePopped uint64
}
func (bi *BatchInfo) FirstBlockNumber() uint64 {
return bi.firstBlockNumber
}
func (bi *BatchInfo) LastBlockNumber() uint64 {
return bi.lastBlockNumber
}
func (bi *BatchInfo) BlockNum() uint64 {
return bi.blockNum
}
func (bi *BatchInfo) TxNum() uint64 {
return bi.txNum
}
// ParseBatch This method is externally referenced for parsing Batch
func (bi *BatchInfo) ParseBatch(batch geth.RPCRollupBatch, morph204Time uint64) error {
if len(batch.Sidecar.Blobs) == 0 {
return fmt.Errorf("blobs length can not be zero")
}
// Parse parent batch header
parentBatchHeader := types.BatchHeaderBytes(batch.ParentBatchHeader)
parentBatchIndex, err := parentBatchHeader.BatchIndex()
if err != nil {
return fmt.Errorf("decode batch header index error: %v", err)
}
totalL1MessagePopped, err := parentBatchHeader.TotalL1MessagePopped()
if err != nil {
return fmt.Errorf("decode batch header totalL1MessagePopped error: %v", err)
}
// Initialize batch info fields
bi.parentTotalL1MessagePopped = totalL1MessagePopped
bi.root = batch.PostStateRoot
bi.batchIndex = parentBatchIndex + 1
bi.withdrawalRoot = batch.WithdrawRoot
bi.version = uint64(batch.Version)
tq := newTxQueue()
var rawBlockContextsAndTxs hexutil.Bytes
// Handle version upgrade scenario
blobData, err := types.RetrieveBlobBytes(&batch.Sidecar.Blobs[0])
if err != nil {
return fmt.Errorf("retrieve blob bytes error: %v", err)
}
batchBytes, err := zstd.DecompressBatchBytes(blobData)
if err != nil {
return fmt.Errorf("decompress batch bytes error: %v", err)
}
// Calculate block count based on version
var blockCount uint64
if batch.Version > 0 {
rawBlockContextsAndTxs = batchBytes
// Ensure we have enough data for block context
if len(batchBytes) < 60 {
return fmt.Errorf("insufficient batch bytes for block context, got %d bytes", len(batchBytes))
}
var startBlock BlockContext
// coinbase does not enter batch at this time
if err := startBlock.Decode(batchBytes[:60]); err != nil {
return fmt.Errorf("decode chunk block context error: %v", err)
}
blockCount = batch.LastBlockNumber - startBlock.Number + 1
} else {
// First 2 bytes contain the block count
if len(batch.BlockContexts) < 2 {
return fmt.Errorf("insufficient block contexts data: %d bytes", len(batch.BlockContexts))
}
blockCount = uint64(binary.BigEndian.Uint16(batch.BlockContexts[:2]))
rawBlockContextsAndTxs = append(rawBlockContextsAndTxs, batch.BlockContexts[2:]...)
rawBlockContextsAndTxs = append(rawBlockContextsAndTxs, batchBytes...)
}
var txsNum uint64
blockContexts := make([]*BlockContext, int(blockCount))
reader := bytes.NewReader(rawBlockContextsAndTxs)
// Process block contexts
for i := 0; i < int(blockCount); i++ {
var block BlockContext
bcBytes := make([]byte, BlockContextLegacyLength)
_, err = reader.Read(bcBytes)
if err != nil {
return fmt.Errorf("read block context numberAndTimeBytes error:%s", err.Error())
}
if err := block.Decode(bcBytes); err != nil {
return fmt.Errorf("decode number and timestamp error: %v", err)
}
var coinbase common.Address
// handle coinbase
if morph204Time != 0 && block.Timestamp >= morph204Time {
coinbaseBytes := make([]byte, common.AddressLength)
_, err = reader.Read(coinbaseBytes)
if err != nil {
return fmt.Errorf("read skipped block context error:%s", err.Error())
}
coinbase, err = decodeCoinbase(coinbaseBytes)
if err != nil {
return err
}
}
// Set boundary block numbers
if i == 0 {
bi.firstBlockNumber = block.Number
}
if i == int(blockCount)-1 {
bi.lastBlockNumber = block.Number
}
// Setup SafeL2Data
var safeL2Data catalyst.SafeL2Data
safeL2Data.Number = block.Number
safeL2Data.GasLimit = block.GasLimit
safeL2Data.BaseFee = block.BaseFee
safeL2Data.Timestamp = block.Timestamp
// TODO coinbase
fmt.Println(coinbase)
// Handle zero BaseFee case
if block.BaseFee != nil && block.BaseFee.Cmp(big.NewInt(0)) == 0 {
safeL2Data.BaseFee = nil
}
// Validate transaction numbers
if block.txsNum < block.l1MsgNum {
return fmt.Errorf("txsNum must be greater than or equal to l1MsgNum, txsNum: %v, l1MsgNum: %v",
block.txsNum, block.l1MsgNum)
}
block.SafeL2Data = &safeL2Data
blockContexts[i] = &block
}
// Read transaction data
txsData, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("read transaction data error: %s", err.Error())
}
// Decode transactions
data, err := types.DecodeTxsFromBytes(txsData)
if err != nil {
return fmt.Errorf("decode transactions error: %v", err)
}
// Process transactions
tq.enqueue(data)
for i := 0; i < int(blockCount); i++ {
// Skip if index is out of bounds
if i >= len(blockContexts) {
return fmt.Errorf("block context index out of bounds: %d >= %d", i, len(blockContexts))
}
txCount := int(blockContexts[i].txsNum) - int(blockContexts[i].l1MsgNum)
txs, err := tq.dequeue(txCount)
if err != nil {
return fmt.Errorf("decode transaction payload error: %v", err)
}
txsNum += uint64(blockContexts[i].txsNum)
// l1 transactions will be inserted later in front of L2 transactions
blockContexts[i].SafeL2Data.Transactions = encodeTransactions(txs)
}
bi.txNum += txsNum
bi.blockContexts = blockContexts
return nil
}
func encodeTransactions(txs []*eth.Transaction) [][]byte {
var enc = make([][]byte, len(txs))
for i, tx := range txs {
enc[i], _ = tx.MarshalBinary()
}
return enc
}
type txQueue struct {
txs eth.Transactions
pointer int
}
func newTxQueue() *txQueue {
var txs eth.Transactions
return &txQueue{
txs: txs,
}
}
func (q *txQueue) enqueue(txs eth.Transactions) {
q.txs = append(q.txs, txs...)
}
func (q *txQueue) dequeue(txNum int) (eth.Transactions, error) {
maxLen := q.txs.Len() - q.pointer
if maxLen < txNum {
return nil, fmt.Errorf("invalid txNum,must small than %v", maxLen)
}
txs := q.txs[q.pointer : q.pointer+txNum]
q.pointer += txNum
return txs, nil
}