transaction_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2014 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 types
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "encoding/json"
  21. "math/big"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // The values in those tests are from the Transaction Tests
  28. // at github.com/ethereum/tests.
  29. var (
  30. emptyTx = NewTransaction(
  31. 0,
  32. common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"),
  33. big.NewInt(0), 0, big.NewInt(0),
  34. nil,
  35. )
  36. rightvrsTx, _ = NewTransaction(
  37. 3,
  38. common.HexToAddress("b94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
  39. big.NewInt(10),
  40. 2000,
  41. big.NewInt(1),
  42. common.FromHex("5544"),
  43. ).WithSignature(
  44. HomesteadSigner{},
  45. common.Hex2Bytes("98ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4a8887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a301"),
  46. )
  47. )
  48. func TestTransactionSigHash(t *testing.T) {
  49. var homestead HomesteadSigner
  50. if homestead.Hash(emptyTx) != common.HexToHash("c775b99e7ad12f50d819fcd602390467e28141316969f4b57f0626f74fe3b386") {
  51. t.Errorf("empty transaction hash mismatch, got %x", emptyTx.Hash())
  52. }
  53. if homestead.Hash(rightvrsTx) != common.HexToHash("fe7a79529ed5f7c3375d06b26b186a8644e0e16c373d7a12be41c62d6042b77a") {
  54. t.Errorf("RightVRS transaction hash mismatch, got %x", rightvrsTx.Hash())
  55. }
  56. }
  57. func TestTransactionEncode(t *testing.T) {
  58. txb, err := rlp.EncodeToBytes(rightvrsTx)
  59. if err != nil {
  60. t.Fatalf("encode error: %v", err)
  61. }
  62. should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
  63. if !bytes.Equal(txb, should) {
  64. t.Errorf("encoded RLP mismatch, got %x", txb)
  65. }
  66. }
  67. func decodeTx(data []byte) (*Transaction, error) {
  68. var tx Transaction
  69. t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx)
  70. return t, err
  71. }
  72. func defaultTestKey() (*ecdsa.PrivateKey, common.Address) {
  73. key, _ := crypto.HexToECDSA("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
  74. addr := crypto.PubkeyToAddress(key.PublicKey)
  75. return key, addr
  76. }
  77. func TestRecipientEmpty(t *testing.T) {
  78. _, addr := defaultTestKey()
  79. tx, err := decodeTx(common.Hex2Bytes("f8498080808080011ca09b16de9d5bdee2cf56c28d16275a4da68cd30273e2525f3959f5d62557489921a0372ebd8fb3345f7db7b5a86d42e24d36e983e259b0664ceb8c227ec9af572f3d"))
  80. if err != nil {
  81. t.Error(err)
  82. t.FailNow()
  83. }
  84. from, err := Sender(HomesteadSigner{}, tx)
  85. if err != nil {
  86. t.Error(err)
  87. t.FailNow()
  88. }
  89. if addr != from {
  90. t.Error("derived address doesn't match")
  91. }
  92. }
  93. func TestRecipientNormal(t *testing.T) {
  94. _, addr := defaultTestKey()
  95. tx, err := decodeTx(common.Hex2Bytes("f85d80808094000000000000000000000000000000000000000080011ca0527c0d8f5c63f7b9f41324a7c8a563ee1190bcbf0dac8ab446291bdbf32f5c79a0552c4ef0a09a04395074dab9ed34d3fbfb843c2f2546cc30fe89ec143ca94ca6"))
  96. if err != nil {
  97. t.Error(err)
  98. t.FailNow()
  99. }
  100. from, err := Sender(HomesteadSigner{}, tx)
  101. if err != nil {
  102. t.Error(err)
  103. t.FailNow()
  104. }
  105. if addr != from {
  106. t.Error("derived address doesn't match")
  107. }
  108. }
  109. // Tests that transactions can be correctly sorted according to their price in
  110. // decreasing order, but at the same time with increasing nonces when issued by
  111. // the same account.
  112. func TestTransactionPriceNonceSort(t *testing.T) {
  113. // Generate a batch of accounts to start with
  114. keys := make([]*ecdsa.PrivateKey, 25)
  115. for i := 0; i < len(keys); i++ {
  116. keys[i], _ = crypto.GenerateKey()
  117. }
  118. signer := HomesteadSigner{}
  119. // Generate a batch of transactions with overlapping values, but shifted nonces
  120. groups := map[common.Address]Transactions{}
  121. for start, key := range keys {
  122. addr := crypto.PubkeyToAddress(key.PublicKey)
  123. for i := 0; i < 25; i++ {
  124. tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, big.NewInt(int64(start+i)), nil), signer, key)
  125. groups[addr] = append(groups[addr], tx)
  126. }
  127. }
  128. // Sort the transactions and cross check the nonce ordering
  129. txset := NewTransactionsByPriceAndNonce(signer, groups)
  130. txs := Transactions{}
  131. for tx := txset.Peek(); tx != nil; tx = txset.Peek() {
  132. txs = append(txs, tx)
  133. txset.Shift()
  134. }
  135. if len(txs) != 25*25 {
  136. t.Errorf("expected %d transactions, found %d", 25*25, len(txs))
  137. }
  138. for i, txi := range txs {
  139. fromi, _ := Sender(signer, txi)
  140. // Make sure the nonce order is valid
  141. for j, txj := range txs[i+1:] {
  142. fromj, _ := Sender(signer, txj)
  143. if fromi == fromj && txi.Nonce() > txj.Nonce() {
  144. t.Errorf("invalid nonce ordering: tx #%d (A=%x N=%v) < tx #%d (A=%x N=%v)", i, fromi[:4], txi.Nonce(), i+j, fromj[:4], txj.Nonce())
  145. }
  146. }
  147. // If the next tx has different from account, the price must be lower than the current one
  148. if i+1 < len(txs) {
  149. next := txs[i+1]
  150. fromNext, _ := Sender(signer, next)
  151. if fromi != fromNext && txi.GasPrice().Cmp(next.GasPrice()) < 0 {
  152. t.Errorf("invalid gasprice ordering: tx #%d (A=%x P=%v) < tx #%d (A=%x P=%v)", i, fromi[:4], txi.GasPrice(), i+1, fromNext[:4], next.GasPrice())
  153. }
  154. }
  155. }
  156. }
  157. // TestTransactionJSON tests serializing/de-serializing to/from JSON.
  158. func TestTransactionJSON(t *testing.T) {
  159. key, err := crypto.GenerateKey()
  160. if err != nil {
  161. t.Fatalf("could not generate key: %v", err)
  162. }
  163. signer := NewEIP155Signer(common.Big1)
  164. for i := uint64(0); i < 25; i++ {
  165. var tx *Transaction
  166. switch i % 2 {
  167. case 0:
  168. tx = NewTransaction(i, common.Address{1}, common.Big0, 1, common.Big2, []byte("abcdef"))
  169. case 1:
  170. tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
  171. }
  172. tx, err := SignTx(tx, signer, key)
  173. if err != nil {
  174. t.Fatalf("could not sign transaction: %v", err)
  175. }
  176. data, err := json.Marshal(tx)
  177. if err != nil {
  178. t.Errorf("json.Marshal failed: %v", err)
  179. }
  180. var parsedTx *Transaction
  181. if err := json.Unmarshal(data, &parsedTx); err != nil {
  182. t.Errorf("json.Unmarshal failed: %v", err)
  183. }
  184. // compare nonce, price, gaslimit, recipient, amount, payload, V, R, S
  185. if tx.Hash() != parsedTx.Hash() {
  186. t.Errorf("parsed tx differs from original tx, want %v, got %v", tx, parsedTx)
  187. }
  188. if tx.ChainId().Cmp(parsedTx.ChainId()) != 0 {
  189. t.Errorf("invalid chain id, want %d, got %d", tx.ChainId(), parsedTx.ChainId())
  190. }
  191. }
  192. }