Make the CheckBlockSanity function context free.

Rather than defining CheckBlockSanity as a member of a BlockChain
instance, define it at the root level so it is truly context free as
intended.  In order to make it context free, the proof of work limit is
now a required parameter.
This commit is contained in:
Dave Collins 2013-11-07 16:06:01 -06:00
parent 09b53a8fca
commit 5295be070d
3 changed files with 7 additions and 14 deletions

View file

@ -112,7 +112,7 @@ func (b *BlockChain) ProcessBlock(block *btcutil.Block) error {
}
// Perform preliminary sanity checks on the block and its transactions.
err = b.CheckBlockSanity(block)
err = CheckBlockSanity(block, b.chainParams().PowLimit)
if err != nil {
return err
}

View file

@ -12,6 +12,7 @@ import (
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"math"
"math/big"
"time"
)
@ -265,7 +266,7 @@ func CheckTransactionSanity(tx *btcutil.Tx) error {
// checkProofOfWork ensures the block header bits which indicate the target
// difficulty is in min/max range and that the block hash is less than the
// target difficulty as claimed.
func (b *BlockChain) checkProofOfWork(block *btcutil.Block) error {
func checkProofOfWork(block *btcutil.Block, powLimit *big.Int) error {
// The target difficulty must be larger than zero.
header := block.MsgBlock().Header
target := CompactToBig(header.Bits)
@ -276,7 +277,6 @@ func (b *BlockChain) checkProofOfWork(block *btcutil.Block) error {
}
// The target difficulty must be less than the maximum allowed.
powLimit := b.chainParams().PowLimit
if target.Cmp(powLimit) > 0 {
str := fmt.Sprintf("block target difficulty of %064x is "+
"higher than max of %064x", target, powLimit)
@ -390,7 +390,7 @@ func countP2SHSigOps(tx *btcutil.Tx, isCoinBaseTx bool, txStore TxStore) (int, e
// CheckBlockSanity performs some preliminary checks on a block to ensure it is
// sane before continuing with block processing. These checks are context free.
func (b *BlockChain) CheckBlockSanity(block *btcutil.Block) error {
func CheckBlockSanity(block *btcutil.Block, powLimit *big.Int) error {
// NOTE: bitcoind does size limits checking here, but the size limits
// have already been checked by btcwire for incoming blocks. Also,
// btcwire checks the size limits on send too, so there is no need
@ -399,7 +399,7 @@ func (b *BlockChain) CheckBlockSanity(block *btcutil.Block) error {
// Ensure the proof of work bits in the block header is in min/max range
// and the block hash is less than the target value described by the
// bits.
err := b.checkProofOfWork(block)
err := checkProofOfWork(block, powLimit)
if err != nil {
return err
}

View file

@ -15,16 +15,9 @@ import (
)
func TestCheckBlockSanity(t *testing.T) {
// Create a new database and chain instance to run tests against.
chain, teardownFunc, err := chainSetup("cbsanity")
if err != nil {
t.Errorf("Failed to setup chain instance: %v", err)
return
}
defer teardownFunc()
powLimit := btcchain.ChainParams(btcwire.MainNet).PowLimit
block := btcutil.NewBlock(&Block100000)
err = chain.CheckBlockSanity(block)
err := btcchain.CheckBlockSanity(block, powLimit)
if err != nil {
t.Errorf("CheckBlockSanity: %v", err)
}