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);
}
}
}
}
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;
}
}
}
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);
}
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);
}
Aggregations