github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/abi/bind/util.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2016 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package bind 26 27 import ( 28 "context" 29 "fmt" 30 "time" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/log" 35 ) 36 37 //WaitMined等待在区块链上挖掘Tx。 38 //当上下文被取消时,它将停止等待。 39 func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { 40 queryTicker := time.NewTicker(time.Second) 41 defer queryTicker.Stop() 42 43 logger := log.New("hash", tx.Hash()) 44 for { 45 receipt, err := b.TransactionReceipt(ctx, tx.Hash()) 46 if receipt != nil { 47 return receipt, nil 48 } 49 if err != nil { 50 logger.Trace("Receipt retrieval failed", "err", err) 51 } else { 52 logger.Trace("Transaction not yet mined") 53 } 54 //等待下一轮。 55 select { 56 case <-ctx.Done(): 57 return nil, ctx.Err() 58 case <-queryTicker.C: 59 } 60 } 61 } 62 63 //waitdeployed等待合同部署事务并返回on-chain 64 //开采合同地址。当取消CTX时,它停止等待。 65 func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { 66 if tx.To() != nil { 67 return common.Address{}, fmt.Errorf("tx is not contract creation") 68 } 69 receipt, err := WaitMined(ctx, b, tx) 70 if err != nil { 71 return common.Address{}, err 72 } 73 if receipt.ContractAddress == (common.Address{}) { 74 return common.Address{}, fmt.Errorf("zero address") 75 } 76 //检查代码是否确实部署在该地址。 77 //这与宅基地前链有关:建筑商中的OOG 78 //可能会留下一个空帐户。 79 code, err := b.CodeAt(ctx, receipt.ContractAddress, nil) 80 if err == nil && len(code) == 0 { 81 err = ErrNoCodeAfterDeploy 82 } 83 return receipt.ContractAddress, err 84 }