From bfef4e4a317c2bc7b867e7922f21c2fbcd1db2b0 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 23 Feb 2014 14:25:15 -0600 Subject: [PATCH] Make regtest subsidy halving interval 150. This commit moves the subsidy halving interval to the chain params so it can be configured per network. With that change it sets the regression test halving interval to 150 to match the regression test params of the reference implementation. This was pointed out by @flammit. --- params.go | 32 ++++++++++++++++++++------------ validate.go | 17 ++++++----------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/params.go b/params.go index fa468698..1ce1fec2 100644 --- a/params.go +++ b/params.go @@ -26,6 +26,11 @@ type Params struct { // have represented in compact form. See CompactToBig for more details // on compact form. PowLimitBits uint32 + + // SubsidyHalvingInterval is the interval of blocks at which the + // baseSubsidy is continually halved. Mathematically this is: + // baseSubsidy / 2^(height/SubsidyHalvingInterval) + SubsidyHalvingInterval int64 } // mainPowLimit is the highest proof of work value a bitcoin block can have for @@ -35,10 +40,11 @@ var mainPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) // mainNetParams contains parameters specific to the main network // (btcwire.MainNet). var mainNetParams = Params{ - GenesisBlock: &btcwire.GenesisBlock, - GenesisHash: &btcwire.GenesisHash, - PowLimit: mainPowLimit, - PowLimitBits: BigToCompact(mainPowLimit), + GenesisBlock: &btcwire.GenesisBlock, + GenesisHash: &btcwire.GenesisHash, + PowLimit: mainPowLimit, + PowLimitBits: BigToCompact(mainPowLimit), + SubsidyHalvingInterval: 210000, } // regressionPowLimit is the highest proof of work value a bitcoin block can @@ -48,10 +54,11 @@ var regressionPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne) // regressionParams contains parameters specific to the regression test network // (btcwire.TestNet). var regressionParams = Params{ - GenesisBlock: &btcwire.TestNetGenesisBlock, - GenesisHash: &btcwire.TestNetGenesisHash, - PowLimit: regressionPowLimit, - PowLimitBits: BigToCompact(regressionPowLimit), + GenesisBlock: &btcwire.TestNetGenesisBlock, + GenesisHash: &btcwire.TestNetGenesisHash, + PowLimit: regressionPowLimit, + PowLimitBits: BigToCompact(regressionPowLimit), + SubsidyHalvingInterval: 150, } // testNetPowLimit is the highest proof of work value a bitcoin block can have @@ -61,10 +68,11 @@ var testNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne) // testNet3Params contains parameters specific to the test network (version 3) // (btcwire.TestNet3). var testNet3Params = Params{ - GenesisBlock: &btcwire.TestNet3GenesisBlock, - GenesisHash: &btcwire.TestNet3GenesisHash, - PowLimit: testNetPowLimit, - PowLimitBits: BigToCompact(testNetPowLimit), + GenesisBlock: &btcwire.TestNet3GenesisBlock, + GenesisHash: &btcwire.TestNet3GenesisHash, + PowLimit: testNetPowLimit, + PowLimitBits: BigToCompact(testNetPowLimit), + SubsidyHalvingInterval: 210000, } // chainParams returns chain parameters specific to the bitcoin network diff --git a/validate.go b/validate.go index 28555404..703741e1 100644 --- a/validate.go +++ b/validate.go @@ -43,13 +43,8 @@ const ( serializedHeightVersion = 2 // baseSubsidy is the starting subsidy amount for mined blocks. This - // value is halved every subsidyHalvingInterval blocks. + // value is halved every SubsidyHalvingInterval blocks. baseSubsidy = 50 * btcutil.SatoshiPerBitcoin - - // subsidyHalvingInterval is the interval of blocks at which the - // baseSubsidy is continually halved. See calcBlockSubsidy for more - // details. - subsidyHalvingInterval = 210000 ) var ( @@ -162,14 +157,14 @@ func isBIP0030Node(node *blockNode) bool { // newly generated blocks awards as well as validating the coinbase for blocks // has the expected value. // -// The subsidy is halved every subsidyHalvingInterval blocks. Mathematically -// this is: baseSubsidy / 2^(height/subsidyHalvingInterval) +// The subsidy is halved every SubsidyHalvingInterval blocks. Mathematically +// this is: baseSubsidy / 2^(height/SubsidyHalvingInterval) // // At the target block generation rate this is approximately every 4 // years. -func calcBlockSubsidy(height int64) int64 { +func (b *BlockChain) calcBlockSubsidy(height int64) int64 { // Equivalent to: baseSubsidy / 2^(height/subsidyHalvingInterval) - return baseSubsidy >> uint(height/subsidyHalvingInterval) + return baseSubsidy >> uint(height/b.chainParams().SubsidyHalvingInterval) } // CheckTransactionSanity performs some preliminary checks on a transaction to @@ -824,7 +819,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er for _, txOut := range transactions[0].MsgTx().TxOut { totalSatoshiOut += txOut.Value } - expectedSatoshiOut := calcBlockSubsidy(node.height) + totalFees + expectedSatoshiOut := b.calcBlockSubsidy(node.height) + totalFees if totalSatoshiOut > expectedSatoshiOut { str := fmt.Sprintf("coinbase transaction for block pays %v "+ "which is more than expected value of %v",