github.com/p202io/bor@v0.1.4/accounts/abi/bind/backends/simulated_test.go (about)

     1  // Copyright 2019 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 backends_test
    18  
    19  import (
    20  	"context"
    21  	"math/big"
    22  	"testing"
    23  
    24  	ethereum "github.com/maticnetwork/bor"
    25  	"github.com/maticnetwork/bor/accounts/abi/bind"
    26  	"github.com/maticnetwork/bor/accounts/abi/bind/backends"
    27  	"github.com/maticnetwork/bor/common"
    28  	"github.com/maticnetwork/bor/core"
    29  	"github.com/maticnetwork/bor/core/types"
    30  	"github.com/maticnetwork/bor/crypto"
    31  )
    32  
    33  func TestSimulatedBackend(t *testing.T) {
    34  	var gasLimit uint64 = 8000029
    35  	key, _ := crypto.GenerateKey() // nolint: gosec
    36  	auth := bind.NewKeyedTransactor(key)
    37  	genAlloc := make(core.GenesisAlloc)
    38  	genAlloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(9223372036854775807)}
    39  
    40  	sim := backends.NewSimulatedBackend(genAlloc, gasLimit)
    41  
    42  	// should return an error if the tx is not found
    43  	txHash := common.HexToHash("2")
    44  	_, isPending, err := sim.TransactionByHash(context.Background(), txHash)
    45  
    46  	if isPending {
    47  		t.Fatal("transaction should not be pending")
    48  	}
    49  	if err != ethereum.NotFound {
    50  		t.Fatalf("err should be `ethereum.NotFound` but received %v", err)
    51  	}
    52  
    53  	// generate a transaction and confirm you can retrieve it
    54  	code := `6060604052600a8060106000396000f360606040526008565b00`
    55  	var gas uint64 = 3000000
    56  	tx := types.NewContractCreation(0, big.NewInt(0), gas, big.NewInt(1), common.FromHex(code))
    57  	tx, _ = types.SignTx(tx, types.HomesteadSigner{}, key)
    58  
    59  	err = sim.SendTransaction(context.Background(), tx)
    60  	if err != nil {
    61  		t.Fatal("error sending transaction")
    62  	}
    63  
    64  	txHash = tx.Hash()
    65  	_, isPending, err = sim.TransactionByHash(context.Background(), txHash)
    66  	if err != nil {
    67  		t.Fatalf("error getting transaction with hash: %v", txHash.String())
    68  	}
    69  	if !isPending {
    70  		t.Fatal("transaction should have pending status")
    71  	}
    72  
    73  	sim.Commit()
    74  	tx, isPending, err = sim.TransactionByHash(context.Background(), txHash)
    75  	if err != nil {
    76  		t.Fatalf("error getting transaction with hash: %v", txHash.String())
    77  	}
    78  	if isPending {
    79  		t.Fatal("transaction should not have pending status")
    80  	}
    81  
    82  }