Skip to content

Commit

Permalink
multicaller: Move Block type to separate package to avoid dependency …
Browse files Browse the repository at this point in the history
…cycles (ethereum-optimism#9768)

* multicaller: Support generic calls, not just eth_call

* multicaller: Support generic calls, not just eth_call

* multicaller: Implement balance_call

* multicaller: Split out a generic rpc stub for testing RPC requests other than eth_call

Test balance call

* multicaller: Move Block type to separate package to avoid dependency cycles for multicaller tests.

* fix(op-service): remove old balance call test

---------

Co-authored-by: refcell <[email protected]>
  • Loading branch information
ajsutton and refcell authored Mar 7, 2024
1 parent 53573e0 commit 70c0778
Show file tree
Hide file tree
Showing 31 changed files with 193 additions and 172 deletions.
37 changes: 19 additions & 18 deletions op-challenger/game/fault/contracts/faultdisputegame.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
gameTypes "github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewFaultDisputeGameContract(addr common.Address, caller *batching.MultiCall
// GetBlockRange returns the block numbers of the absolute pre-state block (typically genesis or the bedrock activation block)
// and the post-state block (that the proposed output root is for).
func (c *FaultDisputeGameContract) GetBlockRange(ctx context.Context) (prestateBlock uint64, poststateBlock uint64, retErr error) {
results, err := c.multiCaller.Call(ctx, batching.BlockLatest,
results, err := c.multiCaller.Call(ctx, rpcblock.Latest,
c.contract.Call(methodGenesisBlockNumber),
c.contract.Call(methodL2BlockNumber))
if err != nil {
Expand All @@ -82,7 +83,7 @@ func (c *FaultDisputeGameContract) GetBlockRange(ctx context.Context) (prestateB

// GetGameMetadata returns the game's L2 block number, root claim, status, and game duration.
func (c *FaultDisputeGameContract) GetGameMetadata(ctx context.Context) (uint64, common.Hash, gameTypes.GameStatus, uint64, error) {
results, err := c.multiCaller.Call(ctx, batching.BlockLatest,
results, err := c.multiCaller.Call(ctx, rpcblock.Latest,
c.contract.Call(methodL2BlockNumber),
c.contract.Call(methodRootClaim),
c.contract.Call(methodStatus),
Expand All @@ -104,23 +105,23 @@ func (c *FaultDisputeGameContract) GetGameMetadata(ctx context.Context) (uint64,
}

func (c *FaultDisputeGameContract) GetGenesisOutputRoot(ctx context.Context) (common.Hash, error) {
genesisOutputRoot, err := c.multiCaller.SingleCall(ctx, batching.BlockLatest, c.contract.Call(methodGenesisOutputRoot))
genesisOutputRoot, err := c.multiCaller.SingleCall(ctx, rpcblock.Latest, c.contract.Call(methodGenesisOutputRoot))
if err != nil {
return common.Hash{}, fmt.Errorf("failed to retrieve genesis output root: %w", err)
}
return genesisOutputRoot.GetHash(0), nil
}

func (c *FaultDisputeGameContract) GetSplitDepth(ctx context.Context) (types.Depth, error) {
splitDepth, err := c.multiCaller.SingleCall(ctx, batching.BlockLatest, c.contract.Call(methodSplitDepth))
splitDepth, err := c.multiCaller.SingleCall(ctx, rpcblock.Latest, c.contract.Call(methodSplitDepth))
if err != nil {
return 0, fmt.Errorf("failed to retrieve split depth: %w", err)
}
return types.Depth(splitDepth.GetBigInt(0).Uint64()), nil
}

func (c *FaultDisputeGameContract) GetCredit(ctx context.Context, recipient common.Address) (*big.Int, gameTypes.GameStatus, error) {
results, err := c.multiCaller.Call(ctx, batching.BlockLatest,
results, err := c.multiCaller.Call(ctx, rpcblock.Latest,
c.contract.Call(methodCredit, recipient),
c.contract.Call(methodStatus))
if err != nil {
Expand All @@ -137,7 +138,7 @@ func (c *FaultDisputeGameContract) GetCredit(ctx context.Context, recipient comm
return credit, status, nil
}

func (c *FaultDisputeGameContract) GetCredits(ctx context.Context, block batching.Block, recipients ...common.Address) ([]*big.Int, error) {
func (c *FaultDisputeGameContract) GetCredits(ctx context.Context, block rpcblock.Block, recipients ...common.Address) ([]*big.Int, error) {
calls := make([]batching.Call, 0, len(recipients))
for _, recipient := range recipients {
calls = append(calls, c.contract.Call(methodCredit, recipient))
Expand All @@ -159,7 +160,7 @@ func (f *FaultDisputeGameContract) ClaimCredit(recipient common.Address) (txmgr.
}

func (c *FaultDisputeGameContract) GetRequiredBond(ctx context.Context, position types.Position) (*big.Int, error) {
bond, err := c.multiCaller.SingleCall(ctx, batching.BlockLatest, c.contract.Call(methodRequiredBond, position.ToGIndex()))
bond, err := c.multiCaller.SingleCall(ctx, rpcblock.Latest, c.contract.Call(methodRequiredBond, position.ToGIndex()))
if err != nil {
return nil, fmt.Errorf("failed to retrieve required bond: %w", err)
}
Expand Down Expand Up @@ -200,63 +201,63 @@ func (f *FaultDisputeGameContract) GetOracle(ctx context.Context) (*PreimageOrac
}

func (f *FaultDisputeGameContract) GetGameDuration(ctx context.Context) (uint64, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodGameDuration))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodGameDuration))
if err != nil {
return 0, fmt.Errorf("failed to fetch game duration: %w", err)
}
return result.GetUint64(0), nil
}

func (f *FaultDisputeGameContract) GetMaxGameDepth(ctx context.Context) (types.Depth, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodMaxGameDepth))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodMaxGameDepth))
if err != nil {
return 0, fmt.Errorf("failed to fetch max game depth: %w", err)
}
return types.Depth(result.GetBigInt(0).Uint64()), nil
}

func (f *FaultDisputeGameContract) GetAbsolutePrestateHash(ctx context.Context) (common.Hash, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodAbsolutePrestate))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodAbsolutePrestate))
if err != nil {
return common.Hash{}, fmt.Errorf("failed to fetch absolute prestate hash: %w", err)
}
return result.GetHash(0), nil
}

func (f *FaultDisputeGameContract) GetL1Head(ctx context.Context) (common.Hash, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodL1Head))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodL1Head))
if err != nil {
return common.Hash{}, fmt.Errorf("failed to fetch L1 head: %w", err)
}
return result.GetHash(0), nil
}

