// 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 }
// block verifies nonces of the given blocks in parallel and returns // an error if one of the blocks nonce verifications failed. func verifyNonces(pow pow.PoW, blocks []*types.Block, quit <-chan struct{}, done chan<- nonceResult) { // Spawn a few workers. They listen for blocks on the in channel // and send results on done. The workers will exit in the // background when in is closed. var ( in = make(chan int) nworkers = runtime.GOMAXPROCS(0) ) defer close(in) if len(blocks) < nworkers { nworkers = len(blocks) } for i := 0; i < nworkers; i++ { go func() { for i := range in { done <- nonceResult{i: i, valid: pow.Verify(blocks[i])} } }() } // Feed block indices to the workers. for i := range blocks { select { case in <- i: continue case <-quit: return } } }
// See YP section 4.3.4. "Block Header Validity" // Validates a block. Returns an error if the block is invalid. func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow, uncle bool) error { if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 { return fmt.Errorf("Block extra data too long (%d)", len(block.Extra)) } if uncle { if block.Time.Cmp(common.MaxBig) == 1 { return BlockTSTooBigErr } } else { if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 { return BlockFutureErr } } if block.Time.Cmp(parent.Time()) != 1 { return BlockEqualTSErr } expd := CalcDifficulty(block.Time.Uint64(), parent.Time().Uint64(), parent.Number(), parent.Difficulty()) if expd.Cmp(block.Difficulty) != 0 { return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd) } var a, b *big.Int a = parent.GasLimit() a = a.Sub(a, block.GasLimit) a.Abs(a) b = parent.GasLimit() b = b.Div(b, params.GasLimitBoundDivisor) if !(a.Cmp(b) < 0) || (block.GasLimit.Cmp(params.MinGasLimit) == -1) { return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b) } num := parent.Number() num.Sub(block.Number, num) if num.Cmp(big.NewInt(1)) != 0 { return BlockNumberErr } if checkPow { // Verify the nonce of the block. Return an error if it's not valid if !pow.Verify(types.NewBlockWithHeader(block)) { return ValidationError("Block's nonce is invalid (= %x)", block.Nonce) } } return nil }
// Validates a header. Returns an error if the header is invalid. // // See YP section 4.3.4. "Block Header Validity" func ValidateHeader(pow pow.PoW, header *types.Header, parent *types.Header, checkPow, uncle bool) error { if big.NewInt(int64(len(header.Extra))).Cmp(params.MaximumExtraDataSize) == 1 { return fmt.Errorf("Header extra data too long (%d)", len(header.Extra)) } if uncle { if header.Time.Cmp(common.MaxBig) == 1 { return BlockTSTooBigErr } } else { if header.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 { return BlockFutureErr } } if header.Time.Cmp(parent.Time) != 1 { return BlockEqualTSErr } expd := CalcDifficulty(header.Time.Uint64(), parent.Time.Uint64(), parent.Number, parent.Difficulty) if expd.Cmp(header.Difficulty) != 0 { return fmt.Errorf("Difficulty check failed for header %v, %v", header.Difficulty, expd) } a := new(big.Int).Set(parent.GasLimit) a = a.Sub(a, header.GasLimit) a.Abs(a) b := new(big.Int).Set(parent.GasLimit) b = b.Div(b, params.GasLimitBoundDivisor) if !(a.Cmp(b) < 0) || (header.GasLimit.Cmp(params.MinGasLimit) == -1) { return fmt.Errorf("GasLimit check failed for header %v (%v > %v)", header.GasLimit, a, b) } num := new(big.Int).Set(parent.Number) num.Sub(header.Number, num) if num.Cmp(big.NewInt(1)) != 0 { return BlockNumberErr } if checkPow { // Verify the nonce of the header. Return an error if it's not valid if !pow.Verify(types.NewBlockWithHeader(header)) { return &BlockNonceErr{header.Number, header.Hash(), header.Nonce.Uint64()} } } return nil }