From 4ca476ff353913b33034ff5e5a1f0c4d734d8c64 Mon Sep 17 00:00:00 2001 From: Simon de la Rouviere Date: Mon, 20 Jan 2014 17:40:57 +0200 Subject: [PATCH] When throwing InsufficientMoneyException, the amount of satoshis missing is required. When the server requests too much value, it is stored so that implementations of the client has access to it. --- .../channels/PaymentChannelClient.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelClient.java b/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelClient.java index ba63119d..e08d1024 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelClient.java +++ b/core/src/main/java/com/google/bitcoin/protocols/channels/PaymentChannelClient.java @@ -86,6 +86,9 @@ public class PaymentChannelClient implements IPaymentChannelClient { // Information used during channel initialization to send to the server or check what the server sends to us private final ECKey myKey; private final BigInteger maxValue; + + private BigInteger missing; + @GuardedBy("lock") private long minPayment; @GuardedBy("lock") SettableFuture increasePaymentFuture; @@ -127,6 +130,16 @@ public class PaymentChannelClient implements IPaymentChannelClient { this.conn = checkNotNull(conn); } + /** + *

Returns the amount of satoshis missing when a server requests too much value.

+ * + *

When InsufficientMoneyException is thrown due to the server requesting too much value, an instance of + * PaymentChannelClient needs access to how many satoshis are missing.

+ */ + public BigInteger getMissing() { + return missing; + } + @Nullable @GuardedBy("lock") private CloseReason receiveInitiate(Protos.Initiate initiate, BigInteger contractValue, Protos.Error.Builder errorBuilder) throws VerificationException, InsufficientMoneyException { @@ -144,9 +157,10 @@ public class PaymentChannelClient implements IPaymentChannelClient { } BigInteger minChannelSize = BigInteger.valueOf(initiate.getMinAcceptedChannelSize()); - if (maxValue.compareTo(minChannelSize) < 0) { + if (contractValue.compareTo(minChannelSize) < 0) { log.error("Server requested too much value"); errorBuilder.setCode(Protos.Error.ErrorCode.CHANNEL_VALUE_TOO_LARGE); + missing = minChannelSize.subtract(contractValue); return CloseReason.SERVER_REQUESTED_TOO_MUCH_VALUE; } @@ -157,6 +171,7 @@ public class PaymentChannelClient implements IPaymentChannelClient { log.error("Server requested a min payment of {} but we expected {}", initiate.getMinPayment(), MIN_PAYMENT); errorBuilder.setCode(Protos.Error.ErrorCode.MIN_PAYMENT_TOO_LARGE); errorBuilder.setExpectedValue(MIN_PAYMENT); + missing = BigInteger.valueOf(initiate.getMinPayment() - MIN_PAYMENT); return CloseReason.SERVER_REQUESTED_TOO_MUCH_VALUE; }