diff --git a/blockchain/accept.go b/blockchain/accept.go index f0de2432..d6d64832 100644 --- a/blockchain/accept.go +++ b/blockchain/accept.go @@ -107,6 +107,16 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, flags BehaviorFlags) } if !fastAdd { + // Reject version 2 blocks once a majority of the network has + // upgraded. This is part of BIP0066. + if blockHeader.Version < 3 && b.isMajorityVersion(3, prevNode, + b.chainParams.BlockRejectNumRequired) { + + str := "new blocks with version %d are no longer valid" + str = fmt.Sprintf(str, blockHeader.Version) + return ruleError(ErrBlockVersionTooOld, str) + } + // Reject version 1 blocks once a majority of the network has // upgraded. This is part of BIP0034. if blockHeader.Version < 2 && b.isMajorityVersion(2, prevNode, diff --git a/blockchain/validate.go b/blockchain/validate.go index cdace9e8..df6284c4 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -915,6 +915,16 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er runScripts = false } + // Get the previous block node. This function is used over simply + // accessing node.parent directly as it will dynamically create previous + // block nodes as needed. This helps allow only the pieces of the chain + // that are needed to remain in memory. + prevNode, err := b.getPrevNodeFromNode(node) + if err != nil { + log.Errorf("getPrevNodeFromNode: %v", err) + return err + } + // Blocks created after the BIP0016 activation time need to have the // pay-to-script-hash checks enabled. var scriptFlags txscript.ScriptFlags @@ -922,6 +932,16 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er scriptFlags |= txscript.ScriptBip16 } + // Enforce DER signatures for block versions 3+ once the majority of the + // network has upgraded to the enforcement threshold. This is part of + // BIP0066. + blockHeader := &block.MsgBlock().Header + if blockHeader.Version >= 3 && b.isMajorityVersion(3, prevNode, + b.chainParams.BlockEnforceNumRequired) { + + scriptFlags |= txscript.ScriptVerifyDERSignatures + } + // Now that the inexpensive checks are done and have passed, verify the // transactions are actually allowed to spend the coins by running the // expensive ECDSA signature check scripts. Doing this last helps diff --git a/mining.go b/mining.go index d6dc4c6d..f17fb1e9 100644 --- a/mining.go +++ b/mining.go @@ -24,7 +24,7 @@ const ( // will require changes to the generated block. Using the wire constant // for generated block version could allow creation of invalid blocks // for the updated version. - generatedBlockVersion = 2 + generatedBlockVersion = 3 // minHighPriority is the minimum priority value that allows a // transaction to be considered high priority. diff --git a/wire/blockheader.go b/wire/blockheader.go index 8e3f79b0..3ca8619c 100644 --- a/wire/blockheader.go +++ b/wire/blockheader.go @@ -11,7 +11,7 @@ import ( ) // BlockVersion is the current latest supported block version. -const BlockVersion = 2 +const BlockVersion = 3 // Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes + // PrevBlock and MerkleRoot hashes.