github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/bind/util.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 //</624450061593481216> 11 12 13 package bind 14 15 import ( 16 "context" 17 "fmt" 18 "time" 19 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/core/types" 22 "github.com/ethereum/go-ethereum/log" 23 ) 24 25 //WaitMined等待在区块链上挖掘Tx。 26 //当上下文被取消时,它将停止等待。 27 func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { 28 queryTicker := time.NewTicker(time.Second) 29 defer queryTicker.Stop() 30 31 logger := log.New("hash", tx.Hash()) 32 for { 33 receipt, err := b.TransactionReceipt(ctx, tx.Hash()) 34 if receipt != nil { 35 return receipt, nil 36 } 37 if err != nil { 38 logger.Trace("Receipt retrieval failed", "err", err) 39 } else { 40 logger.Trace("Transaction not yet mined") 41 } 42 //等待下一轮。 43 select { 44 case <-ctx.Done(): 45 return nil, ctx.Err() 46 case <-queryTicker.C: 47 } 48 } 49 } 50 51 //waitdeployed等待合同部署事务并返回on-chain 52 //开采合同地址。当取消CTX时,它停止等待。 53 func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { 54 if tx.To() != nil { 55 return common.Address{}, fmt.Errorf("tx is not contract creation") 56 } 57 receipt, err := WaitMined(ctx, b, tx) 58 if err != nil { 59 return common.Address{}, err 60 } 61 if receipt.ContractAddress == (common.Address{}) { 62 return common.Address{}, fmt.Errorf("zero address") 63 } 64 //检查代码是否确实部署在该地址。 65 //这与宅基地前链有关:建筑商中的OOG 66 //可能会留下一个空帐户。 67 code, err := b.CodeAt(ctx, receipt.ContractAddress, nil) 68 if err == nil && len(code) == 0 { 69 err = ErrNoCodeAfterDeploy 70 } 71 return receipt.ContractAddress, err 72 } 73