Skip to content

Commit

Permalink
op-e2e: Refactor for asterisc e2e (ethereum-optimism#10214)
Browse files Browse the repository at this point in the history
* op-e2e: Expose fields for asterisc e2e

* op-e2e: Add Helper Initializer methods

* op-e2e: Apply initializer methods and exposed fields

* op-e2e: Expose methods for asterisc e2e
  • Loading branch information
pcw109550 authored Apr 23, 2024
1 parent 1667124 commit d4706da
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 450 deletions.
16 changes: 13 additions & 3 deletions op-e2e/e2eutils/challenger/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ type Helper struct {
chl cliapp.Lifecycle
}

func NewHelper(log log.Logger, t *testing.T, require *require.Assertions, dir string, chl cliapp.Lifecycle) *Helper {
return &Helper{
log: log,
t: t,
require: require,
dir: dir,
chl: chl,
}
}

type Option func(config2 *config.Config)

func WithFactoryAddress(addr common.Address) Option {
Expand All @@ -66,9 +76,9 @@ func WithPollInterval(pollInterval time.Duration) Option {
}
}

// findMonorepoRoot finds the relative path to the monorepo root
// FindMonorepoRoot finds the relative path to the monorepo root
// Different tests might be nested in subdirectories of the op-e2e dir.
func findMonorepoRoot(t *testing.T) string {
func FindMonorepoRoot(t *testing.T) string {
path := "./"
// Only search up 5 directories
// Avoids infinite recursion if the root isn't found for some reason
Expand All @@ -94,7 +104,7 @@ func applyCannonConfig(
) {
require := require.New(t)
c.L2Rpc = l2Endpoint
root := findMonorepoRoot(t)
root := FindMonorepoRoot(t)
c.CannonBin = root + "cannon/bin/cannon"
c.CannonServer = root + "op-program/bin/op-program"
c.CannonAbsolutePreState = root + "op-program/bin/prestate.json"
Expand Down
50 changes: 25 additions & 25 deletions op-e2e/e2eutils/disputegame/claim_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,53 @@ import (
type ClaimHelper struct {
require *require.Assertions
game *OutputGameHelper
index int64
parentIndex uint32
position types.Position
Index int64
ParentIndex uint32
Position types.Position
claim common.Hash
}

func newClaimHelper(game *OutputGameHelper, idx int64, claim ContractClaim) *ClaimHelper {
return &ClaimHelper{
require: game.require,
require: game.Require,
game: game,
index: idx,
parentIndex: claim.ParentIndex,
position: types.NewPositionFromGIndex(claim.Position),
Index: idx,
ParentIndex: claim.ParentIndex,
Position: types.NewPositionFromGIndex(claim.Position),
claim: claim.Claim,
}
}

func (c *ClaimHelper) AgreesWithOutputRoot() bool {
return c.position.Depth()%2 == 0
return c.Position.Depth()%2 == 0
}

func (c *ClaimHelper) IsRootClaim() bool {
return c.position.IsRootPosition()
return c.Position.IsRootPosition()
}

func (c *ClaimHelper) IsOutputRoot(ctx context.Context) bool {
splitDepth := c.game.SplitDepth(ctx)
return c.position.Depth() <= splitDepth
return c.Position.Depth() <= splitDepth
}

func (c *ClaimHelper) IsOutputRootLeaf(ctx context.Context) bool {
splitDepth := c.game.SplitDepth(ctx)
return c.position.Depth() == splitDepth
return c.Position.Depth() == splitDepth
}

func (c *ClaimHelper) IsBottomGameRoot(ctx context.Context) bool {
splitDepth := c.game.SplitDepth(ctx)
return c.position.Depth() == splitDepth+1
return c.Position.Depth() == splitDepth+1
}

func (c *ClaimHelper) IsMaxDepth(ctx context.Context) bool {
maxDepth := c.game.MaxDepth(ctx)
return c.position.Depth() == maxDepth
return c.Position.Depth() == maxDepth
}

func (c *ClaimHelper) Depth() types.Depth {
return c.position.Depth()
return c.Position.Depth()
}

// WaitForCounterClaim waits for the claim to be countered by another claim being posted.
Expand All @@ -72,8 +72,8 @@ func (c *ClaimHelper) WaitForCounterClaim(ctx context.Context, ignoreClaims ...*
// This is the first claim we need to run cannon on, so give it more time
timeout = timeout * 2
}
counterIdx, counterClaim := c.game.waitForClaim(ctx, timeout, fmt.Sprintf("failed to find claim with parent idx %v", c.index), func(claimIdx int64, claim ContractClaim) bool {
return int64(claim.ParentIndex) == c.index && !containsClaim(claimIdx, ignoreClaims)
counterIdx, counterClaim := c.game.waitForClaim(ctx, timeout, fmt.Sprintf("failed to find claim with parent idx %v", c.Index), func(claimIdx int64, claim ContractClaim) bool {
return int64(claim.ParentIndex) == c.Index && !containsClaim(claimIdx, ignoreClaims)
})
return newClaimHelper(c.game, counterIdx, counterClaim)
}
Expand All @@ -83,28 +83,28 @@ func (c *ClaimHelper) WaitForCountered(ctx context.Context) {
timedCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
err := wait.For(timedCtx, time.Second, func() (bool, error) {
latestData := c.game.getClaim(ctx, c.index)
latestData := c.game.getClaim(ctx, c.Index)
return latestData.CounteredBy != common.Address{}, nil
})
if err != nil { // Avoid waiting time capturing game data when there's no error
c.require.NoErrorf(err, "Claim %v was not countered\n%v", c.index, c.game.gameData(ctx))
c.require.NoErrorf(err, "Claim %v was not countered\n%v", c.Index, c.game.GameData(ctx))
}
}

func (c *ClaimHelper) RequireCorrectOutputRoot(ctx context.Context) {
c.require.True(c.IsOutputRoot(ctx), "Should not expect a valid output root in the bottom game")
expected, err := c.game.correctOutputProvider.Get(ctx, c.position)
expected, err := c.game.CorrectOutputProvider.Get(ctx, c.Position)
c.require.NoError(err, "Failed to get correct output root")
c.require.Equalf(expected, c.claim, "Should have correct output root in claim %v and position %v", c.index, c.position)
c.require.Equalf(expected, c.claim, "Should have correct output root in claim %v and position %v", c.Index, c.Position)
}

func (c *ClaimHelper) Attack(ctx context.Context, value common.Hash, opts ...MoveOpt) *ClaimHelper {
c.game.Attack(ctx, c.index, value, opts...)
c.game.Attack(ctx, c.Index, value, opts...)
return c.WaitForCounterClaim(ctx)
}

func (c *ClaimHelper) Defend(ctx context.Context, value common.Hash, opts ...MoveOpt) *ClaimHelper {
c.game.Defend(ctx, c.index, value, opts...)
c.game.Defend(ctx, c.Index, value, opts...)
return c.WaitForCounterClaim(ctx)
}

Expand All @@ -115,19 +115,19 @@ func (c *ClaimHelper) RequireDifferentClaimValue(other *ClaimHelper) {
func (c *ClaimHelper) RequireOnlyCounteredBy(ctx context.Context, expected ...*ClaimHelper) {
claims := c.game.getAllClaims(ctx)
for idx, claim := range claims {
if int64(claim.ParentIndex) != c.index {
if int64(claim.ParentIndex) != c.Index {
// Doesn't counter this claim, so ignore
continue
}
if !containsClaim(int64(idx), expected) {
// Found a countering claim not in the expected list. Fail.
c.require.FailNowf("Found unexpected countering claim", "Parent claim index: %v Game state:\n%v", c.index, c.game.gameData(ctx))
c.require.FailNowf("Found unexpected countering claim", "Parent claim index: %v Game state:\n%v", c.Index, c.game.GameData(ctx))
}
}
}

func containsClaim(claimIdx int64, haystack []*ClaimHelper) bool {
return slices.ContainsFunc(haystack, func(candidate *ClaimHelper) bool {
return candidate.index == claimIdx
return candidate.Index == claimIdx
})
}
6 changes: 3 additions & 3 deletions op-e2e/e2eutils/disputegame/dishonest_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (d *DishonestHelper) ExhaustDishonestClaims(ctx context.Context, rootClaim
}

d.LogGameData(ctx)
d.OutputGameHelper.t.Logf("Dishonest moves against claimIndex %d", claimIndex)
d.OutputGameHelper.T.Logf("Dishonest moves against claimIndex %d", claimIndex)
agreeWithLevel := d.defender == (pos.Depth()%2 == 0)
if !agreeWithLevel {
d.OutputHonestHelper.Attack(ctx, claimIndex, WithIgnoreDuplicates())
Expand All @@ -53,7 +53,7 @@ func (d *DishonestHelper) ExhaustDishonestClaims(ctx context.Context, rootClaim
}
}

numClaimsSeen := rootClaim.index
numClaimsSeen := rootClaim.Index
for {
// Use a short timeout since we don't know the challenger will respond,
// and this is only designed for the alphabet game where the response should be fast.
Expand All @@ -63,7 +63,7 @@ func (d *DishonestHelper) ExhaustDishonestClaims(ctx context.Context, rootClaim
// There's nothing to respond to.
break
}
d.OutputGameHelper.require.NoError(err)
d.OutputGameHelper.Require.NoError(err)

for ; numClaimsSeen < newCount; numClaimsSeen++ {
claimData := d.getClaim(ctx, numClaimsSeen)
Expand Down
Loading

0 comments on commit d4706da

Please sign in to comment.