Skip to content

Commit d61e2cb

Browse files
authored
feat: add codecv5 and codecv6 for Euclid fork (#34)
* feat: add codecv5 and codecv6 for Euclid fork * Update da.go * Update interfaces.go * update l2geth * lint
1 parent 9852fa4 commit d61e2cb

File tree

8 files changed

+84
-16
lines changed

8 files changed

+84
-16
lines changed

encoding/codecv4.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import (
2020

2121
type DACodecV4 struct {
2222
DACodecV3
23+
forcedVersion *CodecVersion
2324
}
2425

2526
// Version returns the codec version.
2627
func (d *DACodecV4) Version() CodecVersion {
28+
if d.forcedVersion != nil {
29+
return *d.forcedVersion
30+
}
2731
return CodecV4
2832
}
2933

@@ -90,7 +94,7 @@ func (d *DACodecV4) NewDABatch(batch *Batch) (DABatch, error) {
9094
l1MessagePopped := totalL1MessagePoppedAfter - batch.TotalL1MessagePoppedBefore
9195

9296
return newDABatchV3(
93-
CodecV4, // version
97+
d.Version(), // version
9498
batch.Index, // batchIndex
9599
l1MessagePopped, // l1MessagePopped
96100
totalL1MessagePoppedAfter, // totalL1MessagePopped
@@ -112,8 +116,8 @@ func (d *DACodecV4) NewDABatchFromBytes(data []byte) (DABatch, error) {
112116
return nil, fmt.Errorf("invalid data length for DABatch, expected %d bytes but got %d", daBatchV3EncodedLength, len(data))
113117
}
114118

115-
if CodecVersion(data[daBatchOffsetVersion]) != CodecV4 {
116-
return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", CodecV4, data[daBatchOffsetVersion])
119+
if CodecVersion(data[daBatchOffsetVersion]) != d.Version() {
120+
return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", d.Version(), data[daBatchOffsetVersion])
117121
}
118122

119123
return newDABatchV3WithProof(

encoding/codecv5.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package encoding
2+
3+
type DACodecV5 struct {
4+
DACodecV4
5+
}
6+
7+
func NewDACodecV5() *DACodecV5 {
8+
v := CodecV5
9+
return &DACodecV5{
10+
DACodecV4: DACodecV4{
11+
forcedVersion: &v,
12+
},
13+
}
14+
}
15+
16+
// MaxNumChunksPerBatch returns the maximum number of chunks per batch.
17+
func (d *DACodecV5) MaxNumChunksPerBatch() int {
18+
return 1
19+
}

encoding/codecv6.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package encoding
2+
3+
type DACodecV6 struct {
4+
DACodecV4
5+
}
6+
7+
func NewDACodecV6() *DACodecV6 {
8+
v := CodecV6
9+
return &DACodecV6{
10+
DACodecV4: DACodecV4{
11+
forcedVersion: &v,
12+
},
13+
}
14+
}

encoding/da.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66
"fmt"
77
"math/big"
8+
"slices"
89

910
"github.com/klauspost/compress/zstd"
1011

@@ -226,6 +227,15 @@ func (c *Chunk) CrcMax() (uint64, error) {
226227
// Map sub-circuit name to row count
227228
crc := make(map[string]uint64)
228229

230+
// if no blocks have row consumption, this is an euclid chunk
231+
isEuclidChunk := slices.IndexFunc(c.Blocks, func(block *Block) bool {
232+
return block.RowConsumption != nil
233+
}) == -1
234+
235+
if isEuclidChunk {
236+
return 0, nil
237+
}
238+
229239
// Iterate over blocks, accumulate row consumption
230240
for _, block := range c.Blocks {
231241
if block.RowConsumption == nil {
@@ -633,8 +643,10 @@ func GetHardforkName(config *params.ChainConfig, blockHeight, blockTimestamp uin
633643
return "curie"
634644
} else if !config.IsDarwinV2(blockTimestamp) {
635645
return "darwin"
636-
} else {
646+
} else if !config.IsEuclid(blockTimestamp) {
637647
return "darwinV2"
648+
} else {
649+
return "euclid"
638650
}
639651
}
640652

@@ -649,8 +661,11 @@ func GetCodecVersion(config *params.ChainConfig, blockHeight, blockTimestamp uin
649661
return CodecV2
650662
} else if !config.IsDarwinV2(blockTimestamp) {
651663
return CodecV3
652-
} else {
664+
} else if !config.IsEuclid(blockTimestamp) {
653665
return CodecV4
666+
} else {
667+
// V5 is skipped, because it is only used for the special Euclid transition batch that we handle explicitly
668+
return CodecV6
654669
}
655670
}
656671

@@ -679,7 +694,7 @@ func GetChunkEnableCompression(codecVersion CodecVersion, chunk *Chunk) (bool, e
679694
return false, nil
680695
case CodecV2, CodecV3:
681696
return true, nil
682-
case CodecV4:
697+
case CodecV4, CodecV5, CodecV6:
683698
return CheckChunkCompressedDataCompatibility(chunk, codecVersion)
684699
default:
685700
return false, fmt.Errorf("unsupported codec version: %v", codecVersion)
@@ -693,7 +708,7 @@ func GetBatchEnableCompression(codecVersion CodecVersion, batch *Batch) (bool, e
693708
return false, nil
694709
case CodecV2, CodecV3:
695710
return true, nil
696-
case CodecV4:
711+
case CodecV4, CodecV5, CodecV6:
697712
return CheckBatchCompressedDataCompatibility(batch, codecVersion)
698713
default:
699714
return false, fmt.Errorf("unsupported codec version: %v", codecVersion)

encoding/da_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func TestUtilFunctions(t *testing.T) {
7878
assert.Equal(t, uint64(5), chunk3.NumTransactions())
7979
assert.Equal(t, uint64(240000), chunk3.TotalGasUsed())
8080

81+
// euclid chunk
82+
chunk3.Blocks[0].RowConsumption = nil
83+
chunk3.Blocks[1].RowConsumption = nil
84+
crc3Max, err = chunk3.CrcMax()
85+
assert.NoError(t, err)
86+
assert.Equal(t, uint64(0), crc3Max)
87+
8188
// Test Batch methods
8289
assert.Equal(t, block6.Header.Root, batch.StateRoot())
8390
assert.Equal(t, block6.WithdrawRoot, batch.WithdrawRoot())

encoding/interfaces.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ const (
7676
CodecV2
7777
CodecV3
7878
CodecV4
79+
CodecV5
80+
CodecV6
7981
)
8082

8183
// CodecFromVersion returns the appropriate codec for the given version.
@@ -91,14 +93,21 @@ func CodecFromVersion(version CodecVersion) (Codec, error) {
9193
return &DACodecV3{}, nil
9294
case CodecV4:
9395
return &DACodecV4{}, nil
96+
case CodecV5:
97+
return NewDACodecV5(), nil
98+
case CodecV6:
99+
return NewDACodecV6(), nil
94100
default:
95101
return nil, fmt.Errorf("unsupported codec version: %v", version)
96102
}
97103
}
98104

99105
// CodecFromConfig determines and returns the appropriate codec based on chain configuration, block number, and timestamp.
100106
func CodecFromConfig(chainCfg *params.ChainConfig, startBlockNumber *big.Int, startBlockTimestamp uint64) Codec {
101-
if chainCfg.IsDarwinV2(startBlockTimestamp) {
107+
if chainCfg.IsEuclid(startBlockTimestamp) {
108+
// V5 is skipped, because it is only used for the special Euclid transition batch that we handle explicitly
109+
return NewDACodecV6()
110+
} else if chainCfg.IsDarwinV2(startBlockTimestamp) {
102111
return &DACodecV4{}
103112
} else if chainCfg.IsDarwin(startBlockTimestamp) {
104113
return &DACodecV3{}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/agiledragon/gomonkey/v2 v2.12.0
7-
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc
7+
github.com/scroll-tech/go-ethereum v1.10.14-0.20250205135740-4bdf6d096c38
88
github.com/stretchr/testify v1.9.0
99
)
1010

@@ -31,7 +31,7 @@ require (
3131
github.com/tklauser/go-sysconf v0.3.12 // indirect
3232
github.com/tklauser/numcpus v0.6.1 // indirect
3333
github.com/yusufpapurcu/wmi v1.2.3 // indirect
34-
golang.org/x/crypto v0.17.0 // indirect
34+
golang.org/x/crypto v0.21.0 // indirect
3535
golang.org/x/sync v0.6.0 // indirect
3636
golang.org/x/sys v0.21.0 // indirect
3737
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
2-
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
1+
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
2+
github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI=
33
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
44
github.com/agiledragon/gomonkey/v2 v2.12.0 h1:ek0dYu9K1rSV+TgkW5LvNNPRWyDZVIxGMCFI6Pz9o38=
55
github.com/agiledragon/gomonkey/v2 v2.12.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
@@ -78,8 +78,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
7878
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
7979
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
8080
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
81-
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc h1:ofQxDFg5aW0ANJcEXt5RJy5lDWz8jdKwKcZhEqvDjx8=
82-
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc/go.mod h1:xRDJvaNUe7lCU2fB+AqyS7gahar+dfJPrUJplfXF4dw=
81+
github.com/scroll-tech/go-ethereum v1.10.14-0.20250205135740-4bdf6d096c38 h1:IKkevP42IQx8DQvtVq9WOmZDQrto59CGdEheXPf20HA=
82+
github.com/scroll-tech/go-ethereum v1.10.14-0.20250205135740-4bdf6d096c38/go.mod h1:Ik3OBLl7cJxPC+CFyCBYNXBPek4wpdzkWehn/y5qLM8=
8383
github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE=
8484
github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
8585
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
@@ -100,8 +100,8 @@ github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFi
100100
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
101101
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
102102
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
103-
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
104-
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
103+
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
104+
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
105105
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
106106
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
107107
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 commit comments

Comments
 (0)