protocol_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 eth
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Tests that the custom union field encoder and decoder works correctly.
  26. func TestGetBlockHeadersDataEncodeDecode(t *testing.T) {
  27. // Create a "random" hash for testing
  28. var hash common.Hash
  29. for i := range hash {
  30. hash[i] = byte(i)
  31. }
  32. // Assemble some table driven tests
  33. tests := []struct {
  34. packet *GetBlockHeadersPacket
  35. fail bool
  36. }{
  37. // Providing the origin as either a hash or a number should both work
  38. {fail: false, packet: &GetBlockHeadersPacket{Origin: HashOrNumber{Number: 314}}},
  39. {fail: false, packet: &GetBlockHeadersPacket{Origin: HashOrNumber{Hash: hash}}},
  40. // Providing arbitrary query field should also work
  41. {fail: false, packet: &GetBlockHeadersPacket{Origin: HashOrNumber{Number: 314}, Amount: 314, Skip: 1, Reverse: true}},
  42. {fail: false, packet: &GetBlockHeadersPacket{Origin: HashOrNumber{Hash: hash}, Amount: 314, Skip: 1, Reverse: true}},
  43. // Providing both the origin hash and origin number must fail
  44. {fail: true, packet: &GetBlockHeadersPacket{Origin: HashOrNumber{Hash: hash, Number: 314}}},
  45. }
  46. // Iterate over each of the tests and try to encode and then decode
  47. for i, tt := range tests {
  48. bytes, err := rlp.EncodeToBytes(tt.packet)
  49. if err != nil && !tt.fail {
  50. t.Fatalf("test %d: failed to encode packet: %v", i, err)
  51. } else if err == nil && tt.fail {
  52. t.Fatalf("test %d: encode should have failed", i)
  53. }
  54. if !tt.fail {
  55. packet := new(GetBlockHeadersPacket)
  56. if err := rlp.DecodeBytes(bytes, packet); err != nil {
  57. t.Fatalf("test %d: failed to decode packet: %v", i, err)
  58. }
  59. if packet.Origin.Hash != tt.packet.Origin.Hash || packet.Origin.Number != tt.packet.Origin.Number || packet.Amount != tt.packet.Amount ||
  60. packet.Skip != tt.packet.Skip || packet.Reverse != tt.packet.Reverse {
  61. t.Fatalf("test %d: encode decode mismatch: have %+v, want %+v", i, packet, tt.packet)
  62. }
  63. }
  64. }
  65. }
  66. // TestEth66EmptyMessages tests encoding of empty eth66 messages
  67. func TestEth66EmptyMessages(t *testing.T) {
  68. // All empty messages encodes to the same format
  69. want := common.FromHex("c4820457c0")
  70. for i, msg := range []interface{}{
  71. // Headers
  72. GetBlockHeadersPacket66{1111, nil},
  73. BlockHeadersPacket66{1111, nil},
  74. // Bodies
  75. GetBlockBodiesPacket66{1111, nil},
  76. BlockBodiesPacket66{1111, nil},
  77. BlockBodiesRLPPacket66{1111, nil},
  78. // Node data
  79. GetNodeDataPacket66{1111, nil},
  80. NodeDataPacket66{1111, nil},
  81. // Receipts
  82. GetReceiptsPacket66{1111, nil},
  83. ReceiptsPacket66{1111, nil},
  84. // Transactions
  85. GetPooledTransactionsPacket66{1111, nil},
  86. PooledTransactionsPacket66{1111, nil},
  87. PooledTransactionsRLPPacket66{1111, nil},
  88. // Headers
  89. BlockHeadersPacket66{1111, BlockHeadersPacket([]*types.Header{})},
  90. // Bodies
  91. GetBlockBodiesPacket66{1111, GetBlockBodiesPacket([]common.Hash{})},
  92. BlockBodiesPacket66{1111, BlockBodiesPacket([]*BlockBody{})},
  93. BlockBodiesRLPPacket66{1111, BlockBodiesRLPPacket([]rlp.RawValue{})},
  94. // Node data
  95. GetNodeDataPacket66{1111, GetNodeDataPacket([]common.Hash{})},
  96. NodeDataPacket66{1111, NodeDataPacket([][]byte{})},
  97. // Receipts
  98. GetReceiptsPacket66{1111, GetReceiptsPacket([]common.Hash{})},
  99. ReceiptsPacket66{1111, ReceiptsPacket([][]*types.Receipt{})},
  100. // Transactions
  101. GetPooledTransactionsPacket66{1111, GetPooledTransactionsPacket([]common.Hash{})},
  102. PooledTransactionsPacket66{1111, PooledTransactionsPacket([]*types.Transaction{})},
  103. PooledTransactionsRLPPacket66{1111, PooledTransactionsRLPPacket([]rlp.RawValue{})},
  104. } {
  105. if have, _ := rlp.EncodeToBytes(msg); !bytes.Equal(have, want) {
  106. t.Errorf("test %d, type %T, have\n\t%x\nwant\n\t%x", i, msg, have, want)
  107. }
  108. }
  109. }
  110. // TestEth66Messages tests the encoding of all redefined eth66 messages
  111. func TestEth66Messages(t *testing.T) {
  112. // Some basic structs used during testing
  113. var (
  114. header *types.Header
  115. blockBody *BlockBody
  116. blockBodyRlp rlp.RawValue
  117. txs []*types.Transaction
  118. txRlps []rlp.RawValue
  119. hashes []common.Hash
  120. receipts []*types.Receipt
  121. receiptsRlp rlp.RawValue
  122. err error
  123. )
  124. header = &types.Header{
  125. Difficulty: big.NewInt(2222),
  126. Number: big.NewInt(3333),
  127. GasLimit: 4444,
  128. GasUsed: 5555,
  129. Time: 6666,
  130. Extra: []byte{0x77, 0x88},
  131. }
  132. // Init the transactions, taken from a different test
  133. {
  134. for _, hexrlp := range []string{
  135. "f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10",
  136. "f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb",
  137. } {
  138. var tx *types.Transaction
  139. rlpdata := common.FromHex(hexrlp)
  140. if err := rlp.DecodeBytes(rlpdata, &tx); err != nil {
  141. t.Fatal(err)
  142. }
  143. txs = append(txs, tx)
  144. txRlps = append(txRlps, rlpdata)
  145. }
  146. }
  147. // init the block body data, both object and rlp form
  148. blockBody = &BlockBody{
  149. Transactions: txs,
  150. Uncles: []*types.Header{header},
  151. }
  152. blockBodyRlp, err = rlp.EncodeToBytes(blockBody)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. hashes = []common.Hash{
  157. common.HexToHash("deadc0de"),
  158. common.HexToHash("feedbeef"),
  159. }
  160. byteSlices := [][]byte{
  161. common.FromHex("deadc0de"),
  162. common.FromHex("feedbeef"),
  163. }
  164. // init the receipts
  165. {
  166. receipts = []*types.Receipt{
  167. {
  168. Status: types.ReceiptStatusFailed,
  169. CumulativeGasUsed: 1,
  170. Logs: []*types.Log{
  171. {
  172. Address: common.BytesToAddress([]byte{0x11}),
  173. Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
  174. Data: []byte{0x01, 0x00, 0xff},
  175. },
  176. },
  177. TxHash: hashes[0],
  178. ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
  179. GasUsed: 111111,
  180. },
  181. }
  182. rlpData, err := rlp.EncodeToBytes(receipts)
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. receiptsRlp = rlpData
  187. }
  188. for i, tc := range []struct {
  189. message interface{}
  190. want []byte
  191. }{
  192. {
  193. GetBlockHeadersPacket66{1111, &GetBlockHeadersPacket{HashOrNumber{hashes[0], 0}, 5, 5, false}},
  194. common.FromHex("e8820457e4a000000000000000000000000000000000000000000000000000000000deadc0de050580"),
  195. },
  196. {
  197. GetBlockHeadersPacket66{1111, &GetBlockHeadersPacket{HashOrNumber{common.Hash{}, 9999}, 5, 5, false}},
  198. common.FromHex("ca820457c682270f050580"),
  199. },
  200. {
  201. BlockHeadersPacket66{1111, BlockHeadersPacket{header}},
  202. common.FromHex("f90202820457f901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000"),
  203. },
  204. {
  205. GetBlockBodiesPacket66{1111, GetBlockBodiesPacket(hashes)},
  206. common.FromHex("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"),
  207. },
  208. {
  209. BlockBodiesPacket66{1111, BlockBodiesPacket([]*BlockBody{blockBody})},
  210. common.FromHex("f902dc820457f902d6f902d3f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afbf901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000"),
  211. },
  212. { // Identical to non-rlp-shortcut version
  213. BlockBodiesRLPPacket66{1111, BlockBodiesRLPPacket([]rlp.RawValue{blockBodyRlp})},
  214. common.FromHex("f902dc820457f902d6f902d3f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afbf901fcf901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000"),
  215. },
  216. {
  217. GetNodeDataPacket66{1111, GetNodeDataPacket(hashes)},
  218. common.FromHex("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"),
  219. },
  220. {
  221. NodeDataPacket66{1111, NodeDataPacket(byteSlices)},
  222. common.FromHex("ce820457ca84deadc0de84feedbeef"),
  223. },
  224. {
  225. GetReceiptsPacket66{1111, GetReceiptsPacket(hashes)},
  226. common.FromHex("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"),
  227. },
  228. {
  229. ReceiptsPacket66{1111, ReceiptsPacket([][]*types.Receipt{receipts})},
  230. common.FromHex("f90172820457f9016cf90169f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"),
  231. },
  232. {
  233. ReceiptsRLPPacket66{1111, ReceiptsRLPPacket([]rlp.RawValue{receiptsRlp})},
  234. common.FromHex("f90172820457f9016cf90169f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"),
  235. },
  236. {
  237. GetPooledTransactionsPacket66{1111, GetPooledTransactionsPacket(hashes)},
  238. common.FromHex("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"),
  239. },
  240. {
  241. PooledTransactionsPacket66{1111, PooledTransactionsPacket(txs)},
  242. common.FromHex("f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb"),
  243. },
  244. {
  245. PooledTransactionsRLPPacket66{1111, PooledTransactionsRLPPacket(txRlps)},
  246. common.FromHex("f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb"),
  247. },
  248. } {
  249. if have, _ := rlp.EncodeToBytes(tc.message); !bytes.Equal(have, tc.want) {
  250. t.Errorf("test %d, type %T, have\n\t%x\nwant\n\t%x", i, tc.message, have, tc.want)
  251. }
  252. }
  253. }