safe_start_test.go 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
package cross

import (
	"errors"
	"testing"

	"github.com/ethereum-optimism/optimism/op-service/eth"
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/backend/depset"
	"github.com/ethereum-optimism/optimism/op-supervisor/supervisor/types"
	"github.com/ethereum/go-ethereum/common"
	"github.com/stretchr/testify/require"
)

func TestCrossSafeHazards(t *testing.T) {
	t.Run("empty execMsgs", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{}
		// when there are no execMsgs,
		// no work is done, and no error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.NoError(t, err)
		require.Empty(t, hazards)
	})
	t.Run("CanExecuteAt returns false", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			canExecuteAtfn: func() (bool, error) {
				return false, nil
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and CanExecuteAt returns false,
		// no work is done and an error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorIs(t, err, types.ErrConflict)
		require.Empty(t, hazards)
	})
	t.Run("CanExecuteAt returns error", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			canExecuteAtfn: func() (bool, error) {
				return false, errors.New("some error")
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and CanExecuteAt returns false,
		// no work is done and an error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
	t.Run("unknown chain", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			chainIDFromIndexfn: func() (types.ChainID, error) {
				return types.ChainID{}, types.ErrUnknownChain
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and ChainIDFromIndex returns ErrUnknownChain,
		// an error is returned as a ErrConflict
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorIs(t, err, types.ErrConflict)
		require.Empty(t, hazards)
	})
	t.Run("ChainIDFromUInt64 returns error", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			chainIDFromIndexfn: func() (types.ChainID, error) {
				return types.ChainID{}, errors.New("some error")
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and ChainIDFromIndex returns some other error,
		// the error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
	t.Run("CanInitiateAt returns false", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			canInitiateAtfn: func() (bool, error) {
				return false, nil
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and CanInitiateAt returns false,
		// the error is returned as a ErrConflict
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorIs(t, err, types.ErrConflict)
		require.Empty(t, hazards)
	})
	t.Run("CanInitiateAt returns error", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{
			canInitiateAtfn: func() (bool, error) {
				return false, errors.New("some error")
			},
		}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{}
		execMsgs := []*types.ExecutingMessage{{}}
		// when there is one execMsg, and CanInitiateAt returns an error,
		// the error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
	t.Run("timestamp is greater than candidate", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 10}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is greater than the candidate,
		// an error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "breaks timestamp invariant")
		require.Empty(t, hazards)
	})
	t.Run("timestamp is equal, Check returns error", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return types.BlockSeal{}, errors.New("some error")
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 2}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timetamp is equal to the candidate,
		// and check returns an error,
		// that error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
	t.Run("timestamp is equal, same hazard twice", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return sampleBlockSeal, nil
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 2}
		em2 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 2}
		execMsgs := []*types.ExecutingMessage{em1, em2}
		// when there are two execMsgs, and both are equal time to the candidate,
		// and check returns the same includedIn for both
		// they load the hazards once, and return no error
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.NoError(t, err)
		require.Equal(t, hazards, map[types.ChainIndex]types.BlockSeal{types.ChainIndex(0): sampleBlockSeal})
	})
	t.Run("timestamp is equal, different hazards", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		// set the check function to return a different BlockSeal for the second call
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		sampleBlockSeal2 := types.BlockSeal{Number: 333, Hash: common.BytesToHash([]byte{0x22})}
		calls := 0
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			defer func() { calls++ }()
			if calls == 0 {
				return sampleBlockSeal, nil
			}
			return sampleBlockSeal2, nil
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 2}
		em2 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 2}
		execMsgs := []*types.ExecutingMessage{em1, em2}
		// when there are two execMsgs, and both are equal time to the candidate,
		// and check returns different includedIn for the two,
		// an error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "but already depend on")
		require.Empty(t, hazards)
	})
	t.Run("timestamp is less, check returns error", func(t *testing.T) {
		ssd := &mockSafeStartDeps{}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return types.BlockSeal{}, errors.New("some error")
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 1}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is less than the candidate,
		// and check returns an error,
		// that error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
226
	t.Run("timestamp is less, CrossDerivedFrom returns error", func(t *testing.T) {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		ssd := &mockSafeStartDeps{}
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return sampleBlockSeal, nil
		}
		ssd.derivedFromFn = func() (derivedFrom types.BlockSeal, err error) {
			return types.BlockSeal{}, errors.New("some error")
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 1}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is less than the candidate,
		// and CrossDerivedFrom returns aan error,
		// that error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorContains(t, err, "some error")
		require.Empty(t, hazards)
	})
248
	t.Run("timestamp is less, CrossDerivedFrom Number is greater", func(t *testing.T) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		ssd := &mockSafeStartDeps{}
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return sampleBlockSeal, nil
		}
		sampleDerivedFrom := types.BlockSeal{Number: 4, Hash: common.BytesToHash([]byte{0x03})}
		ssd.derivedFromFn = func() (derivedFrom types.BlockSeal, err error) {
			return sampleDerivedFrom, nil
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 1}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is less than the candidate,
		// and CrossDerivedFrom returns a BlockSeal with a greater Number than the inL1DerivedFrom,
		// an error is returned as a ErrOutOfScope
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.ErrorIs(t, err, types.ErrOutOfScope)
		require.Empty(t, hazards)
	})
271
	t.Run("timestamp is less, CrossDerivedFrom Number less", func(t *testing.T) {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
		ssd := &mockSafeStartDeps{}
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return sampleBlockSeal, nil
		}
		sampleDerivedFrom := types.BlockSeal{Number: 1, Hash: common.BytesToHash([]byte{0x03})}
		ssd.derivedFromFn = func() (derivedFrom types.BlockSeal, err error) {
			return sampleDerivedFrom, nil
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{Number: 10}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 1}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is less than the candidate,
		// and CrossDerivedFrom returns a BlockSeal with a smaller Number than the inL1DerivedFrom,
		// no error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.NoError(t, err)
		require.Empty(t, hazards)
	})
294
	t.Run("timestamp is less, CrossDerivedFrom Number equal", func(t *testing.T) {
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
		ssd := &mockSafeStartDeps{}
		sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
		ssd.checkFn = func() (includedIn types.BlockSeal, err error) {
			return sampleBlockSeal, nil
		}
		sampleDerivedFrom := types.BlockSeal{Number: 1, Hash: common.BytesToHash([]byte{0x03})}
		ssd.derivedFromFn = func() (derivedFrom types.BlockSeal, err error) {
			return sampleDerivedFrom, nil
		}
		ssd.deps = mockDependencySet{}
		chainID := types.ChainIDFromUInt64(0)
		inL1DerivedFrom := eth.BlockID{Number: 1}
		candidate := types.BlockSeal{Timestamp: 2}
		em1 := &types.ExecutingMessage{Chain: types.ChainIndex(0), Timestamp: 1}
		execMsgs := []*types.ExecutingMessage{em1}
		// when there is one execMsg, and the timestamp is less than the candidate,
		// and CrossDerivedFrom returns a BlockSeal with a equal to the Number of inL1DerivedFrom,
		// no error is returned
		hazards, err := CrossSafeHazards(ssd, chainID, inL1DerivedFrom, candidate, execMsgs)
		require.NoError(t, err)
		require.Empty(t, hazards)
	})
}

type mockSafeStartDeps struct {
	deps          mockDependencySet
	checkFn       func() (includedIn types.BlockSeal, err error)
	derivedFromFn func() (derivedFrom types.BlockSeal, err error)
}

func (m *mockSafeStartDeps) Check(chain types.ChainID, blockNum uint64, logIdx uint32, logHash common.Hash) (includedIn types.BlockSeal, err error) {
	if m.checkFn != nil {
		return m.checkFn()
	}
	return types.BlockSeal{}, nil
}

func (m *mockSafeStartDeps) CrossDerivedFrom(chainID types.ChainID, derived eth.BlockID) (derivedFrom types.BlockSeal, err error) {
	if m.derivedFromFn != nil {
		return m.derivedFromFn()
	}
	return types.BlockSeal{}, nil
}

func (m *mockSafeStartDeps) DependencySet() depset.DependencySet {
	return m.deps
}