use of org.aion.zero.impl.core.IDifficultyCalculator in project aion by aionnetwork.
the class AionBlockchainImpl method createNewMiningBlockInternal.
BlockContext createNewMiningBlockInternal(Block parent, List<AionTransaction> txs, boolean waitUntilBlockTime, long currTimeSeconds) {
BlockHeader parentHdr = parent.getHeader();
long time = currTimeSeconds;
if (parentHdr.getTimestamp() >= time) {
time = parentHdr.getTimestamp() + 1;
while (waitUntilBlockTime && TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) <= time) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
}
long energyLimit = this.energyLimitStrategy.getEnergyLimit(parentHdr);
MiningBlock block;
try {
MiningBlockHeader.Builder headerBuilder = MiningBlockHeader.Builder.newInstance().withParentHash(parent.getHash()).withCoinbase(minerCoinbase).withNumber(parentHdr.getNumber() + 1).withTimestamp(time).withExtraData(minerExtraData).withTxTrieRoot(calcTxTrieRoot(txs)).withEnergyLimit(energyLimit).withDefaultStateRoot().withDefaultReceiptTrieRoot().withDefaultLogsBloom().withDefaultDifficulty().withDefaultNonce().withDefaultSolution();
block = new MiningBlock(headerBuilder.build(), txs);
} catch (Exception e) {
LOG.error("Construct new mining block header exception:", e);
return null;
}
BlockHeader parentMiningBlock;
BlockHeader parentMiningBlocksParent = null;
byte[] newDiff;
IDifficultyCalculator diffCalculator;
// so we use a strict greater than here
if (forkUtility.isUnityForkActive(block.getNumber())) {
if (parentHdr.getSealType() == Seal.PROOF_OF_WORK) {
LOG.warn("Tried to create 2 PoW blocks in a row");
return null;
} else {
Block[] blockFamily = repository.getBlockStore().getTwoGenerationBlocksByHashWithInfo(parentHdr.getParentHash());
Objects.requireNonNull(blockFamily[0]);
parentMiningBlock = blockFamily[0].getHeader();
parentMiningBlocksParent = blockFamily[1].getHeader();
diffCalculator = chainConfiguration.getUnityDifficultyCalculator();
}
} else {
parentMiningBlock = parentHdr;
if (!parentMiningBlock.isGenesis()) {
parentMiningBlocksParent = getParent(parentMiningBlock).getHeader();
}
diffCalculator = chainConfiguration.getPreUnityDifficultyCalculator();
}
newDiff = ByteUtil.bigIntegerToBytes(diffCalculator.calculateDifficulty(parentMiningBlock, parentMiningBlocksParent), DIFFICULTY_BYTES);
block.updateHeaderDifficulty(newDiff);
BigInteger totalTransactionFee = blockPreSeal(parentHdr, block);
if (totalTransactionFee == null) {
return null;
}
// derive base block reward
BigInteger baseBlockReward;
if (forkUtility.isSignatureSwapForkActive(block.getNumber())) {
baseBlockReward = TimeVaryingRewardsCalculator.calculateReward(block.getTimestamp() - parentHdr.getTimestamp());
} else {
baseBlockReward = this.chainConfiguration.getRewardsCalculatorBeforeSignatureSchemeSwap(forkUtility.isUnityForkActive(block.getNumber())).calculateReward(block.getNumber());
}
return new BlockContext(block, baseBlockReward, totalTransactionFee);
}
Aggregations