utils.go 8.28 KB
Newer Older
1 2 3
package withdrawals

import (
4
	"bytes"
5 6 7 8 9 10 11 12 13 14 15
	"context"
	"errors"
	"fmt"
	"math/big"

	"github.com/ethereum/go-ethereum/accounts/abi"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
	"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
	"github.com/ethereum-optimism/optimism/op-e2e/e2eutils"
21 22
)

23
var MessagePassedTopic = crypto.Keccak256Hash([]byte("MessagePassed(uint256,address,address,uint256,uint256,bytes,bytes32)"))
24

25 26 27 28
type ProofClient interface {
	GetProof(context.Context, common.Address, []string, *big.Int) (*gethclient.AccountResult, error)
}

29 30
type ReceiptClient interface {
	TransactionReceipt(context.Context, common.Hash) (*types.Receipt, error)
31 32
}

33 34 35 36
type BlockClient interface {
	BlockByNumber(context.Context, *big.Int) (*types.Block, error)
}

37 38 39
// ProvenWithdrawalParameters is the set of parameters to pass to the ProveWithdrawalTransaction
// and FinalizeWithdrawalTransaction functions
type ProvenWithdrawalParameters struct {
40 41 42 43 44
	Nonce           *big.Int
	Sender          common.Address
	Target          common.Address
	Value           *big.Int
	GasLimit        *big.Int
45
	L2OutputIndex   *big.Int
46
	Data            []byte
47
	OutputRootProof bindings.TypesOutputRootProof
48
	WithdrawalProof [][]byte // List of trie nodes to prove L2 storage
49 50
}

