Search in sources :

Example 1 with NodeDownloadingStatus

use of io.nuls.consensus.entity.NodeDownloadingStatus in project nuls by nuls-io.

the class BlockBatchDownloadUtils method sendRequest.

private void sendRequest(long start, long end, String nodeId) {
    NodeDownloadingStatus status = new NodeDownloadingStatus();
    status.setStart(start);
    status.setEnd(end);
    status.setNodeId(nodeId);
    this.eventBroadcaster.sendToNode(new GetBlockRequest(start, end), nodeId);
    status.setUpdateTime(System.currentTimeMillis());
    nodeStatusMap.put(nodeId, status);
    Log.info("download block :" + start + "-" + end + ",from : " + nodeId);
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus) GetBlockRequest(io.nuls.consensus.event.GetBlockRequest)

Example 2 with NodeDownloadingStatus

use of io.nuls.consensus.entity.NodeDownloadingStatus in project nuls by nuls-io.

the class BlockBatchDownloadUtils method downloadedBlock.

public boolean downloadedBlock(String nodeId, Block block) {
    try {
        NodeDownloadingStatus status = nodeStatusMap.get(nodeId);
        if (null == status) {
            return false;
        }
        this.lastOperateTime = TimeService.currentTimeMillis();
        if (!status.containsHeight(block.getHeader().getHeight())) {
            return false;
        }
        blockMap.put(block.getHeader().getHeight(), block);
        status.downloaded(block.getHeader().getHeight());
        status.setUpdateTime(TimeService.currentTimeMillis());
        if (status.finished()) {
            this.queueService.offer(queueId, nodeId);
        }
        verify();
    } catch (Exception e) {
        Log.error(e);
    }
    return true;
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus)

Example 3 with NodeDownloadingStatus

use of io.nuls.consensus.entity.NodeDownloadingStatus in project nuls by nuls-io.

the class BlockBatchDownloadUtils method verify.

private synchronized void verify() {
    boolean done = true;
    if (nodeStatusMap.isEmpty()) {
        working = false;
        return;
    }
    for (NodeDownloadingStatus status : nodeStatusMap.values()) {
        if (!done) {
            break;
        }
        done = status.finished();
        if (!done && status.getUpdateTime() < (TimeService.currentTimeMillis() - DOWNLOAD_IDLE_TIME_OUT)) {
            nodeStatusMap.remove(status.getNodeId());
            try {
                failedExecute(status);
            } catch (InterruptedException e) {
                Log.error(e);
            }
        }
    }
    if (!done) {
        return;
    }
    Result result;
    try {
        result = checkHash();
    } catch (InterruptedException e) {
        Log.error(e);
        return;
    }
    if (null == result || result.isFailed()) {
        return;
    }
    for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
        Block block = blockMap.get(i);
        if (null == block) {
            // todo
            Log.error("cache block is null");
            break;
        }
        ValidateResult result1 = block.verify();
        if (result1.isFailed() && result1.getErrorCode() != ErrorCode.ORPHAN_TX) {
            if (null != result1.getMessage()) {
                Log.info(result1.getMessage());
            }
            try {
                failedExecute(block.getHeader().getHeight());
            } catch (InterruptedException e) {
                Log.error(e);
            }
            return;
        }
        blockManager.addBlock(block, false, null);
        receivedTxCacheManager.removeTx(block.getTxHashList());
        confirmingTxCacheManager.putTxList(block.getTxs());
    }
    finished();
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 4 with NodeDownloadingStatus

use of io.nuls.consensus.entity.NodeDownloadingStatus in project nuls by nuls-io.

the class BlockBatchDownloadUtils method failedExecute.

private void failedExecute(long height) throws InterruptedException {
    NodeDownloadingStatus status = null;
    for (NodeDownloadingStatus status1 : nodeStatusMap.values()) {
        if (status1.containsHeight(height)) {
            status = status1;
            break;
        }
    }
    this.failedExecute(status);
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus)

Aggregations

NodeDownloadingStatus (io.nuls.consensus.entity.NodeDownloadingStatus)4 GetBlockRequest (io.nuls.consensus.event.GetBlockRequest)1 Block (io.nuls.core.chain.entity.Block)1 Result (io.nuls.core.chain.entity.Result)1 ValidateResult (io.nuls.core.validate.ValidateResult)1