Skip to content

Commit 9a3bbe8

Browse files
committed
[test] Introduce a unit test helper to create a valid mempool transaction.
1 parent 92fee79 commit 9a3bbe8

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/test/util/setup_common.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,55 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
260260
return block;
261261
}
262262

263+
264+
CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactionRef input_transaction,
265+
int input_vout,
266+
int input_height,
267+
CKey input_signing_key,
268+
CScript output_destination,
269+
CAmount output_amount)
270+
{
271+
// Transaction we will submit to the mempool
272+
CMutableTransaction mempool_txn;
273+
274+
// Create an input
275+
COutPoint outpoint_to_spend(input_transaction->GetHash(), input_vout);
276+
CTxIn input(outpoint_to_spend);
277+
mempool_txn.vin.push_back(input);
278+
279+
// Create an output
280+
CTxOut output(output_amount, output_destination);
281+
mempool_txn.vout.push_back(output);
282+
283+
// Sign the transaction
284+
// - Add the signing key to a keystore
285+
FillableSigningProvider keystore;
286+
keystore.AddKey(input_signing_key);
287+
// - Populate a CoinsViewCache with the unspent output
288+
CCoinsView coins_view;
289+
CCoinsViewCache coins_cache(&coins_view);
290+
AddCoins(coins_cache, *input_transaction.get(), input_height);
291+
// - Use GetCoin to properly populate utxo_to_spend,
292+
Coin utxo_to_spend;
293+
assert(coins_cache.GetCoin(outpoint_to_spend, utxo_to_spend));
294+
// - Then add it to a map to pass in to SignTransaction
295+
std::map<COutPoint, Coin> input_coins;
296+
input_coins.insert({outpoint_to_spend, utxo_to_spend});
297+
// - Default signature hashing type
298+
int nHashType = SIGHASH_ALL;
299+
std::map<int, std::string> input_errors;
300+
assert(SignTransaction(mempool_txn, &keystore, input_coins, nHashType, input_errors));
301+
302+
// Add transaction to the mempool
303+
{
304+
LOCK(cs_main);
305+
const MempoolAcceptResult result = AcceptToMemoryPool(*m_node.mempool.get(), MakeTransactionRef(mempool_txn), /* bypass_limits */ false);
306+
assert(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
307+
}
308+
309+
return mempool_txn;
310+
}
311+
263312
TestChain100Setup::~TestChain100Setup()
264313
{
265314
gArgs.ForceSetArg("-segwitheight", "0");

src/test/util/setup_common.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ struct TestChain100Setup : public RegTestingSetup {
123123
//! Mine a series of new blocks on the active chain.
124124
void mineBlocks(int num_blocks);
125125

126+
/**
127+
* Create a transaction and submit to the mempool.
128+
*
129+
* @param input_transaction The transaction to spend
130+
* @param input_vout The vout to spend from the input_transaction
131+
* @param input_height The height of the block that included the input_transaction
132+
* @param input_signing_key The key to spend the input_transaction
133+
* @param output_destination Where to send the output
134+
* @param output_amount How much to send
135+
*/
136+
CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
137+
int input_vout,
138+
int input_height,
139+
CKey input_signing_key,
140+
CScript output_destination,
141+
CAmount output_amount = CAmount(1 * COIN));
142+
126143
~TestChain100Setup();
127144

128145
bool m_deterministic;

0 commit comments

Comments
 (0)