51
// ProveWithdrawalParameters queries L1 & L2 to generate all withdrawal parameters and proof necessary to prove a withdrawal on L1.
52
// The header provided is very important. It should be a block (timestamp) for which there is a submitted output in the L2 Output Oracle
53
// contract. If not, the withdrawal will fail as it the storage proof cannot be verified if there is no submitted state root.
54
func ProveWithdrawalParameters(ctx context.Context, proofCl ProofClient, l2ReceiptCl ReceiptClient, l2BlockCl BlockClient, txHash common.Hash, header *types.Header, l2OutputOracleContract *bindings.L2OutputOracleCaller, disputeGameFactoryContract *bindings.DisputeGameFactoryCaller, optimismPortalContract *bindings.OptimismPortalCaller, optimismPortal2Contract *bindingspreview.OptimismPortal2Caller) (ProvenWithdrawalParameters, error) {
55
	// Transaction receipt
56
	receipt, err := l2ReceiptCl.TransactionReceipt(ctx, txHash)
57
	if err != nil {
58
		return ProvenWithdrawalParameters{}, err
59 60
	}
	// Parse the receipt
61
	ev, err := ParseMessagePassed(receipt)
62
	if err != nil {
63
		return ProvenWithdrawalParameters{}, err
64 65 66
	}
	// Generate then verify the withdrawal proof
	withdrawalHash, err := WithdrawalHash(ev)
67
	if !bytes.Equal(withdrawalHash[:], ev.WithdrawalHash[:]) {
68
		return ProvenWithdrawalParameters{}, errors.New("Computed withdrawal hash incorrectly")
69
	}
70
	if err != nil {
71
		return ProvenWithdrawalParameters{}, err
72 73
	}
	slot := StorageSlotOfWithdrawalHash(withdrawalHash)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	var l2OutputIndex *big.Int
	var l2BlockNumber *big.Int
	if e2eutils.UseFPAC() {
		latestGame, err := FindLatestGame(ctx, disputeGameFactoryContract, optimismPortal2Contract)
		if err != nil {
			return ProvenWithdrawalParameters{}, fmt.Errorf("failed to find latest game: %w", err)
		}

		l2BlockNumber = new(big.Int).SetBytes(latestGame.ExtraData[0:32])
		l2OutputIndex = latestGame.Index
	} else {
		l2OutputIndex, err = l2OutputOracleContract.GetL2OutputIndexAfter(&bind.CallOpts{}, header.Number)
		if err != nil {
			return ProvenWithdrawalParameters{}, fmt.Errorf("failed to get l2OutputIndex: %w", err)
		}

		l2BlockNumber = header.Number
92
	}
93

94 95
	// Fetch the block from the L2 node
	l2Block, err := l2BlockCl.BlockByNumber(ctx, l2BlockNumber)
96
	if err != nil {
97
		return ProvenWithdrawalParameters{}, fmt.Errorf("failed to get l2Block: %w", err)
98
	}
99 100

	p, err := proofCl.GetProof(ctx, predeploys.L2ToL1MessagePasserAddr, []string{slot.String()}, l2Block.Number())
101
	if err != nil {
102
		return ProvenWithdrawalParameters{}, err
103 104
	}
	if len(p.StorageProof) != 1 {
105
		return ProvenWithdrawalParameters{}, errors.New("invalid amount of storage proofs")
106 107
	}

108 109 110 111 112
	err = VerifyProof(l2Block.Root(), p)
	if err != nil {
		return ProvenWithdrawalParameters{}, err
	}

113 114 115 116 117 118
	// Encode it as expected by the contract
	trieNodes := make([][]byte, len(p.StorageProof[0].Proof))
	for i, s := range p.StorageProof[0].Proof {
		trieNodes[i] = common.FromHex(s)
	}

119
	return ProvenWithdrawalParameters{
120 121 122 123 124 125 126
		Nonce:         ev.Nonce,
		Sender:        ev.Sender,
		Target:        ev.Target,
		Value:         ev.Value,
		GasLimit:      ev.GasLimit,
		L2OutputIndex: l2OutputIndex,
		Data:          ev.Data,
127
		OutputRootProof: bindings.TypesOutputRootProof{
128
			Version:                  [32]byte{}, // Empty for version 1
129
			StateRoot:                l2Block.Root(),
130
			MessagePasserStorageRoot: p.StorageHash,
131
			LatestBlockhash:          l2Block.Hash(),
132
		},
133
		WithdrawalProof: trieNodes,
134 135 136
	}, nil
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
// FindLatestGame finds the latest game in the DisputeGameFactory contract.
func FindLatestGame(ctx context.Context, disputeGameFactoryContract *bindings.DisputeGameFactoryCaller, optimismPortal2Contract *bindingspreview.OptimismPortal2Caller) (*bindings.IDisputeGameFactoryGameSearchResult, error) {
	respectedGameType, err := optimismPortal2Contract.RespectedGameType(&bind.CallOpts{})
	if err != nil {
		return nil, fmt.Errorf("failed to get respected game type: %w", err)
	}

	gameCount, err := disputeGameFactoryContract.GameCount(&bind.CallOpts{})
	if err != nil {
		return nil, fmt.Errorf("failed to get game count: %w", err)
	}
	if gameCount.Cmp(common.Big0) == 0 {
		return nil, errors.New("no games")
	}

	searchStart := new(big.Int).Sub(gameCount, common.Big1)
	latestGames, err := disputeGameFactoryContract.FindLatestGames(&bind.CallOpts{}, respectedGameType, searchStart, common.Big1)
	if err != nil {
		return nil, fmt.Errorf("failed to get latest games: %w", err)
	}
	if len(latestGames) == 0 {
		return nil, errors.New("no latest games")
	}

	latestGame := latestGames[0]
	return &latestGame, nil
}

165 166 167 168 169 170 171
// Standard ABI types copied from golang ABI tests
var (
	Uint256Type, _ = abi.NewType("uint256", "", nil)
	BytesType, _   = abi.NewType("bytes", "", nil)
	AddressType, _ = abi.NewType("address", "", nil)
)

172 173
// WithdrawalHash computes the hash of the withdrawal that was stored in the L2toL1MessagePasser
// contract state.
174
// TODO:
175 176 177
//   - I don't like having to use the ABI Generated struct
//   - There should be a better way to run the ABI encoding
//   - These needs to be fuzzed against the solidity
178
func WithdrawalHash(ev *bindings.L2ToL1MessagePasserMessagePassed) (common.Hash, error) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	//  abi.encode(nonce, msg.sender, _target, msg.value, _gasLimit, _data)
	args := abi.Arguments{
		{Name: "nonce", Type: Uint256Type},
		{Name: "sender", Type: AddressType},
		{Name: "target", Type: AddressType},
		{Name: "value", Type: Uint256Type},
		{Name: "gasLimit", Type: Uint256Type},
		{Name: "data", Type: BytesType},
	}
	enc, err := args.Pack(ev.Nonce, ev.Sender, ev.Target, ev.Value, ev.GasLimit, ev.Data)
	if err != nil {
		return common.Hash{}, fmt.Errorf("failed to pack for withdrawal hash: %w", err)
	}
	return crypto.Keccak256Hash(enc), nil
}

195
// ParseMessagePassed parses MessagePassed events from
196 197
// a transaction receipt. It does not support multiple withdrawals
// per receipt.
198
func ParseMessagePassed(receipt *types.Receipt) (*bindings.L2ToL1MessagePasserMessagePassed, error) {
199
	contract, err := bindings.NewL2ToL1MessagePasser(common.Address{}, nil)
200 201 202
	if err != nil {
		return nil, err
	}
203 204

	for _, log := range receipt.Logs {
205
		if len(log.Topics) == 0 || log.Topics[0] != MessagePassedTopic {
206
			continue
207
		}
208

209
		ev, err := contract.ParseMessagePassed(*log)
210 211
		if err != nil {
			return nil, fmt.Errorf("failed to parse log: %w", err)
212
		}
213
		return ev, nil
214
	}
215
	return nil, errors.New("Unable to find MessagePassed event")
216 217
}

218
// StorageSlotOfWithdrawalHash determines the storage slot of the L2ToL1MessagePasser contract to look at
219 220
// given a WithdrawalHash
func StorageSlotOfWithdrawalHash(hash common.Hash) common.Hash {
221
	// The withdrawals mapping is the 0th storage slot in the L2ToL1MessagePasser contract.
222 223 224 225 226 227
	// To determine the storage slot, use keccak256(withdrawalHash ++ p)
	// Where p is the 32 byte value of the storage slot and ++ is concatenation
	buf := make([]byte, 64)
	copy(buf, hash[:])
	return crypto.Keccak256Hash(buf)
}