Search in sources :

Example 1 with BlockHeaderPo

use of io.nuls.db.entity.BlockHeaderPo in project nuls by nuls-io.

the class BlockStorageService method getBlockHeader.

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

Example 2 with BlockHeaderPo

use of io.nuls.db.entity.BlockHeaderPo 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 3 with BlockHeaderPo

use of io.nuls.db.entity.BlockHeaderPo 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)

Example 4 with BlockHeaderPo

use of io.nuls.db.entity.BlockHeaderPo in project nuls by nuls-io.

the class BlockResource method getBlocks.

@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getBlocks(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
    if (pageNumber < 0 || pageSize < 0) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    if (pageNumber == 0) {
        pageNumber = 1;
    }
    if (pageSize == 0) {
        pageSize = 10;
    }
    Page<BlockHeaderPo> page = blockService.getBlockHeaderList(pageNumber, pageSize);
    Page<BlockDto> dtoPage = new Page<>(page);
    List<BlockDto> dtoList = new ArrayList<>();
    long reward = 0;
    for (BlockHeaderPo header : page.getList()) {
        reward = ledgerService.getBlockReward(header.getHeight());
        dtoList.add(new BlockDto(header, reward, 0));
    }
    dtoPage.setList(dtoList);
    return RpcResult.getSuccess().setData(dtoPage);
}
Also used : ArrayList(java.util.ArrayList) Page(io.nuls.core.dto.Page) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo) BlockDto(io.nuls.rpc.entity.BlockDto)

Example 5 with BlockHeaderPo

use of io.nuls.db.entity.BlockHeaderPo in project nuls by nuls-io.

the class BlockResource method getBlocksByAddress.

@GET
@Path("/list/address")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getBlocksByAddress(@QueryParam("address") String address, @QueryParam("type") int type, @QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
    if (type < 1 || type > 2 || pageNumber < 0 || pageSize < 0 || !Address.validAddress(address)) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    if (pageNumber == 0) {
        pageNumber = 1;
    }
    if (pageSize == 0) {
        pageSize = 10;
    }
    Page<BlockHeaderPo> page = blockService.getBlockHeaderList(address, type, pageNumber, pageSize);
    Page<BlockDto> dtoPage = new Page<>(page);
    List<BlockDto> dtoList = new ArrayList<>();
    long reward = 0;
    for (BlockHeaderPo header : page.getList()) {
        reward = ledgerService.getBlockReward(header.getHeight());
        dtoList.add(new BlockDto(header, reward, 0));
    }
    dtoPage.setList(dtoList);
    return RpcResult.getSuccess().setData(dtoPage);
}
Also used : ArrayList(java.util.ArrayList) Page(io.nuls.core.dto.Page) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo) BlockDto(io.nuls.rpc.entity.BlockDto)

Aggregations

BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)9 BlockHeader (io.nuls.core.chain.entity.BlockHeader)4 Page (io.nuls.core.dto.Page)4 ArrayList (java.util.ArrayList)4 Block (io.nuls.core.chain.entity.Block)3 PageInfo (com.github.pagehelper.PageInfo)2 NulsException (io.nuls.core.exception.NulsException)2 Searchable (io.nuls.db.dao.impl.mybatis.util.Searchable)2 BlockDto (io.nuls.rpc.entity.BlockDto)2 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)1 Transaction (io.nuls.core.chain.entity.Transaction)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1