Search in sources :

Example 11 with IEvent

use of org.aion.evtmgr.IEvent 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)

Example 12 with IEvent

use of org.aion.evtmgr.IEvent in project aion by aionnetwork.

the class AionPendingStateImpl method fireTxUpdate.

private void fireTxUpdate(AionTxReceipt txReceipt, PendingTransactionState state, IAionBlock block) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("PendingTransactionUpdate: (Tot: %3s) %12s : %s %8s %s [%s]", getPendingTxSize(), state, txReceipt.getTransaction().getFrom().toString().substring(0, 8), ByteUtil.byteArrayToLong(txReceipt.getTransaction().getNonce()), block.getShortDescr(), txReceipt.getError()));
    }
    IEvent evt = new EventTx(EventTx.CALLBACK.PENDINGTXUPDATE0);
    List<Object> args = new ArrayList<>();
    args.add(txReceipt);
    args.add(state.getValue());
    args.add(block);
    evt.setFuncArgs(args);
    this.evtMgr.newEvent(evt);
}
Also used : EventTx(org.aion.evtmgr.impl.evt.EventTx) IEvent(org.aion.evtmgr.IEvent)

Example 13 with IEvent

use of org.aion.evtmgr.IEvent in project aion by aionnetwork.

the class AionPoW method setupHandler.

/**
 * Sets up the consensus event handler.
 */
private void setupHandler() {
    List<IEvent> txEvts = new ArrayList<>();
    txEvts.add(new EventTx(EventTx.CALLBACK.PENDINGTXRECEIVED0));
    txEvts.add(new EventTx(EventTx.CALLBACK.PENDINGTXUPDATE0));
    txEvts.add(new EventTx(EventTx.CALLBACK.PENDINGTXSTATECHANGE0));
    eventMgr.registerEvent(txEvts);
    List<IEvent> events = new ArrayList<>();
    events.add(new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE));
    events.add(new EventConsensus(EventConsensus.CALLBACK.ON_SOLUTION));
    eventMgr.registerEvent(events);
}
Also used : EventTx(org.aion.evtmgr.impl.evt.EventTx) IEvent(org.aion.evtmgr.IEvent) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus)

Aggregations

IEvent (org.aion.evtmgr.IEvent)13 EventConsensus (org.aion.evtmgr.impl.evt.EventConsensus)4 EventTx (org.aion.evtmgr.impl.evt.EventTx)4 BigInteger (java.math.BigInteger)3 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)3 IHandler (org.aion.evtmgr.IHandler)2 AionBlock (org.aion.zero.impl.types.AionBlock)2 IRepositoryCache (org.aion.base.db.IRepositoryCache)1 Address (org.aion.base.type.Address)1 Solution (org.aion.equihash.Solution)1 EventMiner (org.aion.evtmgr.impl.evt.EventMiner)1 ImportResult (org.aion.mcf.core.ImportResult)1 DataWord (org.aion.mcf.vm.types.DataWord)1 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)1 IAionBlock (org.aion.zero.types.IAionBlock)1 LRUMap (org.apache.commons.collections4.map.LRUMap)1 JSONObject (org.json.JSONObject)1