mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-27 03:33:27 -03:00
refactor: add new helper methods
add Shuffle, Erase, and Add to CoinsResult struct add a helper function for mapping TxoutType to OutputType Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
This commit is contained in:
parent
f5649db9d5
commit
3f27a2adce
2 changed files with 45 additions and 0 deletions
|
@ -105,6 +105,47 @@ void CoinsResult::clear()
|
||||||
other.clear();
|
other.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CoinsResult::Erase(std::set<COutPoint>& preset_coins)
|
||||||
|
{
|
||||||
|
for (auto& it : coins) {
|
||||||
|
auto& vec = it.second;
|
||||||
|
auto i = std::find_if(vec.begin(), vec.end(), [&](const COutput &c) { return preset_coins.count(c.outpoint);});
|
||||||
|
if (i != vec.end()) {
|
||||||
|
vec.erase(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoinsResult::Shuffle(FastRandomContext& rng_fast)
|
||||||
|
{
|
||||||
|
for (auto& it : coins) {
|
||||||
|
::Shuffle(it.second.begin(), it.second.end(), rng_fast);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoinsResult::Add(OutputType type, const COutput& out)
|
||||||
|
{
|
||||||
|
coins[type].emplace_back(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static OutputType GetOutputType(TxoutType type, bool is_from_p2sh)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case TxoutType::WITNESS_V1_TAPROOT:
|
||||||
|
return OutputType::BECH32M;
|
||||||
|
case TxoutType::WITNESS_V0_KEYHASH:
|
||||||
|
case TxoutType::WITNESS_V0_SCRIPTHASH:
|
||||||
|
if (is_from_p2sh) return OutputType::P2SH_SEGWIT;
|
||||||
|
else return OutputType::BECH32;
|
||||||
|
case TxoutType::SCRIPTHASH:
|
||||||
|
case TxoutType::PUBKEYHASH:
|
||||||
|
return OutputType::LEGACY;
|
||||||
|
default:
|
||||||
|
return OutputType::UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CoinsResult AvailableCoins(const CWallet& wallet,
|
CoinsResult AvailableCoins(const CWallet& wallet,
|
||||||
const CCoinControl* coinControl,
|
const CCoinControl* coinControl,
|
||||||
std::optional<CFeeRate> feerate,
|
std::optional<CFeeRate> feerate,
|
||||||
|
|
|
@ -38,6 +38,7 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* walle
|
||||||
* the CoinsResult struct as if it was a vector
|
* the CoinsResult struct as if it was a vector
|
||||||
*/
|
*/
|
||||||
struct CoinsResult {
|
struct CoinsResult {
|
||||||
|
std::map<OutputType, std::vector<COutput>> coins;
|
||||||
/** Vectors for each OutputType */
|
/** Vectors for each OutputType */
|
||||||
std::vector<COutput> legacy;
|
std::vector<COutput> legacy;
|
||||||
std::vector<COutput> P2SH_segwit;
|
std::vector<COutput> P2SH_segwit;
|
||||||
|
@ -54,6 +55,9 @@ struct CoinsResult {
|
||||||
* i.e., methods can work with individual OutputType vectors or on the entire object */
|
* i.e., methods can work with individual OutputType vectors or on the entire object */
|
||||||
uint64_t size() const;
|
uint64_t size() const;
|
||||||
void clear();
|
void clear();
|
||||||
|
void Erase(std::set<COutPoint>& preset_coins);
|
||||||
|
void Shuffle(FastRandomContext& rng_fast);
|
||||||
|
void Add(OutputType type, const COutput& out);
|
||||||
|
|
||||||
/** Sum of all available coins */
|
/** Sum of all available coins */
|
||||||
CAmount total_amount{0};
|
CAmount total_amount{0};
|
||||||
|
|
Loading…
Add table
Reference in a new issue