Search in sources :

Example 6 with EventConsensus

use of org.aion.evtmgr.impl.evt.EventConsensus in project aion by aionnetwork.

the class EquihashMiner method mine.

/**
 * Keeps mining until the thread is interrupted
 */
private void mine() {
    MiningBlock block;
    byte[] nonce;
    while (!Thread.currentThread().isInterrupted()) {
        if ((block = miningBlock) == null) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                break;
            }
        } else {
            // A new array must be created each loop
            // If reference is reused the array contents may be changed
            // before block sealed causing validation to fail
            nonce = new byte[32];
            ThreadLocalRandom.current().nextBytes(nonce);
            AionPowSolution solution = miner.mine(block, nonce);
            if (solution != null) {
                IEvent ev = new EventConsensus(EventConsensus.CALLBACK.ON_SOLUTION);
                ev.setFuncArgs(Collections.singletonList(solution));
                evtMgr.newEvent(ev);
            }
        }
    }
}
Also used : IEvent(org.aion.evtmgr.IEvent) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 7 with EventConsensus

use of org.aion.evtmgr.impl.evt.EventConsensus in project aion by aionnetwork.

the class AionPoW method createNewBlockTemplate.

/**
 * Creates a new block template.
 */
private synchronized void createNewBlockTemplate() {
    if (!shutDown.get()) {
        // it be used in DDOS?
        if (this.syncMgr.getNetworkBestBlockNumber() - blockchain.getBestBlock().getNumber() > syncLimit) {
            return;
        }
        Block bestBlock = blockchain.getBlockByNumber(blockchain.getBestBlock().getNumber());
        if (blockchain.isUnityForkEnabledAtNextBlock() && bestBlock.getHeader().getSealType() == Seal.PROOF_OF_WORK) {
            return;
        } else {
            LOG.debug("Internal miner creating a new mining block template");
            List<AionTransaction> txs = pendingState.getPendingTransactions();
            MiningBlock newBlock;
            try {
                newBlock = blockchain.createNewMiningBlock(bestBlock, txs, false);
            } catch (Exception e) {
                LOG.error("Create new block failed!", e);
                return;
            }
            if (newBlock == null) {
                return;
            }
            EventConsensus ev = new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE);
            ev.setFuncArgs(Collections.singletonList(newBlock));
            eventMgr.newEvent(ev);
            lastUpdate.set(System.currentTimeMillis());
            latestBlockTemplate = newBlock;
        }
    }
}
Also used : EventBlock(org.aion.evtmgr.impl.evt.EventBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 8 with EventConsensus

use of org.aion.evtmgr.impl.evt.EventConsensus in project aion by aionnetwork.

the class AionPoW method setupHandler.

/**
 * Sets up the consensus event handler.
 */
private void setupHandler() {
    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 : IEvent(org.aion.evtmgr.IEvent) ArrayList(java.util.ArrayList) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus)

Example 9 with EventConsensus

use of org.aion.evtmgr.impl.evt.EventConsensus in project aion by aionnetwork.

the class AbstractHandlerTest method testAddEvent.

@Test
public void testAddEvent() {
    AbstractHandler handler = new BlockHandler();
    boolean res = handler.addEvent(new EventDummy());
    assertTrue(res);
    boolean res2 = handler.addEvent(new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE));
    assertTrue(res2);
    boolean res3 = handler.addEvent(null);
    assertTrue(res3);
}
Also used : BlockHandler(org.aion.evtmgr.impl.handler.BlockHandler) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus) EventDummy(org.aion.evtmgr.impl.evt.EventDummy) Test(org.junit.Test)

Aggregations

EventConsensus (org.aion.evtmgr.impl.evt.EventConsensus)9 IEvent (org.aion.evtmgr.IEvent)4 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 EventDummy (org.aion.evtmgr.impl.evt.EventDummy)2 BlockHandler (org.aion.evtmgr.impl.handler.BlockHandler)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 AionTransaction (org.aion.base.AionTransaction)1 Solution (org.aion.equihash.Solution)1 EventMiner (org.aion.evtmgr.impl.evt.EventMiner)1 EventTx (org.aion.evtmgr.impl.evt.EventTx)1 AionBlock (org.aion.zero.impl.types.AionBlock)1 Block (org.aion.zero.impl.types.Block)1 JSONObject (org.json.JSONObject)1