github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/accounts/abi/bind/util_test.go (about)

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