From 12d2de5e11a46af04aa089337f06ab701af90bb1 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sun, 3 May 2020 15:48:54 +0200 Subject: [PATCH] Change CoreWalletService#getAvailableBalance from Tuple2 to long And throw an IllegalStateException with an appropriate message if the wallet is not available or still locked. This change also eliminates the NullPointerException that would sometimes be thrown when calling #getAvailableBalance after the wallet has become available but before the balance has become available. --- .../main/java/bisq/core/grpc/ApiStatus.java | 2 -- .../bisq/core/grpc/CoreWalletService.java | 20 ++++++++----------- .../bisq/core/grpc/GrpcWalletService.java | 15 +++++++------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/ApiStatus.java b/core/src/main/java/bisq/core/grpc/ApiStatus.java index 021d55412f8..a241ba75a51 100644 --- a/core/src/main/java/bisq/core/grpc/ApiStatus.java +++ b/core/src/main/java/bisq/core/grpc/ApiStatus.java @@ -42,8 +42,6 @@ enum ApiStatus { WALLET_ENCRYPTER_NOT_AVAILABLE(Status.FAILED_PRECONDITION, "wallet encrypter is not available"), WALLET_IS_ENCRYPTED(Status.FAILED_PRECONDITION, "wallet is encrypted with a password"), - WALLET_IS_ENCRYPTED_WITH_UNLOCK_INSTRUCTION(Status.FAILED_PRECONDITION, - "wallet is encrypted with a password; unlock it with the 'unlock \"password\" timeout' command"), WALLET_NOT_ENCRYPTED(Status.FAILED_PRECONDITION, "wallet is not encrypted with a password"), WALLET_NOT_AVAILABLE(Status.UNAVAILABLE, "wallet is not available"); diff --git a/core/src/main/java/bisq/core/grpc/CoreWalletService.java b/core/src/main/java/bisq/core/grpc/CoreWalletService.java index ec8c5b709a6..4aa4fef25d2 100644 --- a/core/src/main/java/bisq/core/grpc/CoreWalletService.java +++ b/core/src/main/java/bisq/core/grpc/CoreWalletService.java @@ -36,22 +36,18 @@ public CoreWalletService(Balances balances, WalletsManager walletsManager) { this.walletsManager = walletsManager; } - public Tuple2 getAvailableBalance() { + public long getAvailableBalance() { if (!walletsManager.areWalletsAvailable()) - return new Tuple2<>(-1L, WALLET_NOT_AVAILABLE); + throw new IllegalStateException("wallet is not yet available"); if (walletsManager.areWalletsEncrypted()) - return new Tuple2<>(-1L, WALLET_IS_ENCRYPTED_WITH_UNLOCK_INSTRUCTION); + throw new IllegalStateException("wallet is locked"); - try { - long balance = balances.getAvailableBalance().get().getValue(); - return new Tuple2<>(balance, OK); - } catch (Throwable t) { - // TODO Derive new ApiStatus codes from server stack traces. - t.printStackTrace(); - // TODO Fix bug causing NPE thrown by getAvailableBalance(). - return new Tuple2<>(-1L, INTERNAL); - } + var balance = balances.getAvailableBalance().get(); + if (balance == null) + throw new IllegalStateException("balance is not yet available"); + + return balance.getValue(); } public Tuple2 setWalletPassword(String password, String newPassword) { diff --git a/core/src/main/java/bisq/core/grpc/GrpcWalletService.java b/core/src/main/java/bisq/core/grpc/GrpcWalletService.java index 0d5ea8fd79a..408ebfb273f 100644 --- a/core/src/main/java/bisq/core/grpc/GrpcWalletService.java +++ b/core/src/main/java/bisq/core/grpc/GrpcWalletService.java @@ -12,6 +12,7 @@ import bisq.proto.grpc.UnlockWalletRequest; import bisq.proto.grpc.WalletGrpc; +import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; @@ -28,16 +29,16 @@ public GrpcWalletService(CoreWalletService walletService) { @Override public void getBalance(GetBalanceRequest req, StreamObserver responseObserver) { - var result = walletService.getAvailableBalance(); - if (!result.second.equals(ApiStatus.OK)) { - StatusRuntimeException ex = new StatusRuntimeException(result.second.getGrpcStatus() - .withDescription(result.second.getDescription())); + try { + long result = walletService.getAvailableBalance(); + var reply = GetBalanceReply.newBuilder().setBalance(result).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } catch (IllegalStateException cause) { + var ex = new StatusRuntimeException(Status.UNAVAILABLE.withDescription(cause.getMessage())); responseObserver.onError(ex); throw ex; } - var reply = GetBalanceReply.newBuilder().setBalance(result.first).build(); - responseObserver.onNext(reply); - responseObserver.onCompleted(); } @Override