github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/accounts/abi/bind/util_test.go (about)

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