use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class AionBlockchainImpl method tryConnectAndFork.
/**
* Not thread safe, currently only run in {@link #tryToConnect(AionBlock)},
* assumes that the environment is already locked
*
* @param block
* @return
*/
private AionBlockSummary tryConnectAndFork(final AionBlock block) {
State savedState = pushState(block.getParentHash());
this.fork = true;
final AionBlockSummary summary;
try {
// LOG.info("block " + block.toString());
// FIXME: adding block with no option for flush
summary = add(block);
if (summary == null) {
return null;
}
} catch (Throwable th) {
LOG.error("Unexpected error: ", th);
return null;
} finally {
this.fork = false;
}
if (isMoreThan(this.totalDifficulty, savedState.savedTD)) {
if (LOG.isInfoEnabled())
LOG.info("branching: from = {}/{}, to = {}/{}", savedState.savedBest.getNumber(), Hex.toHexString(savedState.savedBest.getHash()), block.getNumber(), Hex.toHexString(block.getHash()));
// main branch become this branch
// cause we proved that total difficulty
// is greater
getBlockStore().reBranch(block);
// The main repository rebranch
this.repository = savedState.savedRepo;
this.repository.syncToRoot(block.getStateRoot());
// flushing
flush();
dropState();
} else {
// Stay on previous branch
popState();
}
return summary;
}
use of org.aion.zero.impl.types.AionBlockSummary in project aion by aionnetwork.
the class AionBlockchainImpl method tryToConnect.
public synchronized ImportResult tryToConnect(final AionBlock block) {
long currentTimestamp = System.currentTimeMillis() / THOUSAND_MS;
if (block.getTimestamp() > (currentTimestamp + this.chainConfiguration.getConstants().getClockDriftBufferTime()))
return INVALID_BLOCK;
if (LOG.isDebugEnabled()) {
LOG.debug("Try connect block hash: {}, number: {}", Hex.toHexString(block.getHash()).substring(0, 6), block.getNumber());
}
if (getBlockStore().getMaxNumber() >= block.getNumber() && getBlockStore().isBlockExist(block.getHash())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Block already exist hash: {}, number: {}", Hex.toHexString(block.getHash()).substring(0, 6), block.getNumber());
}
// retry of well known block
return EXIST;
}
final ImportResult ret;
// The simple case got the block
// to connect to the main chain
final AionBlockSummary summary;
if (bestBlock.isParentOf(block)) {
summary = add(block);
ret = summary == null ? INVALID_BLOCK : IMPORTED_BEST;
} else {
if (getBlockStore().isBlockExist(block.getParentHash())) {
BigInteger oldTotalDiff = getTotalDifficulty();
summary = tryConnectAndFork(block);
ret = summary == null ? INVALID_BLOCK : (isMoreThan(getTotalDifficulty(), oldTotalDiff) ? IMPORTED_BEST : IMPORTED_NOT_BEST);
} else {
summary = null;
ret = NO_PARENT;
}
}
if (ret.isSuccessful()) {
if (this.evtMgr != null) {
IEvent evtOnBlock = new EventBlock(EventBlock.CALLBACK.ONBLOCK0);
evtOnBlock.setFuncArgs(Collections.singletonList(summary));
this.evtMgr.newEvent(evtOnBlock);
IEvent evtTrace = new EventBlock(EventBlock.CALLBACK.ONTRACE0);
String str = String.format("Block chain size: [ %d ]", this.getSizeInternal());
evtTrace.setFuncArgs(Collections.singletonList(str));
this.evtMgr.newEvent(evtTrace);
if (ret == IMPORTED_BEST) {
if (LOG.isTraceEnabled()) {
LOG.trace("IMPORTED_BEST");
}
IEvent evtOnBest = new EventBlock(EventBlock.CALLBACK.ONBEST0);
evtOnBest.setFuncArgs(Arrays.asList(block, summary.getReceipts()));
this.evtMgr.newEvent(evtOnBest);
}
}
}
return ret;
}
Aggregations