block_validator_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2015 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 core
  17. import (
  18. "runtime"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/consensus/ethash"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/core/vm"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. // Tests that simple header verification works, for both good and bad blocks.
  28. func TestHeaderVerification(t *testing.T) {
  29. // Create a simple chain to verify
  30. var (
  31. testdb = ethdb.NewMemDatabase()
  32. gspec = &Genesis{Config: params.TestChainConfig}
  33. genesis = gspec.MustCommit(testdb)
  34. blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 8, nil)
  35. )
  36. headers := make([]*types.Header, len(blocks))
  37. for i, block := range blocks {
  38. headers[i] = block.Header()
  39. }
  40. // Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
  41. chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
  42. defer chain.Stop()
  43. for i := 0; i < len(blocks); i++ {
  44. for j, valid := range []bool{true, false} {
  45. var results <-chan error
  46. if valid {
  47. engine := ethash.NewFaker()
  48. _, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
  49. } else {
  50. engine := ethash.NewFakeFailer(headers[i].Number.Uint64())
  51. _, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
  52. }
  53. // Wait for the verification result
  54. select {
  55. case result := <-results:
  56. if (result == nil) != valid {
  57. t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, result, valid)
  58. }
  59. case <-time.After(time.Second):
  60. t.Fatalf("test %d.%d: verification timeout", i, j)
  61. }
  62. // Make sure no more data is returned
  63. select {
  64. case result := <-results:
  65. t.Fatalf("test %d.%d: unexpected result returned: %v", i, j, result)
  66. case <-time.After(25 * time.Millisecond):
  67. }
  68. }
  69. chain.InsertChain(blocks[i : i+1])
  70. }
  71. }
  72. // Tests that concurrent header verification works, for both good and bad blocks.
  73. func TestHeaderConcurrentVerification2(t *testing.T) { testHeaderConcurrentVerification(t, 2) }
  74. func TestHeaderConcurrentVerification8(t *testing.T) { testHeaderConcurrentVerification(t, 8) }
  75. func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVerification(t, 32) }
  76. func testHeaderConcurrentVerification(t *testing.T, threads int) {
  77. // Create a simple chain to verify
  78. var (
  79. testdb = ethdb.NewMemDatabase()
  80. gspec = &Genesis{Config: params.TestChainConfig}
  81. genesis = gspec.MustCommit(testdb)
  82. blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 8, nil)
  83. )
  84. headers := make([]*types.Header, len(blocks))
  85. seals := make([]bool, len(blocks))
  86. for i, block := range blocks {
  87. headers[i] = block.Header()
  88. seals[i] = true
  89. }
  90. // Set the number of threads to verify on
  91. old := runtime.GOMAXPROCS(threads)
  92. defer runtime.GOMAXPROCS(old)
  93. // Run the header checker for the entire block chain at once both for a valid and
  94. // also an invalid chain (enough if one arbitrary block is invalid).
  95. for i, valid := range []bool{true, false} {
  96. var results <-chan error
  97. if valid {
  98. chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
  99. _, results = chain.engine.VerifyHeaders(chain, headers, seals)
  100. chain.Stop()
  101. } else {
  102. chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), vm.Config{})
  103. _, results = chain.engine.VerifyHeaders(chain, headers, seals)
  104. chain.Stop()
  105. }
  106. // Wait for all the verification results
  107. checks := make(map[int]error)
  108. for j := 0; j < len(blocks); j++ {
  109. select {
  110. case result := <-results:
  111. checks[j] = result
  112. case <-time.After(time.Second):
  113. t.Fatalf("test %d.%d: verification timeout", i, j)
  114. }
  115. }
  116. // Check nonce check validity
  117. for j := 0; j < len(blocks); j++ {
  118. want := valid || (j < len(blocks)-2) // We chose the last-but-one nonce in the chain to fail
  119. if (checks[j] == nil) != want {
  120. t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, checks[j], want)
  121. }
  122. if !want {
  123. // A few blocks after the first error may pass verification due to concurrent
  124. // workers. We don't care about those in this test, just that the correct block
  125. // errors out.
  126. break
  127. }
  128. }
  129. // Make sure no more data is returned
  130. select {
  131. case result := <-results:
  132. t.Fatalf("test %d: unexpected result returned: %v", i, result)
  133. case <-time.After(25 * time.Millisecond):
  134. }
  135. }
  136. }
  137. // Tests that aborting a header validation indeed prevents further checks from being
  138. // run, as well as checks that no left-over goroutines are leaked.
  139. func TestHeaderConcurrentAbortion2(t *testing.T) { testHeaderConcurrentAbortion(t, 2) }
  140. func TestHeaderConcurrentAbortion8(t *testing.T) { testHeaderConcurrentAbortion(t, 8) }
  141. func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion(t, 32) }
  142. func testHeaderConcurrentAbortion(t *testing.T, threads int) {
  143. // Create a simple chain to verify
  144. var (
  145. testdb = ethdb.NewMemDatabase()
  146. gspec = &Genesis{Config: params.TestChainConfig}
  147. genesis = gspec.MustCommit(testdb)
  148. blocks, _ = GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 1024, nil)
  149. )
  150. headers := make([]*types.Header, len(blocks))
  151. seals := make([]bool, len(blocks))
  152. for i, block := range blocks {
  153. headers[i] = block.Header()
  154. seals[i] = true
  155. }
  156. // Set the number of threads to verify on
  157. old := runtime.GOMAXPROCS(threads)
  158. defer runtime.GOMAXPROCS(old)
  159. // Start the verifications and immediately abort
  160. chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), vm.Config{})
  161. defer chain.Stop()
  162. abort, results := chain.engine.VerifyHeaders(chain, headers, seals)
  163. close(abort)
  164. // Deplete the results channel
  165. verified := 0
  166. for depleted := false; !depleted; {
  167. select {
  168. case result := <-results:
  169. if result != nil {
  170. t.Errorf("header %d: validation failed: %v", verified, result)
  171. }
  172. verified++
  173. case <-time.After(50 * time.Millisecond):
  174. depleted = true
  175. }
  176. }
  177. // Check that abortion was honored by not processing too many POWs
  178. if verified > 2*threads {
  179. t.Errorf("verification count too large: have %d, want below %d", verified, 2*threads)
  180. }
  181. }