From 7811770d3119ea4007df3167348ad6790930dad5 Mon Sep 17 00:00:00 2001 From: David Hill Date: Wed, 28 Oct 2015 11:43:36 -0400 Subject: [PATCH] Implement BIP0065 changeover logic for v4 blocks. This commit implements the changeover logic for version 4 blocks as described by BIP0065. --- blockchain/validate.go | 19 +++++++++++++++++++ mining.go | 2 +- wire/blockheader.go | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/blockchain/validate.go b/blockchain/validate.go index 4b79a607..395ef908 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -653,6 +653,16 @@ func (b *BlockChain) checkBlockHeaderContext(header *wire.BlockHeader, prevNode } if !fastAdd { + // Reject version 3 blocks once a majority of the network has + // upgraded. This is part of BIP0065. + if header.Version < 4 && b.isMajorityVersion(4, prevNode, + b.chainParams.BlockRejectNumRequired) { + + str := "new blocks with version %d are no longer valid" + str = fmt.Sprintf(str, header.Version) + return ruleError(ErrBlockVersionTooOld, str) + } + // Reject version 2 blocks once a majority of the network has // upgraded. This is part of BIP0066. if header.Version < 3 && b.isMajorityVersion(3, prevNode, @@ -1133,6 +1143,15 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er scriptFlags |= txscript.ScriptVerifyDERSignatures } + // Enforce CHECKLOCKTIMEVERIFY for block versions 4+ once the majority + // of the network has upgraded to the enforcement threshold. This is + // part of BIP0065. + if blockHeader.Version >= 4 && b.isMajorityVersion(4, prevNode, + b.chainParams.BlockEnforceNumRequired) { + + scriptFlags |= txscript.ScriptVerifyCheckLockTimeVerify + } + // 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 e222fb76..76dcaa80 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 = 3 + generatedBlockVersion = 4 // 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 5124b248..941d40a5 100644 --- a/wire/blockheader.go +++ b/wire/blockheader.go @@ -11,7 +11,7 @@ import ( ) // BlockVersion is the current latest supported block version. -const BlockVersion = 3 +const BlockVersion = 4 // MaxBlockHeaderPayload is the maximum number of bytes a block header can be. // Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +