3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Move subsidy decrease block count constant to NetworkParameters.

This commit is contained in:
Matt Corallo 2012-07-14 03:16:03 +02:00 committed by Mike Hearn
parent 7c636d7ecc
commit 425126689e
3 changed files with 27 additions and 7 deletions

View File

@ -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.
* <p>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.</p>
*
* <p>The half-life is controlled by {@link com.google.bitcoin.core.NetworkParameters#getSubsidyDecreaseBlockCount()}.
* </p>
*/
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 {

View File

@ -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 {

View File

@ -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;
}
}