Skip to content

Commit

Permalink
Merge bitcoin#15784: rpc: Remove dependency on interfaces::Chain in S…
Browse files Browse the repository at this point in the history
…ignTransaction

99e88a3 rpc: Remove dependency on interfaces::Chain in SignTransaction (Antoine Riard)

Pull request description:

  Assuming wallet RPCs and node RPCs will go into different processes, signrawtransactionwithkey doesn't need to access Coins via interfaces::Chain, it may use directly utility in node/coins.cpp

  Obviously will need rebase after bitcoin#15638

Tree-SHA512: 42ee8fcbcd38643bbd82210db6f68249bed5ee036a4c930a1db534d0469a133e287b8869c977bf0cc79a7296dde04f72adb74d24e1cd20f4a280f4c2b7fceb74
  • Loading branch information
meshcollider authored and vijaydasmp committed Dec 14, 2021
1 parent 4ac8598 commit 71edeac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <key_io.h>
#include <keystore.h>
#include <merkleblock.h>
#include <node/coin.h>
#include <net.h>
#include <node/transaction.h>
#include <policy/policy.h>
Expand Down Expand Up @@ -752,7 +753,14 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
keystore.AddKey(key);
}

return SignTransaction(*g_rpc_interfaces->chain, mtx, request.params[2], &keystore, true, request.params[3]);
// Fetch previous transactions (inputs):
std::map<COutPoint, Coin> coins;
for (const CTxIn& txin : mtx.vin) {
coins[txin.prevout]; // Create empty map entry keyed by prevout.
}
FindCoins(coins);

return SignTransaction(mtx, request.params[2], &keystore, coins, true, request.params[3]);
}

UniValue sendrawtransaction(const JSONRPCRequest& request)
Expand Down
12 changes: 1 addition & 11 deletions src/rpc/rawtransaction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,8 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
vErrorsRet.push_back(entry);
}

// TODO(https://github.com/bitcoin/bitcoin/pull/10973#discussion_r267084237):
// The dependency on interfaces::Chain should be removed, so
// signrawtransactionwithkey doesn't need access to a Chain instance.
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore *keystore, bool is_temp_keystore, const UniValue& hashType)
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival, CBasicKeyStore* keystore, std::map<COutPoint, Coin>& coins, bool is_temp_keystore, const UniValue& hashType)
{
// Fetch previous transactions (inputs):
std::map<COutPoint, Coin> coins;
for (const CTxIn& txin : mtx.vin) {
coins[txin.prevout]; // Create empty map entry keyed by prevout.
}
chain.findCoins(coins);

// Add previous txouts given in the RPC call:
if (!prevTxsUnival.isNull()) {
UniValue prevTxs = prevTxsUnival.get_array();
Expand Down
22 changes: 16 additions & 6 deletions src/rpc/rawtransaction_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
#ifndef BITCOIN_RPC_RAWTRANSACTION_UTIL_H
#define BITCOIN_RPC_RAWTRANSACTION_UTIL_H

#include <map>

class CBasicKeyStore;
class UniValue;
struct CMutableTransaction;
class Coin;
class COutPoint;

namespace interfaces {
class Chain;
} // namespace interfaces

/** Sign a transaction with the given keystore and previous transactions */
UniValue SignTransaction(interfaces::Chain& chain, CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore *keystore, bool tempKeystore, const UniValue& hashType);
/**
* Sign a transaction with the given keystore and previous transactions
*
* @param mtx The transaction to-be-signed
* @param prevTxs Array of previous txns outputs that tx depends on but may not yet be in the block chain
* @param keystore Temporary keystore containing signing keys
* @param coins Map of unspent outputs - coins in mempool and current chain UTXO set, may be extended by previous txns outputs after call
* @param tempKeystore Whether to use temporary keystore
* @param hashType The signature hash type
* @returns JSON object with details of signed transaction
*/
UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxs, CBasicKeyStore* keystore, std::map<COutPoint, Coin>& coins, bool tempKeystore, const UniValue& hashType);

/** Create a transaction from univalue parameters */
CMutableTransaction ConstructTransaction(const UniValue& inputs_in, const UniValue& outputs_in, const UniValue& locktime);
Expand Down
9 changes: 8 additions & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3483,7 +3483,14 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
LOCK2(mempool.cs, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwallet);

return SignTransaction(pwallet->chain(), mtx, request.params[1], pwallet, false, request.params[2]);
// Fetch previous transactions (inputs):
std::map<COutPoint, Coin> coins;
for (const CTxIn& txin : mtx.vin) {
coins[txin.prevout]; // Create empty map entry keyed by prevout.
}
pwallet->chain().findCoins(coins);

return SignTransaction(mtx, request.params[1], pwallet, coins, false, request.params[2]);
}

#if ENABLE_MINER
Expand Down

0 comments on commit 71edeac

Please sign in to comment.