// 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 }