Search in sources :

Example 11 with Block

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

the class ConsensusMeetingRunner method calcRound.

private PocMeetingRound calcRound() {
    PocMeetingRound round = new PocMeetingRound(this.consensusManager.getCurrentRound());
    Block bestBlock = context.getBestBlock();
    BlockRoundData lastRoundData;
    try {
        lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    if (round.getPreviousRound() == null || round.getPreviousRound().getIndex() <= lastRoundData.getRoundIndex()) {
        while (true) {
            if (lastRoundData.getPackingIndexOfRound() == lastRoundData.getConsensusMemberCount() || lastRoundData.getRoundEndTime() <= TimeService.currentTimeMillis()) {
                break;
            }
            try {
                bestBlock = context.getBestBlock();
                lastRoundData = new BlockRoundData(bestBlock.getHeader().getExtend());
            } catch (NulsException e) {
                Log.error(e);
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                Log.error(e);
            }
        }
        PocMeetingRound preRound = new PocMeetingRound(null);
        preRound.setIndex(lastRoundData.getRoundIndex());
        preRound.setStartTime(lastRoundData.getRoundStartTime());
        preRound.setMemberCount(lastRoundData.getConsensusMemberCount());
        round.setPreviousRound(preRound);
    }
    round.setStartTime(round.getPreviousRound().getEndTime());
    round.setIndex(lastRoundData.getRoundIndex() + 1);
    return round;
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 12 with Block

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

the class BlockManager method rollbackAppraval.

private void rollbackAppraval(Block block) {
    if (null == block) {
        Log.warn("the block is null!");
        return;
    }
    this.rollbackTxList(block.getTxs(), 0, block.getTxs().size());
    List<String> hashList = this.bifurcateProcessor.getHashList(block.getHeader().getHeight() - 1);
    if (hashList.size() > 1) {
        Block preBlock = confrimingBlockCacheManager.getBlock(block.getHeader().getPreHash().getDigestHex());
        this.rollbackAppraval(preBlock);
    }
}
Also used : Block(io.nuls.core.chain.entity.Block)

Example 13 with Block

use of io.nuls.core.chain.entity.Block 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 14 with Block

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

the class GetBlockHandler method onEvent.

@Override
public void onEvent(GetBlockRequest event, String fromId) throws NulsException {
    List<Block> blockList = blockService.getBlockList(event.getStart(), event.getEnd());
    for (Block block : blockList) {
        if (null == block) {
            continue;
        }
        BlockEvent blockEvent = new BlockEvent();
        blockEvent.setEventBody(block);
        eventBroadcaster.sendToNode(blockEvent, fromId);
    }
}
Also used : Block(io.nuls.core.chain.entity.Block) BlockEvent(io.nuls.consensus.event.BlockEvent)

Example 15 with Block

use of io.nuls.core.chain.entity.Block 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)

Aggregations

Block (io.nuls.core.chain.entity.Block)31 BlockHeader (io.nuls.core.chain.entity.BlockHeader)11 NulsException (io.nuls.core.exception.NulsException)9 Transaction (io.nuls.core.chain.entity.Transaction)8 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)6 IOException (java.io.IOException)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 ArrayList (java.util.ArrayList)4 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)3 BlockInfo (io.nuls.consensus.utils.BlockInfo)3 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)3 BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)3 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)2 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)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 GetTxGroupParam (io.nuls.consensus.entity.GetTxGroupParam)1 NodeDownloadingStatus (io.nuls.consensus.entity.NodeDownloadingStatus)1