use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockchainImpl method getListOfHeadersStartFrom.
/**
* Returns up to limit headers found with following search parameters
*
* @param blockNumber Identifier of start block, by number
* @param limit Maximum number of headers in return
* @return {@link MiningBlockHeader}'s list or empty list if none found
*/
@Override
public List<BlockHeader> getListOfHeadersStartFrom(long blockNumber, int limit) {
if (limit <= 0) {
return emptyList();
}
// identifying block we'll move from
Block startBlock = getBlockByNumber(blockNumber);
// if nothing found on main chain, return empty array
if (startBlock == null) {
return emptyList();
}
List<BlockHeader> headers;
long bestNumber = bestBlock.getNumber();
headers = getContinuousHeaders(bestNumber, blockNumber, limit);
return headers;
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionImpl method getStakingBlockTemplate.
// Returns null if we're waiting on a Mining block, or if creating a new block template failed for some reason
@Override
public StakingBlock getStakingBlockTemplate(byte[] newSeed, byte[] signingPublicKey, byte[] coinbase) {
lock.lock();
try {
Block best = getBlockchain().getBestBlock();
LOG_GEN.debug("getStakingBlockTemplate best:{}", best);
if (best.getHeader().getSealType() == Seal.PROOF_OF_STAKE) {
return null;
} else {
return getBlockchain().createStakingBlockTemplate(best, getAionHub().getPendingState().getPendingTransactions(), signingPublicKey, newSeed, coinbase);
}
} finally {
lock.unlock();
}
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionImpl method addNewBlock.
/**
* @implNote import a new block from the api server or the internal PoW miner, the kernel will
* reject to import a new block has the same or less than the kernel block height to reduce the orphan
* block happens (AKI-707)
*/
public ImportResult addNewBlock(Block block) {
getLock().lock();
try {
Block bestBlock = getAionHub().getBlockchain().getBestBlock();
ImportResult importResult;
if (bestBlock.getNumber() >= block.getNumber()) {
importResult = ImportResult.INVALID_BLOCK;
} else {
importResult = this.aionHub.getBlockchain().tryToConnect(new BlockWrapper(block, true, false, false, false));
}
LOG_GEN.debug("ImportResult:{} new block:{}", importResult, block);
if (importResult == ImportResult.IMPORTED_BEST) {
this.aionHub.getPropHandler().propagateNewBlock(block);
}
return importResult;
} finally {
getLock().unlock();
}
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class BlockPropagationTest method testPropagateBlockToPeer.
// given two peers, and one sends you a new block, propagate to the other
@Test
public void testPropagateBlockToPeer() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
byte[] receiver = HashUtil.h256("receiver".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
NodeMock receiverMock = new NodeMock(receiver, 0);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
node.put(2, receiverMock);
AtomicInteger times = new AtomicInteger();
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, String s, Msg _msg) {
if (_nodeId != receiverMock.getIdHash()) {
throw new RuntimeException("should only send to receiver");
}
times.getAndIncrement();
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
assertThat(block.getParentHash()).isEqualTo(bundle.bc.genesis.getHash());
assertThat(block.getParentHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
Block bestBlock = bundle.bc.getBestBlock();
assertThat(bestBlock.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
SyncStats syncStats = new SyncStats(bestBlock.getNumber(), true);
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
// block is processed
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
assertThat(times.get()).isEqualTo(1);
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetTwoGenerationBlocksByHashWithInfo.
@Test
public void testGetTwoGenerationBlocksByHashWithInfo() {
Block grandparent = consecutiveBlocks.get(0);
Block parent = consecutiveBlocks.get(1);
AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(grandparent, BigInteger.TWO, true);
store.saveBlock(parent, BigInteger.TEN, true);
Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parent.getHash());
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isEqualTo(parent);
assertThat(blocks[0].getTotalDifficulty()).isEqualTo(BigInteger.TEN);
assertThat(blocks[0].isMainChain()).isTrue();
assertThat(blocks[1]).isEqualTo(grandparent);
assertThat(blocks[1].getTotalDifficulty()).isEqualTo(BigInteger.TWO);
assertThat(blocks[1].isMainChain()).isTrue();
}
Aggregations