Skip to content

Commit

Permalink
Change CoreWalletService#getAvailableBalance from Tuple2 to long
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbeams committed May 3, 2020
1 parent 6334c54 commit 12d2de5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/bisq/core/grpc/ApiStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
20 changes: 8 additions & 12 deletions core/src/main/java/bisq/core/grpc/CoreWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,18 @@ public CoreWalletService(Balances balances, WalletsManager walletsManager) {
this.walletsManager = walletsManager;
}

public Tuple2<Long, ApiStatus> 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<Boolean, ApiStatus> setWalletPassword(String password, String newPassword) {
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/java/bisq/core/grpc/GrpcWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,16 +29,16 @@ public GrpcWalletService(CoreWalletService walletService) {

@Override
public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> 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
Expand Down

0 comments on commit 12d2de5

Please sign in to comment.