Skip to content

Commit

Permalink
add testCommitBatchAndFinalizeBundleCodecV7 for relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis committed Feb 10, 2025
1 parent 8db5339 commit be7dfa7
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 13 deletions.
13 changes: 1 addition & 12 deletions rollup/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,9 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/scroll-tech/da-codec v0.1.3-0.20250205043214-894a93bb6b53 h1:07eHUuUhQ2rOP6ij5+KV1MgQurkwyweomkxp5nAG6o8=
github.com/scroll-tech/da-codec v0.1.3-0.20250205043214-894a93bb6b53/go.mod h1:irqXJdRI5fsGkilJCpNTnJb8oV8KR51j68QXIWoth6U=
github.com/scroll-tech/da-codec v0.1.3-0.20250207090156-f4b274c7bc1f h1:v63UX3vFLBA2KDLh26y0FEK2RQFOhq2z7+Dc7UUPN5c=
github.com/scroll-tech/da-codec v0.1.3-0.20250207090156-f4b274c7bc1f/go.mod h1:UZhhjzqYsyEhcvY0Y+SP+oMdeOUqFn/UXpbAYuPGzg0=
github.com/scroll-tech/da-codec v0.1.3-0.20250209233450-14d07e75cf31 h1:PTTelRlF/X6dprfKgrjd5kbiI82a4VtyMDFeQ1lqxHY=
github.com/scroll-tech/da-codec v0.1.3-0.20250209233450-14d07e75cf31/go.mod h1:UZhhjzqYsyEhcvY0Y+SP+oMdeOUqFn/UXpbAYuPGzg0=
github.com/scroll-tech/da-codec v0.1.3-0.20250209234523-cfb316bd5d65 h1:5OLPpOmvg0N6UignItMVeMSibXlGoVAdzprmxgmnMRM=
github.com/scroll-tech/da-codec v0.1.3-0.20250209234523-cfb316bd5d65/go.mod h1:UZhhjzqYsyEhcvY0Y+SP+oMdeOUqFn/UXpbAYuPGzg0=
github.com/scroll-tech/da-codec v0.1.3-0.20250210001855-2171a439ff1a h1:rz66/mKGbFdDdJNovhVu8sd8E8DTpNdWmCsPmIwf7CE=
github.com/scroll-tech/da-codec v0.1.3-0.20250210001855-2171a439ff1a/go.mod h1:UZhhjzqYsyEhcvY0Y+SP+oMdeOUqFn/UXpbAYuPGzg0=
github.com/scroll-tech/da-codec v0.1.3-0.20250210041951-d028c537b995 h1:Zo1p42CUS9pADSKoDD0ZoDxf4dQ3gttqWZlV+RSeImk=
github.com/scroll-tech/da-codec v0.1.3-0.20250210041951-d028c537b995/go.mod h1:UZhhjzqYsyEhcvY0Y+SP+oMdeOUqFn/UXpbAYuPGzg0=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250129031936-44c72cd3fa47 h1:JbYYsGZMU1yAYsF/Ds6STMlItYy0SXj86nqdXC7nHyk=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250129031936-44c72cd3fa47/go.mod h1:8WbNuuUjie/LTdFXGGT7Z711MRW8Vv2zWLrcibg7hDc=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250206083728-ea43834c198f h1:WgIRuMWa7Q/xD1LHPEbQ9PpltasNiYR04qFzatiP/R0=
github.com/scroll-tech/go-ethereum v1.10.14-0.20250206083728-ea43834c198f/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=
Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ func (r *Layer2Relayer) constructCommitBatchPayloadCodecV7(batchesToSubmit []*db
// TODO: this needs to be updated once the contract interface is finalized
calldata, err := r.l1RollupABI.Pack("commitBatches", version, firstParentBatch.BatchHeader)
if err != nil {
return nil, nil, 0, 0, fmt.Errorf("failed to pack commitBatchWithBlobProof: %w", err)
return nil, nil, 0, 0, fmt.Errorf("failed to pack commitBatches: %w", err)
}
return calldata, blobs, maxBlockHeight, totalGasUsed, nil
}
Expand Down
1 change: 1 addition & 0 deletions rollup/tests/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestFunction(t *testing.T) {
// l1 rollup and watch rollup events
t.Run("TestCommitAndFinalizeGenesisBatch", testCommitAndFinalizeGenesisBatch)
t.Run("TestCommitBatchAndFinalizeBundleCodecV4", testCommitBatchAndFinalizeBundleCodecV4)
t.Run("TestCommitBatchAndFinalizeBundleCodecV7", testCommitBatchAndFinalizeBundleCodecV7)

// l1/l2 gas oracle
t.Run("TestImportL1GasPrice", testImportL1GasPrice)
Expand Down
215 changes: 215 additions & 0 deletions rollup/tests/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"context"
"fmt"
"math"
"math/big"
"testing"
Expand All @@ -12,6 +13,7 @@ import (
gethTypes "github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/params"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"scroll-tech/common/database"
"scroll-tech/common/types"
Expand Down Expand Up @@ -216,3 +218,216 @@ func testCommitBatchAndFinalizeBundleCodecV4(t *testing.T) {
database.CloseDB(db)
}
}

func testCommitBatchAndFinalizeBundleCodecV7(t *testing.T) {
db := setupDB(t)

prepareContracts(t)

chainConfig := &params.ChainConfig{
LondonBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DarwinTime: new(uint64),
DarwinV2Time: new(uint64),
EuclidTime: new(uint64),
EuclidV2Time: new(uint64),
}

// Create L2Relayer
l2Cfg := rollupApp.Config.L2Config
l2Relayer, err := relayer.NewLayer2Relayer(context.Background(), l2Client, db, l2Cfg.RelayerConfig, chainConfig, true, relayer.ServiceTypeL2RollupRelayer, nil)
require.NoError(t, err)

defer l2Relayer.StopSenders()
defer database.CloseDB(db)

// add some blocks to db
var blocks []*encoding.Block
genesis, err := l2Client.HeaderByNumber(context.Background(), big.NewInt(0))
require.NoError(t, err)

var l1MessageIndex uint64 = 0
parentHash := genesis.Hash()
for i := int64(0); i < 10; i++ {
header := gethTypes.Header{
Number: big.NewInt(i + 1),
ParentHash: parentHash,
Difficulty: big.NewInt(i + 1),
BaseFee: big.NewInt(i + 1),
Root: common.HexToHash("0x1"),
}
fmt.Println("block number: ", i+1, header.Hash(), parentHash)
var transactions []*gethTypes.TransactionData
if i%2 == 0 {
txs := []*gethTypes.Transaction{
gethTypes.NewTx(&gethTypes.L1MessageTx{
QueueIndex: l1MessageIndex,
Gas: 0,
To: &common.Address{1, 2, 3},
Value: big.NewInt(10),
Data: nil,
Sender: common.Address{1, 2, 3},
}),
}
transactions = append(transactions, encoding.TxsToTxsData(txs)...)
l1MessageIndex++
}

blocks = append(blocks, &encoding.Block{
Header: &header,
Transactions: transactions,
WithdrawRoot: common.HexToHash("0x2"),
RowConsumption: &gethTypes.RowConsumption{},
})
parentHash = header.Hash()
}

cp := watcher.NewChunkProposer(context.Background(), &config.ChunkProposerConfig{
MaxBlockNumPerChunk: 100,
MaxTxNumPerChunk: 10000,
MaxL1CommitGasPerChunk: 50000000000,
MaxL1CommitCalldataSizePerChunk: 1000000,
MaxRowConsumptionPerChunk: 1048319,
ChunkTimeoutSec: 300,
MaxUncompressedBatchBytesSize: math.MaxUint64,
}, encoding.CodecV7, chainConfig, db, nil)

bap := watcher.NewBatchProposer(context.Background(), &config.BatchProposerConfig{
MaxL1CommitGasPerBatch: 50000000000,
MaxL1CommitCalldataSizePerBatch: 1000000,
BatchTimeoutSec: 300,
MaxUncompressedBatchBytesSize: math.MaxUint64,
}, encoding.CodecV7, chainConfig, db, nil)

bup := watcher.NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: 1000000,
BundleTimeoutSec: 300,
}, encoding.CodecV7, chainConfig, db, nil)

l2BlockOrm := orm.NewL2Block(db)
batchOrm := orm.NewBatch(db)
bundleOrm := orm.NewBundle(db)

fmt.Println("insert first 5 blocks ------------------------")
err = l2BlockOrm.InsertL2Blocks(context.Background(), blocks[:5])
require.NoError(t, err)
batch1ExpectedLastL1MessageQueueHash, err := encoding.MessageQueueV2ApplyL1MessagesFromBlocks(common.Hash{}, blocks[:5])
require.NoError(t, err)

cp.TryProposeChunk()
bap.TryProposeBatch()

fmt.Println("insert last 5 blocks ------------------------")
err = l2BlockOrm.InsertL2Blocks(context.Background(), blocks[5:])
require.NoError(t, err)
batch2ExpectedLastL1MessageQueueHash, err := encoding.MessageQueueV2ApplyL1MessagesFromBlocks(batch1ExpectedLastL1MessageQueueHash, blocks[5:])
require.NoError(t, err)

cp.TryProposeChunk()
bap.TryProposeBatch()

bup.TryProposeBundle() // The proposed bundle contains two batches when codec version is codecv3.

// make sure that batches are created as expected
require.Eventually(t, func() bool {
batches, getErr := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
if getErr != nil {
return false
}
if len(batches) != 3 {
return false
}

// batches[0] is the genesis batch, no need to check

// assert correctness of L1 message queue hashes
require.Equal(t, common.Hash{}, common.HexToHash(batches[1].InitialL1MessageQueueHash))
require.Equal(t, batch1ExpectedLastL1MessageQueueHash, common.HexToHash(batches[1].LastL1MessageQueueHash))
require.Equal(t, batch1ExpectedLastL1MessageQueueHash, common.HexToHash(batches[2].InitialL1MessageQueueHash))
require.Equal(t, batch2ExpectedLastL1MessageQueueHash, common.HexToHash(batches[2].LastL1MessageQueueHash))

return true
}, 30*time.Second, time.Second)

