go-ethereum/rollup/rollup_sync_service/l1client_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

70 lines
2.1 KiB
Go

package rollup_sync_service
import (
"context"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/scroll-tech/go-ethereum"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/rlp"
)
func TestL1Client(t *testing.T) {
ctx := context.Background()
mockClient := &mockEthClient{}
scrollChainABI, err := scrollChainMetaData.GetAbi()
if err != nil {
t.Fatal("failed to get scroll chain abi", "err", err)
}
scrollChainAddress := common.HexToAddress("0x0123456789abcdef")
l1Client, err := newL1Client(ctx, mockClient, 11155111, scrollChainAddress, scrollChainABI)
require.NoError(t, err, "Failed to initialize L1Client")
blockNumber, err := l1Client.getLatestFinalizedBlockNumber(ctx)
assert.NoError(t, err, "Error getting latest confirmed block number")
assert.Equal(t, uint64(36), blockNumber, "Unexpected block number")
logs, err := l1Client.fetchRollupEventsInRange(ctx, 0, blockNumber)
assert.NoError(t, err, "Error fetching rollup events in range")
assert.Empty(t, logs, "Expected no logs from fetchRollupEventsInRange")
}
type mockEthClient struct {
commitBatchRLP []byte
}
func (m *mockEthClient) BlockNumber(ctx context.Context) (uint64, error) {
return 11155111, nil
}
func (m *mockEthClient) ChainID(ctx context.Context) (*big.Int, error) {
return big.NewInt(11155111), nil
}
func (m *mockEthClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
return []types.Log{}, nil
}
func (m *mockEthClient) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return &types.Header{
Number: big.NewInt(100 - 64),
}, nil
}
func (m *mockEthClient) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
return nil, nil
}
func (m *mockEthClient) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) {
var tx types.Transaction
if err := rlp.DecodeBytes(m.commitBatchRLP, &tx); err != nil {
return nil, false, err
}
return &tx, false, nil
}