github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/bind/util_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:31</date> 10 //</624450061668978688> 11 12 13 package bind_test 14 15 import ( 16 "context" 17 "math/big" 18 "testing" 19 "time" 20 21 "github.com/ethereum/go-ethereum/accounts/abi/bind" 22 "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/core" 25 "github.com/ethereum/go-ethereum/core/types" 26 "github.com/ethereum/go-ethereum/crypto" 27 ) 28 29 var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 30 31 var waitDeployedTests = map[string]struct { 32 code string 33 gas uint64 34 wantAddress common.Address 35 wantErr error 36 }{ 37 "successful deploy": { 38 code: `6060604052600a8060106000396000f360606040526008565b00`, 39 gas: 3000000, 40 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 41 }, 42 "empty code": { 43 code: ``, 44 gas: 300000, 45 wantErr: bind.ErrNoCodeAfterDeploy, 46 wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), 47 }, 48 } 49 50 func TestWaitDeployed(t *testing.T) { 51 for name, test := range waitDeployedTests { 52 backend := backends.NewSimulatedBackend( 53 core.GenesisAlloc{ 54 crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)}, 55 }, 10000000, 56 ) 57 58 //创建交易记录。 59 tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code)) 60 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) 61 62 //等待它在后台被挖掘。 63 var ( 64 err error 65 address common.Address 66 mined = make(chan struct{}) 67 ctx = context.Background() 68 ) 69 go func() { 70 address, err = bind.WaitDeployed(ctx, backend, tx) 71 close(mined) 72 }() 73 74 //发送并挖掘事务。 75 backend.SendTransaction(ctx, tx) 76 backend.Commit() 77 78 select { 79 case <-mined: 80 if err != test.wantErr { 81 t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr) 82 } 83 if address != test.wantAddress { 84 t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) 85 } 86 case <-time.After(2 * time.Second): 87 t.Errorf("test %q: timeout", name) 88 } 89 } 90 } 91