merge bitcoin#16415: Get rid of PendingWalletTx class

Co-authored-by: PastaPastaPasta <PastaPastaPasta@users.noreply.github.com>
This commit is contained in:
Kittywhiskers Van Gogh 2021-12-12 19:08:12 +05:30
parent e51c4c41c0
commit 1e11a7ad03
6 changed files with 37 additions and 58 deletions

View File

@ -42,32 +42,6 @@
namespace interfaces { namespace interfaces {
namespace { 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. //! Construct wallet tx struct.
WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, const CWalletTx& wtx) WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, const CWalletTx& wtx)
{ {
@ -308,7 +282,7 @@ public:
LOCK2(cs_main, m_wallet->cs_wallet); LOCK2(cs_main, m_wallet->cs_wallet);
return m_wallet->ListProTxCoins(outputs); return m_wallet->ListProTxCoins(outputs);
} }
std::unique_ptr<PendingWalletTx> createTransaction(const std::vector<CRecipient>& recipients, CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
const CCoinControl& coin_control, const CCoinControl& coin_control,
bool sign, bool sign,
int& change_pos, int& change_pos,
@ -317,12 +291,28 @@ public:
{ {
auto locked_chain = m_wallet->chain().lock(); auto locked_chain = m_wallet->chain().lock();
LOCK2(mempool.cs, m_wallet->cs_wallet); LOCK2(mempool.cs, m_wallet->cs_wallet);
auto pending = MakeUnique<PendingWalletTxImpl>(*m_wallet); CReserveKey m_key(m_wallet.get());
if (!m_wallet->CreateTransaction(*locked_chain, recipients, pending->m_tx, pending->m_key, fee, change_pos, CTransactionRef tx;
if (!m_wallet->CreateTransaction(*locked_chain, recipients, tx, m_key, fee, change_pos,
fail_reason, coin_control, sign)) { fail_reason, coin_control, sign)) {
return {}; 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 transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
bool abandonTransaction(const uint256& txid) override bool abandonTransaction(const uint256& txid) override

View File

@ -32,7 +32,6 @@ struct CRecipient;
namespace interfaces { namespace interfaces {
class Handler; class Handler;
class PendingWalletTx;
struct WalletAddress; struct WalletAddress;
struct WalletBalances; struct WalletBalances;
struct WalletTx; struct WalletTx;
@ -159,13 +158,19 @@ public:
virtual void listProTxCoins(std::vector<COutPoint>& vOutpts) = 0; virtual void listProTxCoins(std::vector<COutPoint>& vOutpts) = 0;
//! Create transaction. //! Create transaction.
virtual std::unique_ptr<PendingWalletTx> createTransaction(const std::vector<CRecipient>& recipients, virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
const CCoinControl& coin_control, const CCoinControl& coin_control,
bool sign, bool sign,
int& change_pos, int& change_pos,
CAmount& fee, CAmount& fee,
std::string& fail_reason) = 0; 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. //! Return whether transaction can be abandoned.
virtual bool transactionCanBeAbandoned(const uint256& txid) = 0; virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
@ -313,21 +318,6 @@ public:
virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0; virtual std::unique_ptr<Handler> 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. //! Information about one wallet address.
struct WalletAddress struct WalletAddress
{ {

View File

@ -411,7 +411,7 @@ void SendCoinsDialog::send(QList<SendCoinsRecipient> recipients)
if (m_coin_control->IsUsingCoinJoin()) { if (m_coin_control->IsUsingCoinJoin()) {
// append number of inputs // append number of inputs
questionString.append("<hr />"); questionString.append("<hr />");
int nInputs = currentTransaction.getWtx()->get().vin.size(); int nInputs = currentTransaction.getWtx()->vin.size();
questionString.append(tr("This transaction will consume %n input(s)", "", nInputs)); questionString.append(tr("This transaction will consume %n input(s)", "", nInputs));
// warn about potential privacy issues when spending too many inputs at once // warn about potential privacy issues when spending too many inputs at once
@ -464,7 +464,7 @@ void SendCoinsDialog::send(QList<SendCoinsRecipient> recipients)
accept(); accept();
m_coin_control->UnSelectAll(); m_coin_control->UnSelectAll();
coinControlUpdateLabels(); coinControlUpdateLabels();
Q_EMIT coinsSent(currentTransaction.getWtx()->get().GetHash()); Q_EMIT coinsSent(currentTransaction.getWtx()->GetHash());
} }
fNewRecipientAllowed = true; fNewRecipientAllowed = true;
} }

View File

@ -312,11 +312,11 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
auto& newTx = transaction.getWtx(); auto& newTx = transaction.getWtx();
std::string rejectReason; 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)); return SendCoinsReturn(TransactionCommitFailed, QString::fromStdString(rejectReason));
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << newTx->get(); ssTx << *newTx;
transaction_array.append(ssTx.data(), ssTx.size()); transaction_array.append(ssTx.data(), ssTx.size());
} }

View File

@ -22,14 +22,14 @@ QList<SendCoinsRecipient> WalletModelTransaction::getRecipients() const
return recipients; return recipients;
} }
std::unique_ptr<interfaces::PendingWalletTx>& WalletModelTransaction::getWtx() CTransactionRef& WalletModelTransaction::getWtx()
{ {
return wtx; return wtx;
} }
unsigned int WalletModelTransaction::getTransactionSize() 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 CAmount WalletModelTransaction::getTransactionFee() const
@ -60,7 +60,7 @@ void WalletModelTransaction::reassignAmounts()
if (out.amount() <= 0) continue; if (out.amount() <= 0) continue;
const unsigned char* scriptStr = (const unsigned char*)out.script().data(); const unsigned char* scriptStr = (const unsigned char*)out.script().data();
CScript scriptPubKey(scriptStr, scriptStr+out.script().size()); 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) { if (txout.scriptPubKey == scriptPubKey) {
subtotal += txout.nValue; subtotal += txout.nValue;
break; break;
@ -72,7 +72,7 @@ void WalletModelTransaction::reassignAmounts()
else // normal recipient (no payment request) else // normal recipient (no payment request)
#endif #endif
{ {
for (const auto& txout : wtx->get().vout) { for (const auto& txout : wtx.get()->vout) {
CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString())); CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString()));
if (txout.scriptPubKey == scriptPubKey) { if (txout.scriptPubKey == scriptPubKey) {
rcp.amount = txout.nValue; rcp.amount = txout.nValue;

View File

@ -16,7 +16,6 @@ class SendCoinsRecipient;
namespace interfaces { namespace interfaces {
class Node; class Node;
class PendingWalletTx;
} }
/** Data model for a walletmodel transaction. */ /** Data model for a walletmodel transaction. */
@ -27,7 +26,7 @@ public:
QList<SendCoinsRecipient> getRecipients() const; QList<SendCoinsRecipient> getRecipients() const;
std::unique_ptr<interfaces::PendingWalletTx>& getWtx(); CTransactionRef& getWtx();
unsigned int getTransactionSize(); unsigned int getTransactionSize();
void setTransactionFee(const CAmount& newFee); void setTransactionFee(const CAmount& newFee);
@ -39,7 +38,7 @@ public:
private: private:
QList<SendCoinsRecipient> recipients; QList<SendCoinsRecipient> recipients;
std::unique_ptr<interfaces::PendingWalletTx> wtx; CTransactionRef wtx;
CAmount fee; CAmount fee;
}; };