From 425126689ea84ceaa27fa835f657153590951609 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 14 Jul 2012 03:16:03 +0200 Subject: [PATCH] Move subsidy decrease block count constant to NetworkParameters. --- .../main/java/com/google/bitcoin/core/Block.java | 11 +++++++---- .../bitcoin/core/FullPrunedBlockChain.java | 7 ++++--- .../google/bitcoin/core/NetworkParameters.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Block.java b/core/src/main/java/com/google/bitcoin/core/Block.java index 1de0b07f..fd53cd5d 100644 --- a/core/src/main/java/com/google/bitcoin/core/Block.java +++ b/core/src/main/java/com/google/bitcoin/core/Block.java @@ -125,13 +125,16 @@ public class Block extends Message { } /** - * A utility method that calculates how much new Bitcoin would be created by the block at the given height. + *

A utility method that calculates how much new Bitcoin would be created by the block at the given height. * The inflation of Bitcoin is predictable and drops roughly every 4 years (210,000 blocks). At the dawn of * the system it was 50 coins per block, in late 2012 it went to 25 coins per block, and so on. The size of - * a coinbase transaction is inflation plus fees. + * a coinbase transaction is inflation plus fees.

+ * + *

The half-life is controlled by {@link com.google.bitcoin.core.NetworkParameters#getSubsidyDecreaseBlockCount()}. + *

*/ - public static BigInteger getBlockInflation(int height) { - return Utils.toNanoCoins(50, 0).shiftRight(height / 210000); + public BigInteger getBlockInflation(int height) { + return Utils.toNanoCoins(50, 0).shiftRight(height / params.getSubsidyDecreaseBlockCount()); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { diff --git a/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java b/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java index 1f51ed5e..66562b4b 100644 --- a/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java +++ b/core/src/main/java/com/google/bitcoin/core/FullPrunedBlockChain.java @@ -179,7 +179,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { totalFees = totalFees.add(valueIn.subtract(valueOut)); } } - if (totalFees.compareTo(params.MAX_MONEY) > 0 || Block.getBlockInflation(height).add(totalFees).compareTo(coinbaseValue) < 0) + if (totalFees.compareTo(params.MAX_MONEY) > 0 || block.getBlockInflation(height).add(totalFees).compareTo(coinbaseValue) < 0) throw new VerificationException("Transaction fees out of range"); } catch (VerificationException e) { blockStore.abortDatabaseBatchWrite(); @@ -228,7 +228,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { boolean isCoinBase = tx.isCoinBase(); BigInteger valueIn = BigInteger.ZERO; BigInteger valueOut = BigInteger.ZERO; - if (!isCoinBase) + if (!isCoinBase) { for(TransactionInput in : tx.getInputs()) { StoredTransactionOutput prevOut = blockStore.getTransactionOutput(in.getOutpoint().getHash(), in.getOutpoint().getIndex()); @@ -252,6 +252,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { blockStore.removeUnspentTransactionOutput(prevOut); txOutsSpent.add(prevOut); } + } Sha256Hash hash = tx.getHash(); for (StoredTransactionOutput out : tx.getOutputs()) { valueOut = valueOut.add(out.getValue()); @@ -274,7 +275,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { } } if (totalFees.compareTo(params.MAX_MONEY) > 0 || - Block.getBlockInflation(newBlock.getHeight()).add(totalFees).compareTo(coinbaseValue) < 0) + newBlock.getHeader().getBlockInflation(newBlock.getHeight()).add(totalFees).compareTo(coinbaseValue) < 0) throw new VerificationException("Transaction fees out of range"); txOutChanges = new TransactionOutputChanges(txOutsCreated, txOutsSpent); } else { diff --git a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java index b167d269..0ffdeef7 100644 --- a/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java +++ b/core/src/main/java/com/google/bitcoin/core/NetworkParameters.java @@ -109,6 +109,11 @@ public class NetworkParameters implements Serializable { * The depth of blocks required for a coinbase transaction to be spendable. */ private int spendableCoinbaseDepth; + + /** + * Returns the number of blocks between subsidy decreases + */ + private int subsidyDecreaseBlockCount; /** * The version codes that prefix addresses which are acceptable on this network. Although Satoshi intended these to @@ -206,6 +211,7 @@ public class NetworkParameters implements Serializable { n.genesisBlock.setDifficultyTarget(0x1d07fff8L); n.genesisBlock.setNonce(384568319); n.setSpendableCoinbaseDepth(100); + n.setSubsidyDecreaseBlockCount(210000); n.id = ID_TESTNET; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"), @@ -242,6 +248,7 @@ public class NetworkParameters implements Serializable { n.genesisBlock.setTime(1231006505L); n.genesisBlock.setNonce(2083236893); n.setSpendableCoinbaseDepth(100); + n.setSubsidyDecreaseBlockCount(210000); n.id = ID_PRODNET; String genesisHash = n.genesisBlock.getHashAsString(); checkState(genesisHash.equals("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"), @@ -270,6 +277,7 @@ public class NetworkParameters implements Serializable { n.interval = 10; n.targetTimespan = 200000000; // 6 years. Just a very big number. n.setSpendableCoinbaseDepth(5); + n.setSubsidyDecreaseBlockCount(100); n.id = "com.google.bitcoin.unittest"; return n; } @@ -335,4 +343,12 @@ public class NetworkParameters implements Serializable { return true; return false; } + + public void setSubsidyDecreaseBlockCount(int subsidyDecreaseBlockCount) { + this.subsidyDecreaseBlockCount = subsidyDecreaseBlockCount; + } + + public int getSubsidyDecreaseBlockCount() { + return subsidyDecreaseBlockCount; + } }