Update getBlocks, countBlocks, countTransactions

This commit is contained in:
2020-03-10 00:25:43 +08:00
parent 0e3952bd49
commit 20526a8bfc
2 changed files with 143 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -225,7 +226,7 @@ public class Client {
/**
* <a
* href="#">返回哈希所指定区块的信息</a>
* href="#">返回哈希所指定区块</a>
* (非阻塞)
*/
public ListenableFuture<GetBlockByHashResponse> getBlockByHash(
@@ -245,7 +246,7 @@ public class Client {
/**
* <a
* href="#">返回哈希所指定区块的信息</a>
* href="#">返回哈希所指定区块</a>
* (阻塞)
*/
public GetBlockByHashResponse getBlockByHashSync(String ledger, String hash, boolean fullTransactions) {
@@ -276,7 +277,99 @@ public class Client {
/**
* <a
* href="#">返回符合条件的区块的数量</a>
* href="#">返回时间范围内的区块</a>
* (非阻塞)
*/
public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, ZonedDateTime startDateTime) {
return getBlocks(ledger, startDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (非阻塞)
*/
public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) {
return getBlocks(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (阻塞)
*/
public GetBlocksResponse getBlocksSync(String ledger, ZonedDateTime startDateTime) {
return getBlocksSync(ledger, startDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (阻塞)
*/
public GetBlocksResponse getBlocksSync(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) {
return getBlocksSync(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (非阻塞)
*/
public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, long startUnixTime) {
return getBlocks(ledger, startUnixTime, -1);
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (非阻塞)
*/
public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, long startUnixTime, long endUnixTime) {
info(
"*** getBlock: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime);
try {
return queryFutureStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (阻塞)
*/
public GetBlocksResponse getBlocksSync(String ledger, long startUnixTime) {
return getBlocksSync(ledger, startUnixTime, -1);
}
/**
* <a
* href="#">返回时间范围内的区块</a>
* (阻塞)
*/
public GetBlocksResponse getBlocksSync(String ledger, long startUnixTime, long endUnixTime) {
info(
"*** getBlockSync: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime);
try {
return queryBlockingStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
/**
* <a
* href="#">返回账本中的区块数量</a>
* (非阻塞)
*/
public ListenableFuture<CountBlocksResponse> countBlocks(String ledger) {
@@ -284,7 +377,7 @@ public class Client {
info("*** blockNumber: ledger={0}", ledger);
try {
return queryFutureStub.countBlocks(blocksRequest(ledger));
return queryFutureStub.countBlocks(getBlocksRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -293,7 +386,7 @@ public class Client {
/**
* <a
* href="#">返回符合条件的区块数量</a>
* href="#">返回账本中的区块数量</a>
* (阻塞)
*/
public CountBlocksResponse countBlocksSync(String ledger) {
@@ -301,20 +394,30 @@ public class Client {
info("*** blockNumberSync: ledger={0}", ledger);
try {
return queryBlockingStub.countBlocks(blocksRequest(ledger));
return queryBlockingStub.countBlocks(getBlocksRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
private BlocksRequest blocksRequest(String ledger) {
return BlocksRequest.newBuilder().setLedger(ledger).build();
private BlocksRequest getBlocksRequest(String ledger, long startTimestamp, long endTimestamp) {
BlocksRequest.Builder reqBuilder =
BlocksRequest.newBuilder().setLedger(ledger);
if (startTimestamp != -1) {
reqBuilder.setStartTimestamp(startTimestamp);
}
if (endTimestamp != -1) {
reqBuilder.setEndTimestamp(endTimestamp);
}
return reqBuilder.build();
}
/**
* <a
* href="#">返回哈希所指定事务的信息</a>
* href="#">返回哈希所指定事务</a>
* (非阻塞)
*/
public ListenableFuture<GetTransactionByHashResponse> getTransactionByHash(String ledger, String hash) {
@@ -331,7 +434,7 @@ public class Client {
/**
* <a
* href="#">返回哈希所指定事务的信息</a>
* href="#">返回哈希所指定事务</a>
* (阻塞)
*/
public GetTransactionByHashResponse getTransactionByHashSync(String ledger, String hash) {
@@ -359,7 +462,7 @@ public class Client {
/**
* <a
* href="#">返回区块的哈希与事务的index所指定事务的信息</a>
* href="#">返回区块的哈希与事务的index所指定事务</a>
* (非阻塞)
*/
public ListenableFuture<GetTransactionByBlockHashAndIndexResponse> getTransactionByBlockHashAndIndex(
@@ -380,7 +483,7 @@ public class Client {
/**
* <a
* href="#">返回区块的哈希与事务的index所指定事务的信息</a>
* href="#">返回区块的哈希与事务的index所指定事务</a>
* (阻塞)
*/
public GetTransactionByBlockHashAndIndexResponse getTransactionByBlockHashAndIndexSync(
@@ -414,7 +517,7 @@ public class Client {
/**
* <a
* href="#">返回符合条件的数量</a>
* href="#">返回账本中的事务数量</a>
* (非阻塞)
*/
public ListenableFuture<CountTransactionsResponse> countTransactions(String ledger) {
@@ -431,7 +534,7 @@ public class Client {
/**
* <a
* href="#">返回符合条件的事务数量</a>
* href="#">返回账本中的事务数量</a>
* (阻塞)
*/
public CountTransactionsResponse countTransactionsSync(String ledger) {