github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/bind/v2/util_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package bind_test 18 19 import ( 20 "context" 21 "errors" 22 "math/big" 23 "testing" 24 "time" 25 26 "github.com/ethereum/go-ethereum/accounts/abi/bind/v2" 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/ethclient/simulated" 31 "github.com/ethereum/go-ethereum/params" 32 ) 33 34 var waitDeployedTests = map[string]struct { 35 code string 36 gas uint64 37 wantAddress common.Address 38 wantErr error 39 }{ 40 "successful deploy": { 41 code: `6060604052600a8060106000396000f360606040526008565b00`, 42 gas: 3000000, 43 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 44 }, 45 "empty code": { 46 code: ``, 47 gas: 300000, 48 wantErr: bind.ErrNoCodeAfterDeploy, 49 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 50 }, 51 } 52 53 func TestWaitDeployed(t *testing.T) { 54 t.Parallel() 55 for name, test := range waitDeployedTests { 56 backend := simulated.NewBackend( 57 types.GenesisAlloc{ 58 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)}, 59 }, 60 ) 61 defer backend.Close() 62 63 // Create the transaction 64 head, _ := backend.Client().HeaderByNumber(context.Background(), nil) // Should be child's, good enough 65 gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(params.GWei)) 66 67 tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code)) 68 tx, _ = types.SignTx(tx, types.LatestSignerForChainID(big.NewInt(1337)), testKey) 69 70 // Wait for it to get mined in the background. 71 var ( 72 err error 73 address common.Address 74 mined = make(chan struct{}) 75 ctx = context.Background() 76 ) 77 go func() { 78 address, err = bind.WaitDeployed(ctx, backend.Client(), tx.Hash()) 79 close(mined) 80 }() 81 82 // Send and mine the transaction. 83 if err := backend.Client().SendTransaction(ctx, tx); err != nil { 84 t.Errorf("test %q: failed to send transaction: %v", name, err) 85 } 86 backend.Commit() 87 88 select { 89 case <-mined: 90 if err != test.wantErr { 91 t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err) 92 } 93 if address != test.wantAddress { 94 t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) 95 } 96 case <-time.After(2 * time.Second): 97 t.Errorf("test %q: timeout", name) 98 } 99 } 100 } 101 102 func TestWaitDeployedCornerCases(t *testing.T) { 103 backend := simulated.NewBackend( 104 types.GenesisAlloc{ 105 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)}, 106 }, 107 ) 108 defer backend.Close() 109 110 head, _ := backend.Client().HeaderByNumber(context.Background(), nil) // Should be child's, good enough 111 gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1)) 112 113 // Create a transaction to an account. 114 code := "6060604052600a8060106000396000f360606040526008565b00" 115 tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code)) 116 tx, _ = types.SignTx(tx, types.LatestSigner(params.AllDevChainProtocolChanges), testKey) 117 ctx, cancel := context.WithCancel(context.Background()) 118 defer cancel() 119 if err := backend.Client().SendTransaction(ctx, tx); err != nil { 120 t.Errorf("failed to send transaction: %q", err) 121 } 122 backend.Commit() 123 if _, err := bind.WaitDeployed(ctx, backend.Client(), tx.Hash()); err != bind.ErrNoAddressInReceipt { 124 t.Errorf("error mismatch: want %q, got %q, ", bind.ErrNoAddressInReceipt, err) 125 } 126 127 // Create a transaction that is not mined. 128 tx = types.NewContractCreation(1, big.NewInt(0), 3000000, gasPrice, common.FromHex(code)) 129 tx, _ = types.SignTx(tx, types.LatestSigner(params.AllDevChainProtocolChanges), testKey) 130 131 go func() { 132 contextCanceled := errors.New("context canceled") 133 if _, err := bind.WaitDeployed(ctx, backend.Client(), tx.Hash()); err.Error() != contextCanceled.Error() { 134 t.Errorf("error mismatch: want %q, got %q, ", contextCanceled, err) 135 } 136 }() 137 138 if err := backend.Client().SendTransaction(ctx, tx); err != nil { 139 t.Errorf("failed to send transaction: %q", err) 140 } 141 cancel() 142 }