Search in sources :

Example 6 with Block

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

the class BlockStorageService method getBlock.

public Block getBlock(String hash) throws Exception {
    Block block = blockCacheManager.getBlock(hash);
    if (null != block) {
        return block;
    }
    BlockHeader header = getBlockHeader(hash);
    if (null == header) {
        return null;
    }
    List<Transaction> txList = null;
    try {
        txList = ledgerService.getTxList(header.getHeight());
    } catch (Exception e) {
        Log.error(e);
    }
    if (txList.size() != header.getTxCount()) {
        System.out.println();
    }
    return fillBlock(header, txList);
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) NulsException(io.nuls.core.exception.NulsException)

Example 7 with Block

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

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

the class BlockMaintenanceThread method rollbackBlock.

private void rollbackBlock(long startHeight) {
    long height = startHeight - 1;
    Block block = this.blockService.getBlock(height);
    try {
        this.blockService.rollbackBlock(startHeight);
        NulsContext.getInstance().setBestBlock(block);
    } catch (NulsException e) {
        Log.error(e);
        return;
    }
    boolean previousRb = false;
    if (height > 0) {
        NulsContext.getInstance().setBestBlock(block);
        BlockInfo blockInfo = DistributedBlockInfoRequestUtils.getInstance().request(height, null);
        if (null != blockInfo && null != blockInfo.getBestHash() && (!blockInfo.getBestHash().equals(block.getHeader().getHash()) && blockInfo.getBestHeight() == height)) {
            previousRb = true;
        }
    }
    if (previousRb) {
        rollbackBlock(height);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) BlockInfo(io.nuls.consensus.utils.BlockInfo) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock)

Example 9 with Block

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

the class BlockPersistenceThread method doPersistence.

private void doPersistence(long height) throws IOException {
    Block block = blockManager.getBlock(height);
    if (null == block) {
        List<Node> nodeList = networkService.getAvailableNodes();
        if (nodeList == null || nodeList.isEmpty()) {
            return;
        }
        List<String> nodeIdList = new ArrayList<>();
        for (Node node : nodeList) {
            nodeIdList.add(node.getId());
        }
        try {
            BlockBatchDownloadUtils.getInstance().request(nodeIdList, height, height);
        } catch (InterruptedException e) {
            Log.error(e);
        }
        return;
    }
    if (block.getTxs().isEmpty()) {
        // todo why
        Log.warn("block has no tx!");
        blockManager.removeBlock(block.getHeader().getHash().getDigestHex());
        return;
    }
    boolean isSuccess = blockService.saveBlock(block);
    if (isSuccess) {
        blockManager.removeBlock(block.getHeader().getHash().getDigestHex());
        blockManager.setStoredHeight(height);
        txCacheManager.removeTxList(block.getTxHashList());
    }
}
Also used : Node(io.nuls.network.entity.Node) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block)

Example 10 with Block

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

the class ConsensusMeetingRunner method checkBestHash.

private boolean checkBestHash() {
    BlockInfo blockInfo;
    try {
        blockInfo = DistributedBlockInfoRequestUtils.getInstance().request(-1, null);
    } catch (Exception e) {
        return false;
    }
    if (blockInfo == null || blockInfo.getBestHash() == null) {
        return false;
    }
    boolean result = blockInfo.getBestHeight() <= context.getBestBlock().getHeader().getHeight();
    if (!result) {
        return result;
    }
    Block localBlock = blockService.getBlock(blockInfo.getBestHeight());
    result = null != localBlock && blockInfo.getBestHash().getDigestHex().equals(localBlock.getHeader().getHash().getDigestHex());
    return result;
}
Also used : BlockInfo(io.nuls.consensus.utils.BlockInfo) Block(io.nuls.core.chain.entity.Block) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

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