go-ethereum/rollup/rollup_sync_service/abi_test.go
colin 441212200a
feat(rollup): sync finalized batches from L1 (#515)
* feat(rollup): sync finalized batches from L1

* tweak

* address comments

* address comments

* tweak

* address comments

* Update rollup/eventwatcher/eventwatcher.go

Co-authored-by: Péter Garamvölgyi <peter@scroll.io>

* address comments

* address comments

* address comments

* add s.rollupSyncService.Stop()

* add block chunk batch tests

* fix goimports-lint

* address comments

* address comments

* remove

* add TestUnpackLog test

* add decodeChunkRanges tests

* add commit batch version check

* fix goimport & golint

* tweak

* fixes

* add TestRollupSyncServiceStartAndStop

* add TestGetChunkRanges

* address comments

* add TestValidateBatch

* add TestCalculateFinalizedBatchMeta

* address comments

* address comments

* fix

* address comments

* address comments

* fix

* tweak log

* fix

* tweak logs

* increase wait time

* add --rollup.verify flag

* add flag check in s.rollupSyncService.Stop()

* bump version

* refactor

* tweak configs

* tweak

* refactor getLocalInfoForBatch

* use syscall.Kill(os.Getpid(), syscall.SIGTERM) to shutdown

* refactor decodeChunkBlockRanges

* nit

* address comments

* address comments

* add WithdrawRoot and StateRoot in FinalizedBatchMeta

* address comments

* address comments

* add ctx.Err() check in retry

* add configs in gen_config.go

* bump version

* fix goimport & golint

* try to fix goimport

* revert change

* fix goimport & golint

* tweak

* bump version

* add l2 block -> batch index mapping

* bump version

* add mainnet config

* bump version

* address comments

* rename some vars

* feat: add more detailed logs in crash case (#547)

add more detailed logs in crash case

* trigger ci

* re-add batch finalization progress log

* remove block number -> batch index mapping

---------

Co-authored-by: Péter Garamvölgyi <peter@scroll.io>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
2023-10-28 06:29:31 +08:00

82 lines
2.5 KiB
Go

package rollup_sync_service
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto"
)
func TestEventSignatures(t *testing.T) {
scrollChainABI, err := scrollChainMetaData.GetAbi()
if err != nil {
t.Fatal("failed to get scroll chain abi", "err", err)
}
assert.Equal(t, crypto.Keccak256Hash([]byte("CommitBatch(uint256,bytes32)")), scrollChainABI.Events["CommitBatch"].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("RevertBatch(uint256,bytes32)")), scrollChainABI.Events["RevertBatch"].ID)
assert.Equal(t, crypto.Keccak256Hash([]byte("FinalizeBatch(uint256,bytes32,bytes32,bytes32)")), scrollChainABI.Events["FinalizeBatch"].ID)
}
func TestUnpackLog(t *testing.T) {
scrollChainABI, err := scrollChainMetaData.GetAbi()
require.NoError(t, err)
mockBatchIndex := big.NewInt(123)
mockBatchHash := crypto.Keccak256Hash([]byte("mockBatch"))
mockStateRoot := crypto.Keccak256Hash([]byte("mockStateRoot"))
mockWithdrawRoot := crypto.Keccak256Hash([]byte("mockWithdrawRoot"))
tests := []struct {
eventName string
mockLog types.Log
expected interface{}
out interface{}
}{
{
"CommitBatch",
types.Log{
Data: []byte{},
Topics: []common.Hash{scrollChainABI.Events["CommitBatch"].ID, common.BigToHash(mockBatchIndex), mockBatchHash},
},
&L1CommitBatchEvent{BatchIndex: mockBatchIndex, BatchHash: mockBatchHash},
&L1CommitBatchEvent{},
},
{
"RevertBatch",
types.Log{
Data: []byte{},
Topics: []common.Hash{scrollChainABI.Events["RevertBatch"].ID, common.BigToHash(mockBatchIndex), mockBatchHash},
},
&L1RevertBatchEvent{BatchIndex: mockBatchIndex, BatchHash: mockBatchHash},
&L1RevertBatchEvent{},
},
{
"FinalizeBatch",
types.Log{
Data: append(mockStateRoot.Bytes(), mockWithdrawRoot.Bytes()...),
Topics: []common.Hash{scrollChainABI.Events["FinalizeBatch"].ID, common.BigToHash(mockBatchIndex), mockBatchHash},
},
&L1FinalizeBatchEvent{
BatchIndex: mockBatchIndex,
BatchHash: mockBatchHash,
StateRoot: mockStateRoot,
WithdrawRoot: mockWithdrawRoot,
},
&L1FinalizeBatchEvent{},
},
}
for _, tt := range tests {
t.Run(tt.eventName, func(t *testing.T) {
err := UnpackLog(scrollChainABI, tt.out, tt.eventName, tt.mockLog)
assert.NoError(t, err)
assert.Equal(t, tt.expected, tt.out)
})
}
}