0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-08 14:34:53 -05:00

coincontrol: Replace HasInputWeight with returning optional from Get

This commit is contained in:
Andrew Chow 2023-01-23 16:59:25 -05:00
parent e1abfb5b20
commit 5321786b9d
4 changed files with 17 additions and 36 deletions

View file

@ -72,15 +72,10 @@ void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
m_selected[outpoint].SetInputWeight(weight); m_selected[outpoint].SetInputWeight(weight);
} }
bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
{ {
const auto it = m_selected.find(outpoint); const auto it = m_selected.find(outpoint);
return it != m_selected.end() && it->second.HasInputWeight(); return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
}
int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
{
return m_selected.at(outpoint).GetInputWeight();
} }
void PreselectedInput::SetTxOut(const CTxOut& txout) void PreselectedInput::SetTxOut(const CTxOut& txout)
@ -104,14 +99,8 @@ void PreselectedInput::SetInputWeight(int64_t weight)
m_weight = weight; m_weight = weight;
} }
int64_t PreselectedInput::GetInputWeight() const std::optional<int64_t> PreselectedInput::GetInputWeight() const
{ {
assert(m_weight.has_value()); return m_weight;
return m_weight.value();
}
bool PreselectedInput::HasInputWeight() const
{
return m_weight.has_value();
} }
} // namespace wallet } // namespace wallet

View file

@ -46,9 +46,7 @@ public:
/** Set the weight for this input. */ /** Set the weight for this input. */
void SetInputWeight(int64_t weight); void SetInputWeight(int64_t weight);
/** Retrieve the input weight for this input. */ /** Retrieve the input weight for this input. */
int64_t GetInputWeight() const; std::optional<int64_t> GetInputWeight() const;
/** Return whether the input weight is set. */
bool HasInputWeight() const;
}; };
/** Coin Control Features. */ /** Coin Control Features. */
@ -131,14 +129,10 @@ public:
* Set an input's weight. * Set an input's weight.
*/ */
void SetInputWeight(const COutPoint& outpoint, int64_t 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. * Returns the input weight.
*/ */
int64_t GetInputWeight(const COutPoint& outpoint) const; std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
private: private:
//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not) //! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)

View file

@ -117,8 +117,9 @@ static std::optional<int64_t> GetSignedTxinWeight(const CWallet* wallet, const C
const bool can_grind_r) const bool can_grind_r)
{ {
// If weight was provided, use that. // If weight was provided, use that.
if (coin_control && coin_control->HasInputWeight(txin.prevout)) { std::optional<int64_t> weight;
return coin_control->GetInputWeight(txin.prevout); if (coin_control && (weight = coin_control->GetInputWeight(txin.prevout))) {
return weight.value();
} }
// Otherwise, use the maximum satisfaction size provided by the descriptor. // Otherwise, use the maximum satisfaction size provided by the descriptor.
@ -261,7 +262,10 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
const bool can_grind_r = wallet.CanGrindR(); const bool can_grind_r = wallet.CanGrindR();
std::map<COutPoint, CAmount> map_of_bump_fees = wallet.chain().calculateIndividualBumpFees(coin_control.ListSelected(), coin_selection_params.m_effective_feerate); std::map<COutPoint, CAmount> map_of_bump_fees = wallet.chain().calculateIndividualBumpFees(coin_control.ListSelected(), coin_selection_params.m_effective_feerate);
for (const COutPoint& outpoint : coin_control.ListSelected()) { for (const COutPoint& outpoint : coin_control.ListSelected()) {
int input_bytes = -1; int64_t input_bytes = coin_control.GetInputWeight(outpoint).value_or(-1);
if (input_bytes != -1) {
input_bytes = GetVirtualTransactionSize(input_bytes, 0, 0);
}
CTxOut txout; CTxOut txout;
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) { if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
// Clearly invalid input, fail // Clearly invalid input, fail
@ -269,7 +273,9 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
return util::Error{strprintf(_("Invalid pre-selected input %s"), outpoint.ToString())}; return util::Error{strprintf(_("Invalid pre-selected input %s"), outpoint.ToString())};
} }
txout = ptr_wtx->tx->vout.at(outpoint.n); txout = ptr_wtx->tx->vout.at(outpoint.n);
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control); if (input_bytes == -1) {
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.
const auto out{coin_control.GetExternalOutput(outpoint)}; const auto out{coin_control.GetExternalOutput(outpoint)};
@ -284,11 +290,6 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, can_grind_r, &coin_control); input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, can_grind_r, &coin_control);
} }
// If available, override calculated size with coin control specified size
if (coin_control.HasInputWeight(outpoint)) {
input_bytes = GetVirtualTransactionSize(coin_control.GetInputWeight(outpoint), 0, 0);
}
if (input_bytes == -1) { if (input_bytes == -1) {
return util::Error{strprintf(_("Not solvable pre-selected input %s"), outpoint.ToString())}; // Not solvable, can't estimate size for fee return util::Error{strprintf(_("Not solvable pre-selected input %s"), outpoint.ToString())}; // Not solvable, can't estimate size for fee
} }

View file

@ -76,10 +76,7 @@ FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
(void)coin_control.SetInputWeight(out_point, weight); (void)coin_control.SetInputWeight(out_point, weight);
}, },
[&] { [&] {
// Condition to avoid the assertion in GetInputWeight (void)coin_control.GetInputWeight(out_point);
if (coin_control.HasInputWeight(out_point)) {
(void)coin_control.GetInputWeight(out_point);
}
}); });
} }
} }