suite.go 5.8 KB

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