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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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 TestCrossUnsafeHazards(t *testing.T) {
t.Run("empty execMsgs", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
chainID := types.ChainIDFromUInt64(0)
candidate := types.BlockSeal{}
execMsgs := []*types.ExecutingMessage{}
// when there are no execMsgs,
// no work is done, and no error is returned
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.NoError(t, err)
require.Empty(t, hazards)
})
t.Run("CanExecuteAt returns false", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
canExecuteAtfn: func() (bool, error) {
return false, nil
},
}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorIs(t, err, types.ErrConflict)
require.Empty(t, hazards)
})
t.Run("CanExecuteAt returns error", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
canExecuteAtfn: func() (bool, error) {
return false, errors.New("some error")
},
}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("unknown chain", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
chainIDFromIndexfn: func() (types.ChainID, error) {
return types.ChainID{}, types.ErrUnknownChain
},
}
chainID := types.ChainIDFromUInt64(0)
candidate := types.BlockSeal{}
execMsgs := []*types.ExecutingMessage{{}}
// when there is one execMsg, and ChainIDFromIndex returns ErrUnknownChain,
// an error is returned as a ErrConflict
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorIs(t, err, types.ErrConflict)
require.Empty(t, hazards)
})
t.Run("ChainIDFromUInt64 returns error", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
chainIDFromIndexfn: func() (types.ChainID, error) {
return types.ChainID{}, errors.New("some error")
},
}
chainID := types.ChainIDFromUInt64(0)
candidate := types.BlockSeal{}
execMsgs := []*types.ExecutingMessage{{}}
// when there is one execMsg, and ChainIDFromIndex returns some other error,
// the error is returned
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("CanInitiateAt returns false", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
canInitiateAtfn: func() (bool, error) {
return false, nil
},
}
chainID := types.ChainIDFromUInt64(0)
candidate := types.BlockSeal{}
execMsgs := []*types.ExecutingMessage{{}}
// when there is one execMsg, and CanInitiateAt returns false,
// the error is returned as a ErrConflict
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorIs(t, err, types.ErrConflict)
require.Empty(t, hazards)
})
t.Run("CanInitiateAt returns error", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{
canInitiateAtfn: func() (bool, error) {
return false, errors.New("some error")
},
}
chainID := types.ChainIDFromUInt64(0)
candidate := types.BlockSeal{}
execMsgs := []*types.ExecutingMessage{{}}
// when there is one execMsg, and CanInitiateAt returns an error,
// the error is returned
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("timestamp is greater than candidate", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, 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) {
usd := &mockUnsafeStartDeps{}
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
return types.BlockSeal{}, errors.New("some error")
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("timestamp is equal, same hazard twice", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
return sampleBlockSeal, nil
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, 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) {
usd := &mockUnsafeStartDeps{}
// 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
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
defer func() { calls++ }()
if calls == 0 {
return sampleBlockSeal, nil
}
return sampleBlockSeal2, nil
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, 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) {
usd := &mockUnsafeStartDeps{}
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
return types.BlockSeal{}, errors.New("some error")
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("timestamp is less, IsCrossUnsafe returns error", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
return sampleBlockSeal, nil
}
usd.isCrossUnsafeFn = func() error {
return errors.New("some error")
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 IsCrossUnsafe returns an error,
// that error is returned
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.ErrorContains(t, err, "some error")
require.Empty(t, hazards)
})
t.Run("timestamp is less, IsCrossUnsafe", func(t *testing.T) {
usd := &mockUnsafeStartDeps{}
sampleBlockSeal := types.BlockSeal{Number: 3, Hash: common.BytesToHash([]byte{0x02})}
usd.checkFn = func() (includedIn types.BlockSeal, err error) {
return sampleBlockSeal, nil
}
usd.isCrossUnsafeFn = func() error {
return nil
}
usd.deps = mockDependencySet{}
chainID := types.ChainIDFromUInt64(0)
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 IsCrossUnsafe returns no error,
// no error is returned
hazards, err := CrossUnsafeHazards(usd, chainID, candidate, execMsgs)
require.NoError(t, err)
require.Empty(t, hazards)
})
}
type mockUnsafeStartDeps struct {
deps mockDependencySet
checkFn func() (includedIn types.BlockSeal, err error)
isCrossUnsafeFn func() error
}
func (m *mockUnsafeStartDeps) 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 *mockUnsafeStartDeps) IsCrossUnsafe(chainID types.ChainID, derived eth.BlockID) error {
if m.isCrossUnsafeFn != nil {
return m.isCrossUnsafeFn()
}
return nil
}
func (m *mockUnsafeStartDeps) DependencySet() depset.DependencySet {
return m.deps
}