Search in sources :

Example 6 with Bloom

use of org.aion.base.Bloom in project aion by aionnetwork.

the class AionBlockchainImpl method blockPreSeal.

private BigInteger blockPreSeal(BlockHeader parentHdr, Block block) {
    lock.lock();
    try {
        // Begin execution phase
        pushState(parentHdr.getHash());
        track = repository.startTracking();
        RetValidPreBlock preBlock = generatePreBlock(block);
        track.flushTo(repository, true);
        // Calculate the gas used for the included transactions
        long totalEnergyUsed = 0;
        BigInteger totalTransactionFee = BigInteger.ZERO;
        for (AionTxExecSummary summary : preBlock.summaries) {
            totalEnergyUsed = totalEnergyUsed + summary.getNrgUsed().longValueExact();
            totalTransactionFee = totalTransactionFee.add(summary.getFee());
        }
        byte[] stateRoot = getRepository().getRoot();
        popState();
        // End execution phase
        Bloom logBloom = new Bloom();
        for (AionTxReceipt receipt : preBlock.receipts) {
            logBloom.or(receipt.getBloomFilter());
        }
        block.updateTransactionAndState(preBlock.txs, calcTxTrieRoot(preBlock.txs), stateRoot, logBloom.getBloomFilterBytes(), calcReceiptsTrie(preBlock.receipts), totalEnergyUsed);
        return totalTransactionFee;
    } catch (IllegalStateException e) {
        LOG.error("blockPreSeal failed.", e);
        popState();
        return null;
    } finally {
        lock.unlock();
    }
}
Also used : RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) AionTxExecSummary(org.aion.base.AionTxExecSummary) Bloom(org.aion.base.Bloom) BigInteger(java.math.BigInteger) AionTxReceipt(org.aion.base.AionTxReceipt)

Example 7 with Bloom

use of org.aion.base.Bloom in project aion by aionnetwork.

the class BlockDetailsValidatorTest method validateBlockHasRejectedTransaction.

@Test
public void validateBlockHasRejectedTransaction() {
    byte[] receiptTrieEncoded = new byte[32];
    Arrays.fill(receiptTrieEncoded, (byte) 1);
    // The block initially has rejected transaction
    when(mockBlock.getNumber()).thenReturn(1L);
    when(mockBlock.getHeader()).thenReturn(mockBlockHeader);
    when(mockBlock.getLogBloom()).thenReturn(new byte[Bloom.SIZE]);
    when(mockBlockHeader.getEnergyConsumed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxExecSummary.isRejected()).thenReturn(true);
    when(mockTxExecSummary.getReceipt()).thenReturn(mockTxReceipt);
    when(mockTxReceipt.getEnergyUsed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxReceipt.getReceiptTrieEncoded()).thenReturn(receiptTrieEncoded);
    when(mockTxReceipt.getBloomFilter()).thenReturn(new Bloom());
    List<AionTxExecSummary> summaryList = new ArrayList<>();
    summaryList.add(mockTxExecSummary);
    List<AionTxReceipt> receiptList = new ArrayList<>();
    receiptList.add(mockTxReceipt);
    byte[] calculatedTrieroot = BlockUtil.calcReceiptsTrie(receiptList);
    when(mockBlock.getReceiptsRoot()).thenReturn(calculatedTrieroot);
    Truth.assertThat(isValidBlock(mockBlock, summaryList, receiptList, true, AionLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))).isTrue();
    // The block has rejected transaction after nonce hard fork active
    when(mockBlock.getNumber()).thenReturn(2L);
    Truth.assertThat(isValidBlock(mockBlock, summaryList, receiptList, false, AionLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))).isFalse();
}
Also used : Bloom(org.aion.base.Bloom) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 8 with Bloom

use of org.aion.base.Bloom in project aion by aionnetwork.

the class BlockDetailsValidatorTest method validateBlockHasInvalidEnergyUsed.

@Test
public void validateBlockHasInvalidEnergyUsed() {
    byte[] receiptTrieEncoded = new byte[32];
    Arrays.fill(receiptTrieEncoded, (byte) 1);
    // The block has invalid total energy use in the block header
    when(mockBlock.getNumber()).thenReturn(2L);
    when(mockBlock.getHeader()).thenReturn(mockBlockHeader);
    when(mockBlock.getLogBloom()).thenReturn(new byte[Bloom.SIZE]);
    when(mockBlockHeader.getEnergyConsumed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT + 1);
    when(mockTxExecSummary.isRejected()).thenReturn(false);
    when(mockTxExecSummary.getReceipt()).thenReturn(mockTxReceipt);
    when(mockTxExecSummary.getTransaction()).thenReturn(mockTransaction);
    when(mockTxReceipt.getEnergyUsed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxReceipt.getReceiptTrieEncoded()).thenReturn(receiptTrieEncoded);
    when(mockTxReceipt.getBloomFilter()).thenReturn(new Bloom());
    List<AionTxExecSummary> summaryList = new ArrayList<>();
    summaryList.add(mockTxExecSummary);
    List<AionTxReceipt> receiptList = new ArrayList<>();
    receiptList.add(mockTxReceipt);
    byte[] calculatedTrieroot = BlockUtil.calcReceiptsTrie(receiptList);
    when(mockBlock.getReceiptsRoot()).thenReturn(calculatedTrieroot);
    Truth.assertThat(isValidBlock(mockBlock, summaryList, receiptList, false, AionLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))).isFalse();
}
Also used : Bloom(org.aion.base.Bloom) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 9 with Bloom

