Search in sources :

Example 1 with GetBlockParam

use of io.nuls.consensus.entity.GetBlockParam 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

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