github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/abi/bind/util_test.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_test
    26  
    27  import (
    28  	"context"
    29  	"math/big"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    34  	"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/core"
    37  	"github.com/ethereum/go-ethereum/core/types"
    38  	"github.com/ethereum/go-ethereum/crypto"
    39  )
    40  
    41  var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    42  
    43  var waitDeployedTests = map[string]struct {
    44  	code        string
    45  	gas         uint64
    46  	wantAddress common.Address
    47  	wantErr     error
    48  }{
    49  	"successful deploy": {
    50  		code:        `6060604052600a8060106000396000f360606040526008565b00`,
    51  		gas:         3000000,
    52  		wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
    53  	},
    54  	"empty code": {
    55  		code:        ``,
    56  		gas:         300000,
    57  		wantErr:     bind.ErrNoCodeAfterDeploy,
    58  		wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
    59  	},
    60  }
    61  
    62  func TestWaitDeployed(t *testing.T) {
    63  	for name, test := range waitDeployedTests {
    64  		backend := backends.NewSimulatedBackend(
    65  			core.GenesisAlloc{
    66  				crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
    67  			}, 10000000,
    68  		)
    69  
    70  //创建交易记录。
    71  		tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
    72  		tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
    73  
    74  //等待它在后台被挖掘。
    75  		var (
    76  			err     error
    77  			address common.Address
    78  			mined   = make(chan struct{})
    79  			ctx     = context.Background()
    80  		)
    81  		go func() {
    82  			address, err = bind.WaitDeployed(ctx, backend, tx)
    83  			close(mined)
    84  		}()
    85  
    86  //发送并挖掘事务。
    87  		backend.SendTransaction(ctx, tx)
    88  		backend.Commit()
    89  
    90  		select {
    91  		case <-mined:
    92  			if err != test.wantErr {
    93  				t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr)
    94  			}
    95  			if address != test.wantAddress {
    96  				t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
    97  			}
    98  		case <-time.After(2 * time.Second):
    99  			t.Errorf("test %q: timeout", name)
   100  		}
   101  	}
   102  }