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