Search in sources :

Example 6 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class BlockMaintenanceThread method getBestCorrectBlock.

private BestCorrectBlock getBestCorrectBlock() {
    BestCorrectBlock resultCorrentInfo = new BestCorrectBlock();
    Block localBestBlock = this.blockService.getLocalBestBlock();
    do {
        if (null == localBestBlock || localBestBlock.getHeader().getHeight() <= 1) {
            break;
        }
        BlockInfo netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(0, null);
        resultCorrentInfo.setNetBestBlockInfo(netBestBlockInfo);
        if (null == netBestBlockInfo || netBestBlockInfo.getBestHash() == null) {
            break;
        }
        // same to network nodes
        if (netBestBlockInfo.getBestHeight() == localBestBlock.getHeader().getHeight() && netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
            break;
        } else if (netBestBlockInfo.getBestHeight() <= localBestBlock.getHeader().getHeight()) {
            if (netBestBlockInfo.getBestHeight() == 0) {
                break;
            }
            // local height is highest
            BlockHeader header = null;
            try {
                header = blockService.getBlockHeader(netBestBlockInfo.getBestHeight());
            } catch (NulsException e) {
                break;
            }
            if (null != header && header.getHash().equals(netBestBlockInfo.getBestHash())) {
                break;
            }
            if (netBestBlockInfo.getNodeIdList().size() == 1) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
            }
            Log.warn("Rollback block start height:{},local is highest and wrong!", localBestBlock.getHeader().getHeight());
            // bifurcation
            rollbackBlock(localBestBlock.getHeader().getHeight());
            localBestBlock = this.blockService.getLocalBestBlock();
            break;
        } else {
            netBestBlockInfo = DistributedBlockInfoRequestUtils.getInstance().request(localBestBlock.getHeader().getHeight(), netBestBlockInfo.getNodeIdList());
            if (netBestBlockInfo.getBestHash().equals(localBestBlock.getHeader().getHash())) {
                break;
            }
            if (localBestBlock.getHeader().getHeight() != netBestBlockInfo.getBestHeight()) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "answer not asked!");
            }
            if (netBestBlockInfo.getNodeIdList().size() == 1) {
                throw new NulsRuntimeException(ErrorCode.FAILED, "node count not enough!");
            }
            Log.warn("Rollback block start height:{},local has wrong blocks!", localBestBlock.getHeader().getHeight());
            // bifurcation
            rollbackBlock(localBestBlock.getHeader().getHeight());
            localBestBlock = this.blockService.getLocalBestBlock();
        }
    } while (false);
    resultCorrentInfo.setLocalBestBlock(localBestBlock);
    return resultCorrentInfo;
}
Also used : BlockInfo(io.nuls.consensus.utils.BlockInfo) NulsException(io.nuls.core.exception.NulsException) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock)

Example 7 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class BlockHeaderHandler method onEvent.

@Override
public void onEvent(BlockHeaderEvent event, String fromId) {
    BlockHeader header = event.getEventBody();
    if (null == header) {
        Log.warn("recieved a null blockHeader!");
        return;
    }
    Block block = blockManager.getBlock(header.getHash().getDigestHex());
    if (null != block) {
        return;
    }
    GetSmallBlockRequest request = new GetSmallBlockRequest();
    GetSmallBlockParam param = new GetSmallBlockParam();
    param.setBlockHash(header.getHash());
    request.setEventBody(param);
    eventBroadcaster.sendToNode(request, fromId);
    temporaryCacheManager.cacheBlockHeader(header);
}
Also used : GetSmallBlockRequest(io.nuls.consensus.event.GetSmallBlockRequest) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) GetSmallBlockParam(io.nuls.consensus.entity.GetSmallBlockParam)

Example 8 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class GetBlockHeaderHandler method onEvent.

@Override
public void onEvent(GetBlockHeaderEvent event, String fromId) {
    BlockHeader header;
    if (null == event.getEventBody() || event.getEventBody().getHeight() == 0) {
        header = blockService.getLocalBestBlock().getHeader();
    } else {
        Block block = blockService.getBlock(event.getEventBody().getHeight());
        if (null == block) {
            header = new BlockHeader();
            header.setHeight(event.getEventBody().getHeight());
        } else {
            header = block.getHeader();
        }
    }
    if (header == null) {
        Log.error("header cannot be null");
        return;
    }
    this.eventBroadcaster.sendToNode(new BlockHeaderEvent(header), fromId);
}
Also used : BlockHeaderEvent(io.nuls.consensus.event.BlockHeaderEvent) GetBlockHeaderEvent(io.nuls.consensus.event.GetBlockHeaderEvent) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 9 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class GetBlocksHashHandler method onEvent.

@Override
public void onEvent(GetBlocksHashRequest event, String fromId) {
    if (event.getEventBody().getEnd() > NulsContext.getInstance().getBestBlock().getHeader().getHeight()) {
        return;
    }
    boolean b = event.getEventBody().getStart() == event.getEventBody().getEnd();
    if (b) {
        BlockHashResponse response = new BlockHashResponse();
        Block block;
        if (event.getEventBody().getEnd() <= 0) {
            block = NulsContext.getInstance().getBestBlock();
        } else {
            block = blockService.getBlock(event.getEventBody().getEnd());
        }
        if (null == block) {
            Log.warn("block can not get:" + event.getEventBody().getEnd());
            return;
        }
        response.put(block.getHeader().getHeight(), block.getHeader().getHash());
        sendResponse(response, fromId);
    } else {
        List<BlockHeader> list = this.blockService.getBlockHeaderList(event.getEventBody().getStart(), event.getEventBody().getEnd(), event.getEventBody().getSplit());
        List<Long> resultHeightList = new ArrayList<>();
        List<NulsDigestData> resultHashList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            resultHeightList.add(list.get(i).getHeight());
            resultHashList.add(list.get(i).getHash());
        }
        if (resultHeightList.isEmpty() || resultHeightList.get(resultHeightList.size() - 1) < event.getEventBody().getEnd()) {
            Block block = this.blockService.getBlock(event.getEventBody().getEnd());
            if (block == null) {
                // todo why?
                Log.warn("block can not get:" + event.getEventBody().getEnd());
                return;
            }
            resultHeightList.add(block.getHeader().getHeight());
            resultHashList.add(block.getHeader().getHash());
        }
        final int size = 50000;
        for (int i = 0; i < resultHashList.size(); i += size) {
            BlockHashResponse response = new BlockHashResponse();
            int end = i + size;
            if (end > resultHeightList.size()) {
                end = resultHeightList.size();
            }
            response.setHeightList(resultHeightList.subList(i, end));
            response.setHashList(resultHashList.subList(i, end));
            sendResponse(response, fromId);
        }
    }
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 10 with BlockHeader

use of io.nuls.core.chain.entity.BlockHeader in project nuls by nuls-io.

the class HeaderContinuityValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    ValidateResult result = ValidateResult.getSuccessResult();
    boolean failed = false;
    do {
        if (header.getHeight() == 0) {
            failed = !header.getPreHash().equals(NulsDigestData.EMPTY_HASH);
            break;
        }
        BlockHeader preHeader = null;
        try {
            preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getHeight() - 1);
        } catch (NulsException e) {
            // todo
            e.printStackTrace();
        }
        if (null == preHeader) {
            break;
        }
        failed = !preHeader.getHash().equals(header.getPreHash());
        if (failed) {
            break;
        }
        BlockRoundData roundData = new BlockRoundData();
        try {
            roundData.parse(header.getExtend());
        } catch (NulsException e) {
            Log.error(e);
        }
        long shouldTime = roundData.getRoundStartTime() + roundData.getPackingIndexOfRound() * PocConsensusConstant.BLOCK_TIME_INTERVAL_SECOND * 1000;
        // todo 3 seconds error
        long difference = header.getTime() - shouldTime;
        failed = difference > 3000 || difference < -3000;
        if (failed) {
            break;
        }
    } while (false);
    if (failed) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Aggregations

BlockHeader (io.nuls.core.chain.entity.BlockHeader)19 Block (io.nuls.core.chain.entity.Block)11 NulsException (io.nuls.core.exception.NulsException)6 Transaction (io.nuls.core.chain.entity.Transaction)5 BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)4 ArrayList (java.util.ArrayList)3 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)2 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)2 ValidateResult (io.nuls.core.validate.ValidateResult)2 BlockDto (io.nuls.rpc.entity.BlockDto)2 RpcResult (io.nuls.rpc.entity.RpcResult)2 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)1 GetSmallBlockParam (io.nuls.consensus.entity.GetSmallBlockParam)1 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)1 GetBlockHeaderEvent (io.nuls.consensus.event.GetBlockHeaderEvent)1 GetSmallBlockRequest (io.nuls.consensus.event.GetSmallBlockRequest)1 BlockInfo (io.nuls.consensus.utils.BlockInfo)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1