Skip to content

Commit

Permalink
Refactored the DeniabilityDialog to use the new wallet::calculateDeni…
Browse files Browse the repository at this point in the history
…abilizaitonCycles API.
  • Loading branch information
denavila committed May 30, 2023
1 parent 6f4cd18 commit db722e4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 80 deletions.
81 changes: 2 additions & 79 deletions src/qt/deniabilitydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,84 +1024,6 @@ void DeniabilityDialog::setModel(WalletModel* model)
}
}

DeniabilityDialog::DeniabilizationStats DeniabilityDialog::calculateDeniabilizationStats(const COutPoint& outpoint) const
{
Assert(m_model);

Wallet& wallet = m_model->wallet();

auto tx = wallet.getTx(outpoint.hash);
if (!tx) {
return DeniabilizationStats(0, false);
}

if (tx->IsCoinBase()) {
// this is a block reward tx, so we tag it as such
return DeniabilizationStats(0, true);
}

// an deniabilized coin is one we sent to ourselves
// all txIn should belong to our wallet
if (tx->vin.empty()) {
return DeniabilizationStats(0, false);
}
for (const auto& txIn : tx->vin) {
if (!wallet.txinIsMine(txIn)) {
return DeniabilizationStats(0, false);
}
}

// all txOut should belong to our wallet
Assert(outpoint.n < tx->vout.size());
uint n = 0;
for (const auto& txOut : tx->vout) {
if (!wallet.txoutIsMine(txOut)) {
Assert(n != outpoint.n);
return DeniabilizationStats(0, false);
}
n++;
}

uint uniqueTxOutCount = 0;
for (const auto& txOut : tx->vout) {
// check if it's a valid destination
CTxDestination txOutDestination;
if (!ExtractDestination(txOut.scriptPubKey, txOutDestination)) {
continue;
}

// don't count outputs that match any input addresses (eg it's change output)
bool matchesInput = false;
for (const auto& txIn : tx->vin) {
auto prevTx = wallet.getTx(txIn.prevout.hash);
if (prevTx && prevTx->vout[txIn.prevout.n].scriptPubKey == txOut.scriptPubKey) {
matchesInput = true;
break;
}
}
if (matchesInput) {
continue;
}

uniqueTxOutCount++;
}

// we consider two or more unique outputs an "deniabilization" of the coin
uint deniabilizationCycles = uniqueTxOutCount >= 2 ? 1 : 0;

// all txIn and txOut are from our wallet
// however if we have multiple txIn this was either an initial deniabilization of multiple UTXOs or the user manually merged deniabilized UTXOs
// in either case we don't need to recurse into parent transactions and we can return the calculated cycles
if (tx->vin.size() > 1) {
return DeniabilizationStats(deniabilizationCycles, false);
}

const auto& txIn = tx->vin[0];
// now recursively calculate the deniabilization cycles of the input
DeniabilizationStats inputStats = calculateDeniabilizationStats(txIn.prevout);
return DeniabilizationStats(inputStats.cycles + deniabilizationCycles, inputStats.blockReward);
};

void DeniabilityDialog::updateCoins()
{
if (!m_model) {
Expand Down Expand Up @@ -1179,7 +1101,8 @@ void DeniabilityDialog::updateCoins()
// skip spent outputs
if (output.walletTxOut.is_spent)
continue;
output.deniabilizationStats = calculateDeniabilizationStats(output.outpoint);
auto result = wallet.calculateDeniabilizationCycles(output.outpoint);
output.deniabilizationStats = DeniabilizationStats(result.first, result.second);
coin.utxos.push_back(std::move(output));
}
// skip any addresses with no unspent outputs
Expand Down
1 change: 0 additions & 1 deletion src/qt/deniabilitydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public Q_SLOTS:
void updateStart();
void updateStatus();
void updateCoinTable();
DeniabilizationStats calculateDeniabilizationStats(const COutPoint& outpoint) const;

bool signExternalSigner(interfaces::Wallet& wallet, CTransactionRef& tx, const QString& message);
void finalizeTxBroadcast(uint256 hash, CAmount txFee);
Expand Down

0 comments on commit db722e4

Please sign in to comment.