Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use new da codec interfaces in syncing from L1 #1078

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/Microsoft/go-winio v0.6.1
github.com/VictoriaMetrics/fastcache v1.12.1
github.com/VictoriaMetrics/fastcache v1.12.2
github.com/aws/aws-sdk-go-v2 v1.21.2
github.com/aws/aws-sdk-go-v2/config v1.18.45
github.com/aws/aws-sdk-go-v2/credentials v1.13.43
Expand Down Expand Up @@ -57,7 +57,7 @@ require (
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7
github.com/rs/cors v1.7.0
github.com/scroll-tech/da-codec v0.1.1-0.20241018065211-60b93622dfd7
github.com/scroll-tech/da-codec v0.1.1-0.20241020144941-65f8a08ff133
github.com/scroll-tech/zktrie v0.8.4
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/sourcegraph/conc v0.3.0
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI=
github.com/agiledragon/gomonkey/v2 v2.12.0 h1:ek0dYu9K1rSV+TgkW5LvNNPRWyDZVIxGMCFI6Pz9o38=
github.com/agiledragon/gomonkey/v2 v2.12.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -474,8 +474,8 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scroll-tech/da-codec v0.1.1-0.20241018065211-60b93622dfd7 h1:AJzlnEBhsgFvp2qWtl0Zi39g1wp71Rn/j3YcKw+WDR4=
github.com/scroll-tech/da-codec v0.1.1-0.20241018065211-60b93622dfd7/go.mod h1:odz1ck3umvYccCG03osaQBISAYGinZktZYbpk94fYRE=
github.com/scroll-tech/da-codec v0.1.1-0.20241020144941-65f8a08ff133 h1:Et0CJw/3o8XfamNF7vgc+UkVTFHRljJPq9lfp5I2v3o=
github.com/scroll-tech/da-codec v0.1.1-0.20241020144941-65f8a08ff133/go.mod h1:odz1ck3umvYccCG03osaQBISAYGinZktZYbpk94fYRE=
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 Down Expand Up @@ -695,9 +695,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down
2 changes: 1 addition & 1 deletion rollup/da_syncer/batch_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (bq *BatchQueue) NextBatch(ctx context.Context) (da.Entry, error) {
return nil, err
}
switch daEntry.Type() {
case da.CommitBatchV0Type, da.CommitBatchV1Type, da.CommitBatchV2Type:
case da.CommitBatchV0Type, da.CommitBatchWithBlobType:
bq.addBatch(daEntry)
case da.RevertBatchType:
bq.deleteBatch(daEntry)
Expand Down
27 changes: 16 additions & 11 deletions rollup/da_syncer/da/calldata_blob_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/scroll-tech/da-codec/encoding"
"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
Expand Down Expand Up @@ -205,19 +206,21 @@ func (ds *CalldataBlobSource) getCommitBatchDA(batchIndex uint64, vLog *types.Lo
if err != nil {
return nil, fmt.Errorf("failed to unpack transaction data using ABI, tx data: %v, err: %w", txData, err)
}

if method.Name == commitBatchMethodName {
args, err := newCommitBatchArgs(method, values)
if err != nil {
return nil, fmt.Errorf("failed to decode calldata into commitBatch args, values: %+v, err: %w", values, err)
}
codecVersion := encoding.CodecVersion(args.Version)
codec, err := encoding.CodecFromVersion(codecVersion)
if err != nil {
return nil, fmt.Errorf("unsupported codec version: %v, batch index: %v, err: %w", codecVersion, batchIndex, err)
}
switch args.Version {
case 0:
return NewCommitBatchDAV0(ds.db, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap, vLog.BlockNumber)
case 1:
return NewCommitBatchDAV1(ds.ctx, ds.db, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
case 2:
return NewCommitBatchDAV2(ds.ctx, ds.db, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
return NewCommitBatchDAV0(ds.db, codec, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap, vLog.BlockNumber)
case 1, 2:
return NewCommitBatchDAWithBlob(ds.ctx, ds.db, codec, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
default:
return nil, fmt.Errorf("failed to decode DA, codec version is unknown: codec version: %d", args.Version)
}
Expand All @@ -226,12 +229,14 @@ func (ds *CalldataBlobSource) getCommitBatchDA(batchIndex uint64, vLog *types.Lo
if err != nil {
return nil, fmt.Errorf("failed to decode calldata into commitBatch args, values: %+v, err: %w", values, err)
}
codecVersion := encoding.CodecVersion(args.Version)
codec, err := encoding.CodecFromVersion(codecVersion)
if err != nil {
return nil, fmt.Errorf("unsupported codec version: %v, batch index: %v, err: %w", codecVersion, batchIndex, err)
}
switch args.Version {
case 3:
// we can use V2 for version 3, because it's same
return NewCommitBatchDAV2(ds.ctx, ds.db, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
case 4:
return NewCommitBatchDAV4(ds.ctx, ds.db, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
case 3, 4:
return NewCommitBatchDAWithBlob(ds.ctx, ds.db, codec, ds.l1Client, ds.blobClient, vLog, args.Version, batchIndex, args.ParentBatchHeader, args.Chunks, args.SkippedL1MessageBitmap)
default:
return nil, fmt.Errorf("failed to decode DA, codec version is unknown: codec version: %d", args.Version)
}
Expand Down
39 changes: 24 additions & 15 deletions rollup/da_syncer/da/commitV0.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package da
import (
"encoding/binary"
"fmt"
"math/big"

"github.com/scroll-tech/da-codec/encoding"
"github.com/scroll-tech/da-codec/encoding/codecv0"

"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/core/types"
Expand All @@ -18,21 +18,22 @@ type CommitBatchDAV0 struct {
batchIndex uint64
parentTotalL1MessagePopped uint64
skippedL1MessageBitmap []byte
chunks []*codecv0.DAChunkRawTx
chunks []*encoding.DAChunkRawTx
l1Txs []*types.L1MessageTx

l1BlockNumber uint64
}

func NewCommitBatchDAV0(db ethdb.Database,
codec encoding.Codec,
version uint8,
batchIndex uint64,
parentBatchHeader []byte,
chunks [][]byte,
skippedL1MessageBitmap []byte,
l1BlockNumber uint64,
) (*CommitBatchDAV0, error) {
decodedChunks, err := codecv0.DecodeDAChunksRawTx(chunks)
decodedChunks, err := codec.DecodeDAChunksRawTx(chunks)
if err != nil {
return nil, fmt.Errorf("failed to unpack chunks: %d, err: %w", batchIndex, err)
}
Expand All @@ -44,7 +45,7 @@ func NewCommitBatchDAV0WithChunks(db ethdb.Database,
version uint8,
batchIndex uint64,
parentBatchHeader []byte,
decodedChunks []*codecv0.DAChunkRawTx,
decodedChunks []*encoding.DAChunkRawTx,
skippedL1MessageBitmap []byte,
l1BlockNumber uint64,
) (*CommitBatchDAV0, error) {
Expand Down Expand Up @@ -100,24 +101,24 @@ func (c *CommitBatchDAV0) Blocks() []*PartialBlock {
for _, chunk := range c.chunks {
for blockId, daBlock := range chunk.Blocks {
// create txs
txs := make(types.Transactions, 0, daBlock.NumTransactions)
txs := make(types.Transactions, 0, daBlock.NumTransactions())
// insert l1 msgs
for l1TxPointer < len(c.l1Txs) && c.l1Txs[l1TxPointer].QueueIndex < curL1TxIndex+uint64(daBlock.NumL1Messages) {
for l1TxPointer < len(c.l1Txs) && c.l1Txs[l1TxPointer].QueueIndex < curL1TxIndex+uint64(daBlock.NumL1Messages()) {
l1Tx := types.NewTx(c.l1Txs[l1TxPointer])
txs = append(txs, l1Tx)
l1TxPointer++
}
curL1TxIndex += uint64(daBlock.NumL1Messages)
curL1TxIndex += uint64(daBlock.NumL1Messages())

// insert l2 txs
txs = append(txs, chunk.Transactions[blockId]...)

block := NewPartialBlock(
&PartialHeader{
Number: daBlock.BlockNumber,
Time: daBlock.Timestamp,
BaseFee: daBlock.BaseFee,
GasLimit: daBlock.GasLimit,
Number: daBlock.Number(),
Time: daBlock.Timestamp(),
BaseFee: daBlock.BaseFee(),
GasLimit: daBlock.GasLimit(),
Difficulty: 10, // TODO: replace with real difficulty
ExtraData: []byte{1, 2, 3, 4, 5, 6, 7, 8}, // TODO: replace with real extra data
},
Expand All @@ -129,28 +130,36 @@ func (c *CommitBatchDAV0) Blocks() []*PartialBlock {
return blocks
}

func getTotalMessagesPoppedFromChunks(decodedChunks []*codecv0.DAChunkRawTx) int {
func getTotalMessagesPoppedFromChunks(decodedChunks []*encoding.DAChunkRawTx) int {
totalL1MessagePopped := 0
for _, chunk := range decodedChunks {
for _, block := range chunk.Blocks {
totalL1MessagePopped += int(block.NumL1Messages)
totalL1MessagePopped += int(block.NumL1Messages())
}
}
return totalL1MessagePopped
}

func getL1Messages(db ethdb.Database, parentTotalL1MessagePopped uint64, skippedBitmap []byte, totalL1MessagePopped int) ([]*types.L1MessageTx, error) {
var txs []*types.L1MessageTx

decodedSkippedBitmap, err := encoding.DecodeBitmap(skippedBitmap, totalL1MessagePopped)
if err != nil {
return nil, fmt.Errorf("failed to decode skipped message bitmap: err: %w", err)
}

isL1MessageSkipped := func(skippedBitmap []*big.Int, index uint64) bool {
if index >= uint64(len(skippedBitmap))*256 {
return false
}
quo := index / 256
rem := index % 256
return skippedBitmap[quo].Bit(int(rem)) == 1
}

// get all necessary l1 messages without skipped
currentIndex := parentTotalL1MessagePopped
for index := 0; index < totalL1MessagePopped; index++ {
if encoding.IsL1MessageSkipped(decodedSkippedBitmap, currentIndex-parentTotalL1MessagePopped) {
if isL1MessageSkipped(decodedSkippedBitmap, currentIndex-parentTotalL1MessagePopped) {
currentIndex++
continue
}
Expand Down
26 changes: 6 additions & 20 deletions rollup/da_syncer/da/commitV1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"crypto/sha256"
"fmt"

"github.com/scroll-tech/da-codec/encoding/codecv0"
"github.com/scroll-tech/da-codec/encoding/codecv1"
"github.com/scroll-tech/da-codec/encoding"

"github.com/scroll-tech/go-ethereum/rollup/da_syncer/blob_client"
"github.com/scroll-tech/go-ethereum/rollup/rollup_sync_service"
Expand All @@ -21,7 +20,8 @@ type CommitBatchDAV1 struct {
*CommitBatchDAV0
}

func NewCommitBatchDAV1(ctx context.Context, db ethdb.Database,
func NewCommitBatchDAWithBlob(ctx context.Context, db ethdb.Database,
codec encoding.Codec,
l1Client *rollup_sync_service.L1Client,
blobClient blob_client.BlobClient,
vLog *types.Log,
Expand All @@ -31,21 +31,7 @@ func NewCommitBatchDAV1(ctx context.Context, db ethdb.Database,
chunks [][]byte,
skippedL1MessageBitmap []byte,
) (*CommitBatchDAV1, error) {
return NewCommitBatchDAV1WithBlobDecodeFunc(ctx, db, l1Client, blobClient, vLog, version, batchIndex, parentBatchHeader, chunks, skippedL1MessageBitmap, codecv1.DecodeTxsFromBlob)
}

func NewCommitBatchDAV1WithBlobDecodeFunc(ctx context.Context, db ethdb.Database,
l1Client *rollup_sync_service.L1Client,
blobClient blob_client.BlobClient,
vLog *types.Log,
version uint8,
batchIndex uint64,
parentBatchHeader []byte,
chunks [][]byte,
skippedL1MessageBitmap []byte,
decodeTxsFromBlobFunc func(*kzg4844.Blob, []*codecv0.DAChunkRawTx) error,
) (*CommitBatchDAV1, error) {
decodedChunks, err := codecv1.DecodeDAChunksRawTx(chunks)
decodedChunks, err := codec.DecodeDAChunksRawTx(chunks)
if err != nil {
return nil, fmt.Errorf("failed to unpack chunks: %v, err: %w", batchIndex, err)
}
Expand Down Expand Up @@ -74,7 +60,7 @@ func NewCommitBatchDAV1WithBlobDecodeFunc(ctx context.Context, db ethdb.Database
}

// decode txs from blob
err = decodeTxsFromBlobFunc(blob, decodedChunks)
err = codec.DecodeTxsFromBlob(blob, decodedChunks)
if err != nil {
return nil, fmt.Errorf("failed to decode txs from blob: %w", err)
}
Expand All @@ -88,5 +74,5 @@ func NewCommitBatchDAV1WithBlobDecodeFunc(ctx context.Context, db ethdb.Database
}

func (c *CommitBatchDAV1) Type() Type {
return CommitBatchV1Type
return CommitBatchWithBlobType
}
40 changes: 0 additions & 40 deletions rollup/da_syncer/da/commitV2.go

This file was deleted.

40 changes: 0 additions & 40 deletions rollup/da_syncer/da/commitV4.go

This file was deleted.

8 changes: 2 additions & 6 deletions rollup/da_syncer/da/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ type Type int
const (
// CommitBatchV0Type contains data of event of CommitBatchV0Type
CommitBatchV0Type Type = iota
// CommitBatchV1Type contains data of event of CommitBatchV1Type
CommitBatchV1Type
// CommitBatchV2Type contains data of event of CommitBatchV2Type
CommitBatchV2Type
// CommitBatchV4Type contains data of event of CommitBatchV2Type
CommitBatchV4Type
// CommitBatchWithBlobType contains data of event of CommitBatchWithBlobType (v1, v2, v3, v4)
CommitBatchWithBlobType
// RevertBatchType contains data of event of RevertBatchType
RevertBatchType
// FinalizeBatchType contains data of event of FinalizeBatchType
Expand Down
Loading