Search in sources :

Example 1 with Block

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

the class ConsensusManager method loadConfigration.

private void loadConfigration() {
    Block bestBlock = null;
    Block genesisBlock = GenesisBlock.getInstance();
    NulsContext.getInstance().setGenesisBlock(genesisBlock);
    try {
        bestBlock = blockStorageService.getBlock(blockStorageService.getBestHeight());
    } catch (Exception e) {
        Log.error(e);
    }
    if (bestBlock == null) {
        bestBlock = genesisBlock;
    }
    NulsContext.getInstance().setBestBlock(bestBlock);
    partakePacking = NulsContext.MODULES_CONFIG.getCfgValue(PocConsensusConstant.CFG_CONSENSUS_SECTION, PocConsensusConstant.PROPERTY_PARTAKE_PACKING, false);
    seedNodeList = new ArrayList<>();
    Set<String> seedAddressSet = new HashSet<>();
    String addresses = NulsContext.MODULES_CONFIG.getCfgValue(PocConsensusConstant.CFG_CONSENSUS_SECTION, PocConsensusConstant.PROPERTY_SEED_NODES, "");
    if (StringUtils.isBlank(addresses)) {
        return;
    }
    String[] array = addresses.split(PocConsensusConstant.SEED_NODES_DELIMITER);
    if (null == array) {
        return;
    }
    for (String address : array) {
        seedAddressSet.add(address);
    }
    this.seedNodeList.addAll(seedAddressSet);
}
Also used : GenesisBlock(io.nuls.consensus.entity.genesis.GenesisBlock) Block(io.nuls.core.chain.entity.Block) HashSet(java.util.HashSet)

Example 2 with Block

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

the class BlockServiceImpl method getBlockList.

@Override
public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
    List<Block> blockList = blockStorageService.getBlockList(startHeight, endHeight);
    if (blockList.size() < (endHeight - startHeight + 1)) {
        long currentMaxHeight = blockList.get(blockList.size() - 1).getHeader().getHeight();
        while (currentMaxHeight < endHeight) {
            long next = currentMaxHeight + 1;
            Block block = blockManager.getBlock(next);
            if (null == block) {
                try {
                    block = blockStorageService.getBlock(next);
                } catch (Exception e) {
                    Log.error(e);
                }
            }
            if (null != block) {
                blockList.add(block);
            }
        }
    }
    Collections.sort(blockList, BlockHeightComparator.getInstance());
    return blockList;
}
Also used : Block(io.nuls.core.chain.entity.Block) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 3 with Block

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

the class BlockStorageService method getBlockList.

public List<Block> getBlockList(long startHeight, long endHeight) throws NulsException {
    List<Block> blockList = new ArrayList<>();
    List<BlockHeaderPo> poList = headerDao.getHeaderList(startHeight, endHeight);
    List<Long> heightList = new ArrayList<>();
    if (!poList.isEmpty()) {
        List<Transaction> txList = null;
        try {
            txList = ledgerService.getTxList(startHeight, endHeight);
        } catch (Exception e) {
            Log.error(e);
        }
        Map<Long, List<Transaction>> txListGroup = txListGrouping(txList);
        for (BlockHeaderPo po : poList) {
            BlockHeader header = null;
            try {
                header = ConsensusTool.fromPojo(po);
            } catch (NulsException e) {
                throw e;
            }
            heightList.add(header.getHeight());
            blockList.add(fillBlock(header, txListGroup.get(header.getHeight())));
        }
    }
    if ((endHeight - startHeight + 1) > blockList.size()) {
        for (long i = startHeight; i <= endHeight; i++) {
            if (heightList.contains(i)) {
                continue;
            }
            try {
                blockList.add(this.getBlock(i));
            } catch (Exception e) {
                Log.error(e);
            }
        }
    }
    return blockList;
}
Also used : ArrayList(java.util.ArrayList) NulsException(io.nuls.core.exception.NulsException) Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) Block(io.nuls.core.chain.entity.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

Example 4 with Block

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

the class BlockStorageService method fillBlock.

private Block fillBlock(BlockHeader header, List<Transaction> txList) {
    Block block = new Block();
    block.setTxs(txList);
    block.setHeader(header);
    return block;
}
Also used : Block(io.nuls.core.chain.entity.Block)

Example 5 with Block

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

the class BlockStorageService method getBlockHeader.

public BlockHeader getBlockHeader(String hash) throws NulsException {
    BlockHeader header = blockCacheManager.getBlockHeader(hash);
    if (null != header) {
        return header;
    }
    Block block = blockCacheManager.getBlock(hash);
    if (null != block) {
        return block.getHeader();
    }
    BlockHeaderPo po = this.headerDao.getHeader(hash);
    return ConsensusTool.fromPojo(po);
}
Also used : Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

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