Skip to content

Commit

Permalink
Merge pull request #71 from Mulavar/feat-limit
Browse files Browse the repository at this point in the history
feat(*): add block、tx query services with limit
  • Loading branch information
Yeqi Tao committed Feb 17, 2020
2 parents a6903dd + 41d90bd commit 022fe81
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/cn/hyperchain/sdk/response/PageResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cn.hyperchain.sdk.response;

import cn.hyperchain.sdk.response.block.BlockResponse;
import cn.hyperchain.sdk.response.tx.TxResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.annotations.Expose;

import java.util.ArrayList;
import java.util.List;

public class PageResult<T> {
@Expose
private String hasmore;
@Expose
private JsonElement data;
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

/**
* parse result to real result(blocks or transactions).
*
* @param clz can only be TxResponse.Transaction.class or BlockResponse.Block.class
* @return parse result
*/
public List<T> parseResult(Class clz) {
if (clz != BlockResponse.Block.class && clz != TxResponse.Transaction.class) {
throw new RuntimeException("method argument `class` must be BlockResponse.Block.class or TxResponse.Transaction.class!");
}
ArrayList<T> results = new ArrayList<>();

if (data.isJsonArray()) {
JsonArray jsonArray = data.getAsJsonArray();
for (JsonElement jsonElement : jsonArray) {
results.add((T) gson.fromJson(jsonElement, clz));
}
} else {
T block = (T) gson.fromJson(data, clz);
results.add(block);
}
return results;
}

@Override
public String toString() {
return "PageResult{" +
"hasmore='" + hasmore + '\'' +
", data=" + data +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.hyperchain.sdk.response.block;

import cn.hyperchain.sdk.response.PageResult;
import cn.hyperchain.sdk.response.Response;
import com.google.gson.annotations.Expose;

import java.util.List;

public class BlockLimitResponse extends Response {
@Expose
private PageResult<BlockResponse.Block> result;

public List<BlockResponse.Block> getResult() {
return result.parseResult(BlockResponse.Block.class);
}

@Override
public String toString() {
return "BlockLimitResponse{" +
"result=" + result +
", jsonrpc='" + jsonrpc + '\'' +
", id='" + id + '\'' +
", code=" + code +
", message='" + message + '\'' +
'}';
}
}
27 changes: 27 additions & 0 deletions src/main/java/cn/hyperchain/sdk/response/tx/TxLimitResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.hyperchain.sdk.response.tx;

import cn.hyperchain.sdk.response.PageResult;
import cn.hyperchain.sdk.response.Response;
import com.google.gson.annotations.Expose;

import java.util.List;

public class TxLimitResponse extends Response {
@Expose
private PageResult<TxResponse.Transaction> result;

public List<TxResponse.Transaction> getResult() {
return result.parseResult(TxResponse.Transaction.class);
}

@Override
public String toString() {
return "TxLimitResponse{" +
"result=" + result +
", jsonrpc='" + jsonrpc + '\'' +
", id='" + id + '\'' +
", code=" + code +
", message='" + message + '\'' +
'}';
}
}
23 changes: 23 additions & 0 deletions src/main/java/cn/hyperchain/sdk/service/BlockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.hyperchain.sdk.request.Request;
import cn.hyperchain.sdk.response.block.BlockAvgTimeResponse;
import cn.hyperchain.sdk.response.block.BlockCountResponse;
import cn.hyperchain.sdk.response.block.BlockLimitResponse;
import cn.hyperchain.sdk.response.block.BlockNumberResponse;
import cn.hyperchain.sdk.response.block.BlockResponse;

Expand All @@ -28,16 +29,19 @@ public interface BlockService {
/**
* @see BlockService#getBlocks(String, String, boolean, int...)
*/
@Deprecated
Request<BlockResponse> getBlocks(BigInteger from, BigInteger to, int... nodeIds);

/**
* @see BlockService#getBlocks(String, String, boolean, int...)
*/
@Deprecated
Request<BlockResponse> getBlocks(String from, String to, int... nodeIds);

/**
* @see BlockService#getBlocks(String, String, boolean, int...)
*/
@Deprecated
Request<BlockResponse> getBlocks(BigInteger from, BigInteger to, boolean isPlain, int... nodeIds);

/**
Expand All @@ -50,8 +54,27 @@ public interface BlockService {
* @param nodeIds specific ids
* @return {@link Request} of {@link BlockResponse}
*/
@Deprecated
Request<BlockResponse> getBlocks(String from, String to, boolean isPlain, int... nodeIds);

/**
* @see BlockService#getBlocksWithLimit(String, String, int, boolean, int...)
*/
Request<BlockLimitResponse> getBlocksWithLimit(String from, String to, boolean isPlain, int... nodeIds);

/**
* query the block of the specified block interval with limit.
*
* @param from start block number
* @param to end block number
* @param isPlain default false, indicating that the returned block includes transaction information within the block.
* * If specified, the returned block does not include transactions within the block
* @param size the count of blocks you want to query, it must not more than 5000
* @param nodeIds specific ids
* @return {@link Request} of {@link BlockLimitResponse}
*/
Request<BlockLimitResponse> getBlocksWithLimit(String from, String to, int size, boolean isPlain, int... nodeIds);

/**
* @see BlockService#getBlockByHash(String, boolean, int...)
*/
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/cn/hyperchain/sdk/service/TxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import cn.hyperchain.sdk.response.tx.TxAvgTimeResponse;
import cn.hyperchain.sdk.response.tx.TxCountResponse;
import cn.hyperchain.sdk.response.tx.TxCountWithTSResponse;
import cn.hyperchain.sdk.response.tx.TxLimitResponse;
import cn.hyperchain.sdk.response.tx.TxResponse;
import cn.hyperchain.sdk.service.params.MetaDataParam;
import cn.hyperchain.sdk.transaction.Transaction;

import java.math.BigInteger;
Expand All @@ -24,6 +26,7 @@ public interface TxService {
/**
* @see TxService#getTx(String, String, int...)
*/
@Deprecated
Request<TxResponse> getTx(BigInteger from, BigInteger to, int... nodeIds);

/**
Expand All @@ -34,8 +37,26 @@ public interface TxService {
* @param nodeIds specific ids
* @return {@link Request} of {@link TxResponse}
*/
@Deprecated
Request<TxResponse> getTx(String from, String to, int... nodeIds);


/**
* @see TxService#getTxsWithLimit(String, String, MetaDataParam, int...)
*/
Request<TxLimitResponse> getTxsWithLimit(String from, String to, int... nodeIds);

/**
* get transactions by a given block number range with limit.
*
* @param from from block number
* @param to to block number
* @param metaData meta data
* @param nodeIds specific ids
* @return {@link Request} of {@link TxLimitResponse}
*/
Request<TxLimitResponse> getTxsWithLimit(String from, String to, MetaDataParam metaData, int... nodeIds);

/**
* get all discard transactions.
*
Expand Down Expand Up @@ -173,6 +194,7 @@ public interface TxService {
/**
* @see TxService#getTransactionsByTime(BigInteger, BigInteger, int, int...)
*/
@Deprecated
Request<TxResponse> getTransactionsByTime(BigInteger startTime, BigInteger endTime, int... nodeIds);

/**
Expand All @@ -184,18 +206,47 @@ public interface TxService {
* @param nodeIds specific ids
* @return {@link Request} of {@link TxResponse}
*/
@Deprecated
Request<TxResponse> getTransactionsByTime(BigInteger startTime, BigInteger endTime, int limit, int... nodeIds);

/**
* @see TxService#getTransactionsByTime(String, String, int, int...)
*/
@Deprecated
Request<TxResponse> getTransactionsByTime(String startTime, String endTime, int... nodeIds);

/**
* @see TxService#getTransactionsByTime(BigInteger, BigInteger, int, int...)
*/
@Deprecated
Request<TxResponse> getTransactionsByTime(String startTime, String endTime, int limit, int... nodeIds);

/**
* @see TxService#getTransactionsByTimeWithLimit(BigInteger, BigInteger, MetaDataParam, int...)
*/
Request<TxLimitResponse> getTransactionsByTimeWithLimit(String startTime, String endTime, int... nodeIds);

/**
* @see TxService#getTransactionsByTimeWithLimit(BigInteger, BigInteger, MetaDataParam, int...)
*/
Request<TxLimitResponse> getTransactionsByTimeWithLimit(BigInteger startTime, BigInteger endTime, int... nodeIds);

/**
* @see TxService#getTransactionsByTimeWithLimit(BigInteger, BigInteger, MetaDataParam, int...)
*/
Request<TxLimitResponse> getTransactionsByTimeWithLimit(String startTime, String endTime, MetaDataParam metaData, int... nodeIds);

/**
* querying transactions within a specified time interval with limit.
*
* @param startTime start time
* @param endTime end time
* @param metaData meta data
* @param nodeIds specific ids
* @return {@link Request} of {@link TxLimitResponse}
*/
Request<TxLimitResponse> getTransactionsByTimeWithLimit(BigInteger startTime, BigInteger endTime, MetaDataParam metaData, int... nodeIds);

/**
* get discard transactions by time.
*
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/cn/hyperchain/sdk/service/impl/BlockServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import cn.hyperchain.sdk.request.Request;
import cn.hyperchain.sdk.response.block.BlockAvgTimeResponse;
import cn.hyperchain.sdk.response.block.BlockCountResponse;
import cn.hyperchain.sdk.response.block.BlockLimitResponse;
import cn.hyperchain.sdk.response.block.BlockNumberResponse;
import cn.hyperchain.sdk.response.block.BlockResponse;
import cn.hyperchain.sdk.service.BlockService;
import cn.hyperchain.sdk.service.params.MetaDataParam;

import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -65,6 +67,33 @@ public Request<BlockResponse> getBlocks(String from, String to, boolean isPlain,
}


@Override
public Request<BlockLimitResponse> getBlocksWithLimit(String from, String to, boolean isPlain, int... nodeIds) {
BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBlocksWithLimit", providerManager, BlockLimitResponse.class, nodeIds);
HashMap<String, Object> params = new HashMap<>();
params.put("from", from);
params.put("to", to);
params.put("isPlain", isPlain);
blockRequest.addParams(params);

return blockRequest;
}

@Override
public Request<BlockLimitResponse> getBlocksWithLimit(String from, String to, int size, boolean isPlain, int... nodeIds) {
BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBlocksWithLimit", providerManager, BlockLimitResponse.class, nodeIds);

HashMap<String, Object> params = new HashMap<>();
params.put("from", from);
params.put("to", to);
params.put("isPlain", isPlain);
MetaDataParam metaDataParam = new MetaDataParam.Builder().limit(size).build();
params.put("metadata", metaDataParam);
blockRequest.addParams(params);

return blockRequest;
}

@Override
public Request<BlockResponse> getBlockByHash(String blockHash, int... nodeIds) {
return getBlockByHash(blockHash, false);
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/cn/hyperchain/sdk/service/impl/TxServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import cn.hyperchain.sdk.response.tx.TxAvgTimeResponse;
import cn.hyperchain.sdk.response.tx.TxCountResponse;
import cn.hyperchain.sdk.response.tx.TxCountWithTSResponse;
import cn.hyperchain.sdk.response.tx.TxLimitResponse;
import cn.hyperchain.sdk.response.tx.TxResponse;
import cn.hyperchain.sdk.service.ContractService;
import cn.hyperchain.sdk.service.ServiceManager;
import cn.hyperchain.sdk.service.TxService;
import cn.hyperchain.sdk.service.params.MetaDataParam;
import cn.hyperchain.sdk.transaction.Transaction;

import java.math.BigInteger;
Expand Down Expand Up @@ -55,6 +57,31 @@ public Request<TxResponse> getTx(String from, String to, int... nodeIds) {
return txRequest;
}

@Override
public Request<TxLimitResponse> getTxsWithLimit(String from, String to, int... nodeIds) {
TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionsWithLimit", providerManager, TxLimitResponse.class, nodeIds);

HashMap<String, Object> params = new HashMap<>();
params.put("from", from);
params.put("to", to);
txRequest.addParams(params);

return txRequest;
}

@Override
public Request<TxLimitResponse> getTxsWithLimit(String from, String to, MetaDataParam metaData, int... nodeIds) {
TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionsWithLimit", providerManager, TxLimitResponse.class, nodeIds);

HashMap<String, Object> params = new HashMap<>();
params.put("from", from);
params.put("to", to);
params.put("metadata", metaData);
txRequest.addParams(params);

return txRequest;
}

@Override
public Request<TxResponse> getDiscardTx(int... nodeIds) {
TxRequest txRequest = new TxRequest(TX_PREFIX + "getDiscardTransactions", providerManager, TxResponse.class, nodeIds);
Expand Down Expand Up @@ -242,6 +269,41 @@ public Request<TxResponse> getTransactionsByTime(String startTime, String endTim
return getTransactionsByTime(new BigInteger(startTime), new BigInteger(endTime), limit, nodeIds);
}

@Override
public Request<TxLimitResponse> getTransactionsByTimeWithLimit(String startTime, String endTime, int... nodeIds) {
return getTransactionsByTimeWithLimit(new BigInteger(startTime), new BigInteger(endTime), nodeIds);
}

@Override
public Request<TxLimitResponse> getTransactionsByTimeWithLimit(String startTime, String endTime, MetaDataParam metaData, int... nodeIds) {
return getTransactionsByTimeWithLimit(new BigInteger(startTime), new BigInteger(endTime), metaData, nodeIds);
}

@Override
public Request<TxLimitResponse> getTransactionsByTimeWithLimit(BigInteger startTime, BigInteger endTime, int... nodeIds) {
Request<TxLimitResponse> txLimitResponseRequest = new TxRequest(TX_PREFIX + "getTransactionsByTimeWithLimit", providerManager, TxLimitResponse.class, nodeIds);

HashMap<String, Object> params = new HashMap<>();
params.put("startTime", startTime);
params.put("endTime", endTime);
txLimitResponseRequest.addParams(params);

return txLimitResponseRequest;
}

@Override
public Request<TxLimitResponse> getTransactionsByTimeWithLimit(BigInteger startTime, BigInteger endTime, MetaDataParam metaData, int... nodeIds) {
Request<TxLimitResponse> txLimitResponseRequest = new TxRequest(TX_PREFIX + "getTransactionsByTimeWithLimit", providerManager, TxLimitResponse.class, nodeIds);

HashMap<String, Object> params = new HashMap<>();
params.put("startTime", startTime);
params.put("endTime", endTime);
params.put("metadata", metaData);
txLimitResponseRequest.addParams(params);

return txLimitResponseRequest;
}

@Override
public Request<TxResponse> getDiscardTransactionsByTime(BigInteger startTime, BigInteger endTime, int... nodeIds) {
TxRequest txRequest = new TxRequest(TX_PREFIX + "getDiscardTransactionsByTime", providerManager, TxResponse.class, nodeIds);
Expand Down

0 comments on commit 022fe81

Please sign in to comment.