Move getRewardByHeight from Block to BlockChain

This commit is contained in:
catbref 2019-06-26 12:30:51 +01:00
parent f45cedb6ff
commit 9435e9576a
3 changed files with 16 additions and 19 deletions

View File

@ -19,7 +19,7 @@ import org.qora.account.PrivateKeyAccount;
import org.qora.account.PublicKeyAccount;
import org.qora.asset.Asset;
import org.qora.at.AT;
import org.qora.block.BlockChain.RewardByHeight;
import org.qora.block.BlockChain;
import org.qora.crypto.Crypto;
import org.qora.data.account.ProxyForgerData;
import org.qora.data.at.ATData;
@ -1104,7 +1104,7 @@ public class Block {
}
protected void processBlockRewards() throws DataException {
BigDecimal reward = Block.getRewardAtHeight(this.blockData.getHeight());
BigDecimal reward = BlockChain.getInstance().getRewardAtHeight(this.blockData.getHeight());
// No reward for our height?
if (reward == null)
@ -1353,7 +1353,7 @@ public class Block {
}
protected void orphanBlockRewards() throws DataException {
BigDecimal reward = Block.getRewardAtHeight(this.blockData.getHeight());
BigDecimal reward = BlockChain.getInstance().getRewardAtHeight(this.blockData.getHeight());
// No reward for our height?
if (reward == null)
@ -1415,21 +1415,6 @@ public class Block {
atRepository.deleteATStates(this.blockData.getHeight());
}
public static BigDecimal getRewardAtHeight(int ourHeight) {
List<RewardByHeight> rewardsByHeight = BlockChain.getInstance().getBlockRewardsByHeight();
// No rewards configured?
if (rewardsByHeight == null)
return null;
// Scan through for reward at our height
for (int i = rewardsByHeight.size() - 1; i >= 0; --i)
if (rewardsByHeight.get(i).height <= ourHeight)
return rewardsByHeight.get(i).reward;
return null;
}
/**
* Return Qora balance adjusted to within min/max limits.
*/

View File

@ -299,6 +299,17 @@ public class BlockChain {
return featureTriggers.get("newAssetPricingTimestamp");
}
// More complex getters for aspects that change by height or timestamp
public BigDecimal getRewardAtHeight(int ourHeight) {
// Scan through for reward at our height
for (int i = rewardsByHeight.size() - 1; i >= 0; --i)
if (rewardsByHeight.get(i).height <= ourHeight)
return rewardsByHeight.get(i).reward;
return null;
}
/** Validate blockchain config read from JSON */
private void validateConfig() {
if (this.genesisInfo == null) {

View File

@ -3,6 +3,7 @@ package org.qora.test.common;
import java.math.BigDecimal;
import org.qora.block.Block;
import org.qora.block.BlockChain;
import org.qora.data.block.BlockData;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
@ -12,7 +13,7 @@ public class BlockUtils {
public static BigDecimal getNextBlockReward(Repository repository) throws DataException {
int currentHeight = repository.getBlockRepository().getBlockchainHeight();
return Block.getRewardAtHeight(currentHeight + 1);
return BlockChain.getInstance().getRewardAtHeight(currentHeight + 1);
}
public static void orphanLastBlock(Repository repository) throws DataException {