Skip to content

Commit c6b82d1

Browse files
committed
Add tests for CWalletTx::nTimeSmart
1 parent 53c300f commit c6b82d1

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,4 +453,57 @@ BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup)
453453
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50*COIN);
454454
}
455455

456+
static int64_t AddTx(CWallet& wallet, uint32_t lockTime, int64_t mockTime, int64_t blockTime)
457+
{
458+
CMutableTransaction tx;
459+
tx.nLockTime = lockTime;
460+
SetMockTime(mockTime);
461+
CBlockIndex* block = nullptr;
462+
if (blockTime > 0) {
463+
auto inserted = mapBlockIndex.emplace(GetRandHash(), new CBlockIndex);
464+
assert(inserted.second);
465+
const uint256& hash = inserted.first->first;
466+
block = inserted.first->second;
467+
block->nTime = blockTime;
468+
block->phashBlock = &hash;
469+
}
470+
471+
CWalletTx wtx(&wallet, MakeTransactionRef(tx));
472+
if (block) {
473+
wtx.SetMerkleBranch(block, 0);
474+
}
475+
wallet.AddToWallet(wtx);
476+
return wallet.mapWallet.at(wtx.GetHash()).nTimeSmart;
477+
}
478+
479+
// Simple test to verify assignment of CWalletTx::nSmartTime value. Could be
480+
// expanded to cover more corner cases of smart time logic.
481+
BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
482+
{
483+
CWallet wallet;
484+
485+
// New transaction should use clock time if lower than block time.
486+
BOOST_CHECK_EQUAL(AddTx(wallet, 1, 100, 120), 100);
487+
488+
// Test that updating existing transaction does not change smart time.
489+
BOOST_CHECK_EQUAL(AddTx(wallet, 1, 200, 220), 100);
490+
491+
// New transaction should use clock time if there's no block time.
492+
BOOST_CHECK_EQUAL(AddTx(wallet, 2, 300, 0), 300);
493+
494+
// New transaction should use block time if lower than clock time.
495+
BOOST_CHECK_EQUAL(AddTx(wallet, 3, 420, 400), 400);
496+
497+
// New transaction should use latest entry time if higher than
498+
// min(block time, clock time).
499+
BOOST_CHECK_EQUAL(AddTx(wallet, 4, 500, 390), 400);
500+
501+
// If there are future entries, new transaction should use time of the
502+
// newest entry that is no more than 300 seconds ahead of the clock time.
503+
BOOST_CHECK_EQUAL(AddTx(wallet, 5, 50, 600), 300);
504+
505+
// Reset mock time for other tests.
506+
SetMockTime(0);
507+
}
508+
456509
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)