Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unity rewardCalculator #1049

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ BlockContext createNewMiningBlockInternal(
// derive base block reward
BigInteger baseBlockReward =
this.chainConfiguration
.getRewardsCalculator()
.getRewardsCalculator(forkUtility.isUnityForkActive(block.getHeader().getNumber()))
.calculateReward(block.getHeader().getNumber());
return new BlockContext(block, baseBlockReward, totalTransactionFee);
}
Expand Down Expand Up @@ -1783,7 +1783,7 @@ private Map<AionAddress, BigInteger> addReward(Block block) {
Map<AionAddress, BigInteger> rewards = new HashMap<>();
BigInteger minerReward =
this.chainConfiguration
.getRewardsCalculator()
.getRewardsCalculator(forkUtility.isUnityForkActive(block.getHeader().getNumber()))
.calculateReward(block.getHeader().getNumber());
rewards.put(block.getCoinbase(), minerReward);

Expand Down Expand Up @@ -2480,6 +2480,13 @@ public boolean isUnityForkEnabledAtNextBlock() {
return forkUtility.isUnityForkActive(bestBlockNumber.get() + 1);
}

@Override
public BigInteger calculateBlockRewards(long block_number) {
return chainConfiguration
.getRewardsCalculator(forkUtility.isUnityForkActive(block_number))
.calculateReward(block_number);
}

/**
* A method for creating a new staking block template for the external staker.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import org.aion.equihash.OptimizedEquiValidator;
import org.aion.mcf.blockchain.BlockHeader.BlockSealType;
import org.aion.zero.impl.core.RewardsCalculatorAfterUnity;
import org.aion.zero.impl.core.UnityBlockDiffCalculator;
import org.aion.zero.impl.types.IBlockConstants;
import org.aion.zero.impl.core.IDifficultyCalculator;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class ChainConfiguration {
protected IDifficultyCalculator preUnityDifficultyCalculator;
protected IDifficultyCalculator unityDifficultyCalculator;
protected IRewardsCalculator rewardsCalculatorAdapter;
protected IRewardsCalculator rewardsCalculatorAfterUnity;
protected OptimizedEquiValidator equiValidator;

public ChainConfiguration(final Long monetaryUpdateBlkNum, final BigInteger initialSupply) {
Expand Down Expand Up @@ -85,6 +87,8 @@ public ChainConfiguration(
};
this.rewardsCalculatorAdapter = rewardsCalcInternal::calculateReward;

this.rewardsCalculatorAfterUnity = RewardsCalculatorAfterUnity::calculateReward;

unityDifficultyCalculator = unityCalc::calcDifficulty;
}

Expand All @@ -95,8 +99,8 @@ public IDifficultyCalculator getPreUnityDifficultyCalculator() {
return preUnityDifficultyCalculator;
}

public IRewardsCalculator getRewardsCalculator() {
return rewardsCalculatorAdapter;
public IRewardsCalculator getRewardsCalculator(boolean isAfterUnityFork) {
return isAfterUnityFork ? rewardsCalculatorAfterUnity : rewardsCalculatorAdapter;
}

private OptimizedEquiValidator getEquihashValidator() {
Expand Down
2 changes: 2 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/blockchain/UnityChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ Block createStakingBlockTemplate(
void loadBestMiningBlock();

boolean isUnityForkEnabledAtNextBlock();

BigInteger calculateBlockRewards(long block_number);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.aion.zero.impl.core;

import java.math.BigInteger;

/**
* The unity protocol block rewards is a fixed rewards equal to 4.5 AION.
*/
public class RewardsCalculatorAfterUnity {

private static final BigInteger fixedBlockRewards = BigInteger.valueOf(4_500_000_000_000_000_000L);

public static BigInteger calculateReward(long number) {
return fixedBlockRewards;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void testBlockContextCreation() {
// check that the correct amount was calculated
assertThat(
configuration
.getRewardsCalculator()
.getRewardsCalculator(false)
.calculateReward(context.block.getHeader().getNumber()))
.isEqualTo(context.baseBlockReward);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChainConfigurationTest {

private static final Logger log = LoggerFactory.getLogger(ChainConfigurationTest.class);

@Mock A0BlockHeader header;

@Before
Expand Down Expand Up @@ -95,11 +91,11 @@ public void testRampUpFunctionBoundaries() {
// UPPER BOUND
when(header.getNumber()).thenReturn(upperBound);
BigInteger blockReward259200 =
config.getRewardsCalculator().calculateReward(header.getNumber());
config.getRewardsCalculator(false).calculateReward(header.getNumber());

when(header.getNumber()).thenReturn(upperBound + 1);
BigInteger blockReward259201 =
config.getRewardsCalculator().calculateReward(header.getNumber());
config.getRewardsCalculator(false).calculateReward(header.getNumber());

// check that at the upper bound of our range (which is not included) blockReward is capped
assertThat(blockReward259200).isEqualTo(new BigInteger("1497989283243258292"));
Expand All @@ -109,17 +105,49 @@ public void testRampUpFunctionBoundaries() {

// check that for an arbitrarily large block, the block reward is still the same
when(header.getNumber()).thenReturn(upperBound + 100000);
BigInteger blockUpper = config.getRewardsCalculator().calculateReward(header.getNumber());
BigInteger blockUpper = config.getRewardsCalculator(false).calculateReward(header.getNumber());
assertThat(blockUpper).isEqualTo(config.getConstants().getBlockReward());

// LOWER BOUNDS
when(header.getNumber()).thenReturn(0l);
BigInteger blockReward0 = config.getRewardsCalculator().calculateReward(header.getNumber());
BigInteger blockReward0 = config.getRewardsCalculator(false).calculateReward(header.getNumber());
assertThat(blockReward0).isEqualTo(new BigInteger("748994641621655092"));

// first block (should have gas value of increment)
when(header.getNumber()).thenReturn(1l);
BigInteger blockReward1 = config.getRewardsCalculator().calculateReward(header.getNumber());
BigInteger blockReward1 = config.getRewardsCalculator(false).calculateReward(header.getNumber());
assertThat(blockReward1).isEqualTo(increment);
}

@Test
public void testCalculatorReturnsByFork() {

long upperBound = 259200L;
ChainConfiguration config = new ChainConfiguration();
BigInteger increment =
config.getConstants()
.getBlockReward()
.subtract(config.getConstants().getRampUpStartValue())
.divide(BigInteger.valueOf(upperBound))
.add(config.getConstants().getRampUpStartValue());

// UPPER BOUND
when(header.getNumber()).thenReturn(upperBound);
BigInteger blockReward259200 =
config.getRewardsCalculator(false).calculateReward(header.getNumber());

// check that at the upper bound of our range (which is not included) blockReward is capped
assertThat(blockReward259200).isEqualTo(new BigInteger("1497989283243258292"));

BigInteger blockRewardAfterUnityFork =
config.getRewardsCalculator(true).calculateReward(header.getNumber());

assertThat(blockRewardAfterUnityFork).isEqualTo(new BigInteger("4500000000000000000"));

BigInteger blockRewardAfterUnityForkPlusOne =
config.getRewardsCalculator(true).calculateReward(header.getNumber() + 1);

assertThat(blockRewardAfterUnityForkPlusOne).isEqualTo(new BigInteger("4500000000000000000"));

}
}
7 changes: 1 addition & 6 deletions modApiServer/src/org/aion/api/server/pb/ApiAion0.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,12 +1002,7 @@ public byte[] process(byte[] request, byte[] socketId) {
BigInteger reward;
try {
req = Message.req_getBlockReward.parseFrom(data);
long num = req.getBlockNumber();
reward =
((AionBlockchainImpl) ac.getBlockchain())
.getChainConfiguration()
.getRewardsCalculator()
.calculateReward(num);
reward = ac.getBlockchain().calculateBlockRewards(req.getBlockNumber());
} catch (Exception e) {
LOG.error("ApiAion0.process.getBlockReward exception: [{}]", e);
return ApiUtil.toReturnHeader(
Expand Down
31 changes: 9 additions & 22 deletions modApiServer/src/org/aion/api/server/rpc/ApiWeb3Aion.java
Original file line number Diff line number Diff line change
Expand Up @@ -1950,12 +1950,7 @@ private JSONObject computeMetrics() {
nrgLimitAccumulator.add(new BigInteger(Long.toString(b.getNrgLimit())));
txnCount += b.getTransactionsList().size();
}

BigInteger lastBlkReward =
((AionBlockchainImpl) ac.getBlockchain())
.getChainConfiguration()
.getRewardsCalculator()
.calculateReward(b.getHeader().getNumber());
BigInteger lastBlkReward = (b == null ? null : ac.getBlockchain().calculateBlockRewards(b.getNumber()));

double blkTime = 0;
double hashRate = 0;
Expand Down Expand Up @@ -1995,7 +1990,7 @@ private JSONObject computeMetrics() {
metrics.put("averageBlockTime", blkTime);
metrics.put("hashRate", hashRate);
metrics.put("transactionPerSecond", txnPerSec);
metrics.put("lastBlockReward", lastBlkReward);
metrics.put("lastBlockReward", (lastBlkReward == null ? "null" : lastBlkReward));
metrics.put("targetBlockTime", 10);
metrics.put("blockWindow", OPS_RECENT_ENTITY_COUNT);

Expand Down Expand Up @@ -2265,10 +2260,12 @@ public RpcMsg ops_getBlock(Object _params) {

Long bn = this.parseBnOrId(_bnOrHash);

AionBlockchainImpl blockchainImpl = (AionBlockchainImpl) this.ac.getBlockchain();

// user passed a Long block number
if (bn != null) {
if (bn >= 0) {
block = this.ac.getBlockchain().getBlockByNumber(bn);
block = blockchainImpl.getBlockByNumber(bn);
if (block == null) {
return new RpcMsg(JSONObject.NULL);
}
Expand All @@ -2279,13 +2276,13 @@ public RpcMsg ops_getBlock(Object _params) {

// see if the user passed in a hash
if (block == null) {
block = this.ac.getBlockchain().getBlockByHash(ByteUtil.hexStringToBytes(_bnOrHash));
block = blockchainImpl.getBlockByHash(ByteUtil.hexStringToBytes(_bnOrHash));
if (block == null) {
return new RpcMsg(JSONObject.NULL);
}
}

Block mainBlock = this.ac.getBlockchain().getBlockByNumber(block.getNumber());
Block mainBlock = blockchainImpl.getBlockByNumber(block.getNumber());
if (mainBlock == null) {
return new RpcMsg(JSONObject.NULL);
}
Expand All @@ -2295,12 +2292,7 @@ public RpcMsg ops_getBlock(Object _params) {
}

// ok so now we have a mainchain block

BigInteger blkReward =
((AionBlockchainImpl) ac.getBlockchain())
.getChainConfiguration()
.getRewardsCalculator()
.calculateReward(block.getHeader().getNumber());
BigInteger blkReward = blockchainImpl.calculateBlockRewards(block.getHeader().getNumber());
BigInteger totalDiff =
this.ac.getAionHub().getTotalDifficultyForHash(block.getHash());

Expand Down Expand Up @@ -2991,12 +2983,7 @@ private Long parseBnOrId(String _bnOrId) {


private RpcMsg rpcBlockDetailsFromBlock(Block block){

BigInteger blkReward =
((AionBlockchainImpl) ac.getBlockchain())
.getChainConfiguration()
.getRewardsCalculator()
.calculateReward(block.getHeader().getNumber());
BigInteger blkReward = ac.getBlockchain().calculateBlockRewards(block.getHeader().getNumber());
BigInteger totalDiff =
this.ac.getAionHub().getTotalDifficultyForHash(block.getHash());

Expand Down