Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send meaningful io.grpc.Status.Code to gRPC clients [No. 2] #6088

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

import bisq.proto.grpc.BalancesInfo;

import io.grpc.Status;
import io.grpc.StatusRuntimeException;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -190,6 +193,15 @@ protected static String encodeToHex(String s) {
return Utilities.bytesAsHexString(s.getBytes(UTF_8));
}

protected static Status.Code getStatusRuntimeExceptionStatusCode(Exception grpcException) {
if (grpcException instanceof StatusRuntimeException)
return ((StatusRuntimeException) grpcException).getStatus().getCode();
else
throw new IllegalArgumentException(
format("Expected a io.grpc.StatusRuntimeException argument, but got a %s",
grpcException.getClass().getName()));
}

protected void verifyNoLoggedNodeExceptions() {
var loggedExceptions = getNodeExceptionMessages();
if (loggedExceptions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void setUp() {
public void testRegisterArbitratorShouldThrowException() {
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
arbClient.registerDisputeAgent(ARBITRATOR, DEV_PRIVILEGE_PRIV_KEY));
assertEquals("INVALID_ARGUMENT: arbitrators must be registered in a Bisq UI",
assertEquals("UNIMPLEMENTED: arbitrators must be registered in a Bisq UI",
exception.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.apitest.config.ApiTestConfig.BTC;
import static io.grpc.Status.Code.NOT_FOUND;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -140,6 +141,8 @@ private void testGetMyBsqSwapOffer(OfferInfo bsqSwapOffer) {
break;
} catch (Exception ex) {
log.warn(ex.getMessage());
var statusCode = getStatusRuntimeExceptionStatusCode(ex);
assertEquals(NOT_FOUND, statusCode, "Expected a NOT_FOUND status code from server");

if (numFetchAttempts >= 9)
fail(format("Alice giving up on fetching her (my) bsq swap offer after %d attempts.", numFetchAttempts), ex);
Expand All @@ -160,6 +163,8 @@ private void testGetBsqSwapOffer(OfferInfo bsqSwapOffer) {
break;
} catch (Exception ex) {
log.warn(ex.getMessage());
var statusCode = getStatusRuntimeExceptionStatusCode(ex);
assertEquals(NOT_FOUND, statusCode, "Expected a NOT_FOUND status code from server");

if (numFetchAttempts > 9)
fail(format("Bob gave up on fetching available bsq swap offer after %d attempts.", numFetchAttempts), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void testCreateSellBTCFor5To10KBSQOffer() {
@Test
@Order(5)
public void testGetAllMyBsqOffers() {
List<OfferInfo> offers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(BSQ);
List<OfferInfo> offers = aliceClient.getMyOffersSortedByDate(BSQ);
log.debug("All Alice's BSQ Offers:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.debug("ALICE'S BALANCES\n{}", formatBalancesTbls(aliceClient.getBalances()));
Expand All @@ -269,7 +269,7 @@ public void testGetAllMyBsqOffers() {
@Test
@Order(6)
public void testGetAvailableBsqOffers() {
List<OfferInfo> offers = bobClient.getCryptoCurrencyOffersSortedByDate(BSQ);
List<OfferInfo> offers = bobClient.getOffersSortedByDate(BSQ);
log.debug("All Bob's Available BSQ Offers:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.debug("BOB'S BALANCES\n{}", formatBalancesTbls(bobClient.getBalances()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void testCreatePriceMarginBasedSell1BTCOffer() {
@Test
@Order(5)
public void testGetAllMyXMROffers() {
List<OfferInfo> offers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(XMR);
List<OfferInfo> offers = aliceClient.getMyOffersSortedByDate(XMR);
log.debug("All of Alice's XMR offers:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.debug("Alice's balances\n{}", formatBalancesTbls(aliceClient.getBalances()));
Expand All @@ -266,7 +266,7 @@ public void testGetAllMyXMROffers() {
@Test
@Order(6)
public void testGetAvailableXMROffers() {
List<OfferInfo> offers = bobClient.getCryptoCurrencyOffersSortedByDate(XMR);
List<OfferInfo> offers = bobClient.getOffersSortedByDate(XMR);
log.debug("All of Bob's available XMR offers:\n{}", toOffersTable.apply(offers));
assertEquals(4, offers.size());
log.debug("Bob's balances\n{}", formatBalancesTbls(bobClient.getBalances()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public void testChangeFixedPricedBsqOfferToPriceMarginBasedOfferShouldThrowExcep
NO_TRIGGER_PRICE,
ACTIVATE_OFFER,
MKT_PRICE_MARGIN_ONLY));
String expectedExceptionMessage = format("UNKNOWN: cannot set mkt price margin or"
String expectedExceptionMessage = format("INVALID_ARGUMENT: cannot set mkt price margin or"
+ " trigger price on fixed price bsq offer with id '%s'",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
Expand Down Expand Up @@ -465,7 +465,7 @@ public void testEditTriggerPriceOnFixedPriceBsqOfferShouldThrowException() {
newTriggerPriceAsLong,
ACTIVATE_OFFER,
TRIGGER_PRICE_ONLY));
String expectedExceptionMessage = format("UNKNOWN: cannot set mkt price margin or"
String expectedExceptionMessage = format("INVALID_ARGUMENT: cannot set mkt price margin or"
+ " trigger price on fixed price bsq offer with id '%s'",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
Expand Down Expand Up @@ -850,8 +850,10 @@ public void testEditBsqSwapOfferShouldThrowException() {
NO_TRIGGER_PRICE,
ACTIVATE_OFFER,
TRIGGER_PRICE_ONLY));
String expectedExceptionMessage = format("UNKNOWN: cannot edit bsq swap offer with id '%s'",
originalOffer.getId());
String expectedExceptionMessage =
format("INVALID_ARGUMENT: cannot edit bsq swap offer with id '%s',"
+ " replace it with a new swap offer instead",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ protected final void sendBsqPayment(Logger log,
String receiverAddress = contract.getIsBuyerMakerAndSellerTaker()
? contract.getTakerPaymentAccountPayload().getAddress()
: contract.getMakerPaymentAccountPayload().getAddress();
// TODO Fix trade vol src bug for subclasses.
// This bug was fixed for production CLI with https://github.com/bisq-network/bisq/pull/5704 on Sep 27, 2021
String sendBsqAmount = trade.getOffer().getVolume();
// String sendBsqAmount = trade.getTradeVolume();
String sendBsqAmount = trade.getTradeVolume();
log.debug("Sending {} BSQ to address {}", sendBsqAmount, receiverAddress);
grpcClient.sendBsq(receiverAddress, sendBsqAmount, "");
}
Expand All @@ -219,10 +216,7 @@ protected final void verifyBsqPaymentHasBeenReceived(Logger log,
GrpcClient grpcClient,
TradeInfo trade) {
var contract = trade.getContract();
// TODO Fix trade vol src bug for subclasses.
// This bug was fixed for production with https://github.com/bisq-network/bisq/pull/5704 on Sep 27, 2021
var receiveAmountAsString = trade.getOffer().getVolume();
// String receiveAmountAsString = trade.getTradeVolume();
String receiveAmountAsString = trade.getTradeVolume();
var address = contract.getIsBuyerMakerAndSellerTaker()
? contract.getTakerPaymentAccountPayload().getAddress()
: contract.getMakerPaymentAccountPayload().getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.core.offer.OfferDirection.BUY;
import static bisq.proto.grpc.GetOfferCategoryReply.OfferCategory.BSQ_SWAP;
import static io.grpc.Status.Code.NOT_FOUND;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -171,6 +172,9 @@ private TradeInfo getBsqSwapTrade(GrpcClient client, String tradeId) {
return client.getTrade(tradeId);
} catch (Exception ex) {
log.warn(ex.getMessage());
var statusCode = getStatusRuntimeExceptionStatusCode(ex);
assertEquals(NOT_FOUND, statusCode, "Expected a NOT_FOUND status code from server");

if (numFetchAttempts > 9) {
if (checkForLoggedExceptions) {
printNodeExceptionMessages(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static bisq.apitest.config.ApiTestConfig.BTC;
import static bisq.core.offer.OfferDirection.SELL;
import static bisq.proto.grpc.GetOfferCategoryReply.OfferCategory.BSQ_SWAP;
import static io.grpc.Status.Code.NOT_FOUND;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down Expand Up @@ -169,6 +170,9 @@ private TradeInfo getBsqSwapTrade(GrpcClient client, String tradeId) {
return client.getTrade(tradeId);
} catch (Exception ex) {
log.warn(ex.getMessage());
var statusCode = getStatusRuntimeExceptionStatusCode(ex);
assertEquals(NOT_FOUND, statusCode, "Expected a NOT_FOUND status code from server");

if (numFetchAttempts > 9) {
if (checkForLoggedExceptions) {
printNodeExceptionMessages(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void testFailAndUnFailBuyBTCTrade(final TestInfo testInfo) {
TakeBuyBTCOfferTest test = new TakeBuyBTCOfferTest();
test.testTakeAlicesBuyOffer(testInfo);

var tradeId = test.getTradeId();
var tradeId = AbstractTradeTest.getTradeId();
aliceClient.failTrade(tradeId);

Throwable exception = assertThrows(StatusRuntimeException.class, () -> aliceClient.getTrade(tradeId));
String expectedExceptionMessage = format("INVALID_ARGUMENT: trade with id '%s' not found", tradeId);
String expectedExceptionMessage = format("NOT_FOUND: trade with id '%s' not found", tradeId);
assertEquals(expectedExceptionMessage, exception.getMessage());

try {
Expand All @@ -82,11 +82,11 @@ public void testFailAndUnFailSellBTCTrade(final TestInfo testInfo) {
TakeSellBTCOfferTest test = new TakeSellBTCOfferTest();
test.testTakeAlicesSellOffer(testInfo);

var tradeId = test.getTradeId();
var tradeId = AbstractTradeTest.getTradeId();
aliceClient.failTrade(tradeId);

Throwable exception = assertThrows(StatusRuntimeException.class, () -> aliceClient.getTrade(tradeId));
String expectedExceptionMessage = format("INVALID_ARGUMENT: trade with id '%s' not found", tradeId);
String expectedExceptionMessage = format("NOT_FOUND: trade with id '%s' not found", tradeId);
assertEquals(expectedExceptionMessage, exception.getMessage());

try {
Expand All @@ -101,14 +101,14 @@ public void testFailAndUnFailSellBTCTrade(final TestInfo testInfo) {
@Order(3)
public void testFailAndUnFailBuyXmrTrade(final TestInfo testInfo) {
TakeBuyXMROfferTest test = new TakeBuyXMROfferTest();
test.createXmrPaymentAccounts();
createXmrPaymentAccounts();
test.testTakeAlicesSellBTCForXMROffer(testInfo);

var tradeId = test.getTradeId();
var tradeId = AbstractTradeTest.getTradeId();
aliceClient.failTrade(tradeId);

Throwable exception = assertThrows(StatusRuntimeException.class, () -> aliceClient.getTrade(tradeId));
String expectedExceptionMessage = format("INVALID_ARGUMENT: trade with id '%s' not found", tradeId);
String expectedExceptionMessage = format("NOT_FOUND: trade with id '%s' not found", tradeId);
assertEquals(expectedExceptionMessage, exception.getMessage());

try {
Expand All @@ -123,14 +123,14 @@ public void testFailAndUnFailBuyXmrTrade(final TestInfo testInfo) {
@Order(4)
public void testFailAndUnFailTakeSellXMRTrade(final TestInfo testInfo) {
TakeSellXMROfferTest test = new TakeSellXMROfferTest();
test.createXmrPaymentAccounts();
createXmrPaymentAccounts();
test.testTakeAlicesBuyBTCForXMROffer(testInfo);

var tradeId = test.getTradeId();
var tradeId = AbstractTradeTest.getTradeId();
aliceClient.failTrade(tradeId);

Throwable exception = assertThrows(StatusRuntimeException.class, () -> aliceClient.getTrade(tradeId));
String expectedExceptionMessage = format("INVALID_ARGUMENT: trade with id '%s' not found", tradeId);
String expectedExceptionMessage = format("NOT_FOUND: trade with id '%s' not found", tradeId);
assertEquals(expectedExceptionMessage, exception.getMessage());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testTakeAlicesSellBTCForBSQOffer(final TestInfo testInfo) {
var offerId = alicesOffer.getId();
assertFalse(alicesOffer.getIsCurrencyForMakerFeeBtc());

var alicesBsqOffers = aliceClient.getMyCryptoCurrencyOffers(btcTradeDirection, BSQ);
var alicesBsqOffers = aliceClient.getMyOffers(btcTradeDirection, BSQ);
assertEquals(1, alicesBsqOffers.size());

var trade = takeAlicesOffer(offerId,
Expand All @@ -97,7 +97,7 @@ public void testTakeAlicesSellBTCForBSQOffer(final TestInfo testInfo) {
tradeId = trade.getTradeId();

genBtcBlocksThenWait(1, 2_500);
alicesBsqOffers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(BSQ);
alicesBsqOffers = aliceClient.getMyOffersSortedByDate(BSQ);
assertEquals(0, alicesBsqOffers.size());

waitForDepositConfirmation(log, testInfo, bobClient, trade.getTradeId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public void testTakeAlicesSellBTCForXMROffer(final TestInfo testInfo) {
var offerId = alicesOffer.getId();
assertFalse(alicesOffer.getIsCurrencyForMakerFeeBtc());

var alicesXmrOffers = aliceClient.getMyCryptoCurrencyOffers(btcTradeDirection, XMR);
var alicesXmrOffers = aliceClient.getMyOffers(btcTradeDirection, XMR);
assertEquals(1, alicesXmrOffers.size());
var trade = takeAlicesOffer(offerId, bobsXmrAcct.getId(), TRADE_FEE_CURRENCY_CODE);
alicesXmrOffers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(XMR);
alicesXmrOffers = aliceClient.getMyOffersSortedByDate(XMR);
assertEquals(0, alicesXmrOffers.size());
genBtcBlocksThenWait(1, 2_500);
waitForDepositConfirmation(log, testInfo, bobClient, trade.getTradeId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public void testTakeAlicesBuyBTCForBSQOffer(final TestInfo testInfo) {
genBtcBlocksThenWait(1, 4_000);
var offerId = alicesOffer.getId();
assertTrue(alicesOffer.getIsCurrencyForMakerFeeBtc());
var alicesBsqOffers = aliceClient.getMyCryptoCurrencyOffers(btcTradeDirection, BSQ);
var alicesBsqOffers = aliceClient.getMyOffers(btcTradeDirection, BSQ);
assertEquals(1, alicesBsqOffers.size());
var trade = takeAlicesOffer(offerId,
bobsLegacyBsqAcct.getId(),
TRADE_FEE_CURRENCY_CODE,
false);
sleep(2_500); // Allow available offer to be removed from offer book.
alicesBsqOffers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(BSQ);
alicesBsqOffers = aliceClient.getMyOffersSortedByDate(BSQ);
assertEquals(0, alicesBsqOffers.size());
genBtcBlocksThenWait(1, 2_500);
waitForDepositConfirmation(log, testInfo, bobClient, trade.getTradeId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public void testTakeAlicesBuyBTCForXMROffer(final TestInfo testInfo) {
var offerId = alicesOffer.getId();
assertTrue(alicesOffer.getIsCurrencyForMakerFeeBtc());

var alicesXmrOffers = aliceClient.getMyCryptoCurrencyOffers(btcTradeDirection, XMR);
var alicesXmrOffers = aliceClient.getMyOffers(btcTradeDirection, XMR);
assertEquals(1, alicesXmrOffers.size());
var trade = takeAlicesOffer(offerId, bobsXmrAcct.getId(), TRADE_FEE_CURRENCY_CODE);
alicesXmrOffers = aliceClient.getMyCryptoCurrencyOffersSortedByDate(XMR);
alicesXmrOffers = aliceClient.getMyOffersSortedByDate(XMR);
assertEquals(0, alicesXmrOffers.size());
genBtcBlocksThenWait(1, 2_500);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testSetInvalidTxFeeRateShouldThrowException(final TestInfo testInfo)
var currentTxFeeRateInfo = TxFeeRateInfo.fromProto(aliceClient.getTxFeeRate());
Throwable exception = assertThrows(StatusRuntimeException.class, () -> aliceClient.setTxFeeRate(1));
String expectedExceptionMessage =
format("UNKNOWN: tx fee rate preference must be >= %d sats/byte",
format("INVALID_ARGUMENT: tx fee rate preference must be >= %d sats/byte",
currentTxFeeRateInfo.getMinFeeServiceRate());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
Expand Down
Loading