Skip to content

Commit

Permalink
for version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei1985 committed Apr 24, 2019
1 parent 636865d commit ccdfba5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,6 +97,11 @@ public Result<Block> getBlock(long height, boolean isNeedContractTransfer) {
return null;
}

@Override
public List<String> getBlockTxHash(long height) {
return null;
}

@Override
public Result saveBlock(Block block) throws NulsException {
return new Result(true, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class BlockHeader extends BaseNulsData {
private transient byte[] packingAddress;
private transient String packingAddressStr;




public BlockHeader() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ public Result<Block> getBlock(long height, boolean isNeedContractTransfer) {
return Result.getSuccess().setData(block);
}

@Override
public List<String> getBlockTxHash(long height) {
BlockHeaderPo headerPo = blockHeaderStorageService.getBlockHeaderPo(height);
List<String> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.swagger.annotations.ApiModelProperty;

import java.io.IOException;
import java.util.List;

/**
* @author: Niels Wang
Expand Down Expand Up @@ -98,6 +99,8 @@ public class BlockHeaderDto {
@ApiModelProperty(name = "size", value = "大小")
private int size;

private List<String> txHash;

public BlockHeaderDto(Block block) throws IOException {
this(block.getHeader());
this.size = block.getHeader().size();
Expand Down Expand Up @@ -292,4 +295,12 @@ public String getExtend() {
public void setExtend(String extend) {
this.extend = extend;
}

public List<String> getTxHash() {
return txHash;
}

public void setTxHash(List<String> txHash) {
this.txHash = txHash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block> blockResult = blockService.getBlock(height);
if (blockResult.isFailed()) {
return blockResult.toRpcClientResult();
}
BlockHeaderDto dto = null;
try {
dto = new BlockHeaderDto(blockResult.getData());
List<String> 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)
Expand Down Expand Up @@ -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<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -113,6 +115,8 @@ public interface BlockService {
*/
Result<Block> getBlock(long height, boolean isNeedContractTransfer);

List<String> getBlockTxHash(long height);

/**
* 保存区块到存储中
* Save the block to the store.
Expand Down

0 comments on commit ccdfba5

Please sign in to comment.