|
5 | 5 | "context" |
6 | 6 | "encoding/hex" |
7 | 7 | "fmt" |
| 8 | + "io" |
8 | 9 | "math/big" |
9 | 10 | "os" |
10 | 11 | "os/exec" |
@@ -73,7 +74,7 @@ func NewDevnet(ctx context.Context, t *testing.T) *Devnet { |
73 | 74 | return d |
74 | 75 | } |
75 | 76 |
|
76 | | -func (d *Devnet) Up(verbose bool) (err error) { |
| 77 | +func (d *Devnet) Up() (err error) { |
77 | 78 | cmd := exec.CommandContext( |
78 | 79 | d.ctx, |
79 | 80 | "docker", "compose", "up", "-d", |
@@ -107,7 +108,7 @@ func (d *Devnet) Up(verbose bool) (err error) { |
107 | 108 | } |
108 | 109 | }() |
109 | 110 |
|
110 | | - if verbose { |
| 111 | + if testing.Verbose() { |
111 | 112 | // Stream logs to stdout while the test runs. This goroutine will automatically exit when |
112 | 113 | // the context is cancelled. |
113 | 114 | go func() { |
@@ -376,6 +377,158 @@ func (d *Devnet) Down() error { |
376 | 377 | return cmd.Run() |
377 | 378 | } |
378 | 379 |
|
| 380 | +type TaggedWriter struct { |
| 381 | + inner io.Writer |
| 382 | + tag string |
| 383 | + newline bool |
| 384 | +} |
| 385 | + |
| 386 | +func NewTaggedWriter(tag string, inner io.Writer) *TaggedWriter { |
| 387 | + return &TaggedWriter{ |
| 388 | + inner: inner, |
| 389 | + tag: tag, |
| 390 | + newline: true, |
| 391 | + } |
| 392 | +} |
| 393 | + |
| 394 | +func (w *TaggedWriter) Write(p []byte) (int, error) { |
| 395 | + if w.newline { |
| 396 | + if _, err := fmt.Fprintf(w.inner, "%s | ", w.tag); err != nil { |
| 397 | + return 0, err |
| 398 | + } |
| 399 | + w.newline = false |
| 400 | + } |
| 401 | + |
| 402 | + written := 0 |
| 403 | + for i := range len(p) { |
| 404 | + // Buffer bytes until we hit a newline. |
| 405 | + if p[i] == '\n' { |
| 406 | + // Print everything we've buffered up to and including the newline. |
| 407 | + line := p[written : i+1] |
| 408 | + n, err := w.inner.Write(line) |
| 409 | + written += n |
| 410 | + if err != nil || n < len(line) { |
| 411 | + return written, err |
| 412 | + } |
| 413 | + |
| 414 | + // If that's the end of the output, return, but make a note that the buffer ended with a |
| 415 | + // newline and we need to print the tag before the next message. |
| 416 | + if written == len(p) { |
| 417 | + w.newline = true |
| 418 | + return written, nil |
| 419 | + } |
| 420 | + |
| 421 | + // Otherwise print the tag now before proceeding with the next line in `p`. |
| 422 | + if _, err := fmt.Fprintf(w.inner, "%s | ", w.tag); err != nil { |
| 423 | + return written, err |
| 424 | + } |
| 425 | + } |
| 426 | + } |
| 427 | + |
| 428 | + // Print anything that was buffered after the final newline. |
| 429 | + if written < len(p) { |
| 430 | + line := p[written:] |
| 431 | + n, err := w.inner.Write(line) |
| 432 | + written += n |
| 433 | + if err != nil || n < len(line) { |
| 434 | + return written, err |
| 435 | + } |
| 436 | + } |
| 437 | + |
| 438 | + return written, nil |
| 439 | +} |
| 440 | + |
| 441 | +func (d *Devnet) OpChallenger(opts ...string) error { |
| 442 | + return d.opChallengerCmd(opts...).Run() |
| 443 | +} |
| 444 | + |
| 445 | +type ChallengeGame struct { |
| 446 | + Index uint64 |
| 447 | + Address common.Address |
| 448 | + OutputRoot []byte |
| 449 | + Claims uint64 |
| 450 | +} |
| 451 | + |
| 452 | +func ParseChallengeGame(s string) (ChallengeGame, error) { |
| 453 | + fields := strings.Fields(s) |
| 454 | + if len(fields) < 8 { |
| 455 | + return ChallengeGame{}, fmt.Errorf("challenge game is missing fields; expected at least 8 but got only %v", len(fields)) |
| 456 | + } |
| 457 | + |
| 458 | + index, err := strconv.ParseUint(fields[0], 10, 64) |
| 459 | + if err != nil { |
| 460 | + return ChallengeGame{}, fmt.Errorf("index invalid: %w", err) |
| 461 | + } |
| 462 | + |
| 463 | + address := common.HexToAddress(fields[1]) |
| 464 | + |
| 465 | + outputRoot := common.Hex2Bytes(fields[6]) |
| 466 | + |
| 467 | + claims, err := strconv.ParseUint(fields[7], 10, 64) |
| 468 | + if err != nil { |
| 469 | + return ChallengeGame{}, fmt.Errorf("claims count invalid: %w", err) |
| 470 | + } |
| 471 | + |
| 472 | + return ChallengeGame{ |
| 473 | + Index: index, |
| 474 | + Address: address, |
| 475 | + OutputRoot: outputRoot, |
| 476 | + Claims: claims, |
| 477 | + }, nil |
| 478 | +} |
| 479 | + |
| 480 | +func (d *Devnet) ListChallengeGames() ([]ChallengeGame, error) { |
| 481 | + output, err := d.OpChallengerOutput("list-games") |
| 482 | + if err != nil { |
| 483 | + return nil, err |
| 484 | + } |
| 485 | + |
| 486 | + var games []ChallengeGame |
| 487 | + for i, line := range strings.Split(output, "\n") { |
| 488 | + if i == 0 { |
| 489 | + // Ignore header. |
| 490 | + continue |
| 491 | + } |
| 492 | + line = strings.TrimSpace(line) |
| 493 | + if len(line) == 0 { |
| 494 | + // Ignore empty lines (e.g. trailing newline) |
| 495 | + continue |
| 496 | + } |
| 497 | + |
| 498 | + game, err := ParseChallengeGame(line) |
| 499 | + if err != nil { |
| 500 | + return nil, fmt.Errorf("game %v is invalid: %w", i, err) |
| 501 | + } |
| 502 | + games = append(games, game) |
| 503 | + } |
| 504 | + return games, nil |
| 505 | +} |
| 506 | + |
| 507 | +func (d *Devnet) OpChallengerOutput(opts ...string) (string, error) { |
| 508 | + cmd := d.opChallengerCmd(opts...) |
| 509 | + buf := new(bytes.Buffer) |
| 510 | + cmd.Stdout = buf |
| 511 | + if err := cmd.Run(); err != nil { |
| 512 | + return "", err |
| 513 | + } |
| 514 | + return buf.String(), nil |
| 515 | +} |
| 516 | + |
| 517 | +func (d *Devnet) opChallengerCmd(opts ...string) *exec.Cmd { |
| 518 | + opts = append([]string{"compose", "exec", "op-challenger", "entrypoint.sh", "op-challenger"}, opts...) |
| 519 | + cmd := exec.CommandContext( |
| 520 | + d.ctx, |
| 521 | + "docker", |
| 522 | + opts..., |
| 523 | + ) |
| 524 | + if testing.Verbose() { |
| 525 | + cmd.Stdout = NewTaggedWriter("op-challenger-cmd", os.Stdout) |
| 526 | + cmd.Stderr = NewTaggedWriter("op-challenger-cmd", os.Stderr) |
| 527 | + } |
| 528 | + log.Info("invoking op-challenger", "cmd", cmd) |
| 529 | + return cmd |
| 530 | +} |
| 531 | + |
379 | 532 | // Get the host port mapped to `privatePort` for the given Docker service. |
380 | 533 | func (d *Devnet) hostPort(service string, privatePort uint16) (uint16, error) { |
381 | 534 | buf := new(bytes.Buffer) |
|
0 commit comments