|
15 | 15 | #include <chain.h>
|
16 | 16 | #include <blockmap.h>
|
17 | 17 | #include <test/FakeWallet.h>
|
| 18 | +#include <test/MockUtxoHasher.h> |
18 | 19 | #include <StakableCoin.h>
|
19 | 20 |
|
20 | 21 | class WalletCoinManagementTestFixture
|
21 | 22 | {
|
22 | 23 | public:
|
23 | 24 | FakeBlockIndexWithHashes fakeChain;
|
24 | 25 | FakeWallet wallet;
|
| 26 | + MockUtxoHasher* utxoHasher; |
25 | 27 |
|
26 | 28 | WalletCoinManagementTestFixture()
|
27 | 29 | : fakeChain(1, 1600000000, 1), wallet(fakeChain)
|
28 | 30 | {
|
29 | 31 | ENTER_CRITICAL_SECTION(wallet.cs_wallet);
|
| 32 | + |
| 33 | + std::unique_ptr<MockUtxoHasher> hasher(new MockUtxoHasher()); |
| 34 | + utxoHasher = hasher.get(); |
| 35 | + wallet.SetUtxoHasherForTesting(std::move(hasher)); |
30 | 36 | }
|
31 | 37 | ~WalletCoinManagementTestFixture()
|
32 | 38 | {
|
@@ -400,4 +406,63 @@ BOOST_AUTO_TEST_CASE(willEnsureStakingBalanceAndTotalBalanceAgreeEvenIfTxsBelong
|
400 | 406 | BOOST_CHECK_EQUAL_MESSAGE(stakableCoins.size(),2,"Missing coins in the stakable set");
|
401 | 407 | }
|
402 | 408 |
|
| 409 | +BOOST_AUTO_TEST_CASE(willUseUtxoHasherForCoinLock) |
| 410 | +{ |
| 411 | + CScript defaultScript = GetScriptForDestination(wallet.getNewKey().GetID()); |
| 412 | + unsigned outputIndex=0; |
| 413 | + const CWalletTx& wtx = wallet.AddDefaultTx(defaultScript,outputIndex, COIN); |
| 414 | + const auto id = utxoHasher->Add(wtx); |
| 415 | + |
| 416 | + wallet.LockCoin(COutPoint(id, outputIndex)); |
| 417 | + |
| 418 | + bool fIsSpendable; |
| 419 | + BOOST_CHECK(!wallet.IsAvailableForSpending(&wtx,outputIndex,false,fIsSpendable,ALL_SPENDABLE_COINS)); |
| 420 | + |
| 421 | + BOOST_CHECK_EQUAL(wallet.ComputeCredit(wtx, ISMINE_SPENDABLE, REQUIRE_UNLOCKED), 0); |
| 422 | + BOOST_CHECK_EQUAL(wallet.ComputeCredit(wtx, ISMINE_SPENDABLE, REQUIRE_LOCKED), COIN); |
| 423 | +} |
| 424 | + |
| 425 | +BOOST_AUTO_TEST_CASE(willMarkOutputsSpentBasedOnUtxoHasher) |
| 426 | +{ |
| 427 | + CScript defaultScript = GetScriptForDestination(wallet.getNewKey().GetID()); |
| 428 | + unsigned outputIndex=0; |
| 429 | + const CWalletTx& wtx = wallet.AddDefaultTx(defaultScript,outputIndex, COIN); |
| 430 | + const auto id = utxoHasher->Add(wtx); |
| 431 | + |
| 432 | + CMutableTransaction mtx; |
| 433 | + mtx.vin.emplace_back(id, outputIndex); |
| 434 | + const auto& spendTx = wallet.Add(mtx); |
| 435 | + |
| 436 | + /* The wallet's IsSpent check verifies also the spending tx' number |
| 437 | + of confirmations, which requires it to be either in the chain or |
| 438 | + in the mempool at least. */ |
| 439 | + wallet.FakeAddToChain(wtx); |
| 440 | + wallet.FakeAddToChain(spendTx); |
| 441 | + |
| 442 | + BOOST_CHECK(wallet.IsSpent(wtx, outputIndex)); |
| 443 | + bool fIsSpendable; |
| 444 | + BOOST_CHECK(!wallet.IsAvailableForSpending(&wtx,outputIndex,false,fIsSpendable,ALL_SPENDABLE_COINS)); |
| 445 | +} |
| 446 | + |
| 447 | +BOOST_AUTO_TEST_CASE(willUseUtxoHashForSpendingCoins) |
| 448 | +{ |
| 449 | + CScript defaultScript = GetScriptForDestination(wallet.getNewKey().GetID()); |
| 450 | + unsigned outputIndex=0; |
| 451 | + const CWalletTx& wtx = wallet.AddDefaultTx(defaultScript,outputIndex, COIN); |
| 452 | + const auto id = utxoHasher->Add(wtx); |
| 453 | + wallet.FakeAddToChain(wtx); |
| 454 | + |
| 455 | + const CScript sendTo = CScript() << OP_TRUE; |
| 456 | + std::string strError; |
| 457 | + CReserveKey reserveKey(wallet); |
| 458 | + CAmount nFeeRequired; |
| 459 | + CWalletTx wtxNew; |
| 460 | + BOOST_CHECK(wallet.CreateTransaction({{sendTo, COIN / 10}}, wtxNew, reserveKey, ALL_SPENDABLE_COINS, nullptr).second); |
| 461 | + |
| 462 | + BOOST_CHECK_EQUAL(wtxNew.vin.size(), 1); |
| 463 | + const auto& prevout = wtxNew.vin[0].prevout; |
| 464 | + BOOST_CHECK(prevout.hash == id); |
| 465 | + BOOST_CHECK_EQUAL(prevout.n, outputIndex); |
| 466 | +} |
| 467 | + |
403 | 468 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments