use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class AionBlockchainImpl method add.
@Override
public synchronized AionBlockSummary add(AionBlock block) {
// typical use without rebuild
AionBlockSummary summary = add(block, false);
if (summary != null) {
List<AionTxReceipt> receipts = summary.getReceipts();
updateTotalDifficulty(block);
summary.setTotalDifficulty(getTotalDifficulty());
storeBlock(block, receipts);
flush();
}
return summary;
}
use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class AionBlockchainImpl method applyBlock.
private AionBlockSummary applyBlock(IAionBlock block) {
long saveTime = System.nanoTime();
List<AionTxReceipt> receipts = new ArrayList<>();
List<AionTxExecSummary> summaries = new ArrayList<>();
for (AionTransaction tx : block.getTransactionsList()) {
TransactionExecutor executor = new TransactionExecutor(tx, block, track);
AionTxExecSummary summary = executor.execute();
track.flush();
AionTxReceipt receipt = summary.getReceipt();
receipt.setPostTxState(repository.getRoot());
receipts.add(receipt);
summaries.add(summary);
}
Map<Address, BigInteger> rewards = addReward(block, summaries);
long totalTime = System.nanoTime() - saveTime;
chainStats.addBlockExecTime(totalTime);
return new AionBlockSummary(block, rewards, receipts, summaries);
}
use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class ApiAion0 method onBlock.
protected void onBlock(AionBlockSummary cbs) {
Set<Long> keys = installedFilters.keySet();
for (Long key : keys) {
Fltr fltr = installedFilters.get(key);
if (fltr.isExpired()) {
LOG.debug("<fltr key={} expired removed>", key);
installedFilters.remove(key);
} else {
List<AionTxReceipt> txrs = cbs.getReceipts();
if (fltr.getType() == Fltr.Type.EVENT && !Optional.ofNullable(txrs).orElse(Collections.emptyList()).isEmpty()) {
FltrCt _fltr = (FltrCt) fltr;
for (AionTxReceipt txr : txrs) {
AionTransaction tx = txr.getTransaction();
Address contractAddress = Optional.ofNullable(tx.getTo()).orElse(tx.getContractAddress());
Integer cnt = 0;
txr.getLogInfoList().forEach(bi -> bi.getTopics().forEach(lg -> {
if (_fltr.isFor(contractAddress, ByteUtil.toHexString(lg))) {
IBlock<AionTransaction, ?> blk = (cbs).getBlock();
List<AionTransaction> txList = blk.getTransactionsList();
int insideCnt = 0;
for (AionTransaction t : txList) {
if (Arrays.equals(t.getHash(), tx.getHash())) {
break;
}
insideCnt++;
}
EvtContract ec = new EvtContract(bi.getAddress().toBytes(), bi.getData(), blk.getHash(), blk.getNumber(), cnt, ByteUtil.toHexString(lg), false, insideCnt, tx.getHash());
_fltr.add(ec);
}
}));
}
}
}
}
}
use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class FltrLg method onBlock.
// -------------------------------------------------------------------------------
@Override
public boolean onBlock(IBlockSummary bs) {
List<AionTxReceipt> receipts = ((AionBlockSummary) bs).getReceipts();
IBlock blk = bs.getBlock();
if (matchBloom(new Bloom(((IAionBlock) blk).getLogBloom()))) {
int txIndex = 0;
for (AionTxReceipt receipt : receipts) {
ITransaction tx = receipt.getTransaction();
if (matchesContractAddress(tx.getTo().toBytes())) {
if (matchBloom(receipt.getBloomFilter())) {
int logIndex = 0;
for (Log logInfo : receipt.getLogInfoList()) {
if (matchBloom(logInfo.getBloom()) && matchesExactly(logInfo)) {
add(new EvtLg(new TxRecptLg(logInfo, blk, txIndex, receipt.getTransaction(), logIndex, true)));
}
logIndex++;
}
}
}
txIndex++;
}
}
return true;
}
use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class AionBlockchainImpl method add.
public synchronized AionBlockSummary add(AionBlock block, boolean rebuild) {
if (block == null) {
LOG.error("Attempting to add NULL block.");
return null;
}
if (exitOn < block.getNumber()) {
LOG.error("Exiting after block.number: ", bestBlock.getNumber());
flush();
System.exit(-1);
}
if (!isValid(block)) {
LOG.error("Attempting to add INVALID block.");
return null;
}
track = repository.startTracking();
byte[] origRoot = repository.getRoot();
// (if not reconstructing old blocks) keep chain continuity
if (!rebuild && !Arrays.equals(bestBlock.getHash(), block.getParentHash())) {
LOG.error("Attempting to add NON-SEQUENTIAL block.");
return null;
}
AionBlockSummary summary = processBlock(block);
List<AionTxReceipt> receipts = summary.getReceipts();
// Sanity checks
byte[] receiptHash = block.getReceiptsRoot();
byte[] receiptListHash = calcReceiptsTrie(receipts);
if (!Arrays.equals(receiptHash, receiptListHash)) {
if (LOG.isWarnEnabled()) {
LOG.warn("Block's given Receipt Hash doesn't match: {} != {}", receiptHash, receiptListHash);
LOG.warn("Calculated receipts: " + receipts);
}
return null;
}
byte[] logBloomHash = block.getLogBloom();
byte[] logBloomListHash = calcLogBloom(receipts);
if (!Arrays.equals(logBloomHash, logBloomListHash)) {
if (LOG.isWarnEnabled())
LOG.warn("Block's given logBloom Hash doesn't match: {} != {}", ByteUtil.toHexString(logBloomHash), ByteUtil.toHexString(logBloomListHash));
track.rollback();
return null;
}
if (!rebuild) {
byte[] blockStateRootHash = block.getStateRoot();
byte[] worldStateRootHash = repository.getRoot();
if (!Arrays.equals(blockStateRootHash, worldStateRootHash)) {
LOG.warn("BLOCK: State conflict or received invalid block. block: {} worldstate {} mismatch", block.getNumber(), worldStateRootHash);
LOG.warn("Conflict block dump: {}", Hex.toHexString(block.getEncoded()));
track.rollback();
// block is bad so 'rollback' the state root to the original
// state
((AionRepositoryImpl) repository).setRoot(origRoot);
if (this.config.getExitOnBlockConflict()) {
chainStats.lostConsensus();
System.out.println("CONFLICT: BLOCK #" + block.getNumber() + ", dump: " + Hex.toHexString(block.getEncoded()));
System.exit(1);
} else {
return null;
}
}
}
// update corresponding account with the new balance
track.flush();
if (rebuild) {
for (int i = 0; i < receipts.size(); i++) {
transactionStore.put(new AionTxInfo(receipts.get(i), block.getHash(), i));
}
((AionRepositoryImpl) repository).commitBlock(block.getHeader());
if (LOG.isDebugEnabled())
LOG.debug("Block rebuilt: number: {}, hash: {}, TD: {}", block.getNumber(), block.getShortHash(), totalDifficulty);
}
return summary;
}
Aggregations