// simulate proof generation -> all batches and bundle are verified
{
batchProof := &message.BatchProof{
Proof: []byte{0, 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},
Instances: []byte{0, 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},
Vk: []byte{0, 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},
}
batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
require.NoError(t, err)
batches = batches[1:]
for _, batch := range batches {
err = batchOrm.UpdateProofByHash(context.Background(), batch.Hash, batchProof, 100)
require.NoError(t, err)
err = batchOrm.UpdateProvingStatus(context.Background(), batch.Hash, types.ProvingTaskVerified)
require.NoError(t, err)
}

bundleProof := &message.BundleProof{
Proof: []byte{0, 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},
Instances: []byte{0, 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},
Vk: []byte{0, 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},
}
bundles, err := bundleOrm.GetBundles(context.Background(), map[string]interface{}{}, nil, 0)
require.NoError(t, err)
for _, bundle := range bundles {
err = bundleOrm.UpdateProofAndProvingStatusByHash(context.Background(), bundle.Hash, bundleProof, types.ProvingTaskVerified, 100)
require.NoError(t, err)
}
}

return
// TODO: assert that batches have been submitted together in a single transaction after contract ABI is updated
//for _, batch := range batches {
// fmt.Println("batch hash: ", batch.Hash, batch.Index, batch.RollupStatus)
// //if types.RollupCommitted != types.RollupStatus(batch.RollupStatus) {
// // return false
// //}
//}
//l2Relayer.ProcessPendingBatches()
//
//assert.Eventually(t, func() bool {
// l2Relayer.ProcessPendingBundles()
//
// batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, nil, 0)
// assert.NoError(t, err)
// assert.Len(t, batches, 3)
// batches = batches[1:]
// for _, batch := range batches {
// if types.RollupStatus(batch.RollupStatus) != types.RollupFinalized {
// return false
// }
//
// assert.NotEmpty(t, batch.FinalizeTxHash)
// receipt, getErr := l1Client.TransactionReceipt(context.Background(), common.HexToHash(batch.FinalizeTxHash))
// assert.NoError(t, getErr)
// assert.Equal(t, gethTypes.ReceiptStatusSuccessful, receipt.Status)
// }
//
// bundles, err := bundleOrm.GetBundles(context.Background(), map[string]interface{}{}, nil, 0)
// assert.NoError(t, err)
// assert.Len(t, bundles, 1)
//
// bundle := bundles[0]
// if types.RollupStatus(bundle.RollupStatus) != types.RollupFinalized {
// return false
// }
// assert.NotEmpty(t, bundle.FinalizeTxHash)
// receipt, err := l1Client.TransactionReceipt(context.Background(), common.HexToHash(bundle.FinalizeTxHash))
// assert.NoError(t, err)
// assert.Equal(t, gethTypes.ReceiptStatusSuccessful, receipt.Status)
// batches, err = batchOrm.GetBatches(context.Background(), map[string]interface{}{"bundle_hash": bundle.Hash}, nil, 0)
// assert.NoError(t, err)
// assert.Len(t, batches, 2)
// for _, batch := range batches {
// assert.Equal(t, batch.RollupStatus, bundle.RollupStatus)
// assert.Equal(t, bundle.FinalizeTxHash, batch.FinalizeTxHash)
// }
//
// return true
//}, 30*time.Second, time.Second)
}

0 comments on commit be7dfa7

Please sign in to comment.