From 5295be070dbb85de218f472c01e6d5066ba049b4 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 7 Nov 2013 16:06:01 -0600 Subject: [PATCH] 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. --- process.go | 2 +- validate.go | 8 ++++---- validate_test.go | 11 ++--------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/process.go b/process.go index fa4e70dc..89ad0726 100644 --- a/process.go +++ b/process.go @@ -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 } diff --git a/validate.go b/validate.go index 700c0045..bc61aaf8 100644 --- a/validate.go +++ b/validate.go @@ -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 } diff --git a/validate_test.go b/validate_test.go index 6ce1e09a..a04148d5 100644 --- a/validate_test.go +++ b/validate_test.go @@ -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) }