suite.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "fmt"
  19. "net"
  20. "time"
  21. "github.com/davecgh/go-spew/spew"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/internal/utesting"
  24. "github.com/ethereum/go-ethereum/p2p/enode"
  25. "github.com/ethereum/go-ethereum/p2p/rlpx"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. var pretty = spew.ConfigState{
  29. Indent: " ",
  30. DisableCapacities: true,
  31. DisablePointerAddresses: true,
  32. SortKeys: true,
  33. }
  34. // Suite represents a structure used to test the eth
  35. // protocol of a node(s).
  36. type Suite struct {
  37. Dest *enode.Node
  38. chain *Chain
  39. fullChain *Chain
  40. }
  41. // NewSuite creates and returns a new eth-test suite that can
  42. // be used to test the given node against the given blockchain
  43. // data.
  44. func NewSuite(dest *enode.Node, chainfile string, genesisfile string) *Suite {
  45. chain, err := loadChain(chainfile, genesisfile)
  46. if err != nil {
  47. panic(err)
  48. }
  49. return &Suite{
  50. Dest: dest,
  51. chain: chain.Shorten(1000),
  52. fullChain: chain,
  53. }
  54. }
  55. func (s *Suite) AllTests() []utesting.Test {
  56. return []utesting.Test{
  57. {Name: "Status", Fn: s.TestStatus},
  58. {Name: "GetBlockHeaders", Fn: s.TestGetBlockHeaders},
  59. {Name: "Broadcast", Fn: s.TestBroadcast},
  60. {Name: "GetBlockBodies", Fn: s.TestGetBlockBodies},
  61. }
  62. }
  63. // TestStatus attempts to connect to the given node and exchange
  64. // a status message with it, and then check to make sure
  65. // the chain head is correct.
  66. func (s *Suite) TestStatus(t *utesting.T) {
  67. conn, err := s.dial()
  68. if err != nil {
  69. t.Fatalf("could not dial: %v", err)
  70. }
  71. // get protoHandshake
  72. conn.handshake(t)
  73. // get status
  74. switch msg := conn.statusExchange(t, s.chain).(type) {
  75. case *Status:
  76. t.Logf("got status message: %s", pretty.Sdump(msg))
  77. default:
  78. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  79. }
  80. }
  81. // TestGetBlockHeaders tests whether the given node can respond to
  82. // a `GetBlockHeaders` request and that the response is accurate.
  83. func (s *Suite) TestGetBlockHeaders(t *utesting.T) {
  84. conn, err := s.dial()
  85. if err != nil {
  86. t.Fatalf("could not dial: %v", err)
  87. }
  88. conn.handshake(t)
  89. conn.statusExchange(t, s.chain)
  90. // get block headers
  91. req := &GetBlockHeaders{
  92. Origin: hashOrNumber{
  93. Hash: s.chain.blocks[1].Hash(),
  94. },
  95. Amount: 2,
  96. Skip: 1,
  97. Reverse: false,
  98. }
  99. if err := conn.Write(req); err != nil {
  100. t.Fatalf("could not write to connection: %v", err)
  101. }
  102. timeout := 20 * time.Second
  103. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  104. case *BlockHeaders:
  105. headers := msg
  106. for _, header := range *headers {
  107. num := header.Number.Uint64()
  108. t.Logf("received header (%d): %s", num, pretty.Sdump(header))
  109. assert.Equal(t, s.chain.blocks[int(num)].Header(), header)
  110. }
  111. default:
  112. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  113. }
  114. }
  115. // TestGetBlockBodies tests whether the given node can respond to
  116. // a `GetBlockBodies` request and that the response is accurate.
  117. func (s *Suite) TestGetBlockBodies(t *utesting.T) {
  118. conn, err := s.dial()
  119. if err != nil {
  120. t.Fatalf("could not dial: %v", err)
  121. }
  122. conn.handshake(t)
  123. conn.statusExchange(t, s.chain)
  124. // create block bodies request
  125. req := &GetBlockBodies{s.chain.blocks[54].Hash(), s.chain.blocks[75].Hash()}
  126. if err := conn.Write(req); err != nil {
  127. t.Fatalf("could not write to connection: %v", err)
  128. }
  129. timeout := 20 * time.Second
  130. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  131. case *BlockBodies:
  132. t.Logf("received %d block bodies", len(*msg))
  133. default:
  134. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  135. }
  136. }
  137. // TestBroadcast tests whether a block announcement is correctly
  138. // propagated to the given node's peer(s).
  139. func (s *Suite) TestBroadcast(t *utesting.T) {
  140. // create conn to send block announcement
  141. sendConn, err := s.dial()
  142. if err != nil {
  143. t.Fatalf("could not dial: %v", err)
  144. }
  145. // create conn to receive block announcement
  146. receiveConn, err := s.dial()
  147. if err != nil {
  148. t.Fatalf("could not dial: %v", err)
  149. }
  150. sendConn.handshake(t)
  151. receiveConn.handshake(t)
  152. sendConn.statusExchange(t, s.chain)
  153. receiveConn.statusExchange(t, s.chain)
  154. // sendConn sends the block announcement
  155. blockAnnouncement := &NewBlock{
  156. Block: s.fullChain.blocks[1000],
  157. TD: s.fullChain.TD(1001),
  158. }
  159. if err := sendConn.Write(blockAnnouncement); err != nil {
  160. t.Fatalf("could not write to connection: %v", err)
  161. }
  162. timeout := 20 * time.Second
  163. switch msg := receiveConn.ReadAndServe(s.chain, timeout).(type) {
  164. case *NewBlock:
  165. t.Logf("received NewBlock message: %s", pretty.Sdump(msg.Block))
  166. assert.Equal(t,
  167. blockAnnouncement.Block.Header(), msg.Block.Header(),
  168. "wrong block header in announcement",
  169. )
  170. assert.Equal(t,
  171. blockAnnouncement.TD, msg.TD,
  172. "wrong TD in announcement",
  173. )
  174. case *NewBlockHashes:
  175. hashes := *msg
  176. t.Logf("received NewBlockHashes message: %s", pretty.Sdump(hashes))
  177. assert.Equal(t,
  178. blockAnnouncement.Block.Hash(), hashes[0].Hash,
  179. "wrong block hash in announcement",
  180. )
  181. default:
  182. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  183. }
  184. // update test suite chain
  185. s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[1000])
  186. // wait for client to update its chain
  187. if err := receiveConn.waitForBlock(s.chain.Head()); err != nil {
  188. t.Fatal(err)
  189. }
  190. }
  191. // dial attempts to dial the given node and perform a handshake,
  192. // returning the created Conn if successful.
  193. func (s *Suite) dial() (*Conn, error) {
  194. var conn Conn
  195. fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", s.Dest.IP(), s.Dest.TCP()))
  196. if err != nil {
  197. return nil, err
  198. }
  199. conn.Conn = rlpx.NewConn(fd, s.Dest.Pubkey())
  200. // do encHandshake
  201. conn.ourKey, _ = crypto.GenerateKey()
  202. _, err = conn.Handshake(conn.ourKey)
  203. if err != nil {
  204. return nil, err
  205. }
  206. return &conn, nil
  207. }