From bc6b9bcb4c1cfdf7a87823b028603b4fad55a118 Mon Sep 17 00:00:00 2001 From: Nex Date: Wed, 4 Nov 2020 11:08:34 +0800 Subject: [PATCH] fix: - authTokens - throw errors - sendTransaction's nonce --- .../org/bdware/bdledger/api/grpc/Client.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/bdware/bdledger/api/grpc/Client.java b/src/main/java/org/bdware/bdledger/api/grpc/Client.java index 1e85d9b..c1bf3c5 100644 --- a/src/main/java/org/bdware/bdledger/api/grpc/Client.java +++ b/src/main/java/org/bdware/bdledger/api/grpc/Client.java @@ -77,7 +77,7 @@ public class Client { if (token != null) { Metadata fixedHeaders = new Metadata(); - fixedHeaders.put(Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER), token); + fixedHeaders.put(Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER), "bearer " + token); nodeFutureStub = MetadataUtils.attachHeaders(nodeFutureStub, fixedHeaders); nodeBlockingStub = MetadataUtils.attachHeaders(nodeBlockingStub, fixedHeaders); ledgerFutureStub = MetadataUtils.attachHeaders(ledgerFutureStub, fixedHeaders); @@ -124,7 +124,7 @@ public class Client { return nodeBlockingStub.clientVersion(Empty.getDefaultInstance()); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -156,7 +156,7 @@ public class Client { return ledgerBlockingStub.createLedger(createLedgerRequest(name)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -192,7 +192,7 @@ public class Client { return ledgerBlockingStub.getLedgers(Empty.getDefaultInstance()); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -202,14 +202,14 @@ public class Client { * (非阻塞) */ public ListenableFuture sendTransaction( - String ledger, TransactionType type, String from, String to, byte[] data) { + String ledger, TransactionType type, String from, long nonce, String to, byte[] data) { info( "*** sendTransaction: ledger={0} type={1} from={2} to={3} data={4}", ledger, type, from, to, data); try { - return ledgerFutureStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data)); + return ledgerFutureStub.sendTransaction(SendTransactionRequest(ledger, type, from, nonce, to, data)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; @@ -222,28 +222,29 @@ public class Client { * (阻塞) */ public SendTransactionResponse sendTransactionSync( - String ledger, TransactionType type, String from, String to, byte[] data) { + String ledger, TransactionType type, String from, long nonce, String to, byte[] data) { info( "*** sendTransactionSync: ledger={0} type={1} from={2} to={3} data={4}", ledger, type, from, to, data); try { - return ledgerBlockingStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data)); + return ledgerBlockingStub.sendTransaction(SendTransactionRequest(ledger, type, from, nonce, to, data)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } private SendTransactionRequest SendTransactionRequest( - String ledger, TransactionType type, String from, String to, byte[] data) { + String ledger, TransactionType type, String from, long nonce, String to, byte[] data) { SendTransactionRequest.Transaction.Builder txBuilder = SendTransactionRequest.Transaction.newBuilder().setType(type); if (from != null) { txBuilder.setFrom(ByteString.copyFrom(Utils.hexStringToByteArray(from))); } + txBuilder.setNonce(nonce); if (to != null) { txBuilder.setTo(ByteString.copyFrom(Utils.hexStringToByteArray(to))); } @@ -289,7 +290,7 @@ public class Client { return queryBlockingStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransactions)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -393,7 +394,7 @@ public class Client { return queryBlockingStub.getBlocks(blocksRequest(ledger, startUnixTime, endUnixTime, includeTransactions)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -427,7 +428,7 @@ public class Client { return queryBlockingStub.countBlocks(blocksRequest(ledger, -1, -1, null)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -483,7 +484,7 @@ public class Client { return queryBlockingStub.getRecentBlocks(getRecentBlocksRequest(ledger, count, includeTransactions)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -531,7 +532,7 @@ public class Client { return queryBlockingStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -584,7 +585,7 @@ public class Client { getTransactionByBlockHashAndIndexRequest(ledger, blockHash, index)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -689,7 +690,7 @@ public class Client { return queryBlockingStub.getTransactions(transactionsRequest(ledger, startUnixTime, endUnixTime)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } } @@ -723,7 +724,7 @@ public class Client { return queryBlockingStub.countTransactions(transactionsRequest(ledger, -1, -1)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); - return null; + throw e; } }