From e7b511fc74652e6576cad98aa2b081478568b47d Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 14 Jul 2020 09:21:53 +0200 Subject: [PATCH] bnb and knapsack selectors --- drongo | 2 +- .../sparrow/wallet/MaxUtxoSelector.java | 14 -------- .../sparrow/wallet/SendController.java | 35 +++++++++---------- 3 files changed, 18 insertions(+), 33 deletions(-) delete mode 100644 src/main/java/com/sparrowwallet/sparrow/wallet/MaxUtxoSelector.java diff --git a/drongo b/drongo index 0a6e2471..9d272c0e 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 0a6e247163f737926b582c12c8ceb1b8161e7a4a +Subproject commit 9d272c0eb2785f0d4f745f7d1ede115e91ab4e28 diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/MaxUtxoSelector.java b/src/main/java/com/sparrowwallet/sparrow/wallet/MaxUtxoSelector.java deleted file mode 100644 index a5ab84f9..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/MaxUtxoSelector.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.sparrowwallet.sparrow.wallet; - -import com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex; -import com.sparrowwallet.drongo.wallet.UtxoSelector; - -import java.util.Collection; -import java.util.Collections; - -public class MaxUtxoSelector implements UtxoSelector { - @Override - public Collection select(long targetValue, Collection candidates) { - return Collections.unmodifiableCollection(candidates); - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 3374228a..deb83016 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -5,7 +5,6 @@ import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.address.InvalidAddressException; import com.sparrowwallet.drongo.address.P2PKHAddress; -import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.Transaction; import com.sparrowwallet.drongo.protocol.TransactionOutput; import com.sparrowwallet.drongo.wallet.*; @@ -207,7 +206,10 @@ public class SendController extends WalletFormController implements Initializabl feeRate.setText("Unknown"); } - setTargetBlocks(5); + int defaultTarget = TARGET_BLOCKS_RANGE.get((TARGET_BLOCKS_RANGE.size() / 2) - 1); + int index = TARGET_BLOCKS_RANGE.indexOf(defaultTarget); + targetBlocks.setValue(index); + feeRatesChart.select(defaultTarget); fee.setTextFormatter(new CoinTextFormatter()); fee.textProperty().addListener(feeListener); @@ -293,8 +295,9 @@ public class SendController extends WalletFormController implements Initializabl private void updateTransaction(boolean sendAll) { try { Address recipientAddress = getRecipientAddress(); - Long recipientAmount = sendAll ? Long.valueOf(1L) : getRecipientValueSats(); - if(recipientAmount != null && recipientAmount > getRecipientDustThreshold() && (!userFeeSet.get() || (getFeeValueSats() != null && getFeeValueSats() > 0))) { + long recipientDustThreshold = getRecipientDustThreshold(); + Long recipientAmount = sendAll ? Long.valueOf(recipientDustThreshold + 1) : getRecipientValueSats(); + if(recipientAmount != null && recipientAmount > recipientDustThreshold && (!userFeeSet.get() || (getFeeValueSats() != null && getFeeValueSats() > 0))) { Wallet wallet = getWalletForm().getWallet(); Long userFee = userFeeSet.get() ? getFeeValueSats() : null; WalletTransaction walletTransaction = wallet.createWalletTransaction(getUtxoSelectors(), recipientAddress, recipientAmount, getFeeRate(), getMinimumFeeRate(), userFee, sendAll); @@ -317,33 +320,29 @@ public class SendController extends WalletFormController implements Initializabl return List.of(utxoSelectorProperty.get()); } - return getBnBSelector(); + return List.of(getBnBSelector(), getKnapsackSelector()); } - private List getBnBSelector() { + private UtxoSelector getBnBSelector() { try { - Transaction transaction = new Transaction(); - if(Arrays.asList(ScriptType.WITNESS_TYPES).contains(getWalletForm().getWallet().getScriptType())) { - transaction.setSegwitVersion(0); - } - transaction.addOutput(getRecipientValueSats(), getRecipientAddress()); - int noInputsWeightUnits = transaction.getWeightUnits(); - - UtxoSelector bnbSelector = new BnBUtxoSelector(getWalletForm().getWallet(), noInputsWeightUnits, getFeeRate(), getMinimumFeeRate()); - return List.of(bnbSelector); + int noInputsWeightUnits = getWalletForm().getWallet().getNoInputsWeightUnits(getRecipientAddress()); + return new BnBUtxoSelector(getWalletForm().getWallet(), noInputsWeightUnits, getFeeRate(), getMinimumFeeRate()); } catch(InvalidAddressException e) { throw new RuntimeException(e); } } - private List getPrioritySelector() { + private UtxoSelector getKnapsackSelector() { + return new KnapsackUtxoSelector(); + } + + private UtxoSelector getPrioritySelector() { Integer blockHeight = AppController.getCurrentBlockHeight(); if(blockHeight == null) { blockHeight = getWalletForm().getWallet().getStoredBlockHeight(); } - UtxoSelector priorityUtxoSelector = new PriorityUtxoSelector(blockHeight); - return List.of(priorityUtxoSelector); + return new PriorityUtxoSelector(blockHeight); } private boolean isValidRecipientAddress() {