From 615381ca5aac8218a18218f46c681d21e4fe81d7 Mon Sep 17 00:00:00 2001 From: catbref Date: Wed, 5 Aug 2020 10:03:08 +0100 Subject: [PATCH] Fix BTC spend txn building to be less aggressive about caching/checking spent keys --- src/main/java/org/qortal/crosschain/BTC.java | 13 ++++++------ .../org/qortal/test/btcacct/BtcTests.java | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/qortal/crosschain/BTC.java b/src/main/java/org/qortal/crosschain/BTC.java index 0f2920a7..5e5a3639 100644 --- a/src/main/java/org/qortal/crosschain/BTC.java +++ b/src/main/java/org/qortal/crosschain/BTC.java @@ -277,12 +277,6 @@ public class BTC { for (; ki < keys.size(); ++ki) { ECKey key = keys.get(ki); - if (btc.spentKeys.contains(key)) { - wallet.getActiveKeyChain().markKeyAsUsed((DeterministicKey) key); - areAllKeysUnspent = false; - continue; - } - Address address = Address.fromKey(btc.params, key, ScriptType.P2PKH); byte[] script = ScriptBuilder.createOutputScript(address).getProgram(); @@ -299,6 +293,13 @@ public class BTC { */ if (unspentOutputs.isEmpty()) { + // If this is a known key that has been spent before, then we can skip asking for transaction history + if (btc.spentKeys.contains(key)) { + wallet.getActiveKeyChain().markKeyAsUsed((DeterministicKey) key); + areAllKeysUnspent = false; + continue; + } + // Ask for transaction history - if it's empty then key has never been used List historicTransactionHashes = btc.electrumX.getAddressTransactions(script); if (historicTransactionHashes == null) diff --git a/src/test/java/org/qortal/test/btcacct/BtcTests.java b/src/test/java/org/qortal/test/btcacct/BtcTests.java index f5829be8..a00e54f6 100644 --- a/src/test/java/org/qortal/test/btcacct/BtcTests.java +++ b/src/test/java/org/qortal/test/btcacct/BtcTests.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import java.util.Arrays; import java.util.List; +import org.bitcoinj.core.Transaction; import org.bitcoinj.store.BlockStoreException; import org.junit.After; import org.junit.Before; @@ -67,10 +68,17 @@ public class BtcTests extends Common { BTC btc = BTC.getInstance(); String xprv58 = "tprv8ZgxMBicQKsPdahhFSrCdvC1bsWyzHHZfTneTVqUXN6s1wEtZLwAkZXzFP6TYLg2aQMecZLXLre5bTVGajEB55L1HYJcawpdFG66STVAWPJ"; + String recipient = "2N8WCg52ULCtDSMjkgVTm5mtPdCsUptkHWE"; long amount = 1000L; - btc.buildSpend(xprv58, recipient, amount); + Transaction transaction = btc.buildSpend(xprv58, recipient, amount); + assertNotNull(transaction); + + // Check spent key caching doesn't affect outcome + + transaction = btc.buildSpend(xprv58, recipient, amount); + assertNotNull(transaction); } @Test @@ -84,6 +92,16 @@ public class BtcTests extends Common { assertNotNull(balance); System.out.println(BTC.format(balance)); + + // Check spent key caching doesn't affect outcome + + Long repeatBalance = btc.getWalletBalance(xprv58); + + assertNotNull(repeatBalance); + + System.out.println(BTC.format(repeatBalance)); + + assertEquals(balance, repeatBalance); } }