Skip to content

Commit

Permalink
Test: decode tx from TxCall
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan committed Sep 27, 2024
1 parent 15e66c1 commit 0d7071e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
26 changes: 20 additions & 6 deletions op-challenger/game/fault/contracts/faultdisputegame.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,30 @@ func (f *FaultDisputeGameContractLatest) GetSubClaims(ctx context.Context, block
}
txHash := moveIter.Event.Raw.TxHash

// todo: replace hardcoded nary, method name
nary := 1
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, batching.NewTxCall(f.contract.Abi(), txHash, "move"))
// todo: replace hardcoded method name
txCall := batching.NewTxCall(f.contract.Abi(), txHash, "move")
result, err := f.multiCaller.SingleCall(ctx, rpcblock.Latest, txCall)
if err != nil {
return nil, fmt.Errorf("failed to load claim calldata: %w", err)
}

txn, err := txCall.DecodeToTx(result)
if err != nil {
return nil, fmt.Errorf("failed to decode tx: %w", err)
}

var subClaims []common.Hash
// We should start from 2 du to the signature of move(Claim _disputed, uint256 _challengeIndex, Claim _claim)
for i := 2; i < nary+2; i++ {
subClaims = append(subClaims, result.GetHash(i))

if len(txn.BlobHashes()) > 0 {
// todo! fetch Blobs and unpack it into subClaims
} else {
inputMap, err := txCall.UnpackCallData(txn)
if err != nil {
return nil, fmt.Errorf("failed to unpack tx resp: %w", err)
}
// todo: replace hardcoded field _claim
claim := *abi.ConvertType(inputMap["_claim"], new([32]byte)).(*[32]byte)
subClaims = append(subClaims, claim)
}

return subClaims, nil
Expand Down
14 changes: 13 additions & 1 deletion op-challenger/game/fault/contracts/faultdisputegame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,19 @@ func TestGetSubClaims(t *testing.T) {
stubRpc.SetFilterLogResponse(topics, fdgAddr, block, out)

contractCall := batching.NewContractCall(fdgAbi, fdgAddr, "move", claim0.ClaimData.Value, challgenIndex[0], claim0.ClaimData.Value, true)
packed, err := contractCall.Pack()
inputData, err := contractCall.Pack()
require.NoError(t, err)

tx := coreTypes.NewTx(&coreTypes.LegacyTx{
Nonce: 0,
GasPrice: big.NewInt(11111),
Gas: 1111,
To: &claim0.Claimant,
Value: big.NewInt(111),
Data: inputData,
})
require.NoError(t, err)
packed, err := tx.MarshalBinary()
require.NoError(t, err)
stubRpc.SetTxResponse(txHash, packed)

Expand Down
24 changes: 14 additions & 10 deletions op-service/sources/batching/tx_call.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package batching

import (
"fmt"

"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -38,8 +37,8 @@ func (b *TxCall) ToBatchElemCreator() (BatchElementCreator, error) {
}

func (c *TxCall) HandleResult(result interface{}) (*CallResult, error) {
out, err := c.Unpack(*result.(*hexutil.Bytes))
return out, err
res := result.(*hexutil.Bytes)
return &CallResult{out: []interface{}{*res}}, nil
}

func (c *TxCall) DecodeTxParams(data []byte) (map[string]interface{}, error) {
Expand All @@ -54,12 +53,17 @@ func (c *TxCall) DecodeTxParams(data []byte) (map[string]interface{}, error) {
return v, nil
}

func (c *TxCall) Unpack(hex hexutil.Bytes) (*CallResult, error) {
inputs := c.Abi.Methods[c.Method].Inputs

out, err := inputs.UnpackValues(hex[4:])
func (c *TxCall) DecodeToTx(res *CallResult) (*types.Transaction, error) {
txn := new(types.Transaction)
hex := res.out[0].(hexutil.Bytes)
err := txn.UnmarshalBinary(hex)
if err != nil {
return nil, fmt.Errorf("failed to unpack inputs: %w", err)
return nil, err
}
return &CallResult{out: out}, nil
return txn, nil
}

func (c *TxCall) UnpackCallData(txn *types.Transaction) (map[string]interface{}, error) {
input := txn.Data()
return c.DecodeTxParams(input)
}
27 changes: 19 additions & 8 deletions op-service/sources/batching/tx_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/test"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)

Expand All @@ -26,11 +27,6 @@ func TestTxCall_ToCallArgs(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectedAmount, unpackedMap["amount"])
require.Equal(t, expectedSpender, unpackedMap["spender"])

unpacked, err := call.Unpack(packed)
require.NoError(t, err)
require.Equal(t, expectedSpender, unpacked.GetAddress(0))
require.Equal(t, expectedAmount, unpacked.GetBigInt(1))
}

func TestGetTxCalldata(t *testing.T) {
Expand All @@ -42,7 +38,17 @@ func TestGetTxCalldata(t *testing.T) {
testAbi, err := test.ERC20MetaData.GetAbi()
require.NoError(t, err)
contractCall := NewContractCall(testAbi, addr, "approve", expectedSpender, expectedAmount)
packed, err := contractCall.Pack()
inputData, err := contractCall.Pack()
tx := types.NewTx(&types.LegacyTx{
Nonce: 0,
GasPrice: big.NewInt(11111),
Gas: 1111,
To: &addr,
Value: big.NewInt(111),
Data: inputData,
})
require.NoError(t, err)
packed, err := tx.MarshalBinary()
require.NoError(t, err)

stub := test.NewRpcStub(t)
Expand All @@ -52,6 +58,11 @@ func TestGetTxCalldata(t *testing.T) {
txCall := NewTxCall(testAbi, txHash, "approve")
result, err := caller.SingleCall(context.Background(), rpcblock.Latest, txCall)
require.NoError(t, err)
require.Equal(t, expectedSpender, result.GetAddress(0))
require.Equal(t, expectedAmount, result.GetBigInt(1))

decodedTx, err := txCall.DecodeToTx(result)
require.NoError(t, err)
unpackedMap, err := txCall.UnpackCallData(decodedTx)
require.NoError(t, err)
require.Equal(t, expectedSpender, unpackedMap["spender"])
require.Equal(t, expectedAmount, unpackedMap["amount"])
}

0 comments on commit 0d7071e

Please sign in to comment.