github.com/klaytn/klaytn@v1.12.1/accounts/abi/bind/util_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from accounts/abi/bind/util_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package bind_test
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"math/big"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/klaytn/klaytn/accounts/abi/bind"
    31  	"github.com/klaytn/klaytn/accounts/abi/bind/backends"
    32  	"github.com/klaytn/klaytn/blockchain"
    33  	"github.com/klaytn/klaytn/blockchain/types"
    34  	"github.com/klaytn/klaytn/common"
    35  	"github.com/klaytn/klaytn/crypto"
    36  	"github.com/klaytn/klaytn/params"
    37  )
    38  
    39  var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    40  
    41  var waitDeployedTests = map[string]struct {
    42  	code        string
    43  	gas         uint64
    44  	wantAddress common.Address
    45  	wantErr     error
    46  }{
    47  	"successful deploy": {
    48  		code:        `6060604052600a8060106000396000f360606040526008565b00`,
    49  		gas:         3000000,
    50  		wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
    51  	},
    52  	"empty code": {
    53  		code:        ``,
    54  		gas:         300000,
    55  		wantErr:     bind.ErrNoCodeAfterDeploy,
    56  		wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
    57  	},
    58  }
    59  
    60  func TestWaitDeployed(t *testing.T) {
    61  	for name, test := range waitDeployedTests {
    62  		backend := backends.NewSimulatedBackend(
    63  			blockchain.GenesisAlloc{
    64  				crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
    65  			},
    66  		)
    67  
    68  		// Create the transaction.
    69  		tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
    70  		tx, _ = types.SignTx(tx, types.LatestSignerForChainID(params.AllGxhashProtocolChanges.ChainID), testKey)
    71  
    72  		// Wait for it to get mined in the background.
    73  		var (
    74  			err     error
    75  			address common.Address
    76  			mined   = make(chan struct{})
    77  			ctx     = context.Background()
    78  		)
    79  		go func() {
    80  			address, err = bind.WaitDeployed(ctx, backend, tx)
    81  			close(mined)
    82  		}()
    83  
    84  		// Send and mine the transaction.
    85  		backend.SendTransaction(ctx, tx)
    86  		backend.Commit()
    87  
    88  		select {
    89  		case <-mined:
    90  			if err != test.wantErr {
    91  				t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err)
    92  			}
    93  			if address != test.wantAddress {
    94  				t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
    95  			}
    96  		case <-time.After(2 * time.Second):
    97  			t.Errorf("test %q: timeout", name)
    98  		}
    99  		backend.Close()
   100  	}
   101  }
   102  
   103  func TestWaitDeployedCornerCases(t *testing.T) {
   104  	backend := backends.NewSimulatedBackend(
   105  		blockchain.GenesisAlloc{
   106  			crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000)},
   107  		},
   108  	)
   109  	defer backend.Close()
   110  
   111  	// Create a transaction to an account.
   112  	code := "6060604052600a8060106000396000f360606040526008565b00"
   113  	tx := types.NewTransaction(0, common.HexToAddress("0x0"), big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code))
   114  	tx, _ = types.SignTx(tx, types.LatestSignerForChainID(params.AllGxhashProtocolChanges.ChainID), testKey)
   115  	ctx, cancel := context.WithCancel(context.Background())
   116  	defer cancel()
   117  	backend.SendTransaction(ctx, tx)
   118  	backend.Commit()
   119  	notContentCreation := errors.New("tx is not contract creation")
   120  	if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() {
   121  		t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err)
   122  	}
   123  
   124  	// Create a transaction that is not mined.
   125  	tx = types.NewContractCreation(1, big.NewInt(0), 3000000, big.NewInt(1), common.FromHex(code))
   126  	tx, _ = types.SignTx(tx, types.LatestSignerForChainID(params.AllGxhashProtocolChanges.ChainID), testKey)
   127  
   128  	go func() {
   129  		contextCanceled := errors.New("context canceled")
   130  		if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() {
   131  			t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err)
   132  		}
   133  	}()
   134  
   135  	backend.SendTransaction(ctx, tx)
   136  	cancel()
   137  }