mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-23 13:29:43 +02:00
Merge #9680: Unify CWalletTx construction
b4bc32a451[wallet] Get rid of CWalletTx default constructor (Russell Yanofsky)a128bdc9e1[wallet] Construct CWalletTx objects in CommitTransaction (Russell Yanofsky) Pull request description: Two commits: - `Construct CWalletTx objects in CommitTransaction` moves a bunch of CWalletTx initialization into CWallet::CommitTransaction to dedup some code and avoid future inconsistencies in how wallet transactions are created. - `Get rid of CWalletTx default constructor` does what is described and eliminates the possibility of empty transaction entries being inadvertently created by mapWallet[hash] accesses. Both of these changes were originally part of #9381 Tree-SHA512: af3841c4f0539e0662d81b33c5369fc70aa06ddde1c59cb00fb21c9e4c7d9ff47f1edc5040cb463af1333838802c56b3ef875b939e2b804ee45b8e0294a4371c
This commit is contained in:
@@ -404,7 +404,7 @@ UniValue getaddressesbyaccount(const JSONRPCRequest& request)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, CWalletTx& wtxNew, const CCoinControl& coin_control)
|
||||
static CTransactionRef SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue, std::string fromAccount)
|
||||
{
|
||||
CAmount curBalance = pwallet->GetBalance();
|
||||
|
||||
@@ -430,16 +430,18 @@ static void SendMoney(CWallet * const pwallet, const CTxDestination &address, CA
|
||||
int nChangePosRet = -1;
|
||||
CRecipient recipient = {scriptPubKey, nValue, fSubtractFeeFromAmount};
|
||||
vecSend.push_back(recipient);
|
||||
if (!pwallet->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosRet, strError, coin_control)) {
|
||||
CTransactionRef tx;
|
||||
if (!pwallet->CreateTransaction(vecSend, tx, reservekey, nFeeRequired, nChangePosRet, strError, coin_control)) {
|
||||
if (!fSubtractFeeFromAmount && nValue + nFeeRequired > curBalance)
|
||||
strError = strprintf("Error: This transaction requires a transaction fee of at least %s", FormatMoney(nFeeRequired));
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, strError);
|
||||
}
|
||||
CValidationState state;
|
||||
if (!pwallet->CommitTransaction(wtxNew, reservekey, g_connman.get(), state)) {
|
||||
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, std::move(fromAccount), reservekey, g_connman.get(), state)) {
|
||||
strError = strprintf("Error: The transaction was rejected! Reason given: %s", FormatStateMessage(state));
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, strError);
|
||||
}
|
||||
return tx;
|
||||
}
|
||||
|
||||
UniValue sendtoaddress(const JSONRPCRequest& request)
|
||||
@@ -498,11 +500,11 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
|
||||
|
||||
// Wallet comments
|
||||
CWalletTx wtx;
|
||||
mapValue_t mapValue;
|
||||
if (!request.params[2].isNull() && !request.params[2].get_str().empty())
|
||||
wtx.mapValue["comment"] = request.params[2].get_str();
|
||||
mapValue["comment"] = request.params[2].get_str();
|
||||
if (!request.params[3].isNull() && !request.params[3].get_str().empty())
|
||||
wtx.mapValue["to"] = request.params[3].get_str();
|
||||
mapValue["to"] = request.params[3].get_str();
|
||||
|
||||
bool fSubtractFeeFromAmount = false;
|
||||
if (!request.params[4].isNull()) {
|
||||
@@ -527,9 +529,8 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
|
||||
|
||||
EnsureWalletIsUnlocked(pwallet);
|
||||
|
||||
SendMoney(pwallet, dest, nAmount, fSubtractFeeFromAmount, wtx, coin_control);
|
||||
|
||||
return wtx.GetHash().GetHex();
|
||||
CTransactionRef tx = SendMoney(pwallet, dest, nAmount, fSubtractFeeFromAmount, coin_control, std::move(mapValue), {} /* fromAccount */);
|
||||
return tx->GetHash().GetHex();
|
||||
}
|
||||
|
||||
UniValue listaddressgroupings(const JSONRPCRequest& request)
|
||||
@@ -995,12 +996,11 @@ UniValue sendfrom(const JSONRPCRequest& request)
|
||||
if (!request.params[3].isNull())
|
||||
nMinDepth = request.params[3].get_int();
|
||||
|
||||
CWalletTx wtx;
|
||||
wtx.strFromAccount = strAccount;
|
||||
mapValue_t mapValue;
|
||||
if (!request.params[4].isNull() && !request.params[4].get_str().empty())
|
||||
wtx.mapValue["comment"] = request.params[4].get_str();
|
||||
mapValue["comment"] = request.params[4].get_str();
|
||||
if (!request.params[5].isNull() && !request.params[5].get_str().empty())
|
||||
wtx.mapValue["to"] = request.params[5].get_str();
|
||||
mapValue["to"] = request.params[5].get_str();
|
||||
|
||||
EnsureWalletIsUnlocked(pwallet);
|
||||
|
||||
@@ -1010,9 +1010,8 @@ UniValue sendfrom(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
||||
|
||||
CCoinControl no_coin_control; // This is a deprecated API
|
||||
SendMoney(pwallet, dest, nAmount, false, wtx, no_coin_control);
|
||||
|
||||
return wtx.GetHash().GetHex();
|
||||
CTransactionRef tx = SendMoney(pwallet, dest, nAmount, false, no_coin_control, std::move(mapValue), std::move(strAccount));
|
||||
return tx->GetHash().GetHex();
|
||||
}
|
||||
|
||||
|
||||
@@ -1083,10 +1082,9 @@ UniValue sendmany(const JSONRPCRequest& request)
|
||||
if (!request.params[2].isNull())
|
||||
nMinDepth = request.params[2].get_int();
|
||||
|
||||
CWalletTx wtx;
|
||||
wtx.strFromAccount = strAccount;
|
||||
mapValue_t mapValue;
|
||||
if (!request.params[3].isNull() && !request.params[3].get_str().empty())
|
||||
wtx.mapValue["comment"] = request.params[3].get_str();
|
||||
mapValue["comment"] = request.params[3].get_str();
|
||||
|
||||
UniValue subtractFeeFromAmount(UniValue::VARR);
|
||||
if (!request.params[4].isNull())
|
||||
@@ -1152,16 +1150,17 @@ UniValue sendmany(const JSONRPCRequest& request)
|
||||
CAmount nFeeRequired = 0;
|
||||
int nChangePosRet = -1;
|
||||
std::string strFailReason;
|
||||
bool fCreated = pwallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
|
||||
CTransactionRef tx;
|
||||
bool fCreated = pwallet->CreateTransaction(vecSend, tx, keyChange, nFeeRequired, nChangePosRet, strFailReason, coin_control);
|
||||
if (!fCreated)
|
||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strFailReason);
|
||||
CValidationState state;
|
||||
if (!pwallet->CommitTransaction(wtx, keyChange, g_connman.get(), state)) {
|
||||
if (!pwallet->CommitTransaction(tx, std::move(mapValue), {} /* orderForm */, std::move(strAccount), keyChange, g_connman.get(), state)) {
|
||||
strFailReason = strprintf("Transaction commit failed:: %s", FormatStateMessage(state));
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, strFailReason);
|
||||
}
|
||||
|
||||
return wtx.GetHash().GetHex();
|
||||
return tx->GetHash().GetHex();
|
||||
}
|
||||
|
||||
UniValue addmultisigaddress(const JSONRPCRequest& request)
|
||||
|
||||
Reference in New Issue
Block a user