github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/accounts/abi/bind/util_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-dubxcoin library.
     3  //
     4  // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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/alexdevranger/node-1.8.27/accounts/abi/bind"
    26  	"github.com/alexdevranger/node-1.8.27/accounts/abi/bind/backends"
    27  	"github.com/alexdevranger/node-1.8.27/common"
    28  	"github.com/alexdevranger/node-1.8.27/core"
    29  	"github.com/alexdevranger/node-1.8.27/core/types"
    30  	"github.com/alexdevranger/node-1.8.27/crypto"
    31  )
    32  
    33  var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    34  
    35  var waitDeployedTests = map[string]struct {
    36  	code        string
    37  	gas         uint64
    38  	wantAddress common.Address
    39  	wantErr     error
    40  }{
    41  	"successful deploy": {
    42  		code:        `6060604052600a8060106000396000f360606040526008565b00`,
    43  		gas:         3000000,
    44  		wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"),
    45  	},
    46  	"empty code": {
    47  		code:        ``,
    48  		gas:         300000,
    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  			}, 10000000,
    60  		)
    61  
    62  		// Create the transaction.
    63  		tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
    64  		tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
    65  
    66  		// Wait for it to get mined in the background.
    67  		var (
    68  			err     error
    69  			address common.Address
    70  			mined   = make(chan struct{})
    71  			ctx     = context.Background()
    72  		)
    73  		go func() {
    74  			address, err = bind.WaitDeployed(ctx, backend, tx)
    75  			close(mined)
    76  		}()
    77  
    78  		// Send and mine the transaction.
    79  		backend.SendTransaction(ctx, tx)
    80  		backend.Commit()
    81  
    82  		select {
    83  		case <-mined:
    84  			if err != test.wantErr {
    85  				t.Errorf("test %q: error mismatch: got %q, want %q", name, err, test.wantErr)
    86  			}
    87  			if address != test.wantAddress {
    88  				t.Errorf("test %q: unexpected contract address %s", name, address.Hex())
    89  			}
    90  		case <-time.After(2 * time.Second):
    91  			t.Errorf("test %q: timeout", name)
    92  		}
    93  	}
    94  }