transaction.go 5.7 KB

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