|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "math/big" |
| 8 | + "math/rand" |
8 | 9 | "os" |
9 | 10 | "path/filepath" |
10 | 11 | "regexp" |
@@ -195,7 +196,7 @@ func (r *Runner) createGameInputs(ctx context.Context, client *sources.RollupCli |
195 | 196 | } |
196 | 197 | l1Head := status.FinalizedL1 |
197 | 198 | if status.FinalizedL1.Number > status.CurrentL1.Number { |
198 | | - // Restrict the L1 head to a block that has actually be processed by op-node. |
| 199 | + // Restrict the L1 head to a block that has actually been processed by op-node. |
199 | 200 | // This only matters if op-node is behind and hasn't processed all finalized L1 blocks yet. |
200 | 201 | l1Head = status.CurrentL1 |
201 | 202 | } |
@@ -235,15 +236,32 @@ func (r *Runner) findL2BlockNumberToDispute(ctx context.Context, client *sources |
235 | 236 | return l2BlockNum, nil |
236 | 237 | } |
237 | 238 | l1HeadNum -= skipSize |
238 | | - priorSafeHead, err := client.SafeHeadAtL1Block(ctx, l1HeadNum) |
| 239 | + prevSafeHead, err := client.SafeHeadAtL1Block(ctx, l1HeadNum) |
239 | 240 | if err != nil { |
240 | 241 | return 0, fmt.Errorf("failed to get prior safe head at L1 block %v: %w", l1HeadNum, err) |
241 | 242 | } |
242 | | - if priorSafeHead.SafeHead.Number < l2BlockNum { |
| 243 | + if prevSafeHead.SafeHead.Number < l2BlockNum { |
| 244 | + switch rand.Intn(3) { |
| 245 | + case 0: // First block of span batch |
| 246 | + return prevSafeHead.SafeHead.Number + 1, nil |
| 247 | + case 1: // Last block of span batch |
| 248 | + return prevSafeHead.SafeHead.Number, nil |
| 249 | + case 2: // Random block, probably but not guaranteed to be in the middle of a span batch |
| 250 | + firstBlockInSpanBatch := prevSafeHead.SafeHead.Number + 1 |
| 251 | + if l2BlockNum <= firstBlockInSpanBatch { |
| 252 | + // There is only one block in the next batch so we just have to use it |
| 253 | + return l2BlockNum, nil |
| 254 | + } |
| 255 | + offset := rand.Intn(int(l2BlockNum - firstBlockInSpanBatch)) |
| 256 | + return firstBlockInSpanBatch + uint64(offset), nil |
| 257 | + } |
| 258 | + |
| 259 | + } |
| 260 | + if prevSafeHead.SafeHead.Number < l2BlockNum { |
243 | 261 | // We walked back far enough to be before the batch that included l2BlockNum |
244 | 262 | // So use the first block after the prior safe head as the disputed block. |
245 | 263 | // It must be the first block in a batch. |
246 | | - return priorSafeHead.SafeHead.Number + 1, nil |
| 264 | + return prevSafeHead.SafeHead.Number + 1, nil |
247 | 265 | } |
248 | 266 | } |
249 | 267 | r.log.Warn("Failed to find prior batch", "l2BlockNum", l2BlockNum, "earliestCheckL1Block", l1HeadNum) |
|
0 commit comments