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