0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -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);
}
bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
{
const auto it = m_selected.find(outpoint);
return it != m_selected.end() && it->second.HasInputWeight();
}
int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
{
return m_selected.at(outpoint).GetInputWeight();
return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
}
void PreselectedInput::SetTxOut(const CTxOut& txout)
@ -104,14 +99,8 @@ void PreselectedInput::SetInputWeight(int64_t weight)
m_weight = weight;
}
int64_t PreselectedInput::GetInputWeight() const
std::optional<int64_t> PreselectedInput::GetInputWeight() const
{
assert(m_weight.has_value());
return m_weight.value();
}
bool PreselectedInput::HasInputWeight() const
{
return m_weight.has_value();
return m_weight;
}
} // namespace wallet

View file

@ -46,9 +46,7 @@ public:
/** Set the weight for this input. */
void SetInputWeight(int64_t weight);
/** Retrieve the input weight for this input. */
int64_t GetInputWeight() const;
/** Return whether the input weight is set. */
bool HasInputWeight() const;
std::optional<int64_t> GetInputWeight() const;
};
/** Coin Control Features. */
@ -131,14 +129,10 @@ public:
* 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;
std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
private:
//! 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)
{
// If weight was provided, use that.
if (coin_control && coin_control->HasInputWeight(txin.prevout)) {
return coin_control->GetInputWeight(txin.prevout);
std::optional<int64_t> weight;
if (coin_control && (weight = coin_control->GetInputWeight(txin.prevout))) {
return weight.value();
}
// 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();
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()) {
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;
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
// 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())};
}
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 {
// The input is external. We did not find the tx in mapWallet.
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);
}
// 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) {
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);
},
[&] {
// Condition to avoid the assertion in GetInputWeight
if (coin_control.HasInputWeight(out_point)) {
(void)coin_control.GetInputWeight(out_point);
}
(void)coin_control.GetInputWeight(out_point);
});
}
}