From 7c636d7ecc239431f15aa3d79157ab4cb99fe278 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Mon, 8 Oct 2012 18:03:50 +0200 Subject: [PATCH] Move block inflation calculator to be a static method of Block. In future this will move again to NetworkParameters. --- core/src/main/java/com/google/bitcoin/core/Block.java | 10 ++++++++++ .../com/google/bitcoin/core/FullPrunedBlockChain.java | 8 ++------ 2 files changed, 12 insertions(+), 6 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 73b302e4..1de0b07f 100644 --- a/core/src/main/java/com/google/bitcoin/core/Block.java +++ b/core/src/main/java/com/google/bitcoin/core/Block.java @@ -124,6 +124,16 @@ public class Block extends Message { super(params, payloadBytes, 0, parseLazy, parseRetain, length); } + /** + * 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. + */ + public static BigInteger getBlockInflation(int height) { + return Utils.toNanoCoins(50, 0).shiftRight(height / 210000); + } + private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); // This code is not actually necessary, as transient fields are initialized to the default value which is in 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 258f900f..1f51ed5e 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 || 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(); @@ -190,10 +190,6 @@ public class FullPrunedBlockChain extends AbstractBlockChain { } return new TransactionOutputChanges(txOutsCreated, txOutsSpent); } - - private BigInteger getBlockInflation(int height) { - return Utils.toNanoCoins(50, 0).shiftRight(height / 210000); - } @Override /** @@ -278,7 +274,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain { } } if (totalFees.compareTo(params.MAX_MONEY) > 0 || - getBlockInflation(newBlock.getHeight()).add(totalFees).compareTo(coinbaseValue) < 0) + Block.getBlockInflation(newBlock.getHeight()).add(totalFees).compareTo(coinbaseValue) < 0) throw new VerificationException("Transaction fees out of range"); txOutChanges = new TransactionOutputChanges(txOutsCreated, txOutsSpent); } else {