From 6c0e2cacfeee1da2b27f1e205b3e70ad829f40c2 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 8 Jul 2019 11:41:46 -0400 Subject: [PATCH] merge unrelated tx blocks in MoneroWallet.cpp:mergeTx() cleanup; remove -g for debugger --- src/daemon/MoneroDaemonModel.cpp | 77 -------------------------------- src/wallet/MoneroWallet.cpp | 47 +++++++++---------- src/wallet/MoneroWalletModel.cpp | 43 +----------------- 3 files changed, 23 insertions(+), 144 deletions(-) diff --git a/src/daemon/MoneroDaemonModel.cpp b/src/daemon/MoneroDaemonModel.cpp index 7122578..f5b82b1 100644 --- a/src/daemon/MoneroDaemonModel.cpp +++ b/src/daemon/MoneroDaemonModel.cpp @@ -64,13 +64,8 @@ namespace monero { // ----------------------- UNDECLARED PRIVATE HELPERS ----------------------- void mergeTx(vector> txs, const shared_ptr& tx) { - cout << "Merging this other guy in: " << tx << endl; for (const shared_ptr& aTx : txs) { - cout << "Comparaing against self tx: " << aTx << endl; if (aTx->id.get() == tx->id.get()) { - cout << "And we're merging..." << endl; - cout << aTx << endl; - cout << tx << endl; aTx->merge(aTx, tx); return; } @@ -150,26 +145,13 @@ namespace monero { } void MoneroBlock::merge(const shared_ptr& self, const shared_ptr& other) { - cout << "CALLING MONERO BLOCK BASE MERGE" << endl; merge(static_pointer_cast(self), static_pointer_cast(other)); } void MoneroBlock::merge(const shared_ptr& self, const shared_ptr& other) { cout << "MoneroBlock::merge()" << endl; - cout << self << ", " << other << endl; if (this != self.get()) throw runtime_error("this != self"); if (self == other) return; - cout << "1" << endl; - - cout << "Lets iterate over self block tx pointers" << endl; - for (const shared_ptr& tx : self->txs) { - cout << tx << endl; - } - - cout << "Lets iterate over other block tx pointers" << endl; - for (const shared_ptr& tx : other->txs) { - cout << tx << endl; - } // merge base classes MoneroBlockHeader::merge(self, other); @@ -178,58 +160,17 @@ namespace monero { hex = MoneroUtils::reconcile(hex, other->hex); txIds = MoneroUtils::reconcile(txIds, other->txIds); - cout << "2" << endl; - // merge coinbase tx if (coinbaseTx == boost::none) coinbaseTx = other->coinbaseTx; else if (other->coinbaseTx != boost::none) coinbaseTx.get()->merge(*coinbaseTx, *other->coinbaseTx); - cout << "3" << endl; - - // merge non-coinbase txs if (!other->txs.empty()) { - cout << "4" << endl; for (const shared_ptr otherTx : other->txs) { // NOTE: not using reference so shared_ptr is not deleted when block is dereferenced - cout << "5" << endl; - cout << "Merging into existing txs: " << otherTx << endl; otherTx->block = self; - cout << "Merging into existing txs: " << otherTx << endl; mergeTx(self->txs, otherTx); } } - -// // merge non-coinbase txs -// if (txs.empty()) { -// txs = other->txs; -// cout << "4" << endl; -// } -// else if (!other->txs.empty()) { -// cout << "5" << endl; -// for (const shared_ptr& thatTx : other->txs) { -// bool found = false; -// for (const shared_ptr& thisTx : txs) { -// cout << "6" << endl; -// if (thatTx->id == thisTx->id) { -// cout << "7" << endl; -// thatTx->block = self; -// cout << "And here we go for merging txs" << endl; -// thisTx->merge(thisTx, thatTx); -// found = true; -// break; -// } -// } -// if (!found) txs.push_back(thatTx); -// } -// } -// cout << "4.2" << endl; -// if (!txs.empty()) { -// for (const shared_ptr& tx : txs) { -// tx->block = self; -// } -// } - - cout << "Returning from MoneroBlock merge" << endl; } // ------------------------------- MONERO TX -------------------------------- @@ -286,35 +227,23 @@ namespace monero { cout << "MoneroTx::merge()" << endl; if (this != self.get()) throw runtime_error("this != self"); cout << self << ", " << other << endl; - cout << "1" << endl; if (self == other) return; - cout << "2" << endl; // merge blocks if they're different which comes back to merging txs if (block != other->block) { - cout << "4" << endl; if (block == boost::none) { - cout << "5" << endl; block = shared_ptr(new MoneroBlock()); block.get()->txs.push_back(self); block.get()->height = other->getHeight(); } if (other->block == boost::none) { - cout << "About to access other->block" << endl; - cout << other->block << endl; - cout << "6" << endl; other->block = shared_ptr(new MoneroBlock()); - cout << "7" << endl; other->block.get()->txs.push_back(other); - cout << "8" << endl; other->block.get()->height = self->getHeight(); - cout << "9" << endl; } - cout << "Merging blocks!" << endl; block.get()->merge(block.get(), other->block.get()); return; } - cout << "merging tx fields!!!" << endl; // otherwise merge tx fields id = MoneroUtils::reconcile(id, other->id); @@ -349,8 +278,6 @@ namespace monero { unlockTime = MoneroUtils::reconcile(unlockTime, other->unlockTime); numConfirmations = MoneroUtils::reconcile(numConfirmations, other->numConfirmations); - cout << "Merging vins!" << endl; - // merge vins if (!other->vins.empty()) { for (shared_ptr merger : other->vins) { @@ -367,8 +294,6 @@ namespace monero { } } - cout << "Merging vouts!" << endl; - // merge vouts if (!other->vouts.empty()) { for (shared_ptr vout : other->vouts) vout->tx = self; @@ -427,8 +352,6 @@ namespace monero { receivedTimestamp = MoneroUtils::reconcile(receivedTimestamp, other->receivedTimestamp, boost::none, boost::none, false); // take earliest receive time lastRelayedTimestamp = MoneroUtils::reconcile(lastRelayedTimestamp, other->lastRelayedTimestamp, boost::none, boost::none, true); // take latest relay time } - - cout << "Returning from MoneroTx::merge()" << endl; } // --------------------------- MONERO KEY IMAGE ----------------------------- diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index f1a79d7..d8080c3 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -348,21 +348,10 @@ namespace monero { * * @param tx is the transaction to merge into the existing txs * @param txMap maps tx ids to txs + * @param blockMap maps block heights to blocks * @param skipIfAbsent specifies if the tx should not be added if it doesn't already exist */ - void mergeTx(shared_ptr tx, map>& txMap, bool skipIfAbsent) { - - if (tx->block != boost::none) { - bool found = false; - for (const shared_ptr& blockTx : tx->block.get()->txs) { - if (tx == blockTx) { - found = true; - break; - } - } - if (!found) throw new runtime_error("But this tx's block does not reference the tx"); - } - + void mergeTx(shared_ptr tx, map>& txMap, map>& blockMap, bool skipIfAbsent) { if (tx->id == boost::none) throw runtime_error("Tx id is not initialized"); // if tx doesn't exist, add it (unless skipped) @@ -381,7 +370,16 @@ namespace monero { aTx->merge(aTx, tx); } - cout << "Now how back is the tx map? " << txMap.size() << endl; + // if confirmed, merge tx's block + if (tx->getHeight() != boost::none) { + map>::const_iterator blockIter = blockMap.find(tx->getHeight().get()); + if (blockIter == blockMap.end()) { + blockMap[tx->getHeight().get()] = tx->block.get(); + } else { + shared_ptr& aBlock = blockMap[tx->getHeight().get()]; + aBlock->merge(aBlock, tx->block.get()); + } + } } /** @@ -1197,8 +1195,9 @@ namespace monero { // cache types into maps for merging and lookup map> txMap; + map> blockMap; for (const shared_ptr& tx : txs) { - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } // fetch and merge outputs if requested @@ -1214,7 +1213,7 @@ namespace monero { for (const shared_ptr& output : outputs) { shared_ptr tx = static_pointer_cast(output->tx); if (outputTxs.find(tx) == outputTxs.end()) { - mergeTx(tx, txMap, true); + mergeTx(tx, txMap, blockMap, true); outputTxs.insert(tx); } } @@ -1316,6 +1315,7 @@ namespace monero { // collect unique txs uint64_t chainHeight = getChainHeight(); map> txMap; + map> blockMap; // get confirmed incoming transfers if (isIn) { @@ -1323,8 +1323,7 @@ namespace monero { wallet2->get_payments(payments, minHeight, maxHeight, accountIndex, subaddressIndices); for (std::list>::const_iterator i = payments.begin(); i != payments.end(); ++i) { shared_ptr tx = buildTxWithIncomingTransfer(*wallet2, chainHeight, i->first, i->second); - if (tx->getHeight().get() != 360559l) continue; - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } } @@ -1336,8 +1335,7 @@ namespace monero { wallet2->get_payments_out(payments, minHeight, maxHeight, accountIndex, subaddressIndices); for (std::list>::const_iterator i = payments.begin(); i != payments.end(); ++i) { shared_ptr tx = buildTxWithOutgoingTransfer(*wallet2, chainHeight, i->first, i->second); - if (tx->getHeight().get() != 360559l) continue; - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } } @@ -1349,9 +1347,8 @@ namespace monero { wallet2->get_unconfirmed_payments_out(upayments, accountIndex, subaddressIndices); for (std::list>::const_iterator i = upayments.begin(); i != upayments.end(); ++i) { shared_ptr tx = buildTxWithOutgoingTransferUnconfirmed(*wallet2, i->first, i->second); - if (tx->getHeight().get() != 360559l) continue; if (txReq.isFailed != boost::none && txReq.isFailed.get() != tx->isFailed.get()) continue; // skip merging if tx unrequested - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } } @@ -1364,8 +1361,7 @@ namespace monero { wallet2->get_unconfirmed_payments(payments, accountIndex, subaddressIndices); for (std::list>::const_iterator i = payments.begin(); i != payments.end(); ++i) { shared_ptr tx = buildTxWithIncomingTransferUnconfirmed(*wallet2, i->first, i->second); - if (tx->getHeight().get() != 360559l) continue; - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } } @@ -1421,10 +1417,11 @@ namespace monero { // collect unique txs and blocks map> txMap; + map> blockMap; for (const auto& outputW2 : outputsW2) { // TODO: skip tx building if w2 output filtered by indices, etc shared_ptr tx = buildTxWithVout(*wallet2, outputW2); - mergeTx(tx, txMap, false); + mergeTx(tx, txMap, blockMap, false); } // collect requested vouts diff --git a/src/wallet/MoneroWalletModel.cpp b/src/wallet/MoneroWalletModel.cpp index 690cc37..8c5f089 100644 --- a/src/wallet/MoneroWalletModel.cpp +++ b/src/wallet/MoneroWalletModel.cpp @@ -129,57 +129,20 @@ namespace monero { void MoneroTxWallet::merge(const shared_ptr& self, const shared_ptr& other) { cout << "MoneroTxWallet::merge(MoneroTxs)" << endl; - cout << self << ", " << other << endl; - - //cout << "MoneroTxWallet::merge(MoneroTx&, MoneroTx&)" << endl; - cout << self->id << endl; - cout << "bump" << endl; - cout << other->id << endl; - cout << "And onward..." << endl; - shared_ptr temp = dynamic_pointer_cast(self); - if (temp == nullptr) cout << "you're gonna be in for a bad time" << endl; - shared_ptr temp2 = dynamic_pointer_cast(other); - if (temp2 == nullptr) cout << "you're gonna be in for a bad time 2" << endl; - merge(static_pointer_cast(self), static_pointer_cast(other)); } void MoneroTxWallet::merge(const shared_ptr& self, const shared_ptr& other) { cout << "MoneroTxWallet::merge()" << endl; - cout << self << ", " << other << endl; - - - cout << "Lets iterate over self block tx pointers" << endl; - if (self->block != boost::none) { - for (const shared_ptr& tx : self->block.get()->txs) { - cout << tx << endl; - } - } - - cout << "Lets iterate over other block tx pointers" << endl; - if (other->block != boost::none) { - for (const shared_ptr& tx : other->block.get()->txs) { - cout << tx << endl; - } - } - if (this != self.get()) throw runtime_error("this != self"); - if (self == other) { - cout << "These are the same reference so we're returning" << endl; - return; - } + if (self == other) return; // merge base classes - cout << "Merging base classes" << endl; MoneroTx::merge(self, other); - cout << "Returned from MoneroTx::merge()" << endl; - // merge wallet extensions note = MoneroUtils::reconcile(note, other->note); - cout << "2" << endl; - // merge incoming transfers if (!other->incomingTransfers.empty()) { for (const shared_ptr transfer : other->incomingTransfers) { // NOTE: not using reference so shared_ptr is not deleted when tx is dereferenced @@ -188,16 +151,12 @@ namespace monero { } } - cout << "3" << endl; - // merge outgoing transfer if (other->outgoingTransfer != boost::none) { other->outgoingTransfer.get()->tx = self; if (self->outgoingTransfer == boost::none) self->outgoingTransfer = other->outgoingTransfer; else self->outgoingTransfer.get()->merge(self->outgoingTransfer.get(), other->outgoingTransfer.get()); } - - cout << "4" << endl; } //boost::property_tree::ptree MoneroUtils::txWalletToPropertyTree(const MoneroTxWallet& tx) {