Call core wallets service methods from CoreApi

This change is a refactoring of the gRPC Wallets service
for the purpose of making CoreApi the entry point to
all core implementations.  These changes should have been
made in PR 4295.
See https://github.com/bisq-network/bisq/pull/4295

The gRPC Wallet proto def name was changed to Wallets because
this service manages BSQ and BTC wallets, and GrpcWalletService
was changed to GrpcWalletsService for the same reason.

This PR should be reviewed/merged after PR 4308.
See https://github.com/bisq-network/bisq/pull/4308

This PR's branch was created from the PR 4308 branch.
This commit is contained in:
ghubstan 2020-06-15 16:43:26 -03:00
parent a7542e98bf
commit bac3ed5697
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
5 changed files with 78 additions and 32 deletions

View File

@ -28,7 +28,7 @@ import bisq.proto.grpc.PaymentAccountsGrpc;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.WalletGrpc;
import bisq.proto.grpc.WalletsGrpc;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
@ -139,7 +139,7 @@ public class CliMain {
var versionService = GetVersionGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var paymentAccountsService = PaymentAccountsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var walletService = WalletGrpc.newBlockingStub(channel).withCallCredentials(credentials);
var walletsService = WalletsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
try {
switch (method) {
@ -151,7 +151,7 @@ public class CliMain {
}
case getbalance: {
var request = GetBalanceRequest.newBuilder().build();
var reply = walletService.getBalance(request);
var reply = walletsService.getBalance(request);
var satoshiBalance = reply.getBalance();
var satoshiDivisor = new BigDecimal(100000000);
var btcFormat = new DecimalFormat("###,##0.00000000");
@ -166,13 +166,13 @@ public class CliMain {
var request = GetAddressBalanceRequest.newBuilder()
.setAddress(nonOptionArgs.get(1)).build();
var reply = walletService.getAddressBalance(request);
var reply = walletsService.getAddressBalance(request);
out.println(reply.getAddressBalanceInfo());
return;
}
case getfundingaddresses: {
var request = GetFundingAddressesRequest.newBuilder().build();
var reply = walletService.getFundingAddresses(request);
var reply = walletsService.getFundingAddresses(request);
out.println(reply.getFundingAddressesInfo());
return;
}
@ -202,7 +202,7 @@ public class CliMain {
}
case lockwallet: {
var request = LockWalletRequest.newBuilder().build();
walletService.lockWallet(request);
walletsService.lockWallet(request);
out.println("wallet locked");
return;
}
@ -222,7 +222,7 @@ public class CliMain {
var request = UnlockWalletRequest.newBuilder()
.setPassword(nonOptionArgs.get(1))
.setTimeout(timeout).build();
walletService.unlockWallet(request);
walletsService.unlockWallet(request);
out.println("wallet unlocked");
return;
}
@ -231,7 +231,7 @@ public class CliMain {
throw new IllegalArgumentException("no password specified");
var request = RemoveWalletPasswordRequest.newBuilder().setPassword(nonOptionArgs.get(1)).build();
walletService.removeWalletPassword(request);
walletsService.removeWalletPassword(request);
out.println("wallet decrypted");
return;
}
@ -243,7 +243,7 @@ public class CliMain {
var hasNewPassword = nonOptionArgs.size() == 3;
if (hasNewPassword)
requestBuilder.setNewPassword(nonOptionArgs.get(2));
walletService.setWalletPassword(requestBuilder.build());
walletsService.setWalletPassword(requestBuilder.build());
out.println("wallet encrypted" + (hasNewPassword ? " with new password" : ""));
return;
}

View File

@ -48,6 +48,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CoreApi {
private final CorePaymentAccountsService paymentAccountsService;
private final CoreWalletsService walletsService;
private final OfferBookService offerBookService;
private final TradeStatisticsManager tradeStatisticsManager;
private final CreateOfferService createOfferService;
@ -56,12 +57,14 @@ public class CoreApi {
@Inject
public CoreApi(CorePaymentAccountsService paymentAccountsService,
CoreWalletsService walletsService,
OfferBookService offerBookService,
TradeStatisticsManager tradeStatisticsManager,
CreateOfferService createOfferService,
OpenOfferManager openOfferManager,
User user) {
this.paymentAccountsService = paymentAccountsService;
this.walletsService = walletsService;
this.offerBookService = offerBookService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.createOfferService = createOfferService;
@ -73,22 +76,54 @@ public class CoreApi {
return Version.VERSION;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////
public long getAvailableBalance() {
return walletsService.getAvailableBalance();
}
public long getAddressBalance(String addressString) {
return walletsService.getAddressBalance(addressString);
}
public String getAddressBalanceInfo(String addressString) {
return walletsService.getAddressBalanceInfo(addressString);
}
public String getFundingAddresses() {
return walletsService.getFundingAddresses();
}
public void setWalletPassword(String password, String newPassword) {
walletsService.setWalletPassword(password, newPassword);
}
public void lockWallet() {
walletsService.lockWallet();
}
public void unlockWallet(String password, long timeout) {
walletsService.unlockWallet(password, timeout);
}
public void removeWalletPassword(String password) {
walletsService.removeWalletPassword(password);
}
public List<TradeStatistics2> getTradeStatistics() {
return new ArrayList<>(tradeStatisticsManager.getObservableTradeStatisticsSet());
}
public int getNumConfirmationsForMostRecentTransaction(String addressString) {
return walletsService.getNumConfirmationsForMostRecentTransaction(addressString);
}
public List<Offer> getOffers() {
return offerBookService.getOffers();
}
public void createPaymentAccount(String accountName, String accountNumber, String fiatCurrencyCode) {
paymentAccountsService.createPaymentAccount(accountName, accountNumber, fiatCurrencyCode);
}
public Set<PaymentAccount> getPaymentAccounts() {
return paymentAccountsService.getPaymentAccounts();
}
public void placeOffer(String currencyCode,
String directionAsString,
long priceAsLong,
@ -152,4 +187,15 @@ public class CoreApi {
log::error);
}
///////////////////////////////////////////////////////////////////////////////////////////
// PaymentAccounts
///////////////////////////////////////////////////////////////////////////////////////////
public void createPaymentAccount(String accountName, String accountNumber, String fiatCurrencyCode) {
paymentAccountsService.createPaymentAccount(accountName, accountNumber, fiatCurrencyCode);
}
public Set<PaymentAccount> getPaymentAccounts() {
return paymentAccountsService.getPaymentAccounts();
}
}

View File

@ -59,7 +59,7 @@ public class GrpcServer {
public GrpcServer(Config config,
CoreApi coreApi,
GrpcPaymentAccountsService paymentAccountsService,
GrpcWalletService walletService) {
GrpcWalletsService walletService) {
this.coreApi = coreApi;
this.server = ServerBuilder.forPort(config.apiPort)
.addService(new GetVersionService())

View File

@ -14,7 +14,7 @@ import bisq.proto.grpc.SetWalletPasswordReply;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletReply;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.WalletGrpc;
import bisq.proto.grpc.WalletsGrpc;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
@ -22,19 +22,19 @@ import io.grpc.stub.StreamObserver;
import javax.inject.Inject;
class GrpcWalletService extends WalletGrpc.WalletImplBase {
class GrpcWalletsService extends WalletsGrpc.WalletsImplBase {
private final CoreWalletsService walletsService;
private final CoreApi coreApi;
@Inject
public GrpcWalletService(CoreWalletsService walletsService) {
this.walletsService = walletsService;
public GrpcWalletsService(CoreApi coreApi) {
this.coreApi = coreApi;
}
@Override
public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> responseObserver) {
try {
long result = walletsService.getAvailableBalance();
long result = coreApi.getAvailableBalance();
var reply = GetBalanceReply.newBuilder().setBalance(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -49,7 +49,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void getAddressBalance(GetAddressBalanceRequest req,
StreamObserver<GetAddressBalanceReply> responseObserver) {
try {
String result = walletsService.getAddressBalanceInfo(req.getAddress());
String result = coreApi.getAddressBalanceInfo(req.getAddress());
var reply = GetAddressBalanceReply.newBuilder().setAddressBalanceInfo(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -64,7 +64,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void getFundingAddresses(GetFundingAddressesRequest req,
StreamObserver<GetFundingAddressesReply> responseObserver) {
try {
String result = walletsService.getFundingAddresses();
String result = coreApi.getFundingAddresses();
var reply = GetFundingAddressesReply.newBuilder().setFundingAddressesInfo(result).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -79,7 +79,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void setWalletPassword(SetWalletPasswordRequest req,
StreamObserver<SetWalletPasswordReply> responseObserver) {
try {
walletsService.setWalletPassword(req.getPassword(), req.getNewPassword());
coreApi.setWalletPassword(req.getPassword(), req.getNewPassword());
var reply = SetWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -94,7 +94,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void removeWalletPassword(RemoveWalletPasswordRequest req,
StreamObserver<RemoveWalletPasswordReply> responseObserver) {
try {
walletsService.removeWalletPassword(req.getPassword());
coreApi.removeWalletPassword(req.getPassword());
var reply = RemoveWalletPasswordReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -109,7 +109,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void lockWallet(LockWalletRequest req,
StreamObserver<LockWalletReply> responseObserver) {
try {
walletsService.lockWallet();
coreApi.lockWallet();
var reply = LockWalletReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
@ -124,7 +124,7 @@ class GrpcWalletService extends WalletGrpc.WalletImplBase {
public void unlockWallet(UnlockWalletRequest req,
StreamObserver<UnlockWalletReply> responseObserver) {
try {
walletsService.unlockWallet(req.getPassword(), req.getTimeout());
coreApi.unlockWallet(req.getPassword(), req.getTimeout());
var reply = UnlockWalletReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();

View File

@ -124,10 +124,10 @@ message PlaceOfferReply {
}
///////////////////////////////////////////////////////////////////////////////////////////
// Wallet
// Wallets
///////////////////////////////////////////////////////////////////////////////////////////
service Wallet {
service Wallets {
rpc GetBalance (GetBalanceRequest) returns (GetBalanceReply) {
}
rpc GetAddressBalance (GetAddressBalanceRequest) returns (GetAddressBalanceReply) {