Skip to content

Commit

Permalink
Merge #14906: refactor: Make explicit CMutableTransaction -> CTransac…
Browse files Browse the repository at this point in the history
…tion 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
  • Loading branch information
laanwj committed Jan 21, 2019
2 parents 6e6b3b9 + b301950 commit f0c9e1c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bitcoin-tx.cpp
Expand Up @@ -818,7 +818,7 @@ static int CommandLineRawTx(int argc, char* argv[])
MutateTx(tx, key, value);
}

OutputTx(tx);
OutputTx(CTransaction(tx));
}
catch (const std::exception& e) {
strPrint = std::string("error: ") + e.what();
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/transaction.h
Expand Up @@ -298,7 +298,7 @@ class CTransaction
CTransaction();

/** Convert a CMutableTransaction into a CTransaction. */
CTransaction(const CMutableTransaction &tx);
explicit CTransaction(const CMutableTransaction &tx);
CTransaction(CMutableTransaction &&tx);

template <typename Stream>
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/rawtransaction.cpp
Expand Up @@ -445,7 +445,7 @@ CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniVal
}
}

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

Expand Down Expand Up @@ -517,7 +517,7 @@ static UniValue createrawtransaction(const JSONRPCRequest& request)

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

return EncodeHexTx(rawTx);
return EncodeHexTx(CTransaction(rawTx));
}

static UniValue decoderawtransaction(const JSONRPCRequest& request)
Expand Down Expand Up @@ -773,7 +773,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
UpdateInput(txin, sigdata);
}

return EncodeHexTx(mergedTx);
return EncodeHexTx(CTransaction(mergedTx));
}

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

UniValue result(UniValue::VOBJ);
result.pushKV("hex", EncodeHexTx(mtx));
result.pushKV("hex", EncodeHexTx(CTransaction(mtx)));
result.pushKV("complete", fComplete);
if (!vErrors.empty()) {
result.pushKV("errors", vErrors);
Expand Down
2 changes: 1 addition & 1 deletion src/script/sign.cpp
Expand Up @@ -509,7 +509,7 @@ bool IsSolvable(const SigningProvider& provider, const CScript& script)
return false;
}

PartiallySignedTransaction::PartiallySignedTransaction(const CTransaction& tx) : tx(tx)
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
{
inputs.resize(tx.vin.size());
outputs.resize(tx.vout.size());
Expand Down
2 changes: 1 addition & 1 deletion src/script/sign.h
Expand Up @@ -574,7 +574,7 @@ struct PartiallySignedTransaction
bool IsSane() const;
PartiallySignedTransaction() {}
PartiallySignedTransaction(const PartiallySignedTransaction& psbt_in) : tx(psbt_in.tx), inputs(psbt_in.inputs), outputs(psbt_in.outputs), unknown(psbt_in.unknown) {}
explicit PartiallySignedTransaction(const CTransaction& tx);
explicit PartiallySignedTransaction(const CMutableTransaction& tx);

// Only checks if they refer to the same transaction
friend bool operator==(const PartiallySignedTransaction& a, const PartiallySignedTransaction &b)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Expand Up @@ -3062,7 +3062,7 @@ static UniValue fundrawtransaction(const JSONRPCRequest& request)
FundTransaction(pwallet, tx, fee, change_position, request.params[1]);

UniValue result(UniValue::VOBJ);
result.pushKV("hex", EncodeHexTx(tx));
result.pushKV("hex", EncodeHexTx(CTransaction(tx)));
result.pushKV("fee", ValueFromAmount(fee));
result.pushKV("changepos", change_position);

Expand Down
4 changes: 2 additions & 2 deletions src/wallet/wallet.cpp
Expand Up @@ -1510,7 +1510,7 @@ int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wall
// implies that we can sign for every input.
return -1;
}
return GetVirtualTransactionSize(txNew);
return GetVirtualTransactionSize(CTransaction(txNew));
}

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

nBytes = CalculateMaximumSignedTxSize(txNew, this, coin_control.fAllowWatchOnly);
nBytes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
if (nBytes < 0) {
strFailReason = _("Signing transaction failed");
return false;
Expand Down

0 comments on commit f0c9e1c

Please sign in to comment.