From 7f72faed3e1bb9515f14409128235c8a5d9c0aa0 Mon Sep 17 00:00:00 2001 From: Matthew Leon Date: Wed, 17 Apr 2019 19:18:25 -0400 Subject: [PATCH] Transaction: Simplify error checking in verify(). --- .../java/org/bitcoinj/core/Transaction.java | 24 +++++++++---------- .../org/bitcoinj/core/TransactionOutput.java | 6 +---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/Transaction.java b/core/src/main/java/org/bitcoinj/core/Transaction.java index 69eb4a993..444cf5d5e 100644 --- a/core/src/main/java/org/bitcoinj/core/Transaction.java +++ b/core/src/main/java/org/bitcoinj/core/Transaction.java @@ -1622,25 +1622,25 @@ public class Transaction extends ChildMessage { if (this.getMessageSize() > Block.MAX_BLOCK_SIZE) throw new VerificationException.LargerThanMaxBlockSize(); - Coin valueOut = Coin.ZERO; HashSet outpoints = new HashSet<>(); for (TransactionInput input : inputs) { if (outpoints.contains(input.getOutpoint())) throw new VerificationException.DuplicatedOutPoint(); outpoints.add(input.getOutpoint()); } - try { - for (TransactionOutput output : outputs) { - if (output.getValue().signum() < 0) // getValue() can throw IllegalStateException - throw new VerificationException.NegativeValueOutput(); - valueOut = valueOut.add(output.getValue()); - if (params.hasMaxMoney() && valueOut.compareTo(params.getMaxMoney()) > 0) - throw new IllegalArgumentException(); + + Coin valueOut = Coin.ZERO; + for (TransactionOutput output : outputs) { + Coin value = output.getValue(); + if (value.signum() < 0) + throw new VerificationException.NegativeValueOutput(); + try { + valueOut = valueOut.add(value); + } catch (ArithmeticException e) { + throw new VerificationException.ExcessiveValue(); } - } catch (IllegalStateException e) { - throw new VerificationException.ExcessiveValue(); - } catch (IllegalArgumentException e) { - throw new VerificationException.ExcessiveValue(); + if (params.hasMaxMoney() && valueOut.compareTo(params.getMaxMoney()) > 0) + throw new VerificationException.ExcessiveValue(); } if (isCoinBase()) { diff --git a/core/src/main/java/org/bitcoinj/core/TransactionOutput.java b/core/src/main/java/org/bitcoinj/core/TransactionOutput.java index 581c0ffe9..ffc4e41d5 100644 --- a/core/src/main/java/org/bitcoinj/core/TransactionOutput.java +++ b/core/src/main/java/org/bitcoinj/core/TransactionOutput.java @@ -156,11 +156,7 @@ public class TransactionOutput extends ChildMessage { * receives. */ public Coin getValue() { - try { - return Coin.valueOf(value); - } catch (IllegalArgumentException e) { - throw new IllegalStateException(e.getMessage(), e); - } + return Coin.valueOf(value); } /**