mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
Merge bitcoin/bitcoin#26066: wallet: Refactor and document CoinControl
daba95700b
refactor: Make ListSelected return vector (Sebastian Falbesoner)94776621ba
wallet: Move CoinCointrol definitions to .cpp (Aurèle Oulès)1db23da6e1
wallet: Use std::optional for GetExternalOutput and fixups (Aurèle Oulès)becc45b589
scripted-diff: Rename setSelected->m_selected_inputs (Aurèle Oulès) Pull request description: - Moves CoinControl function definitions from `coincontrol.h` to `coincontrol.cpp` - Adds more documentation - Renames class member for an improved comprehension - Use `std::optional` for `GetExternalOutput` ACKs for top commit: achow101: ACKdaba95700b
Xekyo: ACKdaba95700b
Tree-SHA512: 3bf2dc834a3246c2f53f8c55154258e605fcb169431d3f7b156931f33c7e3b1ae28e03e16b37f9140a827890eb7798be485b2c36bfc23ff29bb01763f289a07c
This commit is contained in:
commit
0e70a1b625
4 changed files with 133 additions and 83 deletions
|
@ -412,8 +412,7 @@ void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel *
|
||||||
unsigned int nQuantity = 0;
|
unsigned int nQuantity = 0;
|
||||||
bool fWitness = false;
|
bool fWitness = false;
|
||||||
|
|
||||||
std::vector<COutPoint> vCoinControl;
|
auto vCoinControl{m_coin_control.ListSelected()};
|
||||||
m_coin_control.ListSelected(vCoinControl);
|
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (const auto& out : model->wallet().getCoins(vCoinControl)) {
|
for (const auto& out : model->wallet().getCoins(vCoinControl)) {
|
||||||
|
|
|
@ -11,4 +11,72 @@ CCoinControl::CCoinControl()
|
||||||
{
|
{
|
||||||
m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS);
|
m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CCoinControl::HasSelected() const
|
||||||
|
{
|
||||||
|
return !m_selected_inputs.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CCoinControl::IsSelected(const COutPoint& output) const
|
||||||
|
{
|
||||||
|
return m_selected_inputs.count(output) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CCoinControl::IsExternalSelected(const COutPoint& output) const
|
||||||
|
{
|
||||||
|
return m_external_txouts.count(output) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
|
||||||
|
{
|
||||||
|
const auto ext_it = m_external_txouts.find(outpoint);
|
||||||
|
if (ext_it == m_external_txouts.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::make_optional(ext_it->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCoinControl::Select(const COutPoint& output)
|
||||||
|
{
|
||||||
|
m_selected_inputs.insert(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCoinControl::SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
|
||||||
|
{
|
||||||
|
m_selected_inputs.insert(outpoint);
|
||||||
|
m_external_txouts.emplace(outpoint, txout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCoinControl::UnSelect(const COutPoint& output)
|
||||||
|
{
|
||||||
|
m_selected_inputs.erase(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCoinControl::UnSelectAll()
|
||||||
|
{
|
||||||
|
m_selected_inputs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<COutPoint> CCoinControl::ListSelected() const
|
||||||
|
{
|
||||||
|
return {m_selected_inputs.begin(), m_selected_inputs.end()};
|
||||||
|
}
|
||||||
|
|
||||||
|
void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
|
||||||
|
{
|
||||||
|
m_input_weights[outpoint] = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
|
||||||
|
{
|
||||||
|
return m_input_weights.count(outpoint) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
|
||||||
|
{
|
||||||
|
auto it = m_input_weights.find(outpoint);
|
||||||
|
assert(it != m_input_weights.end());
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
} // namespace wallet
|
} // namespace wallet
|
||||||
|
|
|
@ -13,9 +13,9 @@
|
||||||
#include <script/signingprovider.h>
|
#include <script/signingprovider.h>
|
||||||
#include <script/standard.h>
|
#include <script/standard.h>
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <optional>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
|
@ -63,76 +63,62 @@ public:
|
||||||
|
|
||||||
CCoinControl();
|
CCoinControl();
|
||||||
|
|
||||||
bool HasSelected() const
|
/**
|
||||||
{
|
* Returns true if there are pre-selected inputs.
|
||||||
return (setSelected.size() > 0);
|
*/
|
||||||
}
|
bool HasSelected() const;
|
||||||
|
/**
|
||||||
bool IsSelected(const COutPoint& output) const
|
* Returns true if the given output is pre-selected.
|
||||||
{
|
*/
|
||||||
return (setSelected.count(output) > 0);
|
bool IsSelected(const COutPoint& output) const;
|
||||||
}
|
/**
|
||||||
|
* Returns true if the given output is selected as an external input.
|
||||||
bool IsExternalSelected(const COutPoint& output) const
|
*/
|
||||||
{
|
bool IsExternalSelected(const COutPoint& output) const;
|
||||||
return (m_external_txouts.count(output) > 0);
|
/**
|
||||||
}
|
* Returns the external output for the given outpoint if it exists.
|
||||||
|
*/
|
||||||
bool GetExternalOutput(const COutPoint& outpoint, CTxOut& txout) const
|
std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
|
||||||
{
|
/**
|
||||||
const auto ext_it = m_external_txouts.find(outpoint);
|
* Lock-in the given output for spending.
|
||||||
if (ext_it == m_external_txouts.end()) {
|
* The output will be included in the transaction even if it's not the most optimal choice.
|
||||||
return false;
|
*/
|
||||||
}
|
void Select(const COutPoint& output);
|
||||||
txout = ext_it->second;
|
/**
|
||||||
return true;
|
* Lock-in the given output as an external input for spending because it is not in the wallet.
|
||||||
}
|
* The output will be included in the transaction even if it's not the most optimal choice.
|
||||||
|
*/
|
||||||
void Select(const COutPoint& output)
|
void SelectExternal(const COutPoint& outpoint, const CTxOut& txout);
|
||||||
{
|
/**
|
||||||
setSelected.insert(output);
|
* Unselects the given output.
|
||||||
}
|
*/
|
||||||
|
void UnSelect(const COutPoint& output);
|
||||||
void SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
|
/**
|
||||||
{
|
* Unselects all outputs.
|
||||||
setSelected.insert(outpoint);
|
*/
|
||||||
m_external_txouts.emplace(outpoint, txout);
|
void UnSelectAll();
|
||||||
}
|
/**
|
||||||
|
* List the selected inputs.
|
||||||
void UnSelect(const COutPoint& output)
|
*/
|
||||||
{
|
std::vector<COutPoint> ListSelected() const;
|
||||||
setSelected.erase(output);
|
/**
|
||||||
}
|
* Set an input's weight.
|
||||||
|
*/
|
||||||
void UnSelectAll()
|
void SetInputWeight(const COutPoint& outpoint, int64_t weight);
|
||||||
{
|
/**
|
||||||
setSelected.clear();
|
* Returns true if the input weight is set.
|
||||||
}
|
*/
|
||||||
|
bool HasInputWeight(const COutPoint& outpoint) const;
|
||||||
void ListSelected(std::vector<COutPoint>& vOutpoints) const
|
/**
|
||||||
{
|
* Returns the input weight.
|
||||||
vOutpoints.assign(setSelected.begin(), setSelected.end());
|
*/
|
||||||
}
|
int64_t GetInputWeight(const COutPoint& outpoint) const;
|
||||||
|
|
||||||
void SetInputWeight(const COutPoint& outpoint, int64_t weight)
|
|
||||||
{
|
|
||||||
m_input_weights[outpoint] = weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HasInputWeight(const COutPoint& outpoint) const
|
|
||||||
{
|
|
||||||
return m_input_weights.count(outpoint) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t GetInputWeight(const COutPoint& outpoint) const
|
|
||||||
{
|
|
||||||
auto it = m_input_weights.find(outpoint);
|
|
||||||
assert(it != m_input_weights.end());
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::set<COutPoint> setSelected;
|
//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
|
||||||
|
std::set<COutPoint> m_selected_inputs;
|
||||||
|
//! Map of external inputs to include in the transaction
|
||||||
|
//! These are not in the wallet, so we need to track them separately
|
||||||
std::map<COutPoint, CTxOut> m_external_txouts;
|
std::map<COutPoint, CTxOut> m_external_txouts;
|
||||||
//! Map of COutPoints to the maximum weight for that input
|
//! Map of COutPoints to the maximum weight for that input
|
||||||
std::map<COutPoint, int64_t> m_input_weights;
|
std::map<COutPoint, int64_t> m_input_weights;
|
||||||
|
|
|
@ -52,9 +52,7 @@ int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet,
|
||||||
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control)
|
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control)
|
||||||
{
|
{
|
||||||
CMutableTransaction txNew(tx);
|
CMutableTransaction txNew(tx);
|
||||||
if (!wallet->DummySignTx(txNew, txouts, coin_control)) {
|
if (!wallet->DummySignTx(txNew, txouts, coin_control)) return TxSize{-1, -1};
|
||||||
return TxSize{-1, -1};
|
|
||||||
}
|
|
||||||
CTransaction ctx(txNew);
|
CTransaction ctx(txNew);
|
||||||
int64_t vsize = GetVirtualTransactionSize(ctx);
|
int64_t vsize = GetVirtualTransactionSize(ctx);
|
||||||
int64_t weight = GetTransactionWeight(ctx);
|
int64_t weight = GetTransactionWeight(ctx);
|
||||||
|
@ -72,11 +70,9 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *walle
|
||||||
assert(input.prevout.n < mi->second.tx->vout.size());
|
assert(input.prevout.n < mi->second.tx->vout.size());
|
||||||
txouts.emplace_back(mi->second.tx->vout.at(input.prevout.n));
|
txouts.emplace_back(mi->second.tx->vout.at(input.prevout.n));
|
||||||
} else if (coin_control) {
|
} else if (coin_control) {
|
||||||
CTxOut txout;
|
const auto& txout{coin_control->GetExternalOutput(input.prevout)};
|
||||||
if (!coin_control->GetExternalOutput(input.prevout, txout)) {
|
if (!txout) return TxSize{-1, -1};
|
||||||
return TxSize{-1, -1};
|
txouts.emplace_back(*txout);
|
||||||
}
|
|
||||||
txouts.emplace_back(txout);
|
|
||||||
} else {
|
} else {
|
||||||
return TxSize{-1, -1};
|
return TxSize{-1, -1};
|
||||||
}
|
}
|
||||||
|
@ -163,10 +159,8 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
|
||||||
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
|
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
|
||||||
{
|
{
|
||||||
PreSelectedInputs result;
|
PreSelectedInputs result;
|
||||||
std::vector<COutPoint> vPresetInputs;
|
|
||||||
coin_control.ListSelected(vPresetInputs);
|
|
||||||
const bool can_grind_r = wallet.CanGrindR();
|
const bool can_grind_r = wallet.CanGrindR();
|
||||||
for (const COutPoint& outpoint : vPresetInputs) {
|
for (const COutPoint& outpoint : coin_control.ListSelected()) {
|
||||||
int input_bytes = -1;
|
int input_bytes = -1;
|
||||||
CTxOut txout;
|
CTxOut txout;
|
||||||
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
|
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
|
||||||
|
@ -178,9 +172,12 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
|
||||||
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
|
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
|
||||||
} else {
|
} else {
|
||||||
// The input is external. We did not find the tx in mapWallet.
|
// The input is external. We did not find the tx in mapWallet.
|
||||||
if (!coin_control.GetExternalOutput(outpoint, txout)) {
|
const auto out{coin_control.GetExternalOutput(outpoint)};
|
||||||
|
if (!out) {
|
||||||
return util::Error{strprintf(_("Not found pre-selected input %s"), outpoint.ToString())};
|
return util::Error{strprintf(_("Not found pre-selected input %s"), outpoint.ToString())};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
txout = *out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input_bytes == -1) {
|
if (input_bytes == -1) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue