0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05:00

wallet: Move CoinCointrol definitions to .cpp

Move definitions to coincontrol.cpp and add documentation.
This commit is contained in:
Aurèle Oulès 2022-09-12 12:13:17 +02:00
parent 1db23da6e1
commit 94776621ba
2 changed files with 121 additions and 67 deletions

View file

@ -11,4 +11,72 @@ CCoinControl::CCoinControl()
{
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();
}
void CCoinControl::ListSelected(std::vector<COutPoint>& vOutpoints) const
{
vOutpoints.assign(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

View file

@ -63,76 +63,62 @@ public:
CCoinControl();
bool HasSelected() const
{
return !m_selected_inputs.empty();
}
bool IsSelected(const COutPoint& output) const
{
return m_selected_inputs.count(output) > 0;
}
bool IsExternalSelected(const COutPoint& output) const
{
return m_external_txouts.count(output) > 0;
}
std::optional<CTxOut> 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 Select(const COutPoint& output)
{
m_selected_inputs.insert(output);
}
void SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
{
m_selected_inputs.insert(outpoint);
m_external_txouts.emplace(outpoint, txout);
}
void UnSelect(const COutPoint& output)
{
m_selected_inputs.erase(output);
}
void UnSelectAll()
{
m_selected_inputs.clear();
}
void ListSelected(std::vector<COutPoint>& vOutpoints) const
{
vOutpoints.assign(m_selected_inputs.begin(), m_selected_inputs.end());
}
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;
}
/**
* Returns true if there are pre-selected inputs.
*/
bool HasSelected() const;
/**
* Returns true if the given output is pre-selected.
*/
bool IsSelected(const COutPoint& output) const;
/**
* Returns true if the given output is selected as an external input.
*/
bool IsExternalSelected(const COutPoint& output) const;
/**
* Returns the external output for the given outpoint if it exists.
*/
std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
/**
* Lock-in the given output for spending.
* The output will be included in the transaction even if it's not the most optimal choice.
*/
void Select(const COutPoint& output);
/**
* 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 SelectExternal(const COutPoint& outpoint, const CTxOut& txout);
/**
* Unselects the given output.
*/
void UnSelect(const COutPoint& output);
/**
* Unselects all outputs.
*/
void UnSelectAll();
/**
* List the selected inputs.
*/
void ListSelected(std::vector<COutPoint>& vOutpoints) const;
/**
* Set an input's weight.
*/
void SetInputWeight(const COutPoint& outpoint, int64_t weight);
/**
* Returns true if the input weight is set.
*/
bool HasInputWeight(const COutPoint& outpoint) const;
/**
* Returns the input weight.
*/
int64_t GetInputWeight(const COutPoint& outpoint) const;
private:
//! 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;
//! Map of COutPoints to the maximum weight for that input
std::map<COutPoint, int64_t> m_input_weights;