use of io.nuls.consensus.poc.container.BlockContainer in project nuls by nuls-io.
the class ConsensusPocServiceImpl method addBlock.
@Override
public Result addBlock(Block block) {
BlockContainer blockContainer = new BlockContainer(block, BlockContainerStatus.DOWNLOADING);
boolean success = blockQueueProvider.put(blockContainer);
return new Result(success, null);
}
use of io.nuls.consensus.poc.container.BlockContainer in project nuls by nuls-io.
the class BlockQueueProviderTest method testSizeAndClear.
@Test
public void testSizeAndClear() {
assertNotNull(blockQueueProvider);
assertEquals(0, blockQueueProvider.size());
Block block = new Block();
boolean result = blockQueueProvider.put(new BlockContainer(block, BlockContainerStatus.RECEIVED));
assertTrue(result);
assertEquals(1, blockQueueProvider.size());
blockQueueProvider.clear();
assertEquals(0, blockQueueProvider.size());
}
use of io.nuls.consensus.poc.container.BlockContainer in project nuls by nuls-io.
the class OrphanBlockProcess method foundAndProcessPreviousBlock.
private void foundAndProcessPreviousBlock(BlockContainer blockContainer) {
BlockHeader blockHeader = blockContainer.getBlock().getHeader();
// Determine whether the previous block already exists. If it already exists, it will not be downloaded.
// 判断上一区块是否已经存在,如果已经存在则不下载
boolean hasExist = checkHasExist(blockHeader.getPreHash());
if (hasExist) {
return;
}
Block preBlock = downloadService.downloadBlock(blockHeader.getPreHash(), blockContainer.getNode()).getData();
if (preBlock != null) {
ChainLog.debug("get pre block success {} - {}", preBlock.getHeader().getHeight(), preBlock.getHeader().getHash());
orphanBlockProvider.addBlock(new BlockContainer(preBlock, blockContainer.getNode(), BlockContainerStatus.DOWNLOADING));
} else {
ChainLog.debug("get pre block fail {} - {}", blockHeader.getHeight() - 1, blockHeader.getPreHash());
// 失败情况的处理,从其它所有可用的节点去获取,如果都不成功,那么就失败,包括本次失败的节点,再次获取一次
for (Node node : networkService.getAvailableNodes()) {
preBlock = downloadService.downloadBlock(blockHeader.getPreHash(), node).getData();
if (preBlock != null) {
orphanBlockProvider.addBlock(new BlockContainer(preBlock, node, BlockContainerStatus.DOWNLOADING));
ChainLog.debug("get pre block retry success {} - {}", preBlock.getHeader().getHeight() - 1, preBlock.getHeader().getPreHash());
return;
}
}
ChainLog.debug("get pre block complete failure {} - {}", blockHeader.getHeight() - 1, blockHeader.getPreHash());
}
}
use of io.nuls.consensus.poc.container.BlockContainer in project nuls by nuls-io.
the class BlockQueueProvider method get.
public BlockContainer get() {
BlockContainer blockContainer = null;
// check can destory the download queue
if (!downloadBlockQueueHasDestory) {
blockContainer = downloadBlockQueue.poll();
}
checkDownloadService();
boolean hasDownloadSuccess = downloadService.isDownloadSuccess().isSuccess();
if (blockContainer == null && hasDownloadSuccess && !downloadBlockQueueHasDestory) {
downloadBlockQueueHasDestory = true;
if (blockContainer == null) {
blockContainer = blockQueue.poll();
}
} else if (hasDownloadSuccess && blockContainer == null) {
blockContainer = blockQueue.poll();
}
return blockContainer;
}
use of io.nuls.consensus.poc.container.BlockContainer in project nuls by nuls-io.
the class BlockProcessTask method doTask.
private void doTask() {
// wait consensus ready running
if (ConsensusStatusContext.getConsensusStatus().ordinal() <= ConsensusStatus.LOADING_CACHE.ordinal()) {
return;
}
BlockContainer blockContainer;
while ((blockContainer = blockQueueProvider.get()) != null) {
try {
if (first) {
List<Transaction> txList = blockContainer.getBlock().getTxs();
for (int index = txList.size() - 1; index >= 0; index--) {
Transaction tx = blockContainer.getBlock().getTxs().get(index);
Transaction localTx = ledgerService.getTx(tx.getHash());
if (null == localTx || tx.getBlockHeight() != localTx.getBlockHeight()) {
continue;
}
try {
transactionService.rollbackTx(tx, blockContainer.getBlock().getHeader());
} catch (Exception e) {
Log.error(e);
}
}
first = false;
}
// long time = System.currentTimeMillis();
blockProcess.addBlock(blockContainer);
// Log.info("add 区块 " + blockContainer.getBlock().getHeader().getHeight() + " 耗时 " + (System.currentTimeMillis() - time) + " ms , tx count : " + blockContainer.getBlock().getHeader().getTxCount());
} catch (Exception e) {
e.printStackTrace();
Log.error("add block fail , error : " + e.getMessage(), e);
}
}
// 系统启动,本地高度和网络高度一致,不需要下载区块时,系统需要知道并设置共识状态为运行中
if (downloadService.isDownloadSuccess().isSuccess() && ConsensusStatusContext.getConsensusStatus() == ConsensusStatus.WAIT_RUNNING && (blockContainer == null || blockContainer.getStatus() == BlockContainerStatus.RECEIVED)) {
ConsensusStatusContext.setConsensusStatus(ConsensusStatus.RUNNING);
NulsContext.WALLET_STATUS = NulsConstant.RUNNING;
}
}
Aggregations