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();
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations