chain_pow.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "github.com/ethereum/go-ethereum/core/types"
  20. "github.com/ethereum/go-ethereum/pow"
  21. )
  22. // nonceCheckResult contains the result of a nonce verification.
  23. type nonceCheckResult struct {
  24. index int // Index of the item verified from an input array
  25. valid bool // Result of the nonce verification
  26. }
  27. // verifyNoncesFromHeaders starts a concurrent header nonce verification,
  28. // returning a quit channel to abort the operations and a results channel
  29. // to retrieve the async verifications.
  30. func verifyNoncesFromHeaders(checker pow.PoW, headers []*types.Header) (chan<- struct{}, <-chan nonceCheckResult) {
  31. items := make([]pow.Block, len(headers))
  32. for i, header := range headers {
  33. items[i] = types.NewBlockWithHeader(header)
  34. }
  35. return verifyNonces(checker, items)
  36. }
  37. // verifyNoncesFromBlocks starts a concurrent block nonce verification,
  38. // returning a quit channel to abort the operations and a results channel
  39. // to retrieve the async verifications.
  40. func verifyNoncesFromBlocks(checker pow.PoW, blocks []*types.Block) (chan<- struct{}, <-chan nonceCheckResult) {
  41. items := make([]pow.Block, len(blocks))
  42. for i, block := range blocks {
  43. items[i] = block
  44. }
  45. return verifyNonces(checker, items)
  46. }
  47. // verifyNonces starts a concurrent nonce verification, returning a quit channel
  48. // to abort the operations and a results channel to retrieve the async checks.
  49. func verifyNonces(checker pow.PoW, items []pow.Block) (chan<- struct{}, <-chan nonceCheckResult) {
  50. // Spawn as many workers as allowed threads
  51. workers := runtime.GOMAXPROCS(0)
  52. if len(items) < workers {
  53. workers = len(items)
  54. }
  55. // Create a task channel and spawn the verifiers
  56. tasks := make(chan int, workers)
  57. results := make(chan nonceCheckResult, len(items)) // Buffered to make sure all workers stop
  58. for i := 0; i < workers; i++ {
  59. go func() {
  60. for index := range tasks {
  61. results <- nonceCheckResult{index: index, valid: checker.Verify(items[index]) == nil}
  62. }
  63. }()
  64. }
  65. // Feed item indices to the workers until done or aborted
  66. abort := make(chan struct{})
  67. go func() {
  68. defer close(tasks)
  69. for i := range items {
  70. select {
  71. case tasks <- i:
  72. continue
  73. case <-abort:
  74. return
  75. }
  76. }
  77. }()
  78. return abort, results
  79. }