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