github.com/ethereum-optimism/optimism@v1.7.2/op-node/withdrawals/utils.go (about)

     1  package withdrawals
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"fmt"
     8  	"math/big"
     9  
    10  	"github.com/ethereum/go-ethereum/accounts/abi"
    11  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/ethereum/go-ethereum/core/types"
    14  	"github.com/ethereum/go-ethereum/crypto"
    15  	"github.com/ethereum/go-ethereum/ethclient/gethclient"
    16  
    17  	"github.com/ethereum-optimism/optimism/op-bindings/bindings"
    18  	"github.com/ethereum-optimism/optimism/op-bindings/bindingspreview"
    19  	"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
    20  )
    21  
    22  var MessagePassedTopic = crypto.Keccak256Hash([]byte("MessagePassed(uint256,address,address,uint256,uint256,bytes,bytes32)"))
    23  
    24  type ProofClient interface {
    25  	GetProof(context.Context, common.Address, []string, *big.Int) (*gethclient.AccountResult, error)
    26  }
    27  
    28  type ReceiptClient interface {
    29  	TransactionReceipt(context.Context, common.Hash) (*types.Receipt, error)
    30  }
    31  
    32  type BlockClient interface {
    33  	BlockByNumber(context.Context, *big.Int) (*types.Block, error)
    34  }
    35  
    36  // ProvenWithdrawalParameters is the set of parameters to pass to the ProveWithdrawalTransaction
    37  // and FinalizeWithdrawalTransaction functions
    38  type ProvenWithdrawalParameters struct {
    39  	Nonce           *big.Int
    40  	Sender          common.Address
    41  	Target          common.Address
    42  	Value           *big.Int
    43  	GasLimit        *big.Int
    44  	L2OutputIndex   *big.Int
    45  	Data            []byte
    46  	OutputRootProof bindings.TypesOutputRootProof
    47  	WithdrawalProof [][]byte // List of trie nodes to prove L2 storage
    48  }
    49  
    50  // ProveWithdrawalParameters calls ProveWithdrawalParametersForBlock with the most recent L2 output after the given header.
    51  func ProveWithdrawalParameters(ctx context.Context, proofCl ProofClient, l2ReceiptCl ReceiptClient, l2BlockCl BlockClient, txHash common.Hash, header *types.Header, l2OutputOracleContract *bindings.L2OutputOracleCaller) (ProvenWithdrawalParameters, error) {
    52  	l2OutputIndex, err := l2OutputOracleContract.GetL2OutputIndexAfter(&bind.CallOpts{}, header.Number)
    53  	if err != nil {
    54  		return ProvenWithdrawalParameters{}, fmt.Errorf("failed to get l2OutputIndex: %w", err)
    55  	}
    56  	l2BlockNumber := header.Number
    57  	return ProveWithdrawalParametersForBlock(ctx, proofCl, l2ReceiptCl, l2BlockCl, txHash, l2BlockNumber, l2OutputIndex)
    58  }
    59  
    60  // ProveWithdrawalParametersFPAC calls ProveWithdrawalParametersForBlock with the most recent L2 output after the latest game.
    61  func ProveWithdrawalParametersFPAC(ctx context.Context, proofCl ProofClient, l2ReceiptCl ReceiptClient, l2BlockCl BlockClient, txHash common.Hash, disputeGameFactoryContract *bindings.DisputeGameFactoryCaller, optimismPortal2Contract *bindingspreview.OptimismPortal2Caller) (ProvenWithdrawalParameters, error) {
    62  	latestGame, err := FindLatestGame(ctx, disputeGameFactoryContract, optimismPortal2Contract)
    63  	if err != nil {
    64  		return ProvenWithdrawalParameters{}, fmt.Errorf("failed to find latest game: %w", err)
    65  	}
    66  
    67  	l2BlockNumber := new(big.Int).SetBytes(latestGame.ExtraData[0:32])
    68  	l2OutputIndex := latestGame.Index
    69  	return ProveWithdrawalParametersForBlock(ctx, proofCl, l2ReceiptCl, l2BlockCl, txHash, l2BlockNumber, l2OutputIndex)
    70  }
    71  
    72  // ProveWithdrawalParametersForBlock queries L1 & L2 to generate all withdrawal parameters and proof necessary to prove a withdrawal on L1.
    73  // The header provided is very important. It should be a block (timestamp) for which there is a submitted output in the L2 Output Oracle
    74  // contract. If not, the withdrawal will fail as it the storage proof cannot be verified if there is no submitted state root.
    75  func ProveWithdrawalParametersForBlock(ctx context.Context, proofCl ProofClient, l2ReceiptCl ReceiptClient, l2BlockCl BlockClient, txHash common.Hash, l2BlockNumber, l2OutputIndex *big.Int) (ProvenWithdrawalParameters, error) {
    76  	// Transaction receipt
    77  	receipt, err := l2ReceiptCl.TransactionReceipt(ctx, txHash)
    78  	if err != nil {
    79  		return ProvenWithdrawalParameters{}, err
    80  	}
    81  	// Parse the receipt
    82  	ev, err := ParseMessagePassed(receipt)
    83  	if err != nil {
    84  		return ProvenWithdrawalParameters{}, err
    85  	}
    86  	// Generate then verify the withdrawal proof
    87  	withdrawalHash, err := WithdrawalHash(ev)
    88  	if !bytes.Equal(withdrawalHash[:], ev.WithdrawalHash[:]) {
    89  		return ProvenWithdrawalParameters{}, errors.New("Computed withdrawal hash incorrectly")
    90  	}
    91  	if err != nil {
    92  		return ProvenWithdrawalParameters{}, err
    93  	}
    94  	slot := StorageSlotOfWithdrawalHash(withdrawalHash)
    95  
    96  	// Fetch the block from the L2 node
    97  	l2Block, err := l2BlockCl.BlockByNumber(ctx, l2BlockNumber)
    98  	if err != nil {
    99  		return ProvenWithdrawalParameters{}, fmt.Errorf("failed to get l2Block: %w", err)
   100  	}
   101  
   102  	p, err := proofCl.GetProof(ctx, predeploys.L2ToL1MessagePasserAddr, []string{slot.String()}, l2Block.Number())
   103  	if err != nil {
   104  		return ProvenWithdrawalParameters{}, err
   105  	}
   106  	if len(p.StorageProof) != 1 {
   107  		return ProvenWithdrawalParameters{}, errors.New("invalid amount of storage proofs")
   108  	}
   109  
   110  	err = VerifyProof(l2Block.Root(), p)
   111  	if err != nil {
   112  		return ProvenWithdrawalParameters{}, err
   113  	}
   114  
   115  	// Encode it as expected by the contract
   116  	trieNodes := make([][]byte, len(p.StorageProof[0].Proof))
   117  	for i, s := range p.StorageProof[0].Proof {
   118  		trieNodes[i] = common.FromHex(s)
   119  	}
   120  
   121  	return ProvenWithdrawalParameters{
   122  		Nonce:         ev.Nonce,
   123  		Sender:        ev.Sender,
   124  		Target:        ev.Target,
   125  		Value:         ev.Value,
   126  		GasLimit:      ev.GasLimit,
   127  		L2OutputIndex: l2OutputIndex,
   128  		Data:          ev.Data,
   129  		OutputRootProof: bindings.TypesOutputRootProof{
   130  			Version:                  [32]byte{}, // Empty for version 1
   131  			StateRoot:                l2Block.Root(),
   132  			MessagePasserStorageRoot: p.StorageHash,
   133  			LatestBlockhash:          l2Block.Hash(),
   134  		},
   135  		WithdrawalProof: trieNodes,
   136  	}, nil
   137  }
   138  
   139  // FindLatestGame finds the latest game in the DisputeGameFactory contract.
   140  func FindLatestGame(ctx context.Context, disputeGameFactoryContract *bindings.DisputeGameFactoryCaller, optimismPortal2Contract *bindingspreview.OptimismPortal2Caller) (*bindings.IDisputeGameFactoryGameSearchResult, error) {
   141  	respectedGameType, err := optimismPortal2Contract.RespectedGameType(&bind.CallOpts{})
   142  	if err != nil {
   143  		return nil, fmt.Errorf("failed to get respected game type: %w", err)
   144  	}
   145  
   146  	gameCount, err := disputeGameFactoryContract.GameCount(&bind.CallOpts{})
   147  	if err != nil {
   148  		return nil, fmt.Errorf("failed to get game count: %w", err)
   149  	}
   150  	if gameCount.Cmp(common.Big0) == 0 {
   151  		return nil, errors.New("no games")
   152  	}
   153  
   154  	searchStart := new(big.Int).Sub(gameCount, common.Big1)
   155  	latestGames, err := disputeGameFactoryContract.FindLatestGames(&bind.CallOpts{}, respectedGameType, searchStart, common.Big1)
   156  	if err != nil {
   157  		return nil, fmt.Errorf("failed to get latest games: %w", err)
   158  	}
   159  	if len(latestGames) == 0 {
   160  		return nil, errors.New("no latest games")
   161  	}
   162  
   163  	latestGame := latestGames[0]
   164  	return &latestGame, nil
   165  }
   166  
   167  // Standard ABI types copied from golang ABI tests
   168  var (
   169  	Uint256Type, _ = abi.NewType("uint256", "", nil)
   170  	BytesType, _   = abi.NewType("bytes", "", nil)
   171  	AddressType, _ = abi.NewType("address", "", nil)
   172  )
   173  
   174  // WithdrawalHash computes the hash of the withdrawal that was stored in the L2toL1MessagePasser
   175  // contract state.
   176  // TODO:
   177  //   - I don't like having to use the ABI Generated struct
   178  //   - There should be a better way to run the ABI encoding
   179  //   - These needs to be fuzzed against the solidity
   180  func WithdrawalHash(ev *bindings.L2ToL1MessagePasserMessagePassed) (common.Hash, error) {
   181  	//  abi.encode(nonce, msg.sender, _target, msg.value, _gasLimit, _data)
   182  	args := abi.Arguments{
   183  		{Name: "nonce", Type: Uint256Type},
   184  		{Name: "sender", Type: AddressType},
   185  		{Name: "target", Type: AddressType},
   186  		{Name: "value", Type: Uint256Type},
   187  		{Name: "gasLimit", Type: Uint256Type},
   188  		{Name: "data", Type: BytesType},
   189  	}
   190  	enc, err := args.Pack(ev.Nonce, ev.Sender, ev.Target, ev.Value, ev.GasLimit, ev.Data)
   191  	if err != nil {
   192  		return common.Hash{}, fmt.Errorf("failed to pack for withdrawal hash: %w", err)
   193  	}
   194  	return crypto.Keccak256Hash(enc), nil
   195  }
   196  
   197  // ParseMessagePassed parses MessagePassed events from
   198  // a transaction receipt. It does not support multiple withdrawals
   199  // per receipt.
   200  func ParseMessagePassed(receipt *types.Receipt) (*bindings.L2ToL1MessagePasserMessagePassed, error) {
   201  	contract, err := bindings.NewL2ToL1MessagePasser(common.Address{}, nil)
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  
   206  	for _, log := range receipt.Logs {
   207  		if len(log.Topics) == 0 || log.Topics[0] != MessagePassedTopic {
   208  			continue
   209  		}
   210  
   211  		ev, err := contract.ParseMessagePassed(*log)
   212  		if err != nil {
   213  			return nil, fmt.Errorf("failed to parse log: %w", err)
   214  		}
   215  		return ev, nil
   216  	}
   217  	return nil, errors.New("Unable to find MessagePassed event")
   218  }
   219  
   220  // StorageSlotOfWithdrawalHash determines the storage slot of the L2ToL1MessagePasser contract to look at
   221  // given a WithdrawalHash
   222  func StorageSlotOfWithdrawalHash(hash common.Hash) common.Hash {
   223  	// The withdrawals mapping is the 0th storage slot in the L2ToL1MessagePasser contract.
   224  	// To determine the storage slot, use keccak256(withdrawalHash ++ p)
   225  	// Where p is the 32 byte value of the storage slot and ++ is concatenation
   226  	buf := make([]byte, 64)
   227  	copy(buf, hash[:])
   228  	return crypto.Keccak256Hash(buf)
   229  }