github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/scripts/go-ffi/utils.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "math/big" 7 8 "github.com/ethereum-optimism/optimism/op-bindings/bindings" 9 "github.com/ethereum-optimism/optimism/op-chain-ops/crossdomain" 10 "github.com/ethereum-optimism/optimism/op-node/rollup" 11 "github.com/ethereum-optimism/optimism/op-node/rollup/derive" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethereum/go-ethereum/core/types" 14 ) 15 16 var UnknownNonceVersion = errors.New("Unknown nonce version") 17 18 // checkOk checks if ok is false, and panics if so. 19 // Shorthand to ease go's god awful error handling 20 func checkOk(ok bool) { 21 if !ok { 22 panic(fmt.Errorf("checkOk failed")) 23 } 24 } 25 26 // checkErr checks if err is not nil, and throws if so. 27 // Shorthand to ease go's god awful error handling 28 func checkErr(err error, failReason string) { 29 if err != nil { 30 panic(fmt.Errorf("%s: %w", failReason, err)) 31 } 32 } 33 34 // encodeCrossDomainMessage encodes a versioned cross domain message into a byte array. 35 func encodeCrossDomainMessage(nonce *big.Int, sender common.Address, target common.Address, value *big.Int, gasLimit *big.Int, data []byte) ([]byte, error) { 36 _, version := crossdomain.DecodeVersionedNonce(nonce) 37 38 var encoded []byte 39 var err error 40 if version.Cmp(big.NewInt(0)) == 0 { 41 // Encode cross domain message V0 42 encoded, err = crossdomain.EncodeCrossDomainMessageV0(target, sender, data, nonce) 43 } else if version.Cmp(big.NewInt(1)) == 0 { 44 // Encode cross domain message V1 45 encoded, err = crossdomain.EncodeCrossDomainMessageV1(nonce, sender, target, value, gasLimit, data) 46 } else { 47 return nil, UnknownNonceVersion 48 } 49 50 return encoded, err 51 } 52 53 // hashWithdrawal hashes a withdrawal transaction. 54 func hashWithdrawal(nonce *big.Int, sender common.Address, target common.Address, value *big.Int, gasLimit *big.Int, data []byte) (common.Hash, error) { 55 wd := crossdomain.Withdrawal{ 56 Nonce: nonce, 57 Sender: &sender, 58 Target: &target, 59 Value: value, 60 GasLimit: gasLimit, 61 Data: data, 62 } 63 return wd.Hash() 64 } 65 66 // hashOutputRootProof hashes an output root proof. 67 func hashOutputRootProof(version common.Hash, stateRoot common.Hash, messagePasserStorageRoot common.Hash, latestBlockHash common.Hash) (common.Hash, error) { 68 hash, err := rollup.ComputeL2OutputRoot(&bindings.TypesOutputRootProof{ 69 Version: version, 70 StateRoot: stateRoot, 71 MessagePasserStorageRoot: messagePasserStorageRoot, 72 LatestBlockhash: latestBlockHash, 73 }) 74 if err != nil { 75 return common.Hash{}, err 76 } 77 return common.Hash(hash), nil 78 } 79 80 // makeDepositTx creates a deposit transaction type. 81 func makeDepositTx( 82 from common.Address, 83 to common.Address, 84 value *big.Int, 85 mint *big.Int, 86 gasLimit *big.Int, 87 isCreate bool, 88 data []byte, 89 l1BlockHash common.Hash, 90 logIndex *big.Int, 91 ) types.DepositTx { 92 // Create deposit transaction source 93 udp := derive.UserDepositSource{ 94 L1BlockHash: l1BlockHash, 95 LogIndex: logIndex.Uint64(), 96 } 97 98 // Create deposit transaction 99 depositTx := types.DepositTx{ 100 SourceHash: udp.SourceHash(), 101 From: from, 102 Value: value, 103 Gas: gasLimit.Uint64(), 104 IsSystemTransaction: false, // This will never be a system transaction in the tests. 105 Data: data, 106 } 107 108 // Fill optional fields 109 if mint.Cmp(big.NewInt(0)) == 1 { 110 depositTx.Mint = mint 111 } 112 if !isCreate { 113 depositTx.To = &to 114 } 115 116 return depositTx 117 } 118 119 // Custom type to write the generated proof to 120 type proofList [][]byte 121 122 func (n *proofList) Put(key []byte, value []byte) error { 123 *n = append(*n, value) 124 return nil 125 } 126 127 func (n *proofList) Delete(key []byte) error { 128 panic("not supported") 129 }