github.com/bcskill/bcschain/v3@v3.4.9-beta2/accounts/abi/bind/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/bcskill/bcschain/v3/accounts/abi/bind" 27 "github.com/bcskill/bcschain/v3/accounts/abi/bind/backends" 28 "github.com/bcskill/bcschain/v3/common" 29 "github.com/bcskill/bcschain/v3/core" 30 "github.com/bcskill/bcschain/v3/core/types" 31 "github.com/bcskill/bcschain/v3/crypto" 32 ) 33 34 var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 35 36 var waitDeployedTests = map[string]struct { 37 code string 38 gas uint64 39 wantAddress common.Address 40 wantErr error 41 }{ 42 "successful deploy": { 43 code: `6060604052600a8060106000396000f360606040526008565b00`, 44 gas: 3000000, 45 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 46 }, 47 "empty code": { 48 code: ``, 49 gas: 300000, 50 wantErr: bind.ErrNoCodeAfterDeploy, 51 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 52 }, 53 } 54 55 func TestWaitDeployed(t *testing.T) { 56 for name, test := range waitDeployedTests { 57 backend := backends.NewSimulatedBackend( 58 core.GenesisAlloc{ 59 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)}, 60 }, 61 ) 62 63 // Create the transaction. 64 tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code)) 65 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) 66 67 // Wait for it to get mined in the background. 68 var ( 69 err error 70 address common.Address 71 mined = make(chan struct{}) 72 ctx = context.Background() 73 ) 74 go func() { 75 address, err = bind.WaitDeployed(ctx, backend, tx) 76 close(mined) 77 }() 78 79 // Send and mine the transaction. 80 backend.SendTransaction(ctx, tx) 81 backend.Commit() 82 83 select { 84 case <-mined: 85 if err != test.wantErr { 86 t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err) 87 } 88 if address != test.wantAddress { 89 t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) 90 } 91 case <-time.After(2 * time.Second): 92 t.Errorf("test %q: timeout", name) 93 } 94 } 95 } 96 97 func TestWaitDeployedCornerCases(t *testing.T) { 98 backend := backends.NewSimulatedBackend( 99 core.GenesisAlloc{ 100 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)}, 101 }, 102 ) 103 104 // Create a transaction to an account. 105 code := "6060604052600a8060106000396000f360606040526008565b00" 106 tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code)) 107 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) 108 ctx, cancel := context.WithCancel(context.Background()) 109 defer cancel() 110 backend.SendTransaction(ctx, tx) 111 backend.Commit() 112 notContentCreation := errors.New("tx is not contract creation") 113 if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() { 114 t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err) 115 } 116 117 // Create a transaction that is not mined. 118 tx = types.NewContractCreation(1, big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code)) 119 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) 120 121 go func() { 122 contextCanceled := errors.New("context canceled") 123 if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() { 124 t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err) 125 } 126 }() 127 128 backend.SendTransaction(ctx, tx) 129 cancel() 130 }