From b5127f4414277729b8acb3405c0fd20cf1f5785d Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 30 Jan 2014 11:14:13 +0100 Subject: [PATCH] Make change selectable in regtest mode even if there is only one peer. --- .../google/bitcoin/wallet/DefaultCoinSelector.java | 11 +++++++---- .../bitcoin/wallet/DefaultCoinSelectorTest.java | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/wallet/DefaultCoinSelector.java b/core/src/main/java/com/google/bitcoin/wallet/DefaultCoinSelector.java index 43984554..a72171ff 100644 --- a/core/src/main/java/com/google/bitcoin/wallet/DefaultCoinSelector.java +++ b/core/src/main/java/com/google/bitcoin/wallet/DefaultCoinSelector.java @@ -4,6 +4,7 @@ import com.google.bitcoin.core.NetworkParameters; import com.google.bitcoin.core.Transaction; import com.google.bitcoin.core.TransactionConfidence; import com.google.bitcoin.core.TransactionOutput; +import com.google.bitcoin.params.RegTestParams; import com.google.common.annotations.VisibleForTesting; import java.math.BigInteger; @@ -78,10 +79,12 @@ public class DefaultCoinSelector implements CoinSelector { // Only pick chain-included transactions, or transactions that are ours and pending. TransactionConfidence confidence = tx.getConfidence(); TransactionConfidence.ConfidenceType type = confidence.getConfidenceType(); - if (type.equals(TransactionConfidence.ConfidenceType.BUILDING)) - return true; - return type.equals(TransactionConfidence.ConfidenceType.PENDING) && + return type.equals(TransactionConfidence.ConfidenceType.BUILDING) || + + type.equals(TransactionConfidence.ConfidenceType.PENDING) && confidence.getSource().equals(TransactionConfidence.Source.SELF) && - confidence.numBroadcastPeers() > 1; + // In regtest mode we expect to have only one peer, so we won't see transactions propagate. + // TODO: The value 1 below dates from a time when transactions we broadcast *to* were counted, set to 0 + (confidence.numBroadcastPeers() > 1 || tx.getParams() == RegTestParams.get()); } } diff --git a/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java b/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java index c23e1dfa..51a29fe1 100644 --- a/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java +++ b/core/src/test/java/com/google/bitcoin/wallet/DefaultCoinSelectorTest.java @@ -17,6 +17,7 @@ package com.google.bitcoin.wallet; import com.google.bitcoin.core.*; +import com.google.bitcoin.params.RegTestParams; import com.google.bitcoin.params.UnitTestParams; import com.google.bitcoin.utils.TestUtils; import com.google.bitcoin.utils.TestWithWallet; @@ -61,6 +62,10 @@ public class DefaultCoinSelectorTest extends TestWithWallet { t = new Transaction(params); t.getConfidence().setConfidenceType(TransactionConfidence.ConfidenceType.BUILDING); assertTrue(DefaultCoinSelector.isSelectable(t)); + t = new Transaction(RegTestParams.get()); + t.getConfidence().setConfidenceType(TransactionConfidence.ConfidenceType.PENDING); + t.getConfidence().setSource(TransactionConfidence.Source.SELF); + assertTrue(DefaultCoinSelector.isSelectable(t)); } @Test