Search in sources :

Example 1 with RetValidPreBlock

use of org.aion.zero.impl.types.RetValidPreBlock in project aion by aionnetwork.

the class AionBlockchainImpl method generatePreBlock.

/**
 * For generating the necessary transactions for a block
 *
 * @param block
 * @return
 */
private RetValidPreBlock generatePreBlock(IAionBlock block) {
    long saveTime = System.nanoTime();
    List<AionTxReceipt> receipts = new ArrayList<>();
    List<AionTxExecSummary> summaries = new ArrayList<>();
    List<AionTransaction> transactions = new ArrayList<>();
    long energyRemaining = block.getNrgLimit();
    for (AionTransaction tx : block.getTransactionsList()) {
        TransactionExecutor executor = new TransactionExecutor(tx, block, track, false, energyRemaining);
        AionTxExecSummary summary = executor.execute();
        if (!summary.isRejected()) {
            track.flush();
            AionTxReceipt receipt = summary.getReceipt();
            receipt.setPostTxState(repository.getRoot());
            receipt.setTransaction(tx);
            // otherwise, assuming we don't have timeouts, add the
            // transaction
            transactions.add(tx);
            receipts.add(receipt);
            summaries.add(summary);
            energyRemaining -= receipt.getEnergyUsed();
        }
    }
    Map<Address, BigInteger> rewards = addReward(block, summaries);
    track.flush();
    long totalTime = System.nanoTime() - saveTime;
    chainStats.addBlockExecTime(totalTime);
    return new RetValidPreBlock(transactions, rewards, receipts, summaries);
}
Also used : RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) Address(org.aion.base.type.Address) TransactionExecutor(org.aion.vm.TransactionExecutor) BigInteger(java.math.BigInteger)

Example 2 with RetValidPreBlock

use of org.aion.zero.impl.types.RetValidPreBlock in project aion by aionnetwork.

the class AionBlockchainImpl method createNewBlock.

public synchronized AionBlock createNewBlock(AionBlock parent, List<AionTransaction> txs, boolean waitUntilBlockTime) {
    long time = System.currentTimeMillis() / THOUSAND_MS;
    if (parent.getTimestamp() >= time) {
        time = parent.getTimestamp() + 1;
        while (waitUntilBlockTime && System.currentTimeMillis() / THOUSAND_MS <= time) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
    long energyLimit = this.chainConfiguration.calcEnergyLimit(parent.getHeader());
    A0BlockHeader.Builder headerBuilder = new A0BlockHeader.Builder();
    headerBuilder.withParentHash(parent.getHash()).withCoinbase(minerCoinbase).withNumber(parent.getNumber() + 1).withTimestamp(time).withExtraData(minerExtraData).withTxTrieRoot(calcTxTrie(txs)).withEnergyLimit(energyLimit);
    AionBlock block = new AionBlock(headerBuilder.build(), txs);
    block.getHeader().setDifficulty(ByteUtil.bigIntegerToBytes(this.chainConfiguration.getDifficultyCalculator().calculateDifficulty(block.getHeader(), parent.getHeader()), DIFFICULTY_BYTES));
    /*
         * Begin execution phase
         */
    pushState(parent.getHash());
    track = repository.startTracking();
    track.rollback();
    RetValidPreBlock preBlock = generatePreBlock(block);
    /*
         * Calculate the gas used for the included transactions
         */
    long totalEnergyUsed = 0;
    for (AionTxExecSummary summary : preBlock.summaries) {
        totalEnergyUsed = totalEnergyUsed + summary.getNrgUsed().longValueExact();
    }
    byte[] stateRoot = getRepository().getRoot();
    popState();
    /*
         * End execution phase
         */
    Bloom logBloom = new Bloom();
    for (AionTxReceipt receipt : preBlock.receipts) {
        logBloom.or(receipt.getBloomFilter());
    }
    block.seal(preBlock.txs, calcTxTrie(preBlock.txs), stateRoot, logBloom.getData(), calcReceiptsTrie(preBlock.receipts), totalEnergyUsed);
    return block;
}
Also used : RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) Bloom(org.aion.mcf.vm.types.Bloom) AionBlock(org.aion.zero.impl.types.AionBlock)

Aggregations

RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)2 BigInteger (java.math.BigInteger)1 Address (org.aion.base.type.Address)1 Bloom (org.aion.mcf.vm.types.Bloom)1 TransactionExecutor (org.aion.vm.TransactionExecutor)1 AionBlock (org.aion.zero.impl.types.AionBlock)1