forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_test.go
297 lines (259 loc) · 7.25 KB
/
monitor_test.go
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package game
import (
"context"
"fmt"
"math/big"
"sync"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum-optimism/optimism/op-service/clock"
)
// TestMonitorGames tests that the monitor can handle a new head event
// and resubscribe to new heads if the subscription errors.
func TestMonitorGames(t *testing.T) {
t.Run("Schedules games", func(t *testing.T) {
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
monitor, source, sched, mockHeadSource, preimages, _ := setupMonitorTest(t, []common.Address{})
source.games = []types.GameMetadata{newFDG(addr1, 9999), newFDG(addr2, 9999)}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
headerNotSent := true
for {
if len(sched.Scheduled()) >= 1 {
break
}
sub := mockHeadSource.Sub()
if sub == nil {
continue
}
if headerNotSent {
select {
case sub.headers <- ðtypes.Header{
Number: big.NewInt(1),
}:
headerNotSent = false
case <-ctx.Done():
break
default:
}
}
// Just to avoid a tight loop
time.Sleep(100 * time.Millisecond)
}
mockHeadSource.SetErr(fmt.Errorf("eth subscribe test error"))
cancel()
}()
monitor.StartMonitoring()
<-ctx.Done()
monitor.StopMonitoring()
require.Len(t, sched.Scheduled(), 1)
require.Equal(t, []common.Address{addr1, addr2}, sched.Scheduled()[0])
require.GreaterOrEqual(t, preimages.ScheduleCount(), 1, "Should schedule preimage checks")
})
t.Run("Resubscribes on error", func(t *testing.T) {
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
monitor, source, sched, mockHeadSource, preimages, _ := setupMonitorTest(t, []common.Address{})
source.games = []types.GameMetadata{newFDG(addr1, 9999), newFDG(addr2, 9999)}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
// Wait for the subscription to be created
waitErr := wait.For(context.Background(), 5*time.Second, func() (bool, error) {
return mockHeadSource.Sub() != nil, nil
})
require.NoError(t, waitErr)
mockHeadSource.Sub().errChan <- fmt.Errorf("test error")
for {
if len(sched.Scheduled()) >= 1 {
break
}
sub := mockHeadSource.Sub()
if sub == nil {
continue
}
select {
case sub.headers <- ðtypes.Header{
Number: big.NewInt(1),
}:
case <-ctx.Done():
break
default:
}
// Just to avoid a tight loop
time.Sleep(100 * time.Millisecond)
}
require.NoError(t, waitErr)
mockHeadSource.SetErr(fmt.Errorf("eth subscribe test error"))
cancel()
}()
monitor.StartMonitoring()
<-ctx.Done()
monitor.StopMonitoring()
require.NotEmpty(t, sched.Scheduled()) // We might get more than one update scheduled.
require.Equal(t, []common.Address{addr1, addr2}, sched.Scheduled()[0])
require.GreaterOrEqual(t, preimages.ScheduleCount(), 1, "Should schedule preimage checks")
})
}
func TestMonitorCreateAndProgressGameAgents(t *testing.T) {
monitor, source, sched, _, _, _ := setupMonitorTest(t, []common.Address{})
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
source.games = []types.GameMetadata{newFDG(addr1, 9999), newFDG(addr2, 9999)}
require.NoError(t, monitor.progressGames(context.Background(), common.Hash{0x01}, 0))
require.Len(t, sched.Scheduled(), 1)
require.Equal(t, []common.Address{addr1, addr2}, sched.Scheduled()[0])
}
func TestMonitorOnlyScheduleSpecifiedGame(t *testing.T) {
addr1 := common.Address{0xaa}
addr2 := common.Address{0xbb}
monitor, source, sched, _, _, stubClaimer := setupMonitorTest(t, []common.Address{addr2})
source.games = []types.GameMetadata{newFDG(addr1, 9999), newFDG(addr2, 9999)}
require.NoError(t, monitor.progressGames(context.Background(), common.Hash{0x01}, 0))
require.Len(t, sched.Scheduled(), 1)
require.Equal(t, []common.Address{addr2}, sched.Scheduled()[0])
require.Equal(t, 1, stubClaimer.scheduledGames)
}
func newFDG(proxy common.Address, timestamp uint64) types.GameMetadata {
return types.GameMetadata{
Proxy: proxy,
Timestamp: timestamp,
}
}
func setupMonitorTest(
t *testing.T,
allowedGames []common.Address,
) (*gameMonitor, *stubGameSource, *stubScheduler, *mockNewHeadSource, *stubPreimageScheduler, *mockScheduler) {
logger := testlog.Logger(t, log.LevelDebug)
source := &stubGameSource{}
i := uint64(1)
fetchBlockNum := func(ctx context.Context) (uint64, error) {
i++
return i, nil
}
sched := &stubScheduler{}
preimages := &stubPreimageScheduler{}
mockHeadSource := &mockNewHeadSource{}
stubClaimer := &mockScheduler{}
monitor := newGameMonitor(
logger,
clock.NewSimpleClock(),
source,
sched,
preimages,
time.Duration(0),
stubClaimer,
fetchBlockNum,
allowedGames,
mockHeadSource,
)
return monitor, source, sched, mockHeadSource, preimages, stubClaimer
}
type mockNewHeadSource struct {
sync.Mutex
sub *mockSubscription
err error
}
func (m *mockNewHeadSource) Sub() *mockSubscription {
m.Lock()
defer m.Unlock()
return m.sub
}
func (m *mockNewHeadSource) SetSub(sub *mockSubscription) {
m.Lock()
defer m.Unlock()
m.sub = sub
}
func (m *mockNewHeadSource) SetErr(err error) {
m.Lock()
defer m.Unlock()
m.err = err
}
func (m *mockNewHeadSource) EthSubscribe(
_ context.Context,
ch any,
_ ...any,
) (ethereum.Subscription, error) {
m.Lock()
defer m.Unlock()
errChan := make(chan error)
m.sub = &mockSubscription{errChan, (ch).(chan<- *ethtypes.Header)}
if m.err != nil {
return nil, m.err
}
return m.sub, nil
}
type mockScheduler struct {
scheduleErr error
scheduledGames int
}
func (m *mockScheduler) Schedule(_ uint64, games []types.GameMetadata) error {
m.scheduledGames += len(games)
return m.scheduleErr
}
type mockSubscription struct {
errChan chan error
headers chan<- *ethtypes.Header
}
func (m *mockSubscription) Unsubscribe() {}
func (m *mockSubscription) Err() <-chan error {
return m.errChan
}
type stubGameSource struct {
fetchErr error
games []types.GameMetadata
}
func (s *stubGameSource) GetGamesAtOrAfter(
_ context.Context,
_ common.Hash,
_ uint64,
) ([]types.GameMetadata, error) {
if s.fetchErr != nil {
return nil, s.fetchErr
}
return s.games, nil
}
type stubScheduler struct {
sync.Mutex
scheduled [][]common.Address
}
func (s *stubScheduler) Scheduled() [][]common.Address {
s.Lock()
defer s.Unlock()
return s.scheduled
}
func (s *stubScheduler) Schedule(games []types.GameMetadata, blockNumber uint64) error {
s.Lock()
defer s.Unlock()
var addrs []common.Address
for _, game := range games {
addrs = append(addrs, game.Proxy)
}
s.scheduled = append(s.scheduled, addrs)
return nil
}
type stubPreimageScheduler struct {
sync.Mutex
scheduleCount int
}
func (s *stubPreimageScheduler) Schedule(_ common.Hash, _ uint64) error {
s.Lock()
defer s.Unlock()
s.scheduleCount++
return nil
}
func (s *stubPreimageScheduler) ScheduleCount() int {
s.Lock()
defer s.Unlock()
return s.scheduleCount
}