Skip to content

Commit f0c9e1c

Browse files
committed
Merge #14906: refactor: Make explicit CMutableTransaction -> CTransaction conversion.
b301950 Made expicit constructor CTransaction(const CMutableTransaction &tx). (lucash-dev) faf29dd Minimal changes to comply with explicit CMutableTransaction -> CTranaction conversion. (lucash-dev) Pull request description: This PR is re-submission of #14156, which was automatically closed by github (glitch?) Original description: This PR makes explicit the now implicit conversion constructor `CTransaction(const CMutableTransaction&)` in `transaction.h`. Minimal changes were made elsewhere to make the code compilable. I'll follow up with other PRs to address individually refactoring functions that should have a `CMutableTransaction` version, or where a `CTransaction` should be reused. The rationale for this change is: - Conversion constructors should not be explicit unless there's a strong reason for it (in the opinion of, for example, https://google.github.io/styleguide/cppguide.html, and https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ro-conversion. Let me know your take on this). - This particular conversion is very costly -- it implies a serialization plus hash of the transaction. - Even though `CTransaction` and `CMutableTransaction` represent the same data, they have very different use cases and performance properties. - Making it explicit allows for easier reasoning of performance trade-offs. - There has been previous performance issues caused by unneeded use of this implicit conversion. - This PR creates a map for places to look for possible refactoring and performance gains (this benefit still holds if the PR is not merged). Tree-SHA512: 2427462e7211b5ffc7299dae17339d27f8c43266e0895690fda49a83c72751bd2489d4471b3993075a18f3fef25d741243e5010b2f49aeef4a9688b30b6d0631
2 parents 6e6b3b9 + b301950 commit f0c9e1c

File tree

7 files changed

+11
-11
lines changed

7 files changed

+11
-11
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ static int CommandLineRawTx(int argc, char* argv[])
818818
MutateTx(tx, key, value);
819819
}
820820

821-
OutputTx(tx);
821+
OutputTx(CTransaction(tx));
822822
}
823823
catch (const std::exception& e) {
824824
strPrint = std::string("error: ") + e.what();

src/primitives/transaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class CTransaction
298298
CTransaction();
299299

300300
/** Convert a CMutableTransaction into a CTransaction. */
301-
CTransaction(const CMutableTransaction &tx);
301+
explicit CTransaction(const CMutableTransaction &tx);
302302
CTransaction(CMutableTransaction &&tx);
303303

304304
template <typename Stream>

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
445445
}
446446
}
447447

448-
if (!rbf.isNull() && rawTx.vin.size() > 0 && rbfOptIn != SignalsOptInRBF(rawTx)) {
448+
if (!rbf.isNull() && rawTx.vin.size() > 0 && rbfOptIn != SignalsOptInRBF(CTransaction(rawTx))) {
449449
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter combination: Sequence number(s) contradict replaceable option");
450450
}
451451

@@ -517,7 +517,7 @@ static UniValue createrawtransaction(const JSONRPCRequest& request)
517517

518518
CMutableTransaction rawTx = ConstructTransaction(request.params[0], request.params[1], request.params[2], request.params[3]);
519519

520-
return EncodeHexTx(rawTx);
520+
return EncodeHexTx(CTransaction(rawTx));
521521
}
522522

523523
static UniValue decoderawtransaction(const JSONRPCRequest& request)
@@ -773,7 +773,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
773773
UpdateInput(txin, sigdata);
774774
}
775775

776-
return EncodeHexTx(mergedTx);
776+
return EncodeHexTx(CTransaction(mergedTx));
777777
}
778778

779779
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
@@ -906,7 +906,7 @@ UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, con
906906
bool fComplete = vErrors.empty();
907907

908908
UniValue result(UniValue::VOBJ);
909-
result.pushKV("hex", EncodeHexTx(mtx));
909+
result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
910910
result.pushKV("complete", fComplete);
911911
if (!vErrors.empty()) {
912912
result.pushKV("errors", vErrors);

src/script/sign.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ bool IsSolvable(const SigningProvider& provider, const CScript& script)
509509
return false;
510510
}
511511

512-
PartiallySignedTransaction::PartiallySignedTransaction(const CTransaction& tx) : tx(tx)
512+
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
513513
{
514514
inputs.resize(tx.vin.size());
515515
outputs.resize(tx.vout.size());

src/script/sign.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ struct PartiallySignedTransaction
574574
bool IsSane() const;
575575
PartiallySignedTransaction() {}
576576
PartiallySignedTransaction(const PartiallySignedTransaction& psbt_in) : tx(psbt_in.tx), inputs(psbt_in.inputs), outputs(psbt_in.outputs), unknown(psbt_in.unknown) {}
577-
explicit PartiallySignedTransaction(const CTransaction& tx);
577+
explicit PartiallySignedTransaction(const CMutableTransaction& tx);
578578

579579
// Only checks if they refer to the same transaction
580580
friend bool operator==(const PartiallySignedTransaction& a, const PartiallySignedTransaction &b)

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,7 @@ static UniValue fundrawtransaction(const JSONRPCRequest& request)
30623062
FundTransaction(pwallet, tx, fee, change_position, request.params[1]);
30633063

30643064
UniValue result(UniValue::VOBJ);
3065-
result.pushKV("hex", EncodeHexTx(tx));
3065+
result.pushKV("hex", EncodeHexTx(CTransaction(tx)));
30663066
result.pushKV("fee", ValueFromAmount(fee));
30673067
result.pushKV("changepos", change_position);
30683068

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wall
15101510
// implies that we can sign for every input.
15111511
return -1;
15121512
}
1513-
return GetVirtualTransactionSize(txNew);
1513+
return GetVirtualTransactionSize(CTransaction(txNew));
15141514
}
15151515

15161516
int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet, bool use_max_sig)
@@ -2850,7 +2850,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
28502850
txNew.vin.push_back(CTxIn(coin.outpoint,CScript()));
28512851
}
28522852

2853-
nBytes = CalculateMaximumSignedTxSize(txNew, this, coin_control.fAllowWatchOnly);
2853+
nBytes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
28542854
if (nBytes < 0) {
28552855
strFailReason = _("Signing transaction failed");
28562856
return false;

0 commit comments

Comments
 (0)