Search in sources :

Example 1 with DownloadThreadManager

use of io.nuls.protocol.base.download.thread.DownloadThreadManager in project nuls by nuls-io.

the class DownloadProcessor method doSynchronize.

/**
 * 区块同步流程
 * block synchronization process
 */
private void doSynchronize() {
    if (downloadStatus != DownloadStatus.READY) {
        return;
    }
    downloadStatus = DownloadStatus.DOWNLOADING;
    // 查找网络大多数节点一致的最高区块hash
    // Finding the highest block hash consistent with most nodes in the network
    NetworkNewestBlockInfos newestInfos = getNetworkNewestBlockInfos();
    if (newestInfos.getNodes().size() < ProtocolConstant.ALIVE_MIN_NODE_COUNT) {
        downloadStatus = DownloadStatus.WAIT;
        Log.info("too few nodes");
        return;
    }
    NulsContext.getInstance().setNetBestBlockHeight(newestInfos.getNetBestHeight());
    DownloadThreadManager downloadThreadManager = new DownloadThreadManager(newestInfos);
    FutureTask<Boolean> threadManagerFuture = new FutureTask<>(downloadThreadManager);
    TaskManager.createAndRunThread(ProtocolConstant.MODULE_ID_PROTOCOL, "download-thread-manager", threadManagerFuture);
    try {
        Boolean downResult = threadManagerFuture.get();
        boolean success = downResult != null && downResult.booleanValue();
        if (success && checkIsNewest(newestInfos)) {
            downloadStatus = DownloadStatus.SUCCESS;
        } else if (downloadStatus != DownloadStatus.WAIT) {
            downloadStatus = DownloadStatus.FAILED;
        }
    } catch (Exception e) {
        Log.error(e);
        downloadStatus = DownloadStatus.FAILED;
    }
}
Also used : DownloadThreadManager(io.nuls.protocol.base.download.thread.DownloadThreadManager) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NetworkNewestBlockInfos(io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 NetworkNewestBlockInfos (io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos)1 DownloadThreadManager (io.nuls.protocol.base.download.thread.DownloadThreadManager)1