Implement BIP0066 changeover logic for v3 blocks.

This commit implements the changeover logic for version 3 blocks as
described by BIP0066.
This commit is contained in:
Dave Collins 2015-02-25 14:32:37 -06:00
parent 4c53599b67
commit 3ed8f363e7
4 changed files with 32 additions and 2 deletions

View file

@ -107,6 +107,16 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, flags BehaviorFlags)
} }
if !fastAdd { 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 // Reject version 1 blocks once a majority of the network has
// upgraded. This is part of BIP0034. // upgraded. This is part of BIP0034.
if blockHeader.Version < 2 && b.isMajorityVersion(2, prevNode, if blockHeader.Version < 2 && b.isMajorityVersion(2, prevNode,

View file

@ -915,6 +915,16 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er
runScripts = false 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 // Blocks created after the BIP0016 activation time need to have the
// pay-to-script-hash checks enabled. // pay-to-script-hash checks enabled.
var scriptFlags txscript.ScriptFlags var scriptFlags txscript.ScriptFlags
@ -922,6 +932,16 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er
scriptFlags |= txscript.ScriptBip16 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 // Now that the inexpensive checks are done and have passed, verify the
// transactions are actually allowed to spend the coins by running the // transactions are actually allowed to spend the coins by running the
// expensive ECDSA signature check scripts. Doing this last helps // expensive ECDSA signature check scripts. Doing this last helps

View file

@ -24,7 +24,7 @@ const (
// will require changes to the generated block. Using the wire constant // will require changes to the generated block. Using the wire constant
// for generated block version could allow creation of invalid blocks // for generated block version could allow creation of invalid blocks
// for the updated version. // for the updated version.
generatedBlockVersion = 2 generatedBlockVersion = 3
// minHighPriority is the minimum priority value that allows a // minHighPriority is the minimum priority value that allows a
// transaction to be considered high priority. // transaction to be considered high priority.

View file

@ -11,7 +11,7 @@ import (
) )
// BlockVersion is the current latest supported block version. // 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 + // Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +
// PrevBlock and MerkleRoot hashes. // PrevBlock and MerkleRoot hashes.