Update proto and bew client methods

This commit is contained in:
Xiaomin Zhu 2020-03-11 00:12:48 +08:00
parent 20526a8bfc
commit c7889c1874
5 changed files with 148 additions and 31 deletions

View File

@ -328,11 +328,11 @@ public class Client {
public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, long startUnixTime, long endUnixTime) { public ListenableFuture<GetBlocksResponse> getBlocks(String ledger, long startUnixTime, long endUnixTime) {
info( info(
"*** getBlock: ledger={0} startUnixTime={1} endUnixTime={2}", "*** getBlocks: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime); ledger, startUnixTime, endUnixTime);
try { try {
return queryFutureStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime)); return queryFutureStub.getBlocks(blocksRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -356,11 +356,11 @@ public class Client {
public GetBlocksResponse getBlocksSync(String ledger, long startUnixTime, long endUnixTime) { public GetBlocksResponse getBlocksSync(String ledger, long startUnixTime, long endUnixTime) {
info( info(
"*** getBlockSync: ledger={0} startUnixTime={1} endUnixTime={2}", "*** getBlocksSync: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime); ledger, startUnixTime, endUnixTime);
try { try {
return queryBlockingStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime)); return queryBlockingStub.getBlocks(blocksRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -377,7 +377,7 @@ public class Client {
info("*** blockNumber: ledger={0}", ledger); info("*** blockNumber: ledger={0}", ledger);
try { try {
return queryFutureStub.countBlocks(getBlocksRequest(ledger, -1, -1)); return queryFutureStub.countBlocks(blocksRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -394,14 +394,14 @@ public class Client {
info("*** blockNumberSync: ledger={0}", ledger); info("*** blockNumberSync: ledger={0}", ledger);
try { try {
return queryBlockingStub.countBlocks(getBlocksRequest(ledger, -1, -1)); return queryBlockingStub.countBlocks(blocksRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private BlocksRequest getBlocksRequest(String ledger, long startTimestamp, long endTimestamp) { private BlocksRequest blocksRequest(String ledger, long startTimestamp, long endTimestamp) {
BlocksRequest.Builder reqBuilder = BlocksRequest.Builder reqBuilder =
BlocksRequest.newBuilder().setLedger(ledger); BlocksRequest.newBuilder().setLedger(ledger);
@ -515,6 +515,98 @@ public class Client {
return reqBuilder.build(); return reqBuilder.build();
} }
/**
* <a
* href="#">返回时间范围内的事务</a>
* 非阻塞
*/
public ListenableFuture<GetTransactionsResponse> getTransactions(String ledger, ZonedDateTime startDateTime) {
return getTransactions(ledger, startDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 非阻塞
*/
public ListenableFuture<GetTransactionsResponse> getTransactions(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) {
return getTransactions(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 阻塞
*/
public GetTransactionsResponse getTransactionsSync(String ledger, ZonedDateTime startDateTime) {
return getTransactionsSync(ledger, startDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 阻塞
*/
public GetTransactionsResponse getTransactionsSync(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) {
return getTransactionsSync(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond());
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 非阻塞
*/
public ListenableFuture<GetTransactionsResponse> getTransactions(String ledger, long startUnixTime) {
return getTransactions(ledger, startUnixTime, -1);
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 非阻塞
*/
public ListenableFuture<GetTransactionsResponse> getTransactions(String ledger, long startUnixTime, long endUnixTime) {
info(
"*** getTransactions: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime);
try {
return queryFutureStub.getTransactions(transactionsRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 阻塞
*/
public GetTransactionsResponse getTransactionsSync(String ledger, long startUnixTime) {
return getTransactionsSync(ledger, startUnixTime, -1);
}
/**
* <a
* href="#">返回时间范围内的事务</a>
* 阻塞
*/
public GetTransactionsResponse getTransactionsSync(String ledger, long startUnixTime, long endUnixTime) {
info(
"*** getTransactionsSync: ledger={0} startUnixTime={1} endUnixTime={2}",
ledger, startUnixTime, endUnixTime);
try {
return queryBlockingStub.getTransactions(transactionsRequest(ledger, startUnixTime, endUnixTime));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
/** /**
* <a * <a
* href="#">返回账本中的事务数量</a> * href="#">返回账本中的事务数量</a>
@ -525,7 +617,7 @@ public class Client {
info("*** blockNumber: ledger={0}", ledger); info("*** blockNumber: ledger={0}", ledger);
try { try {
return queryFutureStub.countTransactions(transactionsRequest(ledger)); return queryFutureStub.countTransactions(transactionsRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -542,15 +634,25 @@ public class Client {
info("*** blockNumberSync: ledger={0}", ledger); info("*** blockNumberSync: ledger={0}", ledger);
try { try {
return queryBlockingStub.countTransactions(transactionsRequest(ledger)); return queryBlockingStub.countTransactions(transactionsRequest(ledger, -1, -1));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private TransactionsRequest transactionsRequest(String ledger) { private TransactionsRequest transactionsRequest(String ledger, long startTimestamp, long endTimestamp) {
return TransactionsRequest.newBuilder().setLedger(ledger).build();
TransactionsRequest.Builder reqBuilder =
TransactionsRequest.newBuilder().setLedger(ledger);
if (startTimestamp != -1) {
reqBuilder.setStartTimestamp(startTimestamp);
}
if (endTimestamp != -1) {
reqBuilder.setEndTimestamp(endTimestamp);
}
return reqBuilder.build();
} }
private void info(String msg, Object... params) { private void info(String msg, Object... params) {

View File

@ -150,10 +150,6 @@ private static final long serialVersionUID = 0L;
public static final int START_TIMESTAMP_FIELD_NUMBER = 2; public static final int START_TIMESTAMP_FIELD_NUMBER = 2;
private long startTimestamp_; private long startTimestamp_;
/** /**
* <pre>
* required
* </pre>
*
* <code>int64 start_timestamp = 2;</code> * <code>int64 start_timestamp = 2;</code>
* @return The startTimestamp. * @return The startTimestamp.
*/ */
@ -679,10 +675,6 @@ private static final long serialVersionUID = 0L;
private long startTimestamp_ ; private long startTimestamp_ ;
/** /**
* <pre>
* required
* </pre>
*
* <code>int64 start_timestamp = 2;</code> * <code>int64 start_timestamp = 2;</code>
* @return The startTimestamp. * @return The startTimestamp.
*/ */
@ -690,10 +682,6 @@ private static final long serialVersionUID = 0L;
return startTimestamp_; return startTimestamp_;
} }
/** /**
* <pre>
* required
* </pre>
*
* <code>int64 start_timestamp = 2;</code> * <code>int64 start_timestamp = 2;</code>
* @param value The startTimestamp to set. * @param value The startTimestamp to set.
* @return This builder for chaining. * @return This builder for chaining.
@ -705,10 +693,6 @@ private static final long serialVersionUID = 0L;
return this; return this;
} }
/** /**
* <pre>
* required
* </pre>
*
* <code>int64 start_timestamp = 2;</code> * <code>int64 start_timestamp = 2;</code>
* @return This builder for chaining. * @return This builder for chaining.
*/ */

View File

@ -20,10 +20,6 @@ public interface BlocksRequestOrBuilder extends
getLedgerBytes(); getLedgerBytes();
/** /**
* <pre>
* required
* </pre>
*
* <code>int64 start_timestamp = 2;</code> * <code>int64 start_timestamp = 2;</code>
* @return The startTimestamp. * @return The startTimestamp.
*/ */

View File

@ -300,6 +300,9 @@ public final class QueryGrpc {
} }
/** /**
* <pre>
* start_timestamp is required
* </pre>
*/ */
public void getBlocks(bdledger.api.grpc.query.BlocksRequest request, public void getBlocks(bdledger.api.grpc.query.BlocksRequest request,
io.grpc.stub.StreamObserver<bdledger.api.grpc.query.GetBlocksResponse> responseObserver) { io.grpc.stub.StreamObserver<bdledger.api.grpc.query.GetBlocksResponse> responseObserver) {
@ -419,6 +422,9 @@ public final class QueryGrpc {
} }
/** /**
* <pre>
* start_timestamp is required
* </pre>
*/ */
public void getBlocks(bdledger.api.grpc.query.BlocksRequest request, public void getBlocks(bdledger.api.grpc.query.BlocksRequest request,
io.grpc.stub.StreamObserver<bdledger.api.grpc.query.GetBlocksResponse> responseObserver) { io.grpc.stub.StreamObserver<bdledger.api.grpc.query.GetBlocksResponse> responseObserver) {
@ -489,6 +495,9 @@ public final class QueryGrpc {
} }
/** /**
* <pre>
* start_timestamp is required
* </pre>
*/ */
public bdledger.api.grpc.query.GetBlocksResponse getBlocks(bdledger.api.grpc.query.BlocksRequest request) { public bdledger.api.grpc.query.GetBlocksResponse getBlocks(bdledger.api.grpc.query.BlocksRequest request) {
return blockingUnaryCall( return blockingUnaryCall(
@ -554,6 +563,9 @@ public final class QueryGrpc {
} }
/** /**
* <pre>
* start_timestamp is required
* </pre>
*/ */
public com.google.common.util.concurrent.ListenableFuture<bdledger.api.grpc.query.GetBlocksResponse> getBlocks( public com.google.common.util.concurrent.ListenableFuture<bdledger.api.grpc.query.GetBlocksResponse> getBlocks(
bdledger.api.grpc.query.BlocksRequest request) { bdledger.api.grpc.query.BlocksRequest request) {

View File

@ -226,6 +226,29 @@ class ClientTests {
assertEquals(tx, t); assertEquals(tx, t);
} }
// TODO
@Test
@DisplayName("getTransactions#1")
void getTransactions1() throws ExecutionException, InterruptedException {
client.getTransactions(ledger, 0).get();
}
// TODO
@Test
@DisplayName("getTransactions#2")
void getTransactions2() throws ExecutionException, InterruptedException {
client.getTransactions(ledger, 0, 0).get();
}
@Test
@DisplayName("getTransactions#3")
void getTransactions3() {
Throwable e = assertThrows(Exception.class, () -> client.getTransactions("", 0, 0).get());
Status s = Status.fromThrowable(e);
assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode());
assertEquals("ledger must not be empty", s.getDescription());
}
@Test @Test
@DisplayName("CountTransactions#1") @DisplayName("CountTransactions#1")
void CountTransactions1() throws ExecutionException, InterruptedException { void CountTransactions1() throws ExecutionException, InterruptedException {