github.com/sbrajchuk/go-ethereum@v1.9.7/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/ethereum/go-ethereum" 25 "github.com/ethereum/go-ethereum/accounts/abi/bind" 26 "github.com/ethereum/go-ethereum/accounts/abi/bind/backends" 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core" 29 "github.com/ethereum/go-ethereum/core/types" 30 "github.com/ethereum/go-ethereum/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 defer sim.Close() 42 43 // should return an error if the tx is not found 44 txHash := common.HexToHash("2") 45 _, isPending, err := sim.TransactionByHash(context.Background(), txHash) 46 47 if isPending { 48 t.Fatal("transaction should not be pending") 49 } 50 if err != ethereum.NotFound { 51 t.Fatalf("err should be `ethereum.NotFound` but received %v", err) 52 } 53 54 // generate a transaction and confirm you can retrieve it 55 code := `6060604052600a8060106000396000f360606040526008565b00` 56 var gas uint64 = 3000000 57 tx := types.NewContractCreation(0, big.NewInt(0), gas, big.NewInt(1), common.FromHex(code)) 58 tx, _ = types.SignTx(tx, types.HomesteadSigner{}, key) 59 60 err = sim.SendTransaction(context.Background(), tx) 61 if err != nil { 62 t.Fatal("error sending transaction") 63 } 64 65 txHash = tx.Hash() 66 _, isPending, err = sim.TransactionByHash(context.Background(), txHash) 67 if err != nil { 68 t.Fatalf("error getting transaction with hash: %v", txHash.String()) 69 } 70 if !isPending { 71 t.Fatal("transaction should have pending status") 72 } 73 74 sim.Commit() 75 tx, isPending, err = sim.TransactionByHash(context.Background(), txHash) 76 if err != nil { 77 t.Fatalf("error getting transaction with hash: %v", txHash.String()) 78 } 79 if isPending { 80 t.Fatal("transaction should not have pending status") 81 } 82 83 }