chain_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/p2p"
  22. "github.com/stretchr/testify/assert"
  23. )
  24. // TestEthProtocolNegotiation tests whether the test suite
  25. // can negotiate the highest eth protocol in a status message exchange
  26. func TestEthProtocolNegotiation(t *testing.T) {
  27. var tests = []struct {
  28. conn *Conn
  29. caps []p2p.Cap
  30. expected uint32
  31. }{
  32. {
  33. conn: &Conn{},
  34. caps: []p2p.Cap{
  35. {Name: "eth", Version: 63},
  36. {Name: "eth", Version: 64},
  37. {Name: "eth", Version: 65},
  38. },
  39. expected: uint32(65),
  40. },
  41. {
  42. conn: &Conn{},
  43. caps: []p2p.Cap{
  44. {Name: "eth", Version: 0},
  45. {Name: "eth", Version: 89},
  46. {Name: "eth", Version: 65},
  47. },
  48. expected: uint32(65),
  49. },
  50. {
  51. conn: &Conn{},
  52. caps: []p2p.Cap{
  53. {Name: "eth", Version: 63},
  54. {Name: "eth", Version: 64},
  55. {Name: "wrongProto", Version: 65},
  56. },
  57. expected: uint32(64),
  58. },
  59. }
  60. for i, tt := range tests {
  61. t.Run(strconv.Itoa(i), func(t *testing.T) {
  62. tt.conn.negotiateEthProtocol(tt.caps)
  63. assert.Equal(t, tt.expected, uint32(tt.conn.ethProtocolVersion))
  64. })
  65. }
  66. }
  67. // TestChain_GetHeaders tests whether the test suite can correctly
  68. // respond to a GetBlockHeaders request from a node.
  69. func TestChain_GetHeaders(t *testing.T) {
  70. chainFile, err := filepath.Abs("./testdata/chain.rlp.gz")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. genesisFile, err := filepath.Abs("./testdata/genesis.json")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. chain, err := loadChain(chainFile, genesisFile)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. var tests = []struct {
  83. req GetBlockHeaders
  84. expected BlockHeaders
  85. }{
  86. {
  87. req: GetBlockHeaders{
  88. Origin: hashOrNumber{
  89. Number: uint64(2),
  90. },
  91. Amount: uint64(5),
  92. Skip: 1,
  93. Reverse: false,
  94. },
  95. expected: BlockHeaders{
  96. chain.blocks[2].Header(),
  97. chain.blocks[4].Header(),
  98. chain.blocks[6].Header(),
  99. chain.blocks[8].Header(),
  100. chain.blocks[10].Header(),
  101. },
  102. },
  103. {
  104. req: GetBlockHeaders{
  105. Origin: hashOrNumber{
  106. Number: uint64(chain.Len() - 1),
  107. },
  108. Amount: uint64(3),
  109. Skip: 0,
  110. Reverse: true,
  111. },
  112. expected: BlockHeaders{
  113. chain.blocks[chain.Len()-1].Header(),
  114. chain.blocks[chain.Len()-2].Header(),
  115. chain.blocks[chain.Len()-3].Header(),
  116. },
  117. },
  118. {
  119. req: GetBlockHeaders{
  120. Origin: hashOrNumber{
  121. Hash: chain.Head().Hash(),
  122. },
  123. Amount: uint64(1),
  124. Skip: 0,
  125. Reverse: false,
  126. },
  127. expected: BlockHeaders{
  128. chain.Head().Header(),
  129. },
  130. },
  131. }
  132. for i, tt := range tests {
  133. t.Run(strconv.Itoa(i), func(t *testing.T) {
  134. headers, err := chain.GetHeaders(tt.req)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. assert.Equal(t, headers, tt.expected)
  139. })
  140. }
  141. }