From 0185939be6f7c5554b864e33657ce610fd434e18 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 5 Mar 2018 16:29:37 -0500 Subject: Implement Branch and Bound coin selection in a new file Create a new file for coin selection logic and implement the BnB algorithm in it. --- src/wallet/coinselection.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/wallet/coinselection.h (limited to 'src/wallet/coinselection.h') diff --git a/src/wallet/coinselection.h b/src/wallet/coinselection.h new file mode 100644 index 0000000000..a98f1cc7c1 --- /dev/null +++ b/src/wallet/coinselection.h @@ -0,0 +1,15 @@ +// Copyright (c) 2017 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_COINSELECTION_H +#define BITCOIN_COINSELECTION_H + +#include +#include +#include +#include + +bool SelectCoinsBnB(std::vector& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set& out_set, CAmount& value_ret, CAmount not_input_fees); + +#endif // BITCOIN_COINSELECTION_H -- cgit v1.2.3 From 4b2716da46e96c45206db869b83c28c5fc7889f4 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 7 Mar 2018 12:18:37 -0500 Subject: Remove coinselection.h -> wallet.h circular dependency Changes CInputCoin to coinselection and to use CTransactionRef in order to avoid a circular dependency. Also moves other coin selection specific variables out of wallet.h to coinselectoin.h --- src/wallet/coinselection.h | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/wallet/coinselection.h') diff --git a/src/wallet/coinselection.h b/src/wallet/coinselection.h index a98f1cc7c1..bbfa08a242 100644 --- a/src/wallet/coinselection.h +++ b/src/wallet/coinselection.h @@ -8,7 +8,44 @@ #include #include #include -#include + +//! target minimum change amount +static const CAmount MIN_CHANGE = CENT; +//! final minimum change amount after paying for fees +static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2; + +class CInputCoin { +public: + CInputCoin(const CTransactionRef& tx, unsigned int i) + { + if (!tx) + throw std::invalid_argument("tx should not be null"); + if (i >= tx->vout.size()) + throw std::out_of_range("The output index is out of range"); + + outpoint = COutPoint(tx->GetHash(), i); + txout = tx->vout[i]; + effective_value = txout.nValue; + } + + COutPoint outpoint; + CTxOut txout; + CAmount effective_value; + CAmount fee = 0; + CAmount long_term_fee = 0; + + bool operator<(const CInputCoin& rhs) const { + return outpoint < rhs.outpoint; + } + + bool operator!=(const CInputCoin& rhs) const { + return outpoint != rhs.outpoint; + } + + bool operator==(const CInputCoin& rhs) const { + return outpoint == rhs.outpoint; + } +}; bool SelectCoinsBnB(std::vector& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set& out_set, CAmount& value_ret, CAmount not_input_fees); -- cgit v1.2.3 From fb716f7b25927e377f73b904a88ab67facfe3e55 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Fri, 9 Mar 2018 22:51:39 -0500 Subject: Move current coin selection algorithm to coinselection.{cpp,h} Moves the current coin selection algorithm out of SelectCoinsMinConf and puts it in coinselection.{cpp,h}. The new function, KnapsackSolver, instead of taking a vector of COutputs, will take a vector of CInputCoins that is prepared by SelectCoinsMinConf. --- src/wallet/coinselection.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/wallet/coinselection.h') diff --git a/src/wallet/coinselection.h b/src/wallet/coinselection.h index bbfa08a242..4d1a43bc17 100644 --- a/src/wallet/coinselection.h +++ b/src/wallet/coinselection.h @@ -49,4 +49,6 @@ public: bool SelectCoinsBnB(std::vector& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set& out_set, CAmount& value_ret, CAmount not_input_fees); +// Original coin selection algorithm as a fallback +bool KnapsackSolver(const CAmount& nTargetValue, std::vector& vCoins, std::set& setCoinsRet, CAmount& nValueRet); #endif // BITCOIN_COINSELECTION_H -- cgit v1.2.3