Transaction: use Stream.reduce() in getInputSum()

This commit is contained in:
Sean Gilligan 2023-05-09 13:48:39 -07:00 committed by Andreas Schildbach
parent 36129e53ef
commit 74b7b11681

View file

@ -382,20 +382,19 @@ public class Transaction extends BaseMessage {
return IntMath.divide(getWeight(), 4, RoundingMode.CEILING); // round up return IntMath.divide(getWeight(), 4, RoundingMode.CEILING); // round up
} }
/** /**
* Gets the sum of the inputs, regardless of who owns them. * Gets the sum of all transaction inputs, regardless of who owns them.
* <p>
* <b>Warning:</b> Inputs with {@code null} {@link TransactionInput#getValue()} are silently skipped. Before completing
* or signing a transaction you should verify that there are no inputs with {@code null} values.
* @return The sum of all inputs with non-null values.
*/ */
public Coin getInputSum() { public Coin getInputSum() {
Coin inputTotal = Coin.ZERO; return inputs.stream()
.map(TransactionInput::getValue)
for (TransactionInput input: inputs) { .filter(Objects::nonNull)
Coin inputValue = input.getValue(); .reduce(Coin.ZERO, Coin::add);
if (inputValue != null) {
inputTotal = inputTotal.add(inputValue);
}
}
return inputTotal;
} }
/** /**