transaction.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. package ethtest
  17. import (
  18. "math/big"
  19. "strings"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/internal/utesting"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. //var faucetAddr = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7")
  28. var faucetKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  29. func sendSuccessfulTx(t *utesting.T, s *Suite, tx *types.Transaction) {
  30. sendConn := s.setupConnection(t)
  31. defer sendConn.Close()
  32. sendSuccessfulTxWithConn(t, s, tx, sendConn)
  33. }
  34. func sendSuccessfulTxWithConn(t *utesting.T, s *Suite, tx *types.Transaction, sendConn *Conn) {
  35. t.Logf("sending tx: %v %v %v\n", tx.Hash().String(), tx.GasPrice(), tx.Gas())
  36. // Send the transaction
  37. if err := sendConn.Write(&Transactions{tx}); err != nil {
  38. t.Fatal(err)
  39. }
  40. // update last nonce seen
  41. nonce = tx.Nonce()
  42. recvConn := s.setupConnection(t)
  43. // Wait for the transaction announcement
  44. switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
  45. case *Transactions:
  46. recTxs := *msg
  47. for _, gotTx := range recTxs {
  48. if gotTx.Hash() == tx.Hash() {
  49. // Ok
  50. return
  51. }
  52. }
  53. t.Fatalf("missing transaction: got %v missing %v", recTxs, tx.Hash())
  54. case *NewPooledTransactionHashes:
  55. txHashes := *msg
  56. for _, gotHash := range txHashes {
  57. if gotHash == tx.Hash() {
  58. return
  59. }
  60. }
  61. t.Fatalf("missing transaction announcement: got %v missing %v", txHashes, tx.Hash())
  62. default:
  63. t.Fatalf("unexpected message in sendSuccessfulTx: %s", pretty.Sdump(msg))
  64. }
  65. }
  66. var nonce = uint64(99)
  67. func sendMultipleSuccessfulTxs(t *utesting.T, s *Suite, sendConn *Conn, txs []*types.Transaction) {
  68. txMsg := Transactions(txs)
  69. t.Logf("sending %d txs\n", len(txs))
  70. recvConn := s.setupConnection(t)
  71. defer recvConn.Close()
  72. // Send the transactions
  73. if err := sendConn.Write(&txMsg); err != nil {
  74. t.Fatal(err)
  75. }
  76. // update nonce
  77. nonce = txs[len(txs)-1].Nonce()
  78. // Wait for the transaction announcement(s) and make sure all sent txs are being propagated
  79. recvHashes := make([]common.Hash, 0)
  80. // all txs should be announced within 3 announcements
  81. for i := 0; i < 3; i++ {
  82. switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
  83. case *Transactions:
  84. for _, tx := range *msg {
  85. recvHashes = append(recvHashes, tx.Hash())
  86. }
  87. case *NewPooledTransactionHashes:
  88. recvHashes = append(recvHashes, *msg...)
  89. default:
  90. if !strings.Contains(pretty.Sdump(msg), "i/o timeout") {
  91. t.Fatalf("unexpected message while waiting to receive txs: %s", pretty.Sdump(msg))
  92. }
  93. }
  94. // break once all 2000 txs have been received
  95. if len(recvHashes) == 2000 {
  96. break
  97. }
  98. if len(recvHashes) > 0 {
  99. _, missingTxs := compareReceivedTxs(recvHashes, txs)
  100. if len(missingTxs) > 0 {
  101. continue
  102. } else {
  103. t.Logf("successfully received all %d txs", len(txs))
  104. return
  105. }
  106. }
  107. }
  108. _, missingTxs := compareReceivedTxs(recvHashes, txs)
  109. if len(missingTxs) > 0 {
  110. for _, missing := range missingTxs {
  111. t.Logf("missing tx: %v", missing.Hash())
  112. }
  113. t.Fatalf("missing %d txs", len(missingTxs))
  114. }
  115. }
  116. func waitForTxPropagation(t *utesting.T, s *Suite, txs []*types.Transaction, recvConn *Conn) {
  117. // Wait for another transaction announcement
  118. switch msg := recvConn.ReadAndServe(s.chain, time.Second*8).(type) {
  119. case *Transactions:
  120. // check to see if any of the failing txs were in the announcement
  121. recvTxs := make([]common.Hash, len(*msg))
  122. for i, recvTx := range *msg {
  123. recvTxs[i] = recvTx.Hash()
  124. }
  125. badTxs, _ := compareReceivedTxs(recvTxs, txs)
  126. if len(badTxs) > 0 {
  127. for _, tx := range badTxs {
  128. t.Logf("received bad tx: %v", tx)
  129. }
  130. t.Fatalf("received %d bad txs", len(badTxs))
  131. }
  132. case *NewPooledTransactionHashes:
  133. badTxs, _ := compareReceivedTxs(*msg, txs)
  134. if len(badTxs) > 0 {
  135. for _, tx := range badTxs {
  136. t.Logf("received bad tx: %v", tx)
  137. }
  138. t.Fatalf("received %d bad txs", len(badTxs))
  139. }
  140. case *Error:
  141. // Transaction should not be announced -> wait for timeout
  142. return
  143. default:
  144. t.Fatalf("unexpected message in sendFailingTx: %s", pretty.Sdump(msg))
  145. }
  146. }
  147. // compareReceivedTxs compares the received set of txs against the given set of txs,
  148. // returning both the set received txs that were present within the given txs, and
  149. // the set of txs that were missing from the set of received txs
  150. func compareReceivedTxs(recvTxs []common.Hash, txs []*types.Transaction) (present []*types.Transaction, missing []*types.Transaction) {
  151. // create a map of the hashes received from node
  152. recvHashes := make(map[common.Hash]common.Hash)
  153. for _, hash := range recvTxs {
  154. recvHashes[hash] = hash
  155. }
  156. // collect present txs and missing txs separately
  157. present = make([]*types.Transaction, 0)
  158. missing = make([]*types.Transaction, 0)
  159. for _, tx := range txs {
  160. if _, exists := recvHashes[tx.Hash()]; exists {
  161. present = append(present, tx)
  162. } else {
  163. missing = append(missing, tx)
  164. }
  165. }
  166. return present, missing
  167. }
  168. func unknownTx(t *utesting.T, s *Suite) *types.Transaction {
  169. tx := getNextTxFromChain(t, s)
  170. var to common.Address
  171. if tx.To() != nil {
  172. to = *tx.To()
  173. }
  174. txNew := types.NewTransaction(tx.Nonce()+1, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
  175. return signWithFaucet(t, s.chain.chainConfig, txNew)
  176. }
  177. func getNextTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
  178. // Get a new transaction
  179. var tx *types.Transaction
  180. for _, blocks := range s.fullChain.blocks[s.chain.Len():] {
  181. txs := blocks.Transactions()
  182. if txs.Len() != 0 {
  183. tx = txs[0]
  184. break
  185. }
  186. }
  187. if tx == nil {
  188. t.Fatal("could not find transaction")
  189. }
  190. return tx
  191. }
  192. func generateTxs(t *utesting.T, s *Suite, numTxs int) (map[common.Hash]common.Hash, []*types.Transaction) {
  193. txHashMap := make(map[common.Hash]common.Hash, numTxs)
  194. txs := make([]*types.Transaction, numTxs)
  195. nextTx := getNextTxFromChain(t, s)
  196. gas := nextTx.Gas()
  197. nonce = nonce + 1
  198. // generate txs
  199. for i := 0; i < numTxs; i++ {
  200. tx := generateTx(t, s.chain.chainConfig, nonce, gas)
  201. txHashMap[tx.Hash()] = tx.Hash()
  202. txs[i] = tx
  203. nonce = nonce + 1
  204. }
  205. return txHashMap, txs
  206. }
  207. func generateTx(t *utesting.T, chainConfig *params.ChainConfig, nonce uint64, gas uint64) *types.Transaction {
  208. var to common.Address
  209. tx := types.NewTransaction(nonce, to, big.NewInt(1), gas, big.NewInt(1), []byte{})
  210. return signWithFaucet(t, chainConfig, tx)
  211. }
  212. func getOldTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
  213. var tx *types.Transaction
  214. for _, blocks := range s.fullChain.blocks[:s.chain.Len()-1] {
  215. txs := blocks.Transactions()
  216. if txs.Len() != 0 {
  217. tx = txs[0]
  218. break
  219. }
  220. }
  221. if tx == nil {
  222. t.Fatal("could not find transaction")
  223. }
  224. return tx
  225. }
  226. func invalidNonceTx(t *utesting.T, s *Suite) *types.Transaction {
  227. tx := getNextTxFromChain(t, s)
  228. var to common.Address
  229. if tx.To() != nil {
  230. to = *tx.To()
  231. }
  232. txNew := types.NewTransaction(tx.Nonce()-2, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
  233. return signWithFaucet(t, s.chain.chainConfig, txNew)
  234. }
  235. func hugeAmount(t *utesting.T, s *Suite) *types.Transaction {
  236. tx := getNextTxFromChain(t, s)
  237. amount := largeNumber(2)
  238. var to common.Address
  239. if tx.To() != nil {
  240. to = *tx.To()
  241. }
  242. txNew := types.NewTransaction(tx.Nonce(), to, amount, tx.Gas(), tx.GasPrice(), tx.Data())
  243. return signWithFaucet(t, s.chain.chainConfig, txNew)
  244. }
  245. func hugeGasPrice(t *utesting.T, s *Suite) *types.Transaction {
  246. tx := getNextTxFromChain(t, s)
  247. gasPrice := largeNumber(2)
  248. var to common.Address
  249. if tx.To() != nil {
  250. to = *tx.To()
  251. }
  252. txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), gasPrice, tx.Data())
  253. return signWithFaucet(t, s.chain.chainConfig, txNew)
  254. }
  255. func hugeData(t *utesting.T, s *Suite) *types.Transaction {
  256. tx := getNextTxFromChain(t, s)
  257. var to common.Address
  258. if tx.To() != nil {
  259. to = *tx.To()
  260. }
  261. txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), tx.GasPrice(), largeBuffer(2))
  262. return signWithFaucet(t, s.chain.chainConfig, txNew)
  263. }
  264. func signWithFaucet(t *utesting.T, chainConfig *params.ChainConfig, tx *types.Transaction) *types.Transaction {
  265. signer := types.LatestSigner(chainConfig)
  266. signedTx, err := types.SignTx(tx, signer, faucetKey)
  267. if err != nil {
  268. t.Fatalf("could not sign tx: %v\n", err)
  269. }
  270. return signedTx
  271. }