func (f *FaultDisputeGameContract) GetStatus(ctx context.Context) (gameTypes.GameStatus, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodStatus))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodStatus))
if err != nil {
return 0, fmt.Errorf("failed to fetch status: %w", err)
}
return gameTypes.GameStatusFromUint8(result.GetUint8(0))
}

func (f *FaultDisputeGameContract) GetClaimCount(ctx context.Context) (uint64, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodClaimCount))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodClaimCount))
if err != nil {
return 0, fmt.Errorf("failed to fetch claim count: %w", err)
}
return result.GetBigInt(0).Uint64(), nil
}

func (f *FaultDisputeGameContract) GetClaim(ctx context.Context, idx uint64) (types.Claim, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodClaim, new(big.Int).SetUint64(idx)))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodClaim, new(big.Int).SetUint64(idx)))
if err != nil {
return types.Claim{}, fmt.Errorf("failed to fetch claim %v: %w", idx, err)
}
return f.decodeClaim(result, int(idx)), nil
}

func (f *FaultDisputeGameContract) GetAllClaims(ctx context.Context) ([]types.Claim, error) {
results, err := batching.ReadArray(ctx, f.multiCaller, batching.BlockLatest, f.contract.Call(methodClaimCount), func(i *big.Int) *batching.ContractCall {
results, err := batching.ReadArray(ctx, f.multiCaller, rpcblock.Latest, f.contract.Call(methodClaimCount), func(i *big.Int) *batching.ContractCall {
return f.contract.Call(methodClaim, i)
})
if err != nil {
Expand All @@ -271,7 +272,7 @@ func (f *FaultDisputeGameContract) GetAllClaims(ctx context.Context) ([]types.Cl
}

func (f *FaultDisputeGameContract) vm(ctx context.Context) (*VMContract, error) {
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, f.contract.Call(methodVM))
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, f.contract.Call(methodVM))
if err != nil {
return nil, fmt.Errorf("failed to fetch VM addr: %w", err)
}
Expand All @@ -296,7 +297,7 @@ func (f *FaultDisputeGameContract) StepTx(claimIdx uint64, isAttack bool, stateD

func (f *FaultDisputeGameContract) CallResolveClaim(ctx context.Context, claimIdx uint64) error {
call := f.resolveClaimCall(claimIdx)
_, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, call)
_, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, call)
if err != nil {
return fmt.Errorf("failed to call resolve claim: %w", err)
}
Expand All @@ -314,7 +315,7 @@ func (f *FaultDisputeGameContract) resolveClaimCall(claimIdx uint64) *batching.C

func (f *FaultDisputeGameContract) CallResolve(ctx context.Context) (gameTypes.GameStatus, error) {
call := f.resolveCall()
result, err := f.multiCaller.SingleCall(ctx, batching.BlockLatest, call)
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, call)
if err != nil {
return gameTypes.GameStatusInProgress, fmt.Errorf("failed to call resolve: %w", err)
}
Expand Down
Loading

0 comments on commit 70c0778

Please sign in to comment.