Wallet: use getInputSum() in completeTx(), separately warn about null values

Note that this changes behavior slightly. We are now warning on inputs with null value
and skipping inputs with no value in the sum. Previously we were checking for non-null
getConnectedOutput(). I reviewed the code and whenever getConnectedOutput() is non-null,
getValue() should also be non-null. There might be cases where we have a value and
no connected output, but in those cases I think we should use the value.
This commit is contained in:
Sean Gilligan 2023-05-09 13:54:45 -07:00 committed by Andreas Schildbach
parent 9f76385c9d
commit 15a9086122

View file

@ -4540,13 +4540,15 @@ public class Wallet extends BaseTaggableObject
log.info("Completing send tx with {} outputs totalling {} and a fee of {}/vkB", req.tx.getOutputs().size(),
req.tx.getOutputSum().toFriendlyString(), req.feePerKb.toFriendlyString());
// Warn if there are unconnected inputs whose value we do not know
// TODO: Consider throwing if there are inputs that we don't have a value for
if (req.tx.getInputs().stream()
.map(TransactionInput::getValue)
.anyMatch(Objects::isNull))
log.warn("SendRequest transaction already has inputs but we don't know how much they are worth - they will be added to fee.");
// If any inputs have already been added, we don't need to get their value from wallet
Coin totalInput = Coin.ZERO;
for (TransactionInput input : req.tx.getInputs())
if (input.getConnectedOutput() != null)
totalInput = totalInput.add(input.getConnectedOutput().getValue());
else
log.warn("SendRequest transaction already has inputs but we don't know how much they are worth - they will be added to fee.");
Coin totalInput = req.tx.getInputSum();
// Calculate the amount of value we need to import.
Coin valueNeeded = req.tx.getOutputSum().subtract(totalInput);