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