github.com/theQRL/go-zond@v0.2.1/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/theQRL/go-zond/accounts/abi/bind" 27 "github.com/theQRL/go-zond/accounts/abi/bind/backends" 28 "github.com/theQRL/go-zond/common" 29 "github.com/theQRL/go-zond/core" 30 "github.com/theQRL/go-zond/core/types" 31 "github.com/theQRL/go-zond/crypto/pqcrypto" 32 ) 33 34 var testKey, _ = pqcrypto.HexToDilithium("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 35 36 var wantedAddr, _ = common.NewAddressFromString("Zfe66B8AED6e4fb3e12d0B65f61ef246c4d0CfFFA") 37 var waitDeployedTests = map[string]struct { 38 code string 39 gas uint64 40 wantAddress common.Address 41 wantErr error 42 }{ 43 "successful deploy": { 44 code: `6060604052600a8060106000396000f360606040526008565b00`, 45 gas: 3000000, 46 wantAddress: wantedAddr, 47 }, 48 "empty code": { 49 code: ``, 50 gas: 300000, 51 wantErr: bind.ErrNoCodeAfterDeploy, 52 wantAddress: wantedAddr, 53 }, 54 } 55 56 func TestWaitDeployed(t *testing.T) { 57 for name, test := range waitDeployedTests { 58 backend := backends.NewSimulatedBackend( 59 core.GenesisAlloc{ 60 testKey.GetAddress(): {Balance: big.NewInt(10000000000000000)}, 61 }, 62 10000000, 63 ) 64 defer backend.Close() 65 66 // Create the transaction 67 head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough 68 gasFeeCap := new(big.Int).Add(head.BaseFee, big.NewInt(1)) 69 70 tx := types.NewTx(&types.DynamicFeeTx{ 71 Nonce: 0, 72 Value: big.NewInt(0), 73 Gas: test.gas, 74 Data: common.FromHex(test.code), 75 GasFeeCap: gasFeeCap, 76 }) 77 tx, _ = types.SignTx(tx, types.ShanghaiSigner{ChainId: big.NewInt(1337)}, testKey) 78 79 // Wait for it to get mined in the background. 80 var ( 81 err error 82 address common.Address 83 mined = make(chan struct{}) 84 ctx = context.Background() 85 ) 86 go func() { 87 address, err = bind.WaitDeployed(ctx, backend, tx) 88 close(mined) 89 }() 90 91 // Send and mine the transaction. 92 backend.SendTransaction(ctx, tx) 93 backend.Commit() 94 95 select { 96 case <-mined: 97 if err != test.wantErr { 98 t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err) 99 } 100 if address != test.wantAddress { 101 t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) 102 } 103 case <-time.After(2 * time.Second): 104 t.Errorf("test %q: timeout", name) 105 } 106 } 107 } 108 109 func TestWaitDeployedCornerCases(t *testing.T) { 110 backend := backends.NewSimulatedBackend( 111 core.GenesisAlloc{ 112 testKey.GetAddress(): {Balance: big.NewInt(10000000000000000)}, 113 }, 114 10000000, 115 ) 116 defer backend.Close() 117 118 head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough 119 gasFeeCap := new(big.Int).Add(head.BaseFee, big.NewInt(1)) 120 121 // Create a transaction to an account. 122 code := "6060604052600a8060106000396000f360606040526008565b00" 123 to, _ := common.NewAddressFromString("Z0000000000000000000000000000000000000001") 124 tx := types.NewTx(&types.DynamicFeeTx{ 125 Nonce: 0, 126 To: &to, 127 Value: big.NewInt(0), 128 Gas: 3000000, 129 GasFeeCap: gasFeeCap, 130 Data: common.FromHex(code), 131 }) 132 tx, _ = types.SignTx(tx, types.ShanghaiSigner{ChainId: big.NewInt(0)}, testKey) 133 ctx, cancel := context.WithCancel(context.Background()) 134 defer cancel() 135 backend.SendTransaction(ctx, tx) 136 backend.Commit() 137 notContentCreation := errors.New("tx is not contract creation") 138 if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() { 139 t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err) 140 } 141 142 // Create a transaction that is not mined. 143 tx = types.NewTx(&types.DynamicFeeTx{ 144 Nonce: 1, 145 Value: big.NewInt(0), 146 Gas: 3000000, 147 GasFeeCap: gasFeeCap, 148 Data: common.FromHex(code), 149 }) 150 tx, _ = types.SignTx(tx, types.ShanghaiSigner{ChainId: big.NewInt(0)}, testKey) 151 152 go func() { 153 contextCanceled := errors.New("context canceled") 154 if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() { 155 t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err) 156 } 157 }() 158 159 backend.SendTransaction(ctx, tx) 160 cancel() 161 }