Skip to content

Commit 3f27a2a

Browse files
josibakefurszy
andcommitted
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 <[email protected]>
1 parent f5649db commit 3f27a2a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/wallet/spend.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,47 @@ void CoinsResult::clear()
105105
other.clear();
106106
}
107107

108+
void CoinsResult::Erase(std::set<COutPoint>& preset_coins)
109+
{
110+
for (auto& it : coins) {
111+
auto& vec = it.second;
112+
auto i = std::find_if(vec.begin(), vec.end(), [&](const COutput &c) { return preset_coins.count(c.outpoint);});
113+
if (i != vec.end()) {
114+
vec.erase(i);
115+
break;
116+
}
117+
}
118+
}
119+
120+
void CoinsResult::Shuffle(FastRandomContext& rng_fast)
121+
{
122+
for (auto& it : coins) {
123+
::Shuffle(it.second.begin(), it.second.end(), rng_fast);
124+
}
125+
}
126+
127+
void CoinsResult::Add(OutputType type, const COutput& out)
128+
{
129+
coins[type].emplace_back(out);
130+
}
131+
132+
static OutputType GetOutputType(TxoutType type, bool is_from_p2sh)
133+
{
134+
switch (type) {
135+
case TxoutType::WITNESS_V1_TAPROOT:
136+
return OutputType::BECH32M;
137+
case TxoutType::WITNESS_V0_KEYHASH:
138+
case TxoutType::WITNESS_V0_SCRIPTHASH:
139+
if (is_from_p2sh) return OutputType::P2SH_SEGWIT;
140+
else return OutputType::BECH32;
141+
case TxoutType::SCRIPTHASH:
142+
case TxoutType::PUBKEYHASH:
143+
return OutputType::LEGACY;
144+
default:
145+
return OutputType::UNKNOWN;
146+
}
147+
}
148+
108149
CoinsResult AvailableCoins(const CWallet& wallet,
109150
const CCoinControl* coinControl,
110151
std::optional<CFeeRate> feerate,

src/wallet/spend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* walle
3838
* the CoinsResult struct as if it was a vector
3939
*/
4040
struct CoinsResult {
41+
std::map<OutputType, std::vector<COutput>> coins;
4142
/** Vectors for each OutputType */
4243
std::vector<COutput> legacy;
4344
std::vector<COutput> P2SH_segwit;
@@ -54,6 +55,9 @@ struct CoinsResult {
5455
* i.e., methods can work with individual OutputType vectors or on the entire object */
5556
uint64_t size() const;
5657
void clear();
58+
void Erase(std::set<COutPoint>& preset_coins);
59+
void Shuffle(FastRandomContext& rng_fast);
60+
void Add(OutputType type, const COutput& out);
5761

5862
/** Sum of all available coins */
5963
CAmount total_amount{0};

0 commit comments

Comments
 (0)