From 1e11a7ad03d4cb6370d38506b655b45c4bd5941e Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Sun, 12 Dec 2021 19:08:12 +0530 Subject: [PATCH] merge bitcoin#16415: Get rid of PendingWalletTx class Co-authored-by: PastaPastaPasta --- src/interfaces/wallet.cpp | 50 +++++++++++++------------------ src/interfaces/wallet.h | 24 +++++---------- src/qt/sendcoinsdialog.cpp | 4 +-- src/qt/walletmodel.cpp | 4 +-- src/qt/walletmodeltransaction.cpp | 8 ++--- src/qt/walletmodeltransaction.h | 5 ++-- 6 files changed, 37 insertions(+), 58 deletions(-) diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 4419cca9e8..9972711d44 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -42,32 +42,6 @@ namespace interfaces { namespace { -class PendingWalletTxImpl : public PendingWalletTx -{ -public: - explicit PendingWalletTxImpl(CWallet& wallet) : m_wallet(wallet), m_key(&wallet) {} - - const CTransaction& get() override { return *m_tx; } - - bool commit(WalletValueMap value_map, - WalletOrderForm order_form, - std::string& reject_reason) override - { - auto locked_chain = m_wallet.chain().lock(); - LOCK2(mempool.cs, m_wallet.cs_wallet); - CValidationState state; - if (!m_wallet.CommitTransaction(m_tx, std::move(value_map), std::move(order_form), m_key, state)) { - reject_reason = state.GetRejectReason(); - return false; - } - return true; - } - - CTransactionRef m_tx; - CWallet& m_wallet; - CReserveKey m_key; -}; - //! Construct wallet tx struct. WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, const CWalletTx& wtx) { @@ -308,7 +282,7 @@ public: LOCK2(cs_main, m_wallet->cs_wallet); return m_wallet->ListProTxCoins(outputs); } - std::unique_ptr createTransaction(const std::vector& recipients, + CTransactionRef createTransaction(const std::vector& recipients, const CCoinControl& coin_control, bool sign, int& change_pos, @@ -317,12 +291,28 @@ public: { auto locked_chain = m_wallet->chain().lock(); LOCK2(mempool.cs, m_wallet->cs_wallet); - auto pending = MakeUnique(*m_wallet); - if (!m_wallet->CreateTransaction(*locked_chain, recipients, pending->m_tx, pending->m_key, fee, change_pos, + CReserveKey m_key(m_wallet.get()); + CTransactionRef tx; + if (!m_wallet->CreateTransaction(*locked_chain, recipients, tx, m_key, fee, change_pos, fail_reason, coin_control, sign)) { return {}; } - return std::move(pending); + return tx; + } + bool commitTransaction(CTransactionRef tx, + WalletValueMap value_map, + WalletOrderForm order_form, + std::string& reject_reason) override + { + auto locked_chain = m_wallet->chain().lock(); + LOCK2(mempool.cs, m_wallet->cs_wallet); + CReserveKey m_key(m_wallet.get()); + CValidationState state; + if (!m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form), m_key, state)) { + reject_reason = state.GetRejectReason(); + return false; + } + return true; } bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } bool abandonTransaction(const uint256& txid) override diff --git a/src/interfaces/wallet.h b/src/interfaces/wallet.h index 4a538bf59a..da57c10b3b 100644 --- a/src/interfaces/wallet.h +++ b/src/interfaces/wallet.h @@ -32,7 +32,6 @@ struct CRecipient; namespace interfaces { class Handler; -class PendingWalletTx; struct WalletAddress; struct WalletBalances; struct WalletTx; @@ -159,13 +158,19 @@ public: virtual void listProTxCoins(std::vector& vOutpts) = 0; //! Create transaction. - virtual std::unique_ptr createTransaction(const std::vector& recipients, + virtual CTransactionRef createTransaction(const std::vector& recipients, const CCoinControl& coin_control, bool sign, int& change_pos, CAmount& fee, std::string& fail_reason) = 0; + //! Commit transaction. + virtual bool commitTransaction(CTransactionRef tx, + WalletValueMap value_map, + WalletOrderForm order_form, + std::string& reject_reason) = 0; + //! Return whether transaction can be abandoned. virtual bool transactionCanBeAbandoned(const uint256& txid) = 0; @@ -313,21 +318,6 @@ public: virtual std::unique_ptr handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0; }; -//! Tracking object returned by CreateTransaction and passed to CommitTransaction. -class PendingWalletTx -{ -public: - virtual ~PendingWalletTx() {} - - //! Get transaction data. - virtual const CTransaction& get() = 0; - - //! Send pending transaction and commit to wallet. - virtual bool commit(WalletValueMap value_map, - WalletOrderForm order_form, - std::string& reject_reason) = 0; -}; - //! Information about one wallet address. struct WalletAddress { diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 2179d2f199..cca328eaad 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -411,7 +411,7 @@ void SendCoinsDialog::send(QList recipients) if (m_coin_control->IsUsingCoinJoin()) { // append number of inputs questionString.append("
"); - int nInputs = currentTransaction.getWtx()->get().vin.size(); + int nInputs = currentTransaction.getWtx()->vin.size(); questionString.append(tr("This transaction will consume %n input(s)", "", nInputs)); // warn about potential privacy issues when spending too many inputs at once @@ -464,7 +464,7 @@ void SendCoinsDialog::send(QList recipients) accept(); m_coin_control->UnSelectAll(); coinControlUpdateLabels(); - Q_EMIT coinsSent(currentTransaction.getWtx()->get().GetHash()); + Q_EMIT coinsSent(currentTransaction.getWtx()->GetHash()); } fNewRecipientAllowed = true; } diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 556bdcb8c2..364b87918f 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -312,11 +312,11 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran auto& newTx = transaction.getWtx(); std::string rejectReason; - if (!newTx->commit(std::move(mapValue), std::move(vOrderForm), rejectReason)) + if (!wallet().commitTransaction(newTx, std::move(mapValue), std::move(vOrderForm), rejectReason)) return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(rejectReason)); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); - ssTx << newTx->get(); + ssTx << *newTx; transaction_array.append(ssTx.data(), ssTx.size()); } diff --git a/src/qt/walletmodeltransaction.cpp b/src/qt/walletmodeltransaction.cpp index 9e31cb4c2a..8bc3cc2463 100644 --- a/src/qt/walletmodeltransaction.cpp +++ b/src/qt/walletmodeltransaction.cpp @@ -22,14 +22,14 @@ QList WalletModelTransaction::getRecipients() const return recipients; } -std::unique_ptr& WalletModelTransaction::getWtx() +CTransactionRef& WalletModelTransaction::getWtx() { return wtx; } unsigned int WalletModelTransaction::getTransactionSize() { - return wtx ? ::GetSerializeSize(wtx->get(), SER_NETWORK, PROTOCOL_VERSION) : 0; + return wtx != nullptr ? ::GetSerializeSize(*wtx, SER_NETWORK, PROTOCOL_VERSION) : 0; } CAmount WalletModelTransaction::getTransactionFee() const @@ -60,7 +60,7 @@ void WalletModelTransaction::reassignAmounts() if (out.amount() <= 0) continue; const unsigned char* scriptStr = (const unsigned char*)out.script().data(); CScript scriptPubKey(scriptStr, scriptStr+out.script().size()); - for (const auto& txout : wtx->get().vout) { + for (const auto& txout : wtx.get()->vout) { if (txout.scriptPubKey == scriptPubKey) { subtotal += txout.nValue; break; @@ -72,7 +72,7 @@ void WalletModelTransaction::reassignAmounts() else // normal recipient (no payment request) #endif { - for (const auto& txout : wtx->get().vout) { + for (const auto& txout : wtx.get()->vout) { CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString())); if (txout.scriptPubKey == scriptPubKey) { rcp.amount = txout.nValue; diff --git a/src/qt/walletmodeltransaction.h b/src/qt/walletmodeltransaction.h index 2f01051cc3..6f3366cd08 100644 --- a/src/qt/walletmodeltransaction.h +++ b/src/qt/walletmodeltransaction.h @@ -16,7 +16,6 @@ class SendCoinsRecipient; namespace interfaces { class Node; -class PendingWalletTx; } /** Data model for a walletmodel transaction. */ @@ -27,7 +26,7 @@ public: QList getRecipients() const; - std::unique_ptr& getWtx(); + CTransactionRef& getWtx(); unsigned int getTransactionSize(); void setTransactionFee(const CAmount& newFee); @@ -39,7 +38,7 @@ public: private: QList recipients; - std::unique_ptr wtx; + CTransactionRef wtx; CAmount fee; };