Skip to content

Commit

Permalink
feat: add codecv5 and codecv6 for Euclid fork
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Jan 10, 2025
1 parent 9852fa4 commit bc9cd3c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 10 deletions.
10 changes: 7 additions & 3 deletions encoding/codecv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import (

type DACodecV4 struct {
DACodecV3
forcedVersion *CodecVersion
}

// Version returns the codec version.
func (d *DACodecV4) Version() CodecVersion {
if d.forcedVersion != nil {
return *d.forcedVersion
}
return CodecV4
}

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

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

if CodecVersion(data[daBatchOffsetVersion]) != CodecV4 {
return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", CodecV4, data[daBatchOffsetVersion])
if CodecVersion(data[daBatchOffsetVersion]) != d.Version() {
return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", d.Version(), data[daBatchOffsetVersion])
}

return newDABatchV3WithProof(
Expand Down
19 changes: 19 additions & 0 deletions encoding/codecv5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package encoding

type DACodecV5 struct {
DACodecV4
}

func NewDACodecV5() *DACodecV5 {
v := CodecV5
return &DACodecV5{
DACodecV4: DACodecV4{
forcedVersion: &v,
},
}
}

// MaxNumChunksPerBatch returns the maximum number of chunks per batch.
func (d *DACodecV5) MaxNumChunksPerBatch() int {
return 1
}
14 changes: 14 additions & 0 deletions encoding/codecv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package encoding

type DACodecV6 struct {
DACodecV4
}

func NewDACodecV6() *DACodecV6 {
v := CodecV6
return &DACodecV6{
DACodecV4: DACodecV4{
forcedVersion: &v,
},
}
}
22 changes: 18 additions & 4 deletions encoding/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"math/big"
"slices"

"github.com/klauspost/compress/zstd"

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

// if no blocks have row consumption, this is an euclid chunk
isEuclidChunk := slices.IndexFunc(c.Blocks, func(block *Block) bool {
return block.RowConsumption != nil
}) == -1

if isEuclidChunk {
return 0, nil
}

// Iterate over blocks, accumulate row consumption
for _, block := range c.Blocks {
if block.RowConsumption == nil {
Expand Down Expand Up @@ -633,8 +643,10 @@ func GetHardforkName(config *params.ChainConfig, blockHeight, blockTimestamp uin
return "curie"
} else if !config.IsDarwinV2(blockTimestamp) {
return "darwin"
} else {
} else if !config.IsEuclid(blockTimestamp) {
return "darwinV2"
} else {
return "euclid"
}
}

Expand All @@ -649,8 +661,10 @@ func GetCodecVersion(config *params.ChainConfig, blockHeight, blockTimestamp uin
return CodecV2
} else if !config.IsDarwinV2(blockTimestamp) {
return CodecV3
} else {
} else if !config.IsEuclid(blockTimestamp) {
return CodecV4
} else {
return CodecV6
}
}

Expand Down Expand Up @@ -679,7 +693,7 @@ func GetChunkEnableCompression(codecVersion CodecVersion, chunk *Chunk) (bool, e
return false, nil
case CodecV2, CodecV3:
return true, nil
case CodecV4:
case CodecV4, CodecV5, CodecV6:
return CheckChunkCompressedDataCompatibility(chunk, codecVersion)
default:
return false, fmt.Errorf("unsupported codec version: %v", codecVersion)
Expand All @@ -693,7 +707,7 @@ func GetBatchEnableCompression(codecVersion CodecVersion, batch *Batch) (bool, e
return false, nil
case CodecV2, CodecV3:
return true, nil
case CodecV4:
case CodecV4, CodecV5, CodecV6:
return CheckBatchCompressedDataCompatibility(batch, codecVersion)
default:
return false, fmt.Errorf("unsupported codec version: %v", codecVersion)
Expand Down
7 changes: 7 additions & 0 deletions encoding/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func TestUtilFunctions(t *testing.T) {
assert.Equal(t, uint64(5), chunk3.NumTransactions())
assert.Equal(t, uint64(240000), chunk3.TotalGasUsed())

// euclid chunk
chunk3.Blocks[0].RowConsumption = nil
chunk3.Blocks[1].RowConsumption = nil
crc3Max, err = chunk3.CrcMax()
assert.NoError(t, err)
assert.Equal(t, uint64(0), crc3Max)

// Test Batch methods
assert.Equal(t, block6.Header.Root, batch.StateRoot())
assert.Equal(t, block6.WithdrawRoot, batch.WithdrawRoot())
Expand Down
10 changes: 9 additions & 1 deletion encoding/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const (
CodecV2
CodecV3
CodecV4
CodecV5
CodecV6
)

// CodecFromVersion returns the appropriate codec for the given version.
Expand All @@ -91,14 +93,20 @@ func CodecFromVersion(version CodecVersion) (Codec, error) {
return &DACodecV3{}, nil
case CodecV4:
return &DACodecV4{}, nil
case CodecV5:
return NewDACodecV5(), nil
case CodecV6:
return NewDACodecV6(), nil
default:
return nil, fmt.Errorf("unsupported codec version: %v", version)
}
}

// CodecFromConfig determines and returns the appropriate codec based on chain configuration, block number, and timestamp.
func CodecFromConfig(chainCfg *params.ChainConfig, startBlockNumber *big.Int, startBlockTimestamp uint64) Codec {
if chainCfg.IsDarwinV2(startBlockTimestamp) {
if chainCfg.IsEuclid(startBlockTimestamp) {
return NewDACodecV6()
} else if chainCfg.IsDarwinV2(startBlockTimestamp) {
return &DACodecV4{}
} else if chainCfg.IsDarwin(startBlockTimestamp) {
return &DACodecV3{}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/agiledragon/gomonkey/v2 v2.12.0
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc
github.com/scroll-tech/go-ethereum v1.10.14-0.20250103082839-ea3ec93d8c1e
github.com/stretchr/testify v1.9.0
)

Expand All @@ -31,7 +31,7 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc h1:ofQxDFg5aW0ANJcEXt5RJy5lDWz8jdKwKcZhEqvDjx8=
github.com/scroll-tech/go-ethereum v1.10.14-0.20241210104312-bdf64cfb39dc/go.mod h1:xRDJvaNUe7lCU2fB+AqyS7gahar+dfJPrUJplfXF4dw=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250103082839-ea3ec93d8c1e h1:g8jtcGiHbjWYh/V7O245IDho3WfQT4CwEpBV+MhYDrg=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250103082839-ea3ec93d8c1e/go.mod h1:Ik3OBLl7cJxPC+CFyCBYNXBPek4wpdzkWehn/y5qLM8=
github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE=
github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
Expand All @@ -102,6 +104,8 @@ golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down

0 comments on commit bc9cd3c

Please sign in to comment.