Search in sources :

Example 6 with AionBlockSummary

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;
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary)

Example 7 with AionBlockSummary

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;
}
Also used : EventBlock(org.aion.evtmgr.impl.evt.EventBlock) ImportResult(org.aion.mcf.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) IEvent(org.aion.evtmgr.IEvent) BigInteger(java.math.BigInteger)

Aggregations

AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 BigInteger (java.math.BigInteger)3 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Collectors (java.util.stream.Collectors)1 LongStream (java.util.stream.LongStream)1 ApiAion (org.aion.api.server.ApiAion)1 ApiUtil (org.aion.api.server.ApiUtil)1 IApiAion (org.aion.api.server.IApiAion)1 org.aion.api.server.types (org.aion.api.server.types)1 org.aion.base.type (org.aion.base.type)1 Address (org.aion.base.type.Address)1 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)1 ByteUtil (org.aion.base.util.ByteUtil)1