Skip to content

Commit b0d17ea

Browse files
committed
bitcoin_functional working
1 parent 2447e69 commit b0d17ea

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

src/wallet/coinselection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ static void ApproximateBestSubset(const std::vector<OutputGroup>& groups, const
213213
}
214214
}
215215

216+
// ELEMENTS:
216217
bool KnapsackSolver(const CAmountMap& mapTargetValue, std::vector<OutputGroup>& groups, std::set<CInputCoin>& setCoinsRet, CAmountMap& mapValueRet) {
217218
setCoinsRet.clear();
218219
mapValueRet.clear();
@@ -232,6 +233,7 @@ bool KnapsackSolver(const CAmountMap& mapTargetValue, std::vector<OutputGroup>&
232233
}
233234

234235
// Get output groups that only contain this asset.
236+
// We consider that groups will almost always be of one asset entirely.
235237
for (OutputGroup g : groups) {
236238
bool add = true;
237239
for (CInputCoin c : g.m_outputs) {
@@ -254,8 +256,7 @@ bool KnapsackSolver(const CAmountMap& mapTargetValue, std::vector<OutputGroup>&
254256
}
255257

256258
CAmount outValue;
257-
bool ret = KnapsackSolver(it->second, asset_groups, asset_coinsret, outValue);
258-
if (!ret) {
259+
if (!KnapsackSolver(it->second, asset_groups, asset_coinsret, outValue)) {
259260
return false;
260261
}
261262
mapValueRet[it->first] = outValue;

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,9 @@ UniValue AmountMapToUniv(const CAmountMap& balance, std::string strasset)
120120
asset = policyAsset;
121121
}
122122

123-
// The code below circumvents the const constraint that prevents the line below.
124-
//return ValueFromAmount(balance[asset]);
125-
for(std::map<CAsset, CAmount>::const_iterator it = balance.begin(); it != balance.end(); ++it) {
126-
if (it->first == asset) {
127-
return ValueFromAmount(it->second);
128-
}
123+
std::map<CAsset, CAmount>::const_iterator it = balance.find(asset);
124+
if (it != balance.end()) {
125+
return ValueFromAmount(it->second);
129126
}
130127
return ValueFromAmount(0);
131128
}

src/wallet/wallet.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24492449
if (pcoin->tx->vout.size() <= outpoint.n)
24502450
return false;
24512451
// Just to calculate the marginal byte size
2452-
mapValueFromPresetInputs[pcoin->GetOutputAsset(outpoint.n)] += pcoin->tx->vout[outpoint.n].nValue.GetAmount();
2452+
mapValueFromPresetInputs[pcoin->GetOutputAsset(outpoint.n)] += pcoin->GetOutputValueOut(outpoint.n);
24532453
setPresetCoins.insert(CInputCoin(pcoin->tx, outpoint.n));
24542454
} else
24552455
return false; // TODO: Allow non-wallet inputs
@@ -2478,8 +2478,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24782478
size_t max_descendants = (size_t)std::max<int64_t>(1, gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT));
24792479
bool fRejectLongChains = gArgs.GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
24802480

2481-
CAmountMap mapTargetMinusPreset = mapTargetValue;
2482-
mapTargetMinusPreset -= mapValueFromPresetInputs;
2481+
CAmountMap mapTargetMinusPreset = mapTargetValue - mapValueFromPresetInputs;
24832482

24842483
bool res = mapTargetValue <= mapValueFromPresetInputs ||
24852484
SelectCoinsMinConf(mapTargetMinusPreset, CoinEligibilityFilter(1, 6, 0), groups, setCoinsRet, mapValueRet, coin_selection_params, bnb_used) ||
@@ -2814,40 +2813,44 @@ bool CWallet::CreateTransaction(const std::vector<CRecipient>& vecSend, CTransac
28142813
const CAmountMap mapChange = mapValueIn - mapValueToSelect;
28152814
LogPrintf("mapChange:\n");
28162815
PrintAmountMap(mapChange);
2816+
unsigned int changeCounter = 0;
28172817
for(std::map<CAsset, CAmount>::const_iterator it = mapChange.begin(); it != mapChange.end(); ++it) {
2818-
if (it->second > 0)
2819-
{
2820-
// Fill a vout to ourself
2821-
CTxOut newTxOut(it->first, it->second, scriptChange);
2818+
if (it->second == 0) {
2819+
continue;
2820+
}
2821+
2822+
// Fill a vout to ourself
2823+
CTxOut newTxOut(it->first, it->second, scriptChange);
28222824

2823-
// Never create dust outputs; if we would, just
2824-
// add the dust to the fee.
2825-
// The nChange when BnB is used is always going to go to fees.
2826-
if (it->first == policyAsset && (IsDust(newTxOut, discard_rate) || bnb_used))
2825+
// Never create dust outputs; if we would, just
2826+
// add the dust to the fee.
2827+
// The nChange when BnB is used is always going to go to fees.
2828+
if (it->first == policyAsset && (IsDust(newTxOut, discard_rate) || bnb_used))
2829+
{
2830+
nFeeRet += it->second;
2831+
}
2832+
else
2833+
{
2834+
if (nChangePosInOut == -1)
28272835
{
2828-
nChangePosInOut = -1;
2829-
nFeeRet += it->second;
2836+
// Insert change txn at random position:
2837+
nChangePosInOut = GetRandInt(txNew.vout.size()+1);
28302838
}
2831-
else
2839+
else if ((unsigned int)nChangePosInOut > txNew.vout.size())
28322840
{
2833-
if (nChangePosInOut == -1)
2834-
{
2835-
// Insert change txn at random position:
2836-
nChangePosInOut = GetRandInt(txNew.vout.size()+1);
2837-
}
2838-
else if ((unsigned int)nChangePosInOut > txNew.vout.size())
2839-
{
2840-
strFailReason = _("Change index out of range");
2841-
return false;
2842-
}
2843-
2844-
std::vector<CTxOut>::iterator position = txNew.vout.begin()+nChangePosInOut;
2845-
txNew.vout.insert(position, newTxOut);
2841+
strFailReason = _("Change index out of range");
2842+
return false;
28462843
}
2847-
} else {
2848-
nChangePosInOut = -1;
2844+
2845+
std::vector<CTxOut>::iterator position = txNew.vout.begin()+nChangePosInOut;
2846+
txNew.vout.insert(position, newTxOut);
2847+
changeCounter++;
28492848
}
28502849
}
2850+
if (changeCounter == 0) {
2851+
// no change inserted
2852+
nChangePosInOut = -1;
2853+
}
28512854

28522855
// Dummy fill vin for maximum size estimation
28532856
//

0 commit comments

Comments
 (0)