Search in sources :

Example 1 with IDifficultyCalculator

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);
}
Also used : BlockContext(org.aion.zero.impl.types.BlockContext) MiningBlock(org.aion.zero.impl.types.MiningBlock) IDifficultyCalculator(org.aion.zero.impl.core.IDifficultyCalculator) VmFatalException(org.aion.zero.impl.vm.common.VmFatalException) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Block(org.aion.zero.impl.types.Block) BlockDetailsValidator.isValidBlock(org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock) GenesisStakingBlock(org.aion.zero.impl.types.GenesisStakingBlock) RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) EventBlock(org.aion.evtmgr.impl.evt.EventBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock) BigInteger(java.math.BigInteger) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader)

Aggregations

BigInteger (java.math.BigInteger)1 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)1 IDifficultyCalculator (org.aion.zero.impl.core.IDifficultyCalculator)1 Block (org.aion.zero.impl.types.Block)1 BlockContext (org.aion.zero.impl.types.BlockContext)1 BlockHeader (org.aion.zero.impl.types.BlockHeader)1 GenesisStakingBlock (org.aion.zero.impl.types.GenesisStakingBlock)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)1 RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)1 StakingBlock (org.aion.zero.impl.types.StakingBlock)1 StakingBlockHeader (org.aion.zero.impl.types.StakingBlockHeader)1 BlockDetailsValidator.isValidBlock (org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock)1 VmFatalException (org.aion.zero.impl.vm.common.VmFatalException)1