Search in sources :

Example 1 with BlockDto

use of io.nuls.rpc.entity.BlockDto 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 2 with BlockDto

use of io.nuls.rpc.entity.BlockDto in project nuls by nuls-io.

the class BlockResource method loadBlock.

@GET
@Path("/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult loadBlock(@PathParam("hash") String hash) {
    RpcResult result;
    if (!StringUtils.validHash(hash)) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    Block block = blockService.getBlock(hash);
    if (block == null) {
        result = RpcResult.getFailed(ErrorCode.DATA_NOT_FOUND);
    } else {
        long reward = ledgerService.getBlockReward(block.getHeader().getHeight());
        long fee = ledgerService.getBlockFee(block.getHeader().getHeight());
        result = RpcResult.getSuccess();
        try {
            result.setData(new BlockDto(block, reward, fee));
        } catch (IOException e) {
            // todo
            e.printStackTrace();
        }
    }
    return result;
}
Also used : RpcResult(io.nuls.rpc.entity.RpcResult) Block(io.nuls.core.chain.entity.Block) IOException(java.io.IOException) BlockDto(io.nuls.rpc.entity.BlockDto)

Example 3 with BlockDto

use of io.nuls.rpc.entity.BlockDto in project nuls by nuls-io.

the class BlockResource method getBestBlockHash.

@GET
@Path("/newest")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getBestBlockHash() throws IOException {
    RpcResult result = RpcResult.getSuccess();
    result.setData(new BlockDto(blockService.getLocalBestBlock().getHeader(), 0, 0));
    return result;
}
Also used : RpcResult(io.nuls.rpc.entity.RpcResult) BlockDto(io.nuls.rpc.entity.BlockDto)

Example 4 with BlockDto

use of io.nuls.rpc.entity.BlockDto 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)

Example 5 with BlockDto

use of io.nuls.rpc.entity.BlockDto in project nuls by nuls-io.

the class BlockResource method getHeader.

@GET
@Path("/header/hash/{hash}")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getHeader(@PathParam("hash") String hash) throws NulsException, IOException {
    RpcResult result = RpcResult.getSuccess();
    BlockHeader blockHeader = blockService.getBlockHeader(hash);
    if (blockHeader == null) {
        return RpcResult.getFailed(ErrorCode.DATA_NOT_FOUND);
    }
    long reward = ledgerService.getBlockReward(blockHeader.getHeight());
    long fee = ledgerService.getBlockFee(blockHeader.getHeight());
    result.setData(new BlockDto(blockHeader, reward, fee));
    return result;
}
Also used : RpcResult(io.nuls.rpc.entity.RpcResult) BlockHeader(io.nuls.core.chain.entity.BlockHeader) BlockDto(io.nuls.rpc.entity.BlockDto)

Aggregations

BlockDto (io.nuls.rpc.entity.BlockDto)7 RpcResult (io.nuls.rpc.entity.RpcResult)5 Block (io.nuls.core.chain.entity.Block)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 Page (io.nuls.core.dto.Page)2 BlockHeaderPo (io.nuls.db.entity.BlockHeaderPo)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2