Wallet: add more diagnostic info to InsufficientMoneyException

Add a new constructor that provides more information about available funds, total
outputs, and fee in addition to amount missing. Use it in `Wallet.calculatFee` to
provide more information when the exception occurs.
This commit is contained in:
Sean Gilligan 2023-08-08 18:58:00 -07:00 committed by Andreas Schildbach
parent 2f9ce85e07
commit 370c5d382c
2 changed files with 16 additions and 1 deletions

View File

@ -41,4 +41,19 @@ public class InsufficientMoneyException extends Exception {
super(message);
this.missing = Objects.requireNonNull(missing);
}
public InsufficientMoneyException(Coin missing, Coin available, Coin outputs, Coin fee) {
this(missing, format(missing, available, outputs, fee));
}
/**
* @param missing Amount missing
* @param available Amount available (e.g. found by CoinSelector)
* @param outputs total amount of output requested
* @param fee calculated fee required for transaction
* @return An exception message
*/
static String format(Coin missing, Coin available, Coin outputs, Coin fee) {
return String.format("Insufficient money, missing %s (available: %s, total outputs: %s, fee: %s)", missing.toFriendlyString(), available.toFriendlyString(), outputs.toFriendlyString(), fee.toFriendlyString());
}
}

View File

@ -5483,7 +5483,7 @@ public class Wallet extends BaseTaggableObject
// Can we afford this?
if (selection.totalValue().compareTo(valueNeeded) < 0) {
Coin valueMissing = valueNeeded.subtract(selection.totalValue());
throw new InsufficientMoneyException(valueMissing);
throw new InsufficientMoneyException(valueMissing, selection.totalValue(), value, fee);
}
Coin change = selection.totalValue().subtract(valueNeeded);
if (change.isGreaterThan(Coin.ZERO)) {