-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathl1_block_info.go
508 lines (471 loc) · 17.6 KB
/
l1_block_info.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package derive
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/solabi"
)
const (
L1InfoFuncBedrockSignature = "setL1BlockValues(uint64,uint64,uint256,bytes32,uint64,bytes32,uint256,uint256)"
L1InfoFuncEcotoneSignature = "setL1BlockValuesEcotone()"
L1InfoFuncInteropSignature = "setL1BlockValuesInterop()"
L1InfoArguments = 8
L1InfoBedrockLen = 4 + 32*L1InfoArguments
L1InfoEcotoneLen = 4 + 32*5 // after Ecotone upgrade, args are packed into 5 32-byte slots
)
var (
L1InfoFuncBedrockBytes4 = crypto.Keccak256([]byte(L1InfoFuncBedrockSignature))[:4]
L1InfoFuncEcotoneBytes4 = crypto.Keccak256([]byte(L1InfoFuncEcotoneSignature))[:4]
L1InfoFuncInteropBytes4 = crypto.Keccak256([]byte(L1InfoFuncInteropSignature))[:4]
L1InfoDepositerAddress = common.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001")
L1BlockAddress = predeploys.L1BlockAddr
)
const (
RegolithSystemTxGas = 1_000_000
)
// The following hardcoded chain IDs are for the first devnet release
var (
InteropDependencySet = []*big.Int{big.NewInt(10666), big.NewInt(10777), big.NewInt(10888)}
)
// L1BlockInfo presents the information stored in a L1Block.setL1BlockValues call
type L1BlockInfo struct {
Number uint64
Time uint64
BaseFee *big.Int
BlockHash common.Hash
// Not strictly a piece of L1 information. Represents the number of L2 blocks since the start of the epoch,
// i.e. when the actual L1 info was first introduced.
SequenceNumber uint64
// BatcherHash version 0 is just the address with 0 padding to the left.
BatcherAddr common.Address
L1FeeOverhead eth.Bytes32 // ignored after Ecotone upgrade
L1FeeScalar eth.Bytes32 // ignored after Ecotone upgrade
BlobBaseFee *big.Int // added by Ecotone upgrade
BaseFeeScalar uint32 // added by Ecotone upgrade
BlobBaseFeeScalar uint32 // added by Ecotone upgrade
DependencySet []*big.Int // added by Interop upgrade
}
// Bedrock Binary Format
// +---------+--------------------------+
// | Bytes | Field |
// +---------+--------------------------+
// | 4 | Function signature |
// | 32 | Number |
// | 32 | Time |
// | 32 | BaseFee |
// | 32 | BlockHash |
// | 32 | SequenceNumber |
// | 32 | BatcherHash |
// | 32 | L1FeeOverhead |
// | 32 | L1FeeScalar |
// +---------+--------------------------+
func (info *L1BlockInfo) marshalBinaryBedrock() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, 0, L1InfoBedrockLen))
if err := solabi.WriteSignature(w, L1InfoFuncBedrockBytes4); err != nil {
return nil, err
}
if err := solabi.WriteUint64(w, info.Number); err != nil {
return nil, err
}
if err := solabi.WriteUint64(w, info.Time); err != nil {
return nil, err
}
if err := solabi.WriteUint256(w, info.BaseFee); err != nil {
return nil, err
}
if err := solabi.WriteHash(w, info.BlockHash); err != nil {
return nil, err
}
if err := solabi.WriteUint64(w, info.SequenceNumber); err != nil {
return nil, err
}
if err := solabi.WriteAddress(w, info.BatcherAddr); err != nil {
return nil, err
}
if err := solabi.WriteEthBytes32(w, info.L1FeeOverhead); err != nil {
return nil, err
}
if err := solabi.WriteEthBytes32(w, info.L1FeeScalar); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (info *L1BlockInfo) unmarshalBinaryBedrock(data []byte) error {
if len(data) != L1InfoBedrockLen {
return fmt.Errorf("data is unexpected length: got %d, expected %d", len(data), L1InfoBedrockLen)
}
reader := bytes.NewReader(data)
var err error
if _, err := solabi.ReadAndValidateSignature(reader, L1InfoFuncBedrockBytes4); err != nil {
return err
}
if info.Number, err = solabi.ReadUint64(reader); err != nil {
return err
}
if info.Time, err = solabi.ReadUint64(reader); err != nil {
return err
}
if info.BaseFee, err = solabi.ReadUint256(reader); err != nil {
return err
}
if info.BlockHash, err = solabi.ReadHash(reader); err != nil {
return err
}
if info.SequenceNumber, err = solabi.ReadUint64(reader); err != nil {
return err
}
if info.BatcherAddr, err = solabi.ReadAddress(reader); err != nil {
return err
}
if info.L1FeeOverhead, err = solabi.ReadEthBytes32(reader); err != nil {
return err
}
if info.L1FeeScalar, err = solabi.ReadEthBytes32(reader); err != nil {
return err
}
if !solabi.EmptyReader(reader) {
return errors.New("too many bytes")
}
return nil
}
// Ecotone Binary Format
// +---------+--------------------------+
// | Bytes | Field |
// +---------+--------------------------+
// | 4 | Function signature |
// | 4 | BaseFeeScalar |
// | 4 | BlobBaseFeeScalar |
// | 8 | SequenceNumber |
// | 8 | Timestamp |
// | 8 | L1BlockNumber |
// | 32 | BaseFee |
// | 32 | BlobBaseFee |
// | 32 | BlockHash |
// | 32 | BatcherHash |
// +---------+--------------------------+
func (info *L1BlockInfo) marshalBinaryEcotone() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, 0, L1InfoEcotoneLen))
if err := solabi.WriteSignature(w, L1InfoFuncEcotoneBytes4); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.BaseFeeScalar); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.BlobBaseFeeScalar); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.SequenceNumber); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.Time); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.Number); err != nil {
return nil, err
}
if err := solabi.WriteUint256(w, info.BaseFee); err != nil {
return nil, err
}
blobBasefee := info.BlobBaseFee
if blobBasefee == nil {
blobBasefee = big.NewInt(1) // set to 1, to match the min blob basefee as defined in EIP-4844
}
if err := solabi.WriteUint256(w, blobBasefee); err != nil {
return nil, err
}
if err := solabi.WriteHash(w, info.BlockHash); err != nil {
return nil, err
}
// ABI encoding will perform the left-padding with zeroes to 32 bytes, matching the "batcherHash" SystemConfig format and version 0 byte.
if err := solabi.WriteAddress(w, info.BatcherAddr); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (info *L1BlockInfo) unmarshalBinaryEcotone(data []byte) error {
if len(data) != L1InfoEcotoneLen {
return fmt.Errorf("data is unexpected length: got %d, expected %d", len(data), L1InfoBedrockLen)
}
r := bytes.NewReader(data)
var err error
if _, err := solabi.ReadAndValidateSignature(r, L1InfoFuncEcotoneBytes4); err != nil {
return err
}
if err := binary.Read(r, binary.BigEndian, &info.BaseFeeScalar); err != nil {
return fmt.Errorf("invalid ecotone l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.BlobBaseFeeScalar); err != nil {
return fmt.Errorf("invalid ecotone l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.SequenceNumber); err != nil {
return fmt.Errorf("invalid ecotone l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.Time); err != nil {
return fmt.Errorf("invalid ecotone l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.Number); err != nil {
return fmt.Errorf("invalid ecotone l1 block info format")
}
if info.BaseFee, err = solabi.ReadUint256(r); err != nil {
return err
}
if info.BlobBaseFee, err = solabi.ReadUint256(r); err != nil {
return err
}
if info.BlockHash, err = solabi.ReadHash(r); err != nil {
return err
}
// The "batcherHash" will be correctly parsed as address, since the version 0 and left-padding matches the ABI encoding format.
if info.BatcherAddr, err = solabi.ReadAddress(r); err != nil {
return err
}
if !solabi.EmptyReader(r) {
return errors.New("too many bytes")
}
return nil
}
// Interop Binary Format
// +----------------------+----------------------+
// | Bytes | Field |
// +----------------------+----------------------+
// | 4 | Function signature |
// | 4 | BaseFeeScalar |
// | 4 | BlobBaseFeeScalar |
// | 8 | SequenceNumber |
// | 8 | Timestamp |
// | 8 | L1BlockNumber |
// | 32 | BaseFee |
// | 32 | BlobBaseFee |
// | 32 | BlockHash |
// | 32 | BatcherHash |
// | 1 | DependencySetSize |
// | 32*DependencySetSize | DependencySet |
// +----------------------+----------------------+
func (info *L1BlockInfo) marshalBinaryInterop() ([]byte, error) {
w := bytes.NewBuffer(make([]byte, 0, L1InfoInteropLen(uint8(len(info.DependencySet)))))
if err := solabi.WriteSignature(w, L1InfoFuncInteropBytes4); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.BaseFeeScalar); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.BlobBaseFeeScalar); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.SequenceNumber); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.Time); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.Number); err != nil {
return nil, err
}
if err := solabi.WriteUint256(w, info.BaseFee); err != nil {
return nil, err
}
blobBasefee := info.BlobBaseFee
if blobBasefee == nil {
blobBasefee = big.NewInt(1) // set to 1, to match the min blob basefee as defined in EIP-4844
}
if err := solabi.WriteUint256(w, blobBasefee); err != nil {
return nil, err
}
if err := solabi.WriteHash(w, info.BlockHash); err != nil {
return nil, err
}
// ABI encoding will perform the left-padding with zeroes to 32 bytes, matching the "batcherHash" SystemConfig format and version 0 byte.
if err := solabi.WriteAddress(w, info.BatcherAddr); err != nil {
return nil, err
}
if err := binary.Write(w, binary.BigEndian, info.DependencySetSize()); err != nil {
return nil, err
}
for _, chainID := range info.DependencySet {
if err := solabi.WriteUint256(w, chainID); err != nil {
return nil, err
}
}
return w.Bytes(), nil
}
func (info *L1BlockInfo) unmarshalBinaryInterop(data []byte) error {
r := bytes.NewReader(data)
var err error
if _, err := solabi.ReadAndValidateSignature(r, L1InfoFuncInteropBytes4); err != nil {
return err
}
if err := binary.Read(r, binary.BigEndian, &info.BaseFeeScalar); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.BlobBaseFeeScalar); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.SequenceNumber); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.Time); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
if err := binary.Read(r, binary.BigEndian, &info.Number); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
if info.BaseFee, err = solabi.ReadUint256(r); err != nil {
return err
}
if info.BlobBaseFee, err = solabi.ReadUint256(r); err != nil {
return err
}
if info.BlockHash, err = solabi.ReadHash(r); err != nil {
return err
}
// The "batcherHash" will be correctly parsed as address, since the version 0 and left-padding matches the ABI encoding format.
if info.BatcherAddr, err = solabi.ReadAddress(r); err != nil {
return err
}
var dependencySetSize uint8
if err := binary.Read(r, binary.BigEndian, &dependencySetSize); err != nil {
return fmt.Errorf("invalid interop l1 block info format")
}
// we make the check here because it's the soonest InteroptSetSize is available, which is needed to calculate the expected length
l1InfoLen := L1InfoInteropLen(dependencySetSize)
if len(data) != l1InfoLen {
return fmt.Errorf("data is unexpected length: got %d, expected %d", len(data), l1InfoLen)
}
info.DependencySet = make([]*big.Int, dependencySetSize)
for i := uint8(0); i < dependencySetSize; i++ {
if info.DependencySet[i], err = solabi.ReadUint256(r); err != nil {
return err
}
}
if !solabi.EmptyReader(r) {
return errors.New("too many bytes")
}
return nil
}
func (info *L1BlockInfo) DependencySetSize() uint8 {
return uint8(len(info.DependencySet))
}
// isEcotoneButNotFirstBlock returns whether the specified block is subject to the Ecotone upgrade,
// but is not the actiation block itself.
func isEcotoneButNotFirstBlock(rollupCfg *rollup.Config, l2BlockTime uint64) bool {
return rollupCfg.IsEcotone(l2BlockTime) && !rollupCfg.IsEcotoneActivationBlock(l2BlockTime)
}
// isInteropButNotFirstBlock returns whether the specified block is subject to the Interop upgrade,
// but is not the activation block itself.
func isInteropButNotFirstBlock(rollupCfg *rollup.Config, l2BlockTime uint64) bool {
return rollupCfg.IsInterop(l2BlockTime) && !rollupCfg.IsInteropActivationBlock(l2BlockTime)
}
// L1BlockInfoFromBytes is the inverse of L1InfoDeposit, to see where the L2 chain is derived from
func L1BlockInfoFromBytes(rollupCfg *rollup.Config, l2BlockTime uint64, data []byte) (*L1BlockInfo, error) {
var info L1BlockInfo
if isInteropButNotFirstBlock(rollupCfg, l2BlockTime) {
return &info, info.unmarshalBinaryInterop(data)
} else if isEcotoneButNotFirstBlock(rollupCfg, l2BlockTime) {
return &info, info.unmarshalBinaryEcotone(data)
}
return &info, info.unmarshalBinaryBedrock(data)
}
// L1InfoDeposit creates a L1 Info deposit transaction based on the L1 block,
// and the L2 block-height difference with the start of the epoch.
func L1InfoDeposit(rollupCfg *rollup.Config, sysCfg eth.SystemConfig, seqNumber uint64, block eth.BlockInfo, l2BlockTime uint64) (*types.DepositTx, error) {
l1BlockInfo := L1BlockInfo{
Number: block.NumberU64(),
Time: block.Time(),
BaseFee: block.BaseFee(),
BlockHash: block.Hash(),
SequenceNumber: seqNumber,
BatcherAddr: sysCfg.BatcherAddr,
}
var data []byte
if isInteropButNotFirstBlock(rollupCfg, l2BlockTime) {
l1BlockInfo.BlobBaseFee = block.BlobBaseFee()
if l1BlockInfo.BlobBaseFee == nil {
// The L2 spec states to use the MIN_BLOB_GASPRICE from EIP-4844 if not yet active on L1.
l1BlockInfo.BlobBaseFee = big.NewInt(1)
}
blobBaseFeeScalar, baseFeeScalar, err := sysCfg.EcotoneScalars()
if err != nil {
return nil, err
}
l1BlockInfo.BlobBaseFeeScalar = blobBaseFeeScalar
l1BlockInfo.BaseFeeScalar = baseFeeScalar
l1BlockInfo.DependencySet = InteropDependencySet
out, err := l1BlockInfo.marshalBinaryInterop()
if err != nil {
return nil, fmt.Errorf("failed to marshal Interop l1 block info: %w", err)
}
data = out
} else if isEcotoneButNotFirstBlock(rollupCfg, l2BlockTime) {
l1BlockInfo.BlobBaseFee = block.BlobBaseFee()
if l1BlockInfo.BlobBaseFee == nil {
// The L2 spec states to use the MIN_BLOB_GASPRICE from EIP-4844 if not yet active on L1.
l1BlockInfo.BlobBaseFee = big.NewInt(1)
}
blobBaseFeeScalar, baseFeeScalar, err := sysCfg.EcotoneScalars()
if err != nil {
return nil, err
}
l1BlockInfo.BlobBaseFeeScalar = blobBaseFeeScalar
l1BlockInfo.BaseFeeScalar = baseFeeScalar
out, err := l1BlockInfo.marshalBinaryEcotone()
if err != nil {
return nil, fmt.Errorf("failed to marshal Ecotone l1 block info: %w", err)
}
data = out
} else {
l1BlockInfo.L1FeeOverhead = sysCfg.Overhead
l1BlockInfo.L1FeeScalar = sysCfg.Scalar
out, err := l1BlockInfo.marshalBinaryBedrock()
if err != nil {
return nil, fmt.Errorf("failed to marshal Bedrock l1 block info: %w", err)
}
data = out
}
source := L1InfoDepositSource{
L1BlockHash: block.Hash(),
SeqNumber: seqNumber,
}
// Set a very large gas limit with `IsSystemTransaction` to ensure
// that the L1 Attributes Transaction does not run out of gas.
out := &types.DepositTx{
SourceHash: source.SourceHash(),
From: L1InfoDepositerAddress,
To: &L1BlockAddress,
Mint: nil,
Value: big.NewInt(0),
Gas: 150_000_000,
IsSystemTransaction: true,
Data: data,
}
// With the regolith fork we disable the IsSystemTx functionality, and allocate real gas
if rollupCfg.IsRegolith(l2BlockTime) {
out.IsSystemTransaction = false
out.Gas = RegolithSystemTxGas
}
return out, nil
}
// L1InfoDepositBytes returns a serialized L1-info attributes transaction.
func L1InfoDepositBytes(rollupCfg *rollup.Config, sysCfg eth.SystemConfig, seqNumber uint64, l1Info eth.BlockInfo, l2BlockTime uint64) ([]byte, error) {
dep, err := L1InfoDeposit(rollupCfg, sysCfg, seqNumber, l1Info, l2BlockTime)
if err != nil {
return nil, fmt.Errorf("failed to create L1 info tx: %w", err)
}
l1Tx := types.NewTx(dep)
opaqueL1Tx, err := l1Tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to encode L1 info tx: %w", err)
}
return opaqueL1Tx, nil
}
// L1InfoInteropLen returns the length of the L1 block under the Interop upgrade.
func L1InfoInteropLen(dependencySetSize uint8) int {
return 4 + 32*5 + 1 + 32*int(dependencySetSize)
}