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

test: add unit test for Stage: L1 syncer #1474

Draft
wants to merge 3 commits into
base: zkevm
Choose a base branch
from
Draft
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
151 changes: 0 additions & 151 deletions cmd/rpcdaemon/commands/mocks/l1_syncer_mock.go

This file was deleted.

16 changes: 8 additions & 8 deletions erigon-lib/direct/sentry_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion zk/stages/stage_l1_info_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
ethereum "github.com/ledgerwatch/erigon"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/commands/mocks"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/erigon/zk/l1infotree"
"github.com/ledgerwatch/erigon/zk/syncer"
"github.com/ledgerwatch/erigon/zk/syncer/mocks"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion zk/stages/stage_l1_sequencer_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
ethereum "github.com/ledgerwatch/erigon"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/commands/mocks"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/ledgerwatch/erigon/zk/contracts"
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/erigon/zk/syncer"
"github.com/ledgerwatch/erigon/zk/syncer/mocks"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
4 changes: 1 addition & 3 deletions zk/stages/stage_l1syncer.go → zk/stages/stage_l1_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

type IL1Syncer interface {

// atomic
IsSyncStarted() bool
IsDownloading() bool
Expand All @@ -46,8 +45,7 @@ type IL1Syncer interface {
}

var (
ErrStateRootMismatch = errors.New("state root mismatch")

ErrStateRootMismatch = errors.New("state root mismatch")
lastCheckedL1BlockCounter = metrics.GetOrCreateGauge(`last_checked_l1_block`)
)

Expand Down
171 changes: 171 additions & 0 deletions zk/stages/stage_l1_syncer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package stages

import (
"context"
"math/big"
"testing"
"time"

ethereum "github.com/ledgerwatch/erigon"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/smt/pkg/db"
"github.com/ledgerwatch/erigon/zk/contracts"
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/erigon/zk/syncer"
"github.com/ledgerwatch/erigon/zk/syncer/mocks"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestSpawnStageL1Syncer(t *testing.T) {
// Arrange
ctx, db1 := context.Background(), memdb.NewTestDB(t)
tx := memdb.BeginRw(t, db1)
err := hermez_db.CreateHermezBuckets(tx)
require.NoError(t, err)
err = db.CreateEriDbBuckets(tx)
require.NoError(t, err)

l1FirstBlock := big.NewInt(20)
l2BlockNumber := uint64(10)
verifiedBatchNumber := uint64(2)

hDB := hermez_db.NewHermezDb(tx)
err = hDB.WriteBlockBatch(0, 0)
require.NoError(t, err)
err = hDB.WriteBlockBatch(l2BlockNumber-1, verifiedBatchNumber-1)
require.NoError(t, err)
err = hDB.WriteBlockBatch(l2BlockNumber, verifiedBatchNumber)
require.NoError(t, err)
err = stages.SaveStageProgress(tx, stages.L1Syncer, 0)
require.NoError(t, err)
err = stages.SaveStageProgress(tx, stages.IntermediateHashes, l2BlockNumber-1)
require.NoError(t, err)

err = hDB.WriteVerification(l1FirstBlock.Uint64(), verifiedBatchNumber-1, common.HexToHash("0x1"), common.HexToHash("0x99990"))
require.NoError(t, err)
err = hDB.WriteVerification(l1FirstBlock.Uint64(), verifiedBatchNumber, common.HexToHash("0x2"), common.HexToHash("0x99999"))
require.NoError(t, err)

genesisHeader := &types.Header{
Number: big.NewInt(0).SetUint64(l2BlockNumber - 1),
Time: 0,
Difficulty: big.NewInt(1),
GasLimit: 8000000,
GasUsed: 0,
ParentHash: common.HexToHash("0x1"),
TxHash: common.HexToHash("0x2"),
ReceiptHash: common.HexToHash("0x3"),
Root: common.HexToHash("0x99990"),
}

txs := []types.Transaction{}
uncles := []*types.Header{}
receipts := []*types.Receipt{}
withdrawals := []*types.Withdrawal{}

genesisBlock := types.NewBlock(genesisHeader, txs, uncles, receipts, withdrawals)

err = rawdb.WriteBlock(tx, genesisBlock)
require.NoError(t, err)
err = rawdb.WriteCanonicalHash(tx, genesisBlock.Hash(), genesisBlock.NumberU64())
require.NoError(t, err)

s := &stagedsync.StageState{ID: stages.L1Syncer, BlockNumber: 0}
u := &stagedsync.Sync{}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
EthermanMock := mocks.NewMockIEtherman(mockCtrl)

l1ContractAddresses := []common.Address{
common.HexToAddress("0x1"),
common.HexToAddress("0x2"),
common.HexToAddress("0x3"),
}
l1ContractTopics := [][]common.Hash{
[]common.Hash{common.HexToHash("0x1")},
[]common.Hash{common.HexToHash("0x2")},
[]common.Hash{common.HexToHash("0x3")},
}

latestBlockParentHash := common.HexToHash("0x123456789")
latestBlockTime := uint64(time.Now().Unix())
latestBlockNumber := big.NewInt(21)
latestBlockHeader := &types.Header{ParentHash: latestBlockParentHash, Number: latestBlockNumber, Time: latestBlockTime}
latestBlock := types.NewBlockWithHeader(latestBlockHeader)

EthermanMock.EXPECT().BlockByNumber(gomock.Any(), nil).Return(latestBlock, nil).AnyTimes()

filterQuery := ethereum.FilterQuery{
FromBlock: l1FirstBlock,
ToBlock: latestBlockNumber,
Addresses: l1ContractAddresses,
Topics: l1ContractTopics,
}

type testCase struct {
name string
getLog func(hDB *hermez_db.HermezDb) (types.Log, error)
assert func(t *testing.T, hDB *hermez_db.HermezDb)
}

testCases := []testCase{
// contracts.SequencedBatchTopicPreEtrog
// contracts.SequencedBatchTopicEtrog
// contracts.VerificationTopicPreEtrog
// contracts.VerificationValidiumTopicEtrog
// contracts.VerificationTopicEtrog
// contracts.UpdateL1InfoTreeTopic
// contracts.RollbackBatchesTopic
{
name: "",
getLog: func(hDB *hermez_db.HermezDb) (types.Log, error) {
batchNum := uint64(1)
batchNumHash := common.BytesToHash(big.NewInt(0).SetUint64(batchNum).Bytes())
return types.Log{
BlockNumber: latestBlockNumber.Uint64(),
Address: l1ContractAddresses[0],
Topics: []common.Hash{contracts.SequencedBatchTopicPreEtrog, batchNumHash},
Data: []byte{},
}, nil
},
assert: func(t *testing.T, hDB *hermez_db.HermezDb) {

},
},
}

filteredLogs := []types.Log{}
for _, tc := range testCases {
ll, err := tc.getLog(hDB)
require.NoError(t, err)
filteredLogs = append(filteredLogs, ll)
}

EthermanMock.EXPECT().FilterLogs(gomock.Any(), filterQuery).Return(filteredLogs, nil).AnyTimes()

l1Syncer := syncer.NewL1Syncer(ctx, []syncer.IEtherman{EthermanMock}, l1ContractAddresses, l1ContractTopics, 10, 0, "latest")

zkCfg := &ethconfig.Zk{
L1RollupId: uint64(1),
L1FirstBlock: l1FirstBlock.Uint64(),
}
cfg := StageL1SyncerCfg(db1, l1Syncer, zkCfg)
quiet := false

// Act
err = SpawnStageL1Syncer(s, u, ctx, tx, cfg, quiet)
require.NoError(t, err)

// Assert
for _, tc := range testCases {
tc.assert(t, hDB)
}
}
Loading
Loading