chain_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "path/filepath"
  19. "strconv"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  22. "github.com/ethereum/go-ethereum/p2p"
  23. "github.com/stretchr/testify/assert"
  24. )
  25. // TestEthProtocolNegotiation tests whether the test suite
  26. // can negotiate the highest eth protocol in a status message exchange
  27. func TestEthProtocolNegotiation(t *testing.T) {
  28. var tests = []struct {
  29. conn *Conn
  30. caps []p2p.Cap
  31. expected uint32
  32. }{
  33. {
  34. conn: &Conn{
  35. ourHighestProtoVersion: 65,
  36. },
  37. caps: []p2p.Cap{
  38. {Name: "eth", Version: 63},
  39. {Name: "eth", Version: 64},
  40. {Name: "eth", Version: 65},
  41. },
  42. expected: uint32(65),
  43. },
  44. {
  45. conn: &Conn{
  46. ourHighestProtoVersion: 65,
  47. },
  48. caps: []p2p.Cap{
  49. {Name: "eth", Version: 63},
  50. {Name: "eth", Version: 64},
  51. {Name: "eth", Version: 65},
  52. },
  53. expected: uint32(65),
  54. },
  55. {
  56. conn: &Conn{
  57. ourHighestProtoVersion: 65,
  58. },
  59. caps: []p2p.Cap{
  60. {Name: "eth", Version: 63},
  61. {Name: "eth", Version: 64},
  62. {Name: "eth", Version: 65},
  63. },
  64. expected: uint32(65),
  65. },
  66. {
  67. conn: &Conn{
  68. ourHighestProtoVersion: 64,
  69. },
  70. caps: []p2p.Cap{
  71. {Name: "eth", Version: 63},
  72. {Name: "eth", Version: 64},
  73. {Name: "eth", Version: 65},
  74. },
  75. expected: 64,
  76. },
  77. {
  78. conn: &Conn{
  79. ourHighestProtoVersion: 65,
  80. },
  81. caps: []p2p.Cap{
  82. {Name: "eth", Version: 0},
  83. {Name: "eth", Version: 89},
  84. {Name: "eth", Version: 65},
  85. },
  86. expected: uint32(65),
  87. },
  88. {
  89. conn: &Conn{
  90. ourHighestProtoVersion: 64,
  91. },
  92. caps: []p2p.Cap{
  93. {Name: "eth", Version: 63},
  94. {Name: "eth", Version: 64},
  95. {Name: "wrongProto", Version: 65},
  96. },
  97. expected: uint32(64),
  98. },
  99. {
  100. conn: &Conn{
  101. ourHighestProtoVersion: 65,
  102. },
  103. caps: []p2p.Cap{
  104. {Name: "eth", Version: 63},
  105. {Name: "eth", Version: 64},
  106. {Name: "wrongProto", Version: 65},
  107. },
  108. expected: uint32(64),
  109. },
  110. }
  111. for i, tt := range tests {
  112. t.Run(strconv.Itoa(i), func(t *testing.T) {
  113. tt.conn.negotiateEthProtocol(tt.caps)
  114. assert.Equal(t, tt.expected, uint32(tt.conn.negotiatedProtoVersion))
  115. })
  116. }
  117. }
  118. // TestChain_GetHeaders tests whether the test suite can correctly
  119. // respond to a GetBlockHeaders request from a node.
  120. func TestChain_GetHeaders(t *testing.T) {
  121. chainFile, err := filepath.Abs("./testdata/chain.rlp")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. genesisFile, err := filepath.Abs("./testdata/genesis.json")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. chain, err := loadChain(chainFile, genesisFile)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. var tests = []struct {
  134. req GetBlockHeaders
  135. expected BlockHeaders
  136. }{
  137. {
  138. req: GetBlockHeaders{
  139. Origin: eth.HashOrNumber{
  140. Number: uint64(2),
  141. },
  142. Amount: uint64(5),
  143. Skip: 1,
  144. Reverse: false,
  145. },
  146. expected: BlockHeaders{
  147. chain.blocks[2].Header(),
  148. chain.blocks[4].Header(),
  149. chain.blocks[6].Header(),
  150. chain.blocks[8].Header(),
  151. chain.blocks[10].Header(),
  152. },
  153. },
  154. {
  155. req: GetBlockHeaders{
  156. Origin: eth.HashOrNumber{
  157. Number: uint64(chain.Len() - 1),
  158. },
  159. Amount: uint64(3),
  160. Skip: 0,
  161. Reverse: true,
  162. },
  163. expected: BlockHeaders{
  164. chain.blocks[chain.Len()-1].Header(),
  165. chain.blocks[chain.Len()-2].Header(),
  166. chain.blocks[chain.Len()-3].Header(),
  167. },
  168. },
  169. {
  170. req: GetBlockHeaders{
  171. Origin: eth.HashOrNumber{
  172. Hash: chain.Head().Hash(),
  173. },
  174. Amount: uint64(1),
  175. Skip: 0,
  176. Reverse: false,
  177. },
  178. expected: BlockHeaders{
  179. chain.Head().Header(),
  180. },
  181. },
  182. }
  183. for i, tt := range tests {
  184. t.Run(strconv.Itoa(i), func(t *testing.T) {
  185. headers, err := chain.GetHeaders(tt.req)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. assert.Equal(t, headers, tt.expected)
  190. })
  191. }
  192. }