-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathoutput_alphabet_test.go
331 lines (269 loc) · 11.8 KB
/
output_alphabet_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
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
package faultproofs
import (
"context"
"math/big"
"testing"
"time"
op_e2e "github.com/ethereum-optimism/optimism/op-e2e"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/challenger"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/disputegame"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/wait"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
func TestOutputAlphabetGame_ChallengerWins(t *testing.T) {
op_e2e.InitParallel(t)
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
game := disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 3, common.Hash{0xff})
correctTrace := game.CreateHonestActor(ctx, "sequencer")
game.LogGameData(ctx)
opts := challenger.WithPrivKey(sys.Cfg.Secrets.Alice)
game.StartChallenger(ctx, "sequencer", "Challenger", opts)
game.LogGameData(ctx)
// Challenger should post an output root to counter claims down to the leaf level of the top game
claim := game.RootClaim(ctx)
for claim.IsOutputRoot(ctx) && !claim.IsOutputRootLeaf(ctx) {
if claim.AgreesWithOutputRoot() {
// If the latest claim agrees with the output root, expect the honest challenger to counter it
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
claim.RequireCorrectOutputRoot(ctx)
} else {
// Otherwise we should counter
claim = claim.Attack(ctx, common.Hash{0xaa})
game.LogGameData(ctx)
}
}
// Wait for the challenger to post the first claim in the cannon trace
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
// Attack the root of the alphabet trace subgame
claim = correctTrace.AttackClaim(ctx, claim)
for !claim.IsMaxDepth(ctx) {
if claim.AgreesWithOutputRoot() {
// If the latest claim supports the output root, wait for the honest challenger to respond
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
} else {
// Otherwise we need to counter the honest claim
claim = correctTrace.AttackClaim(ctx, claim)
game.LogGameData(ctx)
}
}
// Challenger should be able to call step and counter the leaf claim.
claim.WaitForCountered(ctx)
game.LogGameData(ctx)
sys.TimeTravelClock.AdvanceTime(game.MaxClockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
game.WaitForGameStatus(ctx, types.GameStatusChallengerWon)
game.LogGameData(ctx)
}
func TestOutputAlphabetGame_ReclaimBond(t *testing.T) {
op_e2e.InitParallel(t)
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
game := disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 3, common.Hash{0xff})
game.LogGameData(ctx)
// The dispute game should have a zero balance
balance := game.WethBalance(ctx, game.Addr)
require.Zero(t, balance.Uint64())
alice := sys.Cfg.Secrets.Addresses().Alice
// Grab the root claim
claim := game.RootClaim(ctx)
opts := challenger.WithPrivKey(sys.Cfg.Secrets.Alice)
game.StartChallenger(ctx, "sequencer", "Challenger", opts)
game.LogGameData(ctx)
// Perform a few moves
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
claim = claim.Attack(ctx, common.Hash{})
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
claim = claim.Attack(ctx, common.Hash{})
game.LogGameData(ctx)
_ = claim.WaitForCounterClaim(ctx)
// Expect posted claims so the game balance is non-zero
balance = game.WethBalance(ctx, game.Addr)
require.Truef(t, balance.Cmp(big.NewInt(0)) > 0, "Expected game balance to be above zero")
sys.TimeTravelClock.AdvanceTime(game.MaxClockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
game.WaitForGameStatus(ctx, types.GameStatusChallengerWon)
game.LogGameData(ctx)
// Advance the time past the finalization delay
// Finalization delay is the same as the credit unlock delay
// But just warp way into the future to be safe
sys.TimeTravelClock.AdvanceTime(game.CreditUnlockDuration(ctx) * 2)
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
// Wait for the game to have bond mode set
game.WaitForBondModeDecided(ctx)
// Expect Alice's credit to be non-zero
// But it can't be claimed right now since there is a delay on the weth unlock
require.Truef(t, game.AvailableCredit(ctx, alice).Cmp(big.NewInt(0)) > 0, "Expected alice credit to be above zero")
// The actor should have no credit available because all its bonds were paid to Alice.
actorCredit := game.AvailableCredit(ctx, disputegame.TestAddress)
require.True(t, actorCredit.Cmp(big.NewInt(0)) == 0, "Expected actor available credit to be zero")
// Advance the time past the weth unlock delay
sys.TimeTravelClock.AdvanceTime(game.CreditUnlockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
// Wait for alice to have no available credit
// aka, wait for the challenger to claim its credit
game.WaitForNoAvailableCredit(ctx, alice)
// The dispute game delayed weth balance should be zero since it's all claimed
require.True(t, game.WethBalance(ctx, game.Addr).Cmp(big.NewInt(0)) == 0)
}
func TestOutputAlphabetGame_ValidOutputRoot(t *testing.T) {
op_e2e.InitParallel(t)
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
game := disputeGameFactory.StartOutputAlphabetGameWithCorrectRoot(ctx, "sequencer", 2)
correctTrace := game.CreateHonestActor(ctx, "sequencer")
game.LogGameData(ctx)
claim := game.DisputeLastBlock(ctx)
// Invalid root claim of the alphabet game
claim = claim.Attack(ctx, common.Hash{0x01})
opts := challenger.WithPrivKey(sys.Cfg.Secrets.Alice)
game.StartChallenger(ctx, "sequencer", "Challenger", opts)
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
for !claim.IsMaxDepth(ctx) {
// Dishonest actor always attacks with the correct trace
claim = correctTrace.AttackClaim(ctx, claim)
claim = claim.WaitForCounterClaim(ctx)
game.LogGameData(ctx)
}
sys.TimeTravelClock.AdvanceTime(game.MaxClockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
game.WaitForGameStatus(ctx, types.GameStatusDefenderWon)
}
func TestChallengerCompleteExhaustiveDisputeGame(t *testing.T) {
op_e2e.InitParallel(t, op_e2e.IsSlow)
testCase := func(t *testing.T, isRootCorrect bool) {
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
var game *disputegame.OutputAlphabetGameHelper
if isRootCorrect {
game = disputeGameFactory.StartOutputAlphabetGameWithCorrectRoot(ctx, "sequencer", 1)
} else {
game = disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 1, common.Hash{0xaa, 0xbb, 0xcc})
}
claim := game.DisputeLastBlock(ctx)
game.LogGameData(ctx)
// Start honest challenger
game.StartChallenger(ctx, "sequencer", "Challenger",
challenger.WithAlphabet(),
challenger.WithPrivKey(sys.Cfg.Secrets.Alice),
// Ensures the challenger responds to all claims before test timeout
challenger.WithPollInterval(time.Millisecond*400),
)
if isRootCorrect {
// Attack the correct output root with an invalid alphabet trace
claim = claim.Attack(ctx, common.Hash{0x01})
} else {
// Wait for the challenger to counter the invalid output root
claim = claim.WaitForCounterClaim(ctx)
}
// Start dishonest challenger
dishonestHelper := game.CreateDishonestHelper(ctx, "sequencer", !isRootCorrect)
dishonestHelper.ExhaustDishonestClaims(ctx, claim)
// Wait until we've reached max depth before checking for inactivity
game.WaitForClaimAtDepth(ctx, game.MaxDepth(ctx))
// Wait for 4 blocks of no challenger responses. The challenger may still be stepping on invalid claims at max depth
game.WaitForInactivity(ctx, 4, false)
gameDuration := game.MaxClockDuration(ctx)
sys.TimeTravelClock.AdvanceTime(gameDuration)
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
expectedStatus := types.GameStatusChallengerWon
if isRootCorrect {
expectedStatus = types.GameStatusDefenderWon
}
game.WaitForGameStatus(ctx, expectedStatus)
game.LogGameData(ctx)
}
t.Run("RootCorrect", func(t *testing.T) {
op_e2e.InitParallel(t)
testCase(t, true)
})
t.Run("RootIncorrect", func(t *testing.T) {
op_e2e.InitParallel(t)
testCase(t, false)
})
}
func TestOutputAlphabetGame_FreeloaderEarnsNothing(t *testing.T) {
op_e2e.InitParallel(t)
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
freeloaderOpts, err := bind.NewKeyedTransactorWithChainID(sys.Cfg.Secrets.Mallory, sys.Cfg.L1ChainIDBig())
require.Nil(t, err)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
game := disputeGameFactory.StartOutputAlphabetGameWithCorrectRoot(ctx, "sequencer", 2)
correctTrace := game.CreateHonestActor(ctx, "sequencer")
game.LogGameData(ctx)
claim := game.DisputeLastBlock(ctx)
// Invalid root claim of the alphabet game
claim = claim.Attack(ctx, common.Hash{0x01})
// Chronology of claims:
// dishonest root claim:
// - honest counter
// - dishonest
// - freeloader
// - honest
// - freeloader
// The freeloader must be positioned leftmost (gindex positioning) or at the same position as honest claims.
// honest counter
claim = correctTrace.AttackClaim(ctx, claim)
var freeloaders []*disputegame.ClaimHelper
// dishonest
dishonest := correctTrace.AttackClaim(ctx, claim)
freeloaders = append(freeloaders, correctTrace.AttackClaim(ctx, dishonest, disputegame.WithTransactOpts(freeloaderOpts)))
freeloaders = append(freeloaders, dishonest.Attack(ctx, common.Hash{0x02}, disputegame.WithTransactOpts(freeloaderOpts)))
freeloaders = append(freeloaders, dishonest.Defend(ctx, common.Hash{0x03}, disputegame.WithTransactOpts(freeloaderOpts)))
// Ensure freeloaders respond before the honest challenger
game.StartChallenger(ctx, "sequencer", "Challenger", challenger.WithPrivKey(sys.Cfg.Secrets.Alice))
dishonest.WaitForCounterClaim(ctx, freeloaders...)
// Freeloaders after the honest challenger
freeloaders = append(freeloaders, dishonest.Attack(ctx, common.Hash{0x04}, disputegame.WithTransactOpts(freeloaderOpts)))
freeloaders = append(freeloaders, dishonest.Defend(ctx, common.Hash{0x05}, disputegame.WithTransactOpts(freeloaderOpts)))
for _, freeloader := range freeloaders {
if freeloader.IsMaxDepth(ctx) {
freeloader.WaitForCountered(ctx)
} else {
freeloader.WaitForCounterClaim(ctx)
}
}
game.LogGameData(ctx)
sys.TimeTravelClock.AdvanceTime(game.MaxClockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
game.WaitForGameStatus(ctx, types.GameStatusDefenderWon)
game.LogGameData(ctx)
amt := game.Credit(ctx, freeloaderOpts.From)
require.Truef(t, amt.BitLen() == 0, "freeloaders should not be rewarded. Credit: %v", amt)
}
func TestHighestActedL1BlockMetric(t *testing.T) {
op_e2e.InitParallel(t)
ctx := context.Background()
sys, l1Client := StartFaultDisputeSystem(t)
t.Cleanup(sys.Close)
disputeGameFactory := disputegame.NewFactoryHelper(t, ctx, sys)
honestChallenger := disputeGameFactory.StartChallenger(ctx, "Honest", challenger.WithAlphabet(), challenger.WithPrivKey(sys.Cfg.Secrets.Alice))
game1 := disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 1, common.Hash{0xaa})
sys.AdvanceTime(game1.MaxClockDuration(ctx))
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
game1.WaitForGameStatus(ctx, types.GameStatusDefenderWon)
disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 2, common.Hash{0xaa})
disputeGameFactory.StartOutputAlphabetGame(ctx, "sequencer", 3, common.Hash{0xaa})
honestChallenger.WaitL1HeadActedOn(ctx, l1Client)
require.NoError(t, wait.ForNextBlock(ctx, l1Client))
honestChallenger.WaitL1HeadActedOn(ctx, l1Client)
}