Search in sources :

Example 21 with Block

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

the class BlockBatchDownloadUtils method checkHash.

private Result checkHash() throws InterruptedException {
    for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
        Block block = blockMap.get(i);
        if (null == block) {
            return Result.getFailed("data error");
        }
        if ((i - currentRound.getStart()) % DOWNLOAD_BLOCKS_PER_TIME == 0) {
            NulsDigestData mustHash = blocksHash.getHash(block.getHeader().getHeight());
            NulsDigestData hash = block.getHeader().getHash();
            if (null == hash || null == mustHash || !mustHash.getDigestHex().equals(hash.getDigestHex())) {
                failedExecute(block.getHeader().getHeight());
                return Result.getFailed("hash wrong!");
            }
        }
        String preHash = block.getHeader().getPreHash().getDigestHex();
        Block preBlock = blockMap.get(block.getHeader().getHeight() - 1);
        if (preBlock == null) {
            preBlock = blockService.getBlock(preHash);
        }
        if (null == preBlock || preBlock.getHeader().getHeight() != (block.getHeader().getHeight() - 1)) {
            failedExecute(block.getHeader().getHeight());
            return Result.getFailed("prehash wrong!");
        }
    }
    return Result.getSuccess();
}
Also used : Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 22 with Block

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

the class BlockBatchDownloadUtils method verify.

private synchronized void verify() {
    boolean done = true;
    if (nodeStatusMap.isEmpty()) {
        working = false;
        return;
    }
    for (NodeDownloadingStatus status : nodeStatusMap.values()) {
        if (!done) {
            break;
        }
        done = status.finished();
        if (!done && status.getUpdateTime() < (TimeService.currentTimeMillis() - DOWNLOAD_IDLE_TIME_OUT)) {
            nodeStatusMap.remove(status.getNodeId());
            try {
                failedExecute(status);
            } catch (InterruptedException e) {
                Log.error(e);
            }
        }
    }
    if (!done) {
        return;
    }
    Result result;
    try {
        result = checkHash();
    } catch (InterruptedException e) {
        Log.error(e);
        return;
    }
    if (null == result || result.isFailed()) {
        return;
    }
    for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
        Block block = blockMap.get(i);
        if (null == block) {
            // todo
            Log.error("cache block is null");
            break;
        }
        ValidateResult result1 = block.verify();
        if (result1.isFailed() && result1.getErrorCode() != ErrorCode.ORPHAN_TX) {
            if (null != result1.getMessage()) {
                Log.info(result1.getMessage());
            }
            try {
                failedExecute(block.getHeader().getHeight());
            } catch (InterruptedException e) {
                Log.error(e);
            }
            return;
        }
        blockManager.addBlock(block, false, null);
        receivedTxCacheManager.removeTx(block.getTxHashList());
        confirmingTxCacheManager.putTxList(block.getTxs());
    }
    finished();
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 23 with Block

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

the class BlockServiceImpl method rollbackBlock.

@Override
@DbSession
public void rollbackBlock(long height) {
    Block block = this.getBlock(height);
    if (null == block) {
        return;
    }
    blockManager.rollback(block);
    this.rollback(block.getTxs(), block.getTxs().size() - 1);
    this.ledgerService.deleteTx(block.getHeader().getHeight());
    blockStorageService.delete(block.getHeader().getHash().getDigestHex());
}
Also used : Block(io.nuls.core.chain.entity.Block) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 24 with Block

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

the class BlockStorageService method getBlockHeaderList.

public List<BlockHeader> getBlockHeaderList(long startHeight, long endHeight, long split) {
    List<BlockHeaderPo> strList = this.headerDao.getHashList(startHeight, endHeight, split);
    Map<Long, BlockHeader> headerMap = new HashMap<>();
    for (BlockHeaderPo po : strList) {
        BlockHeader header = new BlockHeader();
        header.setHash(NulsDigestData.fromDigestHex(po.getHash()));
        header.setHeight(po.getHeight());
        headerMap.put(po.getHeight(), header);
    }
    if ((endHeight - startHeight + 1) == headerMap.size()) {
        return new ArrayList<>(headerMap.values());
    }
    List<BlockHeader> headerList = new ArrayList<>();
    for (long i = startHeight; i <= endHeight; i++) {
        if (headerMap.containsKey(i)) {
            headerList.add(headerMap.get(i));
            continue;
        }
        BlockHeader header = blockCacheManager.getBlockHeader(i);
        if (null == header) {
            Block block = blockCacheManager.getBlock(i);
            if (null != block) {
                header = block.getHeader();
            }
        }
        if (null != header) {
            headerList.add(header);
        }
    }
    return headerList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

Example 25 with Block

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

the class BlockStorageService method getBlock.

public Block getBlock(long height) throws Exception {
    Block block = blockCacheManager.getBlock(height);
    if (null != block && block.getTxs().size() == block.getHeader().getTxCount()) {
        return block;
    }
    BlockHeader header = getBlockHeader(height);
    if (null == header) {
        return null;
    }
    List<Transaction> txList = null;
    try {
        txList = ledgerService.getTxList(height);
    } catch (Exception e) {
        Log.error(e);
    }
    if (header.getTxCount() != txList.size()) {
        Log.warn("block has wrong tx size!");
    }
    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)

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