Skip to content

Commit 569b5ba

Browse files
author
MarcoFalke
committed
Merge #21121: [test] Small unit test improvements, including helper to make mempool transaction
1363b6c [doc / util] Use comments to clarify time unit for int64_t type. (Amiti Uttarwar) 47a7a16 [util] Introduce a SetMockTime that takes chrono time (Amiti Uttarwar) df6a5fc [util] Change GetMockTime to return chrono type instead of int (Amiti Uttarwar) a2d908e [test] Throw error instead of segfaulting in failure scenario (Amiti Uttarwar) 9a3bbe8 [test] Introduce a unit test helper to create a valid mempool transaction. (Amiti Uttarwar) Pull request description: Some miscellaneous improvements that came up when working on #21061 - The first commit is a helper to make valid mempool transactions & submit via ATMP. Introducing in this PR, using in #21061. - The second commit is a small improvement in `miner_tests.cpp` that uses `BOOST_REQUIRE_EQUAL` to properly terminate the program instead of segfaulting in the failure scenario where the blocks do not include the expected number of transactions. - The third commit changes the function signature of `GetMockTime()` to return a chrono type. - The fourth & fifth commit overload `SetMockTime` to also accept chrono type, and adds documentation to indicate that the `int64_t` function signature is deprecated. ACKs for top commit: vasild: ACK 1363b6c Tree-SHA512: c72574d73668ea04ee4c33858f8de68b368780f445e05afb569aaf8564093f8112259b3afe93cf6dc2ee12a1ab5af1130ac73c16416132c1ba2851c054a67d78
2 parents 8639c44 + 1363b6c commit 569b5ba

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

src/logging.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
203203
strStamped.pop_back();
204204
strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
205205
}
206-
int64_t mocktime = GetMockTime();
207-
if (mocktime) {
208-
strStamped += " (mocktime: " + FormatISO8601DateTime(mocktime) + ")";
206+
std::chrono::seconds mocktime = GetMockTime();
207+
if (mocktime > 0s) {
208+
strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
209209
}
210210
strStamped += ' ' + str;
211211
} else

src/test/miner_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
123123
m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
124124

125125
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
126+
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4);
126127
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
127128
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
128129
BOOST_CHECK(pblocktemplate->block.vtx[3]->GetHash() == hashMediumFeeTx);
@@ -157,6 +158,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
157158
hashLowFeeTx = tx.GetHash();
158159
m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
159160
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
161+
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6);
160162
BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
161163
BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
162164

@@ -191,6 +193,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
191193
tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
192194
m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
193195
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
196+
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9);
194197
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
195198
}
196199

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;

src/util/time.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ void SetMockTime(int64_t nMockTimeIn)
5353
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
5454
}
5555

56-
int64_t GetMockTime()
56+
void SetMockTime(std::chrono::seconds mock_time_in)
5757
{
58-
return nMockTime.load(std::memory_order_relaxed);
58+
nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
59+
}
60+
61+
std::chrono::seconds GetMockTime()
62+
{
63+
return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
5964
}
6065

6166
int64_t GetTimeMillis()

src/util/time.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ int64_t GetTimeMicros();
4343
/** Returns the system time (not mockable) */
4444
int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable
4545

46-
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
46+
/**
47+
* DEPRECATED
48+
* Use SetMockTime with chrono type
49+
*
50+
* @param[in] nMockTimeIn Time in seconds.
51+
*/
4752
void SetMockTime(int64_t nMockTimeIn);
53+
54+
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
55+
void SetMockTime(std::chrono::seconds mock_time_in);
56+
4857
/** For testing */
49-
int64_t GetMockTime();
58+
std::chrono::seconds GetMockTime();
5059

5160
/** Return system time (or mocked time, if set) */
5261
template <typename T>

0 commit comments

Comments
 (0)