l1_traversal_test.go 4.31 KB
Newer Older
protolambda's avatar
protolambda committed
1 2 3
package derive

import (
4
	"context"
protolambda's avatar
protolambda committed
5
	"errors"
6
	"io"
protolambda's avatar
protolambda committed
7 8 9
	"math/rand"
	"testing"

10 11
	"github.com/stretchr/testify/require"

12 13 14 15 16 17
	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"

	"github.com/ethereum-optimism/optimism/op-node/rollup"
18
	"github.com/ethereum-optimism/optimism/op-service/eth"
19
	"github.com/ethereum-optimism/optimism/op-service/testlog"
Sabnock01's avatar
Sabnock01 committed
20
	"github.com/ethereum-optimism/optimism/op-service/testutils"
protolambda's avatar
protolambda committed
21 22
)

23 24 25 26 27 28
// TestL1TraversalNext tests that the `Next` function only returns
// a block reference once and then properly returns io.EOF afterwards
func TestL1TraversalNext(t *testing.T) {
	rng := rand.New(rand.NewSource(1234))
	a := testutils.RandomBlockRef(rng)
	// Load up the initial state with a reset
29 30 31 32 33 34 35 36 37 38 39 40 41
	l1Cfg := eth.SystemConfig{
		BatcherAddr: testutils.RandomAddress(rng),
		Overhead:    [32]byte{42},
		Scalar:      [32]byte{69},
	}
	sysCfgAddr := testutils.RandomAddress(rng)
	cfg := &rollup.Config{
		Genesis:               rollup.Genesis{SystemConfig: l1Cfg},
		L1SystemConfigAddress: sysCfgAddr,
	}
	tr := NewL1Traversal(testlog.Logger(t, log.LvlError), cfg, nil)

	_ = tr.Reset(context.Background(), a, l1Cfg)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

	// First call should always succeed
	ref, err := tr.NextL1Block(context.Background())
	require.Nil(t, err)
	require.Equal(t, a, ref)

	// Subsequent calls should return io.EOF
	ref, err = tr.NextL1Block(context.Background())
	require.Equal(t, eth.L1BlockRef{}, ref)
	require.Equal(t, io.EOF, err)

	ref, err = tr.NextL1Block(context.Background())
	require.Equal(t, eth.L1BlockRef{}, ref)
	require.Equal(t, io.EOF, err)
}

// TestL1TraversalAdvance tests that the `Advance` function properly
// handles different error cases and returns the expected block ref
// if there is no error.
func TestL1TraversalAdvance(t *testing.T) {
protolambda's avatar
protolambda committed
62 63 64
	rng := rand.New(rand.NewSource(1234))
	a := testutils.RandomBlockRef(rng)
	b := testutils.NextRandomRef(rng, a)
65 66 67
	// x is at the same height as b but does not extend `a`
	x := testutils.RandomBlockRef(rng)
	x.Number = b.Number
68
	sysCfgAddr := testutils.RandomAddress(rng)
69 70

	tests := []struct {
71 72 73 74 75 76 77
		name         string
		startBlock   eth.L1BlockRef
		nextBlock    eth.L1BlockRef
		initialL1Cfg eth.SystemConfig
		l1Receipts   []*types.Receipt
		fetcherErr   error
		expectedErr  error
78 79
	}{
		{
80 81 82 83 84 85 86 87 88
			name:       "simple extension",
			startBlock: a,
			nextBlock:  b,
			initialL1Cfg: eth.SystemConfig{
				BatcherAddr: common.Address{11},
				Overhead:    [32]byte{22},
				Scalar:      [32]byte{33},
			},
			l1Receipts:  []*types.Receipt{},
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
			fetcherErr:  nil,
			expectedErr: nil,
		},
		{
			name:        "reorg",
			startBlock:  a,
			nextBlock:   x,
			fetcherErr:  nil,
			expectedErr: ErrReset,
		},
		{
			name:        "not found",
			startBlock:  a,
			nextBlock:   eth.L1BlockRef{},
			fetcherErr:  ethereum.NotFound,
			expectedErr: io.EOF,
		},
		{
			name:        "temporary error",
			startBlock:  a,
			nextBlock:   eth.L1BlockRef{},
			fetcherErr:  errors.New("interrupted connection"),
			expectedErr: ErrTemporary,
		},
113
		// TODO: add tests that cover the receipts to config data updates
114 115 116 117 118 119
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			src := &testutils.MockL1Source{}
			src.ExpectL1BlockRefByNumber(test.startBlock.Number+1, test.nextBlock, test.fetcherErr)
120 121 122 123 124 125 126 127 128 129
			info := &testutils.MockBlockInfo{
				InfoHash:       test.nextBlock.Hash,
				InfoParentHash: test.nextBlock.ParentHash,
				InfoNum:        test.nextBlock.Number,
				InfoTime:       test.nextBlock.Time,
				// TODO: don't need full L1 info in receipts fetching API maybe?
			}
			if test.l1Receipts != nil {
				src.ExpectFetchReceipts(test.nextBlock.Hash, info, test.l1Receipts, nil)
			}
130

131 132 133 134 135
			cfg := &rollup.Config{
				Genesis:               rollup.Genesis{SystemConfig: test.initialL1Cfg},
				L1SystemConfigAddress: sysCfgAddr,
			}
			tr := NewL1Traversal(testlog.Logger(t, log.LvlError), cfg, src)
136
			// Load up the initial state with a reset
137
			_ = tr.Reset(context.Background(), test.startBlock, test.initialL1Cfg)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

			// Advance it + assert output
			err := tr.AdvanceL1Block(context.Background())
			require.ErrorIs(t, err, test.expectedErr)

			if test.expectedErr == nil {
				ref, err := tr.NextL1Block(context.Background())
				require.Nil(t, err)
				require.Equal(t, test.nextBlock, ref)
			}

			src.AssertExpectations(t)
		})
	}

protolambda's avatar
protolambda committed
153
}