forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_pow.go
87 lines (79 loc) · 2.96 KB
/
chain_pow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"runtime"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/pow"
)
// nonceCheckResult contains the result of a nonce verification.
type nonceCheckResult struct {
index int // Index of the item verified from an input array
valid bool // Result of the nonce verification
}
// verifyNoncesFromHeaders starts a concurrent header nonce verification,
// returning a quit channel to abort the operations and a results channel
// to retrieve the async verifications.
func verifyNoncesFromHeaders(checker pow.PoW, headers []*types.Header) (chan<- struct{}, <-chan nonceCheckResult) {
items := make([]pow.Block, len(headers))
for i, header := range headers {
items[i] = types.NewBlockWithHeader(header)
}
return verifyNonces(checker, items)
}
// verifyNoncesFromBlocks starts a concurrent block nonce verification,
// returning a quit channel to abort the operations and a results channel
// to retrieve the async verifications.
func verifyNoncesFromBlocks(checker pow.PoW, blocks []*types.Block) (chan<- struct{}, <-chan nonceCheckResult) {
items := make([]pow.Block, len(blocks))
for i, block := range blocks {
items[i] = block
}
return verifyNonces(checker, items)
}
// verifyNonces starts a concurrent nonce verification, returning a quit channel
// to abort the operations and a results channel to retrieve the async checks.
func verifyNonces(checker pow.PoW, items []pow.Block) (chan<- struct{}, <-chan nonceCheckResult) {
// Spawn as many workers as allowed threads
workers := runtime.GOMAXPROCS(0)
if len(items) < workers {
workers = len(items)
}
// Create a task channel and spawn the verifiers
tasks := make(chan int, workers)
results := make(chan nonceCheckResult, len(items)) // Buffered to make sure all workers stop
for i := 0; i < workers; i++ {
go func() {
for index := range tasks {
results <- nonceCheckResult{index: index, valid: checker.Verify(items[index])}
}
}()
}
// Feed item indices to the workers until done or aborted
abort := make(chan struct{})
go func() {
defer close(tasks)
for i := range items {
select {
case tasks <- i:
continue
case <-abort:
return
}
}
}()
return abort, results
}