|
| 1 | +// Copyright 2020 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 ethtest |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/ethereum/go-ethereum/common" |
| 23 | + "github.com/ethereum/go-ethereum/core/types" |
| 24 | + "github.com/ethereum/go-ethereum/crypto" |
| 25 | + "github.com/ethereum/go-ethereum/internal/utesting" |
| 26 | +) |
| 27 | + |
| 28 | +//var faucetAddr = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7") |
| 29 | +var faucetKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 30 | + |
| 31 | +func sendSuccessfulTx(t *utesting.T, s *Suite, tx *types.Transaction) { |
| 32 | + sendConn := s.setupConnection(t) |
| 33 | + t.Logf("sending tx: %v %v %v\n", tx.Hash().String(), tx.GasPrice(), tx.Gas()) |
| 34 | + // Send the transaction |
| 35 | + if err := sendConn.Write(Transactions([]*types.Transaction{tx})); err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + time.Sleep(100 * time.Millisecond) |
| 39 | + recvConn := s.setupConnection(t) |
| 40 | + // Wait for the transaction announcement |
| 41 | + switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) { |
| 42 | + case *Transactions: |
| 43 | + recTxs := *msg |
| 44 | + if len(recTxs) < 1 { |
| 45 | + t.Fatalf("received transactions do not match send: %v", recTxs) |
| 46 | + } |
| 47 | + if tx.Hash() != recTxs[len(recTxs)-1].Hash() { |
| 48 | + t.Fatalf("received transactions do not match send: got %v want %v", recTxs, tx) |
| 49 | + } |
| 50 | + case *NewPooledTransactionHashes: |
| 51 | + txHashes := *msg |
| 52 | + if len(txHashes) < 1 { |
| 53 | + t.Fatalf("received transactions do not match send: %v", txHashes) |
| 54 | + } |
| 55 | + if tx.Hash() != txHashes[len(txHashes)-1] { |
| 56 | + t.Fatalf("wrong announcement received, wanted %v got %v", tx, txHashes) |
| 57 | + } |
| 58 | + default: |
| 59 | + t.Fatalf("unexpected message in sendSuccessfulTx: %s", pretty.Sdump(msg)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func sendFailingTx(t *utesting.T, s *Suite, tx *types.Transaction) { |
| 64 | + sendConn, recvConn := s.setupConnection(t), s.setupConnection(t) |
| 65 | + // Wait for a transaction announcement |
| 66 | + switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) { |
| 67 | + case *NewPooledTransactionHashes: |
| 68 | + break |
| 69 | + default: |
| 70 | + t.Logf("unexpected message, logging: %v", pretty.Sdump(msg)) |
| 71 | + } |
| 72 | + // Send the transaction |
| 73 | + if err := sendConn.Write(Transactions([]*types.Transaction{tx})); err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + // Wait for another transaction announcement |
| 77 | + switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) { |
| 78 | + case *Transactions: |
| 79 | + t.Fatalf("Received unexpected transaction announcement: %v", msg) |
| 80 | + case *NewPooledTransactionHashes: |
| 81 | + t.Fatalf("Received unexpected pooledTx announcement: %v", msg) |
| 82 | + case *Error: |
| 83 | + // Transaction should not be announced -> wait for timeout |
| 84 | + return |
| 85 | + default: |
| 86 | + t.Fatalf("unexpected message in sendFailingTx: %s", pretty.Sdump(msg)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func unknownTx(t *utesting.T, s *Suite) *types.Transaction { |
| 91 | + tx := getNextTxFromChain(t, s) |
| 92 | + var to common.Address |
| 93 | + if tx.To() != nil { |
| 94 | + to = *tx.To() |
| 95 | + } |
| 96 | + txNew := types.NewTransaction(tx.Nonce()+1, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data()) |
| 97 | + return signWithFaucet(t, txNew) |
| 98 | +} |
| 99 | + |
| 100 | +func getNextTxFromChain(t *utesting.T, s *Suite) *types.Transaction { |
| 101 | + // Get a new transaction |
| 102 | + var tx *types.Transaction |
| 103 | + for _, blocks := range s.fullChain.blocks[s.chain.Len():] { |
| 104 | + txs := blocks.Transactions() |
| 105 | + if txs.Len() != 0 { |
| 106 | + tx = txs[0] |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + if tx == nil { |
| 111 | + t.Fatal("could not find transaction") |
| 112 | + } |
| 113 | + return tx |
| 114 | +} |
| 115 | + |
| 116 | +func getOldTxFromChain(t *utesting.T, s *Suite) *types.Transaction { |
| 117 | + var tx *types.Transaction |
| 118 | + for _, blocks := range s.fullChain.blocks[:s.chain.Len()-1] { |
| 119 | + txs := blocks.Transactions() |
| 120 | + if txs.Len() != 0 { |
| 121 | + tx = txs[0] |
| 122 | + break |
| 123 | + } |
| 124 | + } |
| 125 | + if tx == nil { |
| 126 | + t.Fatal("could not find transaction") |
| 127 | + } |
| 128 | + return tx |
| 129 | +} |
| 130 | + |
| 131 | +func invalidNonceTx(t *utesting.T, s *Suite) *types.Transaction { |
| 132 | + tx := getNextTxFromChain(t, s) |
| 133 | + var to common.Address |
| 134 | + if tx.To() != nil { |
| 135 | + to = *tx.To() |
| 136 | + } |
| 137 | + txNew := types.NewTransaction(tx.Nonce()-2, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data()) |
| 138 | + return signWithFaucet(t, txNew) |
| 139 | +} |
| 140 | + |
| 141 | +func hugeAmount(t *utesting.T, s *Suite) *types.Transaction { |
| 142 | + tx := getNextTxFromChain(t, s) |
| 143 | + amount := largeNumber(2) |
| 144 | + var to common.Address |
| 145 | + if tx.To() != nil { |
| 146 | + to = *tx.To() |
| 147 | + } |
| 148 | + txNew := types.NewTransaction(tx.Nonce(), to, amount, tx.Gas(), tx.GasPrice(), tx.Data()) |
| 149 | + return signWithFaucet(t, txNew) |
| 150 | +} |
| 151 | + |
| 152 | +func hugeGasPrice(t *utesting.T, s *Suite) *types.Transaction { |
| 153 | + tx := getNextTxFromChain(t, s) |
| 154 | + gasPrice := largeNumber(2) |
| 155 | + var to common.Address |
| 156 | + if tx.To() != nil { |
| 157 | + to = *tx.To() |
| 158 | + } |
| 159 | + txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), gasPrice, tx.Data()) |
| 160 | + return signWithFaucet(t, txNew) |
| 161 | +} |
| 162 | + |
| 163 | +func hugeData(t *utesting.T, s *Suite) *types.Transaction { |
| 164 | + tx := getNextTxFromChain(t, s) |
| 165 | + var to common.Address |
| 166 | + if tx.To() != nil { |
| 167 | + to = *tx.To() |
| 168 | + } |
| 169 | + txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), tx.GasPrice(), largeBuffer(2)) |
| 170 | + return signWithFaucet(t, txNew) |
| 171 | +} |
| 172 | + |
| 173 | +func signWithFaucet(t *utesting.T, tx *types.Transaction) *types.Transaction { |
| 174 | + signer := types.HomesteadSigner{} |
| 175 | + signedTx, err := types.SignTx(tx, signer, faucetKey) |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("could not sign tx: %v\n", err) |
| 178 | + } |
| 179 | + return signedTx |
| 180 | +} |
0 commit comments