Search in sources :

Example 1 with GetBlockRequest

use of io.nuls.consensus.event.GetBlockRequest 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 GetBlockRequest

use of io.nuls.consensus.event.GetBlockRequest in project nuls by nuls-io.

the class BlockManager method addBlock.

public void addBlock(Block block, boolean verify, String nodeId) {
    if (block == null || block.getHeader() == null || block.getTxs() == null || block.getTxs().isEmpty()) {
        return;
    }
    if (storedHeight == 0) {
        BlockService blockService = NulsContext.getServiceBean(BlockService.class);
        if (null != blockService) {
            storedHeight = blockService.getLocalSavedHeight();
        }
    }
    if (block.getHeader().getHeight() <= storedHeight) {
        return;
    }
    if (verify) {
        block.verifyWithException();
    }
    boolean success = confrimingBlockCacheManager.cacheBlock(block);
    if (!success) {
        blockCacheBuffer.cacheBlock(block);
        boolean hasPre = blockCacheBuffer.getBlock(block.getHeader().getPreHash().getDigestHex()) != null;
        if (!hasPre && null != nodeId) {
            GetBlockRequest request = new GetBlockRequest();
            GetBlockParam params = new GetBlockParam();
            long height = block.getHeader().getHeight();
            if (height > this.bifurcateProcessor.getMaxHeight()) {
                height = this.bifurcateProcessor.getMaxHeight() + 1;
            }
            params.setStart(height);
            params.setEnd(height);
            request.setEventBody(params);
            this.eventBroadcaster.sendToNode(request, nodeId);
        }
        return;
    }
    bifurcateProcessor.addHeader(block.getHeader());
    if (bifurcateProcessor.getChainSize() == 1) {
        try {
            this.appravalBlock(block);
            context.setBestBlock(block);
            this.lastAppravedHash = block.getHeader().getHash().getDigestHex();
            checkNextblock(block.getHeader().getHash().getDigestHex());
        } catch (Exception e) {
            confrimingBlockCacheManager.removeBlock(block.getHeader().getHash().getDigestHex());
            blockCacheBuffer.cacheBlock(block);
            return;
        }
    } else {
        this.rollbackAppraval(block);
    }
}
Also used : GetBlockParam(io.nuls.consensus.entity.GetBlockParam) BlockService(io.nuls.consensus.service.intf.BlockService) GetBlockRequest(io.nuls.consensus.event.GetBlockRequest) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Aggregations

GetBlockRequest (io.nuls.consensus.event.GetBlockRequest)2 GetBlockParam (io.nuls.consensus.entity.GetBlockParam)1 NodeDownloadingStatus (io.nuls.consensus.entity.NodeDownloadingStatus)1 BlockService (io.nuls.consensus.service.intf.BlockService)1 NulsException (io.nuls.core.exception.NulsException)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1