Skip to content

Commit

Permalink
Implement BIP0066 changeover logic for v3 blocks.
Browse files Browse the repository at this point in the history
This commit implements the changeover logic for version 3 blocks as
described by BIP0066.
  • Loading branch information
davecgh committed Feb 26, 2015
1 parent 4c53599 commit 3ed8f36
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
10 changes: 10 additions & 0 deletions blockchain/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,13 +915,33 @@ 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
if block.MsgBlock().Header.Timestamp.After(txscript.Bip16Activation) {
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
Expand Down
2 changes: 1 addition & 1 deletion mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion wire/blockheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3ed8f36

Please sign in to comment.