diff --git a/consensus-module/poc/consensus-poc-base/src/test/java/io/nuls/consensus/poc/customer/ConsensusBlockServiceImpl.java b/consensus-module/poc/consensus-poc-base/src/test/java/io/nuls/consensus/poc/customer/ConsensusBlockServiceImpl.java index 97479758f..a0ebc92d7 100644 --- a/consensus-module/poc/consensus-poc-base/src/test/java/io/nuls/consensus/poc/customer/ConsensusBlockServiceImpl.java +++ b/consensus-module/poc/consensus-poc-base/src/test/java/io/nuls/consensus/poc/customer/ConsensusBlockServiceImpl.java @@ -36,6 +36,7 @@ import io.nuls.protocol.service.BlockService; import java.util.ArrayList; +import java.util.List; /** * Created by ln on 2018/5/8. @@ -96,6 +97,11 @@ public Result getBlock(long height, boolean isNeedContractTransfer) { return null; } + @Override + public List getBlockTxHash(long height) { + return null; + } + @Override public Result saveBlock(Block block) throws NulsException { return new Result(true, null); diff --git a/core-module/kernel/src/main/java/io/nuls/kernel/model/BlockHeader.java b/core-module/kernel/src/main/java/io/nuls/kernel/model/BlockHeader.java index 61c6fa7ca..940b0ff9e 100644 --- a/core-module/kernel/src/main/java/io/nuls/kernel/model/BlockHeader.java +++ b/core-module/kernel/src/main/java/io/nuls/kernel/model/BlockHeader.java @@ -59,6 +59,9 @@ public class BlockHeader extends BaseNulsData { private transient byte[] packingAddress; private transient String packingAddressStr; + + + public BlockHeader() { } diff --git a/protocol-module/base/protocol-base/src/main/java/io/nuls/protocol/base/service/BlockServiceImpl.java b/protocol-module/base/protocol-base/src/main/java/io/nuls/protocol/base/service/BlockServiceImpl.java index 5a30d1b7c..fad422aa1 100644 --- a/protocol-module/base/protocol-base/src/main/java/io/nuls/protocol/base/service/BlockServiceImpl.java +++ b/protocol-module/base/protocol-base/src/main/java/io/nuls/protocol/base/service/BlockServiceImpl.java @@ -276,6 +276,18 @@ public Result getBlock(long height, boolean isNeedContractTransfer) { return Result.getSuccess().setData(block); } + @Override + public List getBlockTxHash(long height) { + BlockHeaderPo headerPo = blockHeaderStorageService.getBlockHeaderPo(height); + List list = new ArrayList<>(); + if (null != headerPo && null != headerPo.getTxHashList()) { + for (NulsDigestData hash : headerPo.getTxHashList()) { + list.add(hash.getDigestHex()); + } + } + return list; + } + /** * 保存区块到存储中 * Save the block to the store. diff --git a/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/model/BlockHeaderDto.java b/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/model/BlockHeaderDto.java index 2242fa153..645c88141 100644 --- a/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/model/BlockHeaderDto.java +++ b/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/model/BlockHeaderDto.java @@ -37,6 +37,7 @@ import io.swagger.annotations.ApiModelProperty; import java.io.IOException; +import java.util.List; /** * @author: Niels Wang @@ -98,6 +99,8 @@ public class BlockHeaderDto { @ApiModelProperty(name = "size", value = "大小") private int size; + private List txHash; + public BlockHeaderDto(Block block) throws IOException { this(block.getHeader()); this.size = block.getHeader().size(); @@ -292,4 +295,12 @@ public String getExtend() { public void setExtend(String extend) { this.extend = extend; } + + public List getTxHash() { + return txHash; + } + + public void setTxHash(List txHash) { + this.txHash = txHash; + } } \ No newline at end of file diff --git a/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/resources/BlockResource.java b/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/resources/BlockResource.java index 5e7e53d19..ea4b3e2ab 100644 --- a/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/resources/BlockResource.java +++ b/protocol-module/base/protocol-rpc/src/main/java/io/nuls/protocol/rpc/resources/BlockResource.java @@ -85,6 +85,32 @@ public RpcClientResult getHeaderByHeight(@ApiParam(name = "height", value = "区 } + @GET + @Path("/header/txHash/height/{height}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "根据区块高度查询区块头", notes = "result.data: blockHeaderJson 返回对应的区块头信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "success", response = BlockDto.class) + }) + public RpcClientResult getHeaderHashesByHeight(@ApiParam(name = "height", value = "区块高度", required = true) + @PathParam("height") Integer height) { + AssertUtil.canNotEmpty(height); + Result blockResult = blockService.getBlock(height); + if (blockResult.isFailed()) { + return blockResult.toRpcClientResult(); + } + BlockHeaderDto dto = null; + try { + dto = new BlockHeaderDto(blockResult.getData()); + List list = blockService.getBlockTxHash(height); + dto.setTxHash(list); + } catch (IOException e) { + Log.error(e); + return Result.getFailed(KernelErrorCode.IO_ERROR).toRpcClientResult(); + } + return Result.getSuccess().setData(dto).toRpcClientResult(); + } + @GET @Path("/header/hash/{hash}") @Produces(MediaType.APPLICATION_JSON) @@ -118,6 +144,42 @@ public RpcClientResult getHeader(@ApiParam(name = "hash", value = "区块hash", return result.toRpcClientResult(); } + @GET + @Path("/header/txHash/hash/{hash}") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "根据区块hash查询区块头", notes = "result.data: blockHeaderJson 返回对应的区块头信息") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "success", response = BlockDto.class) + }) + public RpcClientResult getHeaderHash(@ApiParam(name = "hash", value = "区块hash", required = true) + @PathParam("hash") String hash) { + AssertUtil.canNotEmpty(hash); + hash = StringUtils.formatStringPara(hash); + if (!NulsDigestData.validHash(hash)) { + return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult(); + } + Result result = Result.getSuccess(); + Block block = null; + try { + block = blockService.getBlock(NulsDigestData.fromDigestHex(hash)).getData(); + } catch (NulsException e) { + Log.error(e); + } + if (block == null) { + return Result.getFailed(ProtocolErroeCode.BLOCK_IS_NULL).toRpcClientResult(); + } + try { + BlockHeaderDto dto = new BlockHeaderDto(block); + List list = blockService.getBlockTxHash(block.getHeader().getHeight()); + dto.setTxHash(list); + result.setData(dto); + } catch (IOException e) { + Log.error(e); + return Result.getFailed(KernelErrorCode.IO_ERROR).toRpcClientResult(); + } + return result.toRpcClientResult(); + } + @GET @Path("/hash/{hash}") @Produces(MediaType.APPLICATION_JSON) diff --git a/protocol-module/protocol/src/main/java/io/nuls/protocol/service/BlockService.java b/protocol-module/protocol/src/main/java/io/nuls/protocol/service/BlockService.java index 6eda60933..517d37117 100644 --- a/protocol-module/protocol/src/main/java/io/nuls/protocol/service/BlockService.java +++ b/protocol-module/protocol/src/main/java/io/nuls/protocol/service/BlockService.java @@ -32,6 +32,8 @@ import io.nuls.network.model.Node; import io.nuls.protocol.model.SmallBlock; +import java.util.List; + /** * 区块处理服务接口 * The block handles the service interface. @@ -113,6 +115,8 @@ public interface BlockService { */ Result getBlock(long height, boolean isNeedContractTransfer); + List getBlockTxHash(long height); + /** * 保存区块到存储中 * Save the block to the store.