github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/abi/bind/util_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package bind_test 13 14 import ( 15 "context" 16 "math/big" 17 "testing" 18 "time" 19 20 "github.com/Sberex/go-sberex/accounts/abi/bind" 21 "github.com/Sberex/go-sberex/accounts/abi/bind/backends" 22 "github.com/Sberex/go-sberex/common" 23 "github.com/Sberex/go-sberex/core" 24 "github.com/Sberex/go-sberex/core/types" 25 "github.com/Sberex/go-sberex/crypto" 26 ) 27 28 var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 29 30 var waitDeployedTests = map[string]struct { 31 code string 32 gas uint64 33 wantAddress common.Address 34 wantErr error 35 }{ 36 "successful deploy": { 37 code: `6060604052600a8060106000396000f360606040526008565b00`, 38 gas: 3000000, 39 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 40 }, 41 "empty code": { 42 code: ``, 43 gas: 300000, 44 wantErr: bind.ErrNoCodeAfterDeploy, 45 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 46 }, 47 } 48 49 func TestWaitDeployed(t *testing.T) { 50 for name, test := range waitDeployedTests { 51 backend := backends.NewSimulatedBackend(core.GenesisAlloc{ 52 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)}, 53 }) 54 55 // Create the transaction. 56 tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code)) 57 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) 58 59 // Wait for it to get mined in the background. 60 var ( 61 err error 62 address common.Address 63 mined = make(chan struct{}) 64 ctx = context.Background() 65 ) 66 go func() { 67 address, err = bind.WaitDeployed(ctx, backend, tx) 68 close(mined) 69 }() 70 71 // Send and mine the transaction. 72 backend.SendTransaction(ctx, tx) 73 backend.Commit() 74 75 select { 76 case <-mined: 77 if err != test.wantErr { 78 t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr) 79 } 80 if address != test.wantAddress { 81 t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) 82 } 83 case <-time.After(2 * time.Second): 84 t.Errorf("test %q: timeout", name) 85 } 86 } 87 }