github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/bind/util_test.go (about)

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