use of org.aion.base.Bloom in project aion by aionnetwork.

the class BlockDetailsValidatorTest method validateBlockHasInvalidReceiptRoot.

@Test
public void validateBlockHasInvalidReceiptRoot() {
    byte[] receiptTrieEncoded = new byte[32];
    Arrays.fill(receiptTrieEncoded, (byte) 1);
    // The block has invalid receipt root in the block header
    when(mockBlock.getNumber()).thenReturn(2L);
    when(mockBlock.getHeader()).thenReturn(mockBlockHeader);
    when(mockBlock.getLogBloom()).thenReturn(new byte[Bloom.SIZE]);
    when(mockBlockHeader.getEnergyConsumed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxExecSummary.isRejected()).thenReturn(false);
    when(mockTxExecSummary.getReceipt()).thenReturn(mockTxReceipt);
    when(mockTxExecSummary.getTransaction()).thenReturn(mockTransaction);
    when(mockTxReceipt.getEnergyUsed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxReceipt.getReceiptTrieEncoded()).thenReturn(receiptTrieEncoded);
    when(mockTxReceipt.getBloomFilter()).thenReturn(new Bloom());
    when(mockTxReceipt.getTransaction()).thenReturn(mockTransaction);
    List<AionTxExecSummary> summaryList = new ArrayList<>();
    summaryList.add(mockTxExecSummary);
    List<AionTxReceipt> receiptList = new ArrayList<>();
    receiptList.add(mockTxReceipt);
    when(mockBlock.getReceiptsRoot()).thenReturn(ConstantUtil.EMPTY_TRIE_HASH);
    Truth.assertThat(isValidBlock(mockBlock, summaryList, receiptList, false, AionLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))).isFalse();
}
Also used : Bloom(org.aion.base.Bloom) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 10 with Bloom

use of org.aion.base.Bloom in project aion by aionnetwork.

the class BlockDetailsValidatorTest method validateBlockHasInvalidLogBloom.

@Test
public void validateBlockHasInvalidLogBloom() {
    byte[] receiptTrieEncoded = new byte[32];
    Arrays.fill(receiptTrieEncoded, (byte) 1);
    byte[] txBloom = new byte[Bloom.SIZE];
    Arrays.fill(txBloom, (byte) 1);
    // The block has invalid receipt root in the block header
    when(mockBlock.getNumber()).thenReturn(2L);
    when(mockBlock.getHeader()).thenReturn(mockBlockHeader);
    when(mockBlock.getLogBloom()).thenReturn(new byte[Bloom.SIZE]);
    when(mockBlockHeader.getEnergyConsumed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxExecSummary.isRejected()).thenReturn(false);
    when(mockTxExecSummary.getReceipt()).thenReturn(mockTxReceipt);
    when(mockTxExecSummary.getTransaction()).thenReturn(mockTransaction);
    when(mockTxReceipt.getEnergyUsed()).thenReturn((long) Constants.NRG_TRANSACTION_DEFAULT);
    when(mockTxReceipt.getReceiptTrieEncoded()).thenReturn(receiptTrieEncoded);
    when(mockTxReceipt.getBloomFilter()).thenReturn(new Bloom(txBloom));
    when(mockTxReceipt.getTransaction()).thenReturn(mockTransaction);
    List<AionTxExecSummary> summaryList = new ArrayList<>();
    summaryList.add(mockTxExecSummary);
    List<AionTxReceipt> receiptList = new ArrayList<>();
    receiptList.add(mockTxReceipt);
    byte[] calculatedTrieroot = BlockUtil.calcReceiptsTrie(receiptList);
    when(mockBlock.getReceiptsRoot()).thenReturn(calculatedTrieroot);
    Truth.assertThat(isValidBlock(mockBlock, summaryList, receiptList, false, AionLoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))).isFalse();
}
Also used : Bloom(org.aion.base.Bloom) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Aggregations

Bloom (org.aion.base.Bloom)14 Test (org.junit.Test)9 AionTxReceipt (org.aion.base.AionTxReceipt)8 ArrayList (java.util.ArrayList)5 AionTxExecSummary (org.aion.base.AionTxExecSummary)5 BlockchainIntegrationTest (org.aion.zero.impl.blockchain.BlockchainIntegrationTest)5 AionTransaction (org.aion.base.AionTransaction)2 AionAddress (org.aion.types.AionAddress)2 Log (org.aion.types.Log)2 BigInteger (java.math.BigInteger)1 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)1 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)1 Block (org.aion.zero.impl.types.Block)1 RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)1