Skip to content

Commit

Permalink
Merge pull request #5259 from ghubstan/04-fix-feerate-validation-bug
Browse files Browse the repository at this point in the history
Fix CLI number opt validation, improve server-not-up msg
  • Loading branch information
sqrrm authored Mar 9, 2021
2 parents 6f0f0ef + 8590c67 commit 3ab184c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
42 changes: 25 additions & 17 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
import static bisq.cli.Method.*;
import static bisq.cli.TableFormat.*;
import static bisq.cli.opts.OptLabel.OPT_HELP;
import static bisq.cli.opts.OptLabel.OPT_HOST;
import static bisq.cli.opts.OptLabel.OPT_PASSWORD;
import static bisq.cli.opts.OptLabel.OPT_PORT;
import static bisq.cli.opts.OptLabel.*;
import static java.lang.String.format;
import static java.lang.System.err;
import static java.lang.System.exit;
Expand Down Expand Up @@ -230,11 +227,11 @@ public static void run(String[] args) {
}
var address = opts.getAddress();
var amount = opts.getAmount();
verifyStringIsValidDecimal(amount);
verifyStringIsValidDecimal(OPT_AMOUNT, amount);

var txFeeRate = opts.getFeeRate();
if (txFeeRate.isEmpty())
verifyStringIsValidLong(txFeeRate);
if (!txFeeRate.isEmpty())
verifyStringIsValidLong(OPT_TX_FEE_RATE, txFeeRate);

var txInfo = client.sendBsq(address, amount, txFeeRate);
out.printf("%s bsq sent to %s in tx %s%n",
Expand All @@ -251,11 +248,11 @@ public static void run(String[] args) {
}
var address = opts.getAddress();
var amount = opts.getAmount();
verifyStringIsValidDecimal(amount);
verifyStringIsValidDecimal(OPT_AMOUNT, amount);

var txFeeRate = opts.getFeeRate();
if (txFeeRate.isEmpty())
verifyStringIsValidLong(txFeeRate);
if (!txFeeRate.isEmpty())
verifyStringIsValidLong(OPT_TX_FEE_RATE, txFeeRate);

var memo = opts.getMemo();

Expand Down Expand Up @@ -605,7 +602,10 @@ public static void run(String[] args) {
} catch (StatusRuntimeException ex) {
// Remove the leading gRPC status code (e.g. "UNKNOWN: ") from the message
String message = ex.getMessage().replaceFirst("^[A-Z_]+: ", "");
throw new RuntimeException(message, ex);
if (message.equals("io exception"))
throw new RuntimeException(message + ", server may not be running", ex);
else
throw new RuntimeException(message, ex);
}
}

Expand All @@ -616,19 +616,27 @@ private static Method getMethodFromCmd(String methodName) {
return Method.valueOf(methodName.toLowerCase());
}

private static void verifyStringIsValidDecimal(String param) {
@SuppressWarnings("SameParameterValue")
private static void verifyStringIsValidDecimal(String optionLabel, String optionValue) {
try {
Double.parseDouble(param);
Double.parseDouble(optionValue);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("'%s' is not a number", param));
throw new IllegalArgumentException(format("--%s=%s, '%s' is not a number",
optionLabel,
optionValue,
optionValue));
}
}

private static void verifyStringIsValidLong(String param) {
@SuppressWarnings("SameParameterValue")
private static void verifyStringIsValidLong(String optionLabel, String optionValue) {
try {
Long.parseLong(param);
Long.parseLong(optionValue);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(format("'%s' is not a number", param));
throw new IllegalArgumentException(format("--%s=%s, '%s' is not a number",
optionLabel,
optionValue,
optionValue));
}
}

Expand Down
5 changes: 2 additions & 3 deletions cli/src/main/java/bisq/cli/CurrencyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class CurrencyFormat {

static final BigDecimal SATOSHI_DIVISOR = new BigDecimal(100000000);
static final DecimalFormat BTC_FORMAT = new DecimalFormat("###,##0.00000000");
static final DecimalFormat BTC_TX_FEE_FORMAT = new DecimalFormat("###,##0.00");
static final DecimalFormat BTC_TX_FEE_FORMAT = new DecimalFormat("###,###,##0");

static final BigDecimal BSQ_SATOSHI_DIVISOR = new BigDecimal(100);
static final DecimalFormat BSQ_FORMAT = new DecimalFormat("###,###,###,##0.00");
Expand Down Expand Up @@ -115,8 +115,7 @@ public static double toSecurityDepositAsPct(String securityDepositInput) {
}
}

@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
public static String formatFeeSatoshis(long sats) {
return BTC_TX_FEE_FORMAT.format(BigDecimal.valueOf(sats).divide(SATOSHI_DIVISOR));
return BTC_TX_FEE_FORMAT.format(BigDecimal.valueOf(sats));
}
}

0 comments on commit 3ab184c

Please sign in to comment.