start_test.go 7.94 KB
Newer Older
1 2 3 4 5 6
package sync

import (
	"context"
	"testing"

7 8
	"github.com/stretchr/testify/require"

9
	"github.com/ethereum-optimism/optimism/op-node/rollup"
10
	"github.com/ethereum-optimism/optimism/op-service/eth"
11
	"github.com/ethereum-optimism/optimism/op-service/testlog"
Sabnock01's avatar
Sabnock01 committed
12
	"github.com/ethereum-optimism/optimism/op-service/testutils"
protolambda's avatar
protolambda committed
13
	"github.com/ethereum/go-ethereum/common"
14 15 16 17 18 19 20 21 22 23
	"github.com/ethereum/go-ethereum/log"
)

var _ L1Chain = (*testutils.FakeChainSource)(nil)
var _ L2Chain = (*testutils.FakeChainSource)(nil)

// generateFakeL2 creates a fake L2 chain with the following conditions:
// - The L2 chain is based off of the L1 chain
// - The actual L1 chain is the New L1 chain
// - Both heads are at the tip of their respective chains
protolambda's avatar
protolambda committed
24
func (c *syncStartTestCase) generateFakeL2(t *testing.T) (*testutils.FakeChainSource, rollup.Genesis) {
25
	t.Helper()
26 27 28 29 30 31 32 33
	log := testlog.Logger(t, log.LvlError)
	chain := testutils.NewFakeChainSource([]string{c.L1, c.NewL1}, []string{c.L2}, int(c.GenesisL1Num), log)
	chain.SetL2Head(len(c.L2) - 1)
	genesis := testutils.FakeGenesis(c.GenesisL1, c.GenesisL2, int(c.GenesisL1Num))
	chain.ReorgL1()
	for i := 0; i < len(c.NewL1)-1; i++ {
		chain.AdvanceL1()
	}
protolambda's avatar
protolambda committed
34 35 36
	return chain, genesis

}
37

protolambda's avatar
protolambda committed
38 39 40 41
func runeToHash(id rune) common.Hash {
	var h common.Hash
	copy(h[:], string(id))
	return h
42 43 44 45 46 47 48 49 50
}

type syncStartTestCase struct {
	Name string

	L1    string // L1 Chain prior to a re-org or other change
	L2    string // L2 Chain that follows from L1Chain
	NewL1 string // New L1 chain

protolambda's avatar
protolambda committed
51 52 53
	PreFinalizedL2 rune
	PreSafeL2      rune

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	GenesisL1    rune
	GenesisL1Num uint64
	GenesisL2    rune

	SeqWindowSize uint64
	SafeL2Head    rune
	UnsafeL2Head  rune
	ExpectedErr   error
}

func refToRune(r eth.BlockID) rune {
	return rune(r.Hash.Bytes()[0])
}

func (c *syncStartTestCase) Run(t *testing.T) {
protolambda's avatar
protolambda committed
69 70 71
	chain, genesis := c.generateFakeL2(t)
	chain.SetL2Finalized(runeToHash(c.PreFinalizedL2))
	chain.SetL2Safe(runeToHash(c.PreSafeL2))
72

protolambda's avatar
protolambda committed
73 74 75 76
	cfg := &rollup.Config{
		Genesis:       genesis,
		SeqWindowSize: c.SeqWindowSize,
	}
77 78
	lgr := log.New()
	lgr.SetHandler(log.DiscardHandler())
79
	result, err := FindL2Heads(context.Background(), cfg, chain, chain, lgr, &Config{})
80
	if c.ExpectedErr != nil {
protolambda's avatar
protolambda committed
81 82
		require.ErrorIs(t, err, c.ExpectedErr, "expected error")
		return
83
	} else {
protolambda's avatar
protolambda committed
84 85
		require.NoError(t, err, "expected no error")
	}
86

protolambda's avatar
protolambda committed
87 88
	gotUnsafeHead := refToRune(result.Unsafe.ID())
	require.Equal(t, string(c.UnsafeL2Head), string(gotUnsafeHead), "Unsafe L2 Head not equal")
89

protolambda's avatar
protolambda committed
90 91
	gotSafeHead := refToRune(result.Safe.ID())
	require.Equal(t, string(c.SafeL2Head), string(gotSafeHead), "Safe L2 Head not equal")
92 93 94 95 96
}

func TestFindSyncStart(t *testing.T) {
	testCases := []syncStartTestCase{
		{
protolambda's avatar
protolambda committed
97 98 99 100 101 102 103 104 105 106 107 108 109
			Name:           "already synced",
			GenesisL1Num:   0,
			L1:             "ab",
			L2:             "AB",
			NewL1:          "ab",
			PreFinalizedL2: 'A',
			PreSafeL2:      'A',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'B',
			SeqWindowSize:  2,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
110 111
		},
		{
protolambda's avatar
protolambda committed
112 113 114 115 116 117 118 119 120 121 122 123 124
			Name:           "small reorg long chain",
			GenesisL1Num:   0,
			L1:             "abcdefgh",
			L2:             "ABCDEFGH",
			NewL1:          "abcdefgx",
			PreFinalizedL2: 'B',
			PreSafeL2:      'H',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'G',
			SeqWindowSize:  2,
			SafeL2Head:     'C',
			ExpectedErr:    nil,
125 126
		},
		{
protolambda's avatar
protolambda committed
127 128 129 130 131 132 133 134 135 136 137 138 139
			Name:           "L1 Chain ahead",
			GenesisL1Num:   0,
			L1:             "abcdef",
			L2:             "ABCDE",
			NewL1:          "abcdef",
			PreFinalizedL2: 'A',
			PreSafeL2:      'D',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'E',
			SeqWindowSize:  2,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
140 141
		},
		{
protolambda's avatar
protolambda committed
142 143 144 145 146 147 148 149 150 151 152 153 154
			Name:           "L2 Chain ahead after reorg",
			GenesisL1Num:   0,
			L1:             "abcxyz",
			L2:             "ABCXYZ",
			NewL1:          "abcx",
			PreFinalizedL2: 'B',
			PreSafeL2:      'X',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'Z',
			SeqWindowSize:  2,
			SafeL2Head:     'B',
			ExpectedErr:    nil,
155 156
		},
		{
protolambda's avatar
protolambda committed
157 158 159 160 161 162 163 164 165 166 167 168 169
			Name:           "genesis",
			GenesisL1Num:   0,
			L1:             "a",
			L2:             "A",
			NewL1:          "a",
			PreFinalizedL2: 'A',
			PreSafeL2:      'A',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'A',
			SeqWindowSize:  2,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
170 171
		},
		{
protolambda's avatar
protolambda committed
172 173 174 175 176 177 178 179 180 181 182 183 184
			Name:           "reorg one step back",
			GenesisL1Num:   0,
			L1:             "abcdefg",
			L2:             "ABCDEFG",
			NewL1:          "abcdefx",
			PreFinalizedL2: 'A',
			PreSafeL2:      'E',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'F',
			SeqWindowSize:  3,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
185 186
		},
		{
protolambda's avatar
protolambda committed
187 188 189 190 191 192 193 194 195 196 197 198 199
			Name:           "reorg two steps back, clip genesis and finalized",
			GenesisL1Num:   0,
			L1:             "abc",
			L2:             "ABC",
			PreFinalizedL2: 'A',
			PreSafeL2:      'B',
			NewL1:          "axy",
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'A',
			SeqWindowSize:  2,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
200 201
		},
		{
protolambda's avatar
protolambda committed
202 203 204 205 206 207 208 209 210 211 212 213 214
			Name:           "reorg three steps back",
			GenesisL1Num:   0,
			L1:             "abcdefgh",
			L2:             "ABCDEFGH",
			NewL1:          "abcdexyz",
			PreFinalizedL2: 'A',
			PreSafeL2:      'D',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'E',
			SeqWindowSize:  2,
			SafeL2Head:     'A',
			ExpectedErr:    nil,
215 216
		},
		{
protolambda's avatar
protolambda committed
217 218 219 220 221 222 223 224 225 226
			Name:           "unexpected L1 chain",
			GenesisL1Num:   0,
			L1:             "abcdef",
			L2:             "ABCDEF",
			NewL1:          "xyzwio",
			PreFinalizedL2: 'A',
			PreSafeL2:      'B',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   0,
227
			SeqWindowSize:  2,
protolambda's avatar
protolambda committed
228
			ExpectedErr:    WrongChainErr,
229 230
		},
		{
protolambda's avatar
protolambda committed
231 232 233 234 235 236 237 238 239 240
			Name:           "unexpected L2 chain",
			GenesisL1Num:   0,
			L1:             "abcdef",
			L2:             "ABCDEF",
			NewL1:          "xyzwio",
			PreFinalizedL2: 'A',
			PreSafeL2:      'B',
			GenesisL1:      'a',
			GenesisL2:      'X',
			UnsafeL2Head:   0,
241
			SeqWindowSize:  2,
protolambda's avatar
protolambda committed
242
			ExpectedErr:    WrongChainErr,
243 244
		},
		{
protolambda's avatar
protolambda committed
245 246 247 248 249 250 251 252 253 254 255 256 257
			Name:           "offset L2 genesis",
			GenesisL1Num:   3,
			L1:             "abcdefghi",
			L2:             "DEFGHI",
			NewL1:          "abcdefghi",
			PreFinalizedL2: 'E',
			PreSafeL2:      'H',
			GenesisL1:      'd',
			GenesisL2:      'D',
			UnsafeL2Head:   'I',
			SeqWindowSize:  2,
			SafeL2Head:     'E',
			ExpectedErr:    nil,
258 259
		},
		{
protolambda's avatar
protolambda committed
260 261 262 263 264 265 266 267 268 269 270 271 272
			Name:           "offset L2 genesis reorg",
			GenesisL1Num:   3,
			L1:             "abcdefgh",
			L2:             "DEFGH",
			NewL1:          "abcdxyzw",
			PreFinalizedL2: 'D',
			PreSafeL2:      'D',
			GenesisL1:      'd',
			GenesisL2:      'D',
			UnsafeL2Head:   'D',
			SeqWindowSize:  2,
			SafeL2Head:     'D',
			ExpectedErr:    nil,
273 274
		},
		{
protolambda's avatar
protolambda committed
275 276 277 278 279 280 281 282 283 284 285 286 287
			Name:           "reorg past offset genesis",
			GenesisL1Num:   3,
			L1:             "abcdefgh",
			L2:             "DEFGH",
			NewL1:          "abxyzwio",
			PreFinalizedL2: 'D',
			PreSafeL2:      'D',
			GenesisL1:      'd',
			GenesisL2:      'D',
			UnsafeL2Head:   0,
			SeqWindowSize:  2,
			SafeL2Head:     'D',
			ExpectedErr:    WrongChainErr,
288
		},
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		{
			// FindL2Heads() keeps walking back to safe head after finding canonical unsafe head
			// TooDeepReorgErr must not be raised
			Name:           "long traverse to safe head",
			GenesisL1Num:   0,
			L1:             "abcdefgh",
			L2:             "ABCDEFGH",
			NewL1:          "abcdefgx",
			PreFinalizedL2: 'B',
			PreSafeL2:      'B',
			GenesisL1:      'a',
			GenesisL2:      'A',
			UnsafeL2Head:   'G',
			SeqWindowSize:  1,
			SafeL2Head:     'B',
			ExpectedErr:    nil,
		},
		{
			// L2 reorg is too deep
			Name:           "reorg too deep",
			GenesisL1Num:   0,
			L1:             "abcdefgh",
			L2:             "ABCDEFGH",
			NewL1:          "abijklmn",
			PreFinalizedL2: 'B',
			PreSafeL2:      'B',
			GenesisL1:      'a',
			GenesisL2:      'A',
			SeqWindowSize:  1,
			ExpectedErr:    TooDeepReorgErr,
		},
320 321 322 323 324 325
	}

	for _, testCase := range testCases {
		t.Run(testCase.Name, testCase.Run)
	}
}