|
| 1 | +// Copyright (c) 2021 The Divi developers |
| 2 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "PoSTransactionCreator.h" |
| 6 | + |
| 7 | +#include "chain.h" |
| 8 | +#include "chainparams.h" |
| 9 | +#include "ProofOfStakeModule.h" |
| 10 | +#include "script/standard.h" |
| 11 | +#include "Settings.h" |
| 12 | +#include "WalletTx.h" |
| 13 | + |
| 14 | +#include "test/FakeBlockIndexChain.h" |
| 15 | +#include "test/FakeWallet.h" |
| 16 | +#include "test/MockBlockIncentivesPopulator.h" |
| 17 | +#include "test/MockBlockSubsidyProvider.h" |
| 18 | +#include "test/MockUtxoHasher.h" |
| 19 | + |
| 20 | +#include <boost/test/unit_test.hpp> |
| 21 | +#include "test/test_only.h" |
| 22 | + |
| 23 | +#include <gmock/gmock.h> |
| 24 | + |
| 25 | +#include <map> |
| 26 | + |
| 27 | +extern Settings& settings; |
| 28 | + |
| 29 | +namespace |
| 30 | +{ |
| 31 | + |
| 32 | +using testing::_; |
| 33 | +using testing::AtLeast; |
| 34 | +using testing::Return; |
| 35 | + |
| 36 | +class PoSTransactionCreatorTestFixture |
| 37 | +{ |
| 38 | + |
| 39 | +protected: |
| 40 | + |
| 41 | + FakeBlockIndexWithHashes fakeChain; |
| 42 | + FakeWallet wallet; |
| 43 | + |
| 44 | +private: |
| 45 | + |
| 46 | + const CChainParams& chainParams; |
| 47 | + |
| 48 | + MockBlockIncentivesPopulator blockIncentivesPopulator; |
| 49 | + MockBlockSubsidyProvider blockSubsidyProvider; |
| 50 | + ProofOfStakeModule posModule; |
| 51 | + |
| 52 | + std::map<unsigned, unsigned> hashedBlockTimestamps; |
| 53 | + |
| 54 | + PoSTransactionCreator txCreator; |
| 55 | + |
| 56 | +protected: |
| 57 | + |
| 58 | + /** A script from the wallet for convenience. */ |
| 59 | + const CScript walletScript; |
| 60 | + |
| 61 | + /** UTXO hasher used in the wallet. The instance is owned by the wallet, |
| 62 | + * and a reference is kept here. */ |
| 63 | + MockUtxoHasher* utxoHasher; |
| 64 | + |
| 65 | + /* Convenience variables for tests to use in calls to CreatePoS. */ |
| 66 | + CMutableTransaction mtx; |
| 67 | + unsigned txTime; |
| 68 | + |
| 69 | + /* Convenience variable for the wallet's AddDefaultTx. */ |
| 70 | + unsigned outputIndex; |
| 71 | + |
| 72 | + PoSTransactionCreatorTestFixture() |
| 73 | + : fakeChain(1, 1600000000, 1), |
| 74 | + wallet(fakeChain), |
| 75 | + chainParams(Params(CBaseChainParams::REGTEST)), |
| 76 | + posModule(chainParams, *fakeChain.activeChain, *fakeChain.blockIndexByHash), |
| 77 | + txCreator(settings, chainParams, *fakeChain.activeChain, *fakeChain.blockIndexByHash, |
| 78 | + blockSubsidyProvider, blockIncentivesPopulator, |
| 79 | + posModule.proofOfStakeGenerator(), wallet, hashedBlockTimestamps), |
| 80 | + walletScript(GetScriptForDestination(wallet.getNewKey().GetID())) |
| 81 | + { |
| 82 | + std::unique_ptr<MockUtxoHasher> hasher(new MockUtxoHasher()); |
| 83 | + utxoHasher = hasher.get(); |
| 84 | + wallet.SetUtxoHasherForTesting(std::move (hasher)); |
| 85 | + |
| 86 | + /* Set up a default block reward if we don't need anything else. */ |
| 87 | + EXPECT_CALL(blockSubsidyProvider, GetFullBlockValue(_)) |
| 88 | + .WillRepeatedly(Return(11 * COIN)); |
| 89 | + EXPECT_CALL(blockSubsidyProvider, GetBlockSubsidity(_)) |
| 90 | + .WillRepeatedly(Return(CBlockRewards(10 * COIN, COIN, 0, 0, 0, 0))); |
| 91 | + |
| 92 | + /* We don't care about the block payments. */ |
| 93 | + EXPECT_CALL(blockIncentivesPopulator, FillBlockPayee(_, _, _, _)).Times(AtLeast(0)); |
| 94 | + EXPECT_CALL(blockIncentivesPopulator, IsBlockValueValid(_, _, _)) |
| 95 | + .WillRepeatedly(Return(true)); |
| 96 | + EXPECT_CALL(blockIncentivesPopulator, HasValidPayees(_, _)) |
| 97 | + .WillRepeatedly(Return(true)); |
| 98 | + } |
| 99 | + |
| 100 | + /** Calls CreateProofOfStake on our PoSTransactionCreator with the |
| 101 | + * fake wallet's chain tip and regtest difficulty. */ |
| 102 | + bool CreatePoS(CMutableTransaction& txCoinStake, unsigned& nTxNewTime) |
| 103 | + { |
| 104 | + return txCreator.CreateProofOfStake(fakeChain.activeChain->Tip(), 0x207fffff, txCoinStake, nTxNewTime); |
| 105 | + } |
| 106 | + |
| 107 | + bool CreatePoS() |
| 108 | + { |
| 109 | + return CreatePoS(mtx, txTime); |
| 110 | + } |
| 111 | + |
| 112 | +}; |
| 113 | + |
| 114 | +BOOST_FIXTURE_TEST_SUITE(PoSTransactionCreator_tests, PoSTransactionCreatorTestFixture) |
| 115 | + |
| 116 | +BOOST_AUTO_TEST_CASE(failsWithoutCoinsInWallet) |
| 117 | +{ |
| 118 | + BOOST_CHECK(!CreatePoS()); |
| 119 | +} |
| 120 | + |
| 121 | +BOOST_AUTO_TEST_CASE(checksForConfirmationsAndAge) |
| 122 | +{ |
| 123 | + const auto& tx = wallet.AddDefaultTx(walletScript, outputIndex, 1000 * COIN); |
| 124 | + wallet.FakeAddToChain(tx); |
| 125 | + |
| 126 | + BOOST_CHECK(!CreatePoS()); |
| 127 | + |
| 128 | + wallet.AddConfirmations(20, 1000); |
| 129 | + BOOST_CHECK(CreatePoS()); |
| 130 | +} |
| 131 | + |
| 132 | +BOOST_AUTO_TEST_CASE(usesUtxoHashForInputs) |
| 133 | +{ |
| 134 | + const auto& tx = wallet.AddDefaultTx(walletScript, outputIndex, 1000 * COIN); |
| 135 | + wallet.FakeAddToChain(tx); |
| 136 | + wallet.AddConfirmations(20, 1000); |
| 137 | + |
| 138 | + const auto hash = utxoHasher->Add(tx); |
| 139 | + BOOST_CHECK(CreatePoS()); |
| 140 | + |
| 141 | + BOOST_CHECK_EQUAL(mtx.vin.size(), 1); |
| 142 | + BOOST_CHECK_EQUAL(mtx.vin[0].prevout.n, outputIndex); |
| 143 | + BOOST_CHECK(mtx.vin[0].prevout.hash == hash); |
| 144 | +} |
| 145 | + |
| 146 | +BOOST_AUTO_TEST_SUITE_END() |
| 147 | + |
| 148 | +} // anonymous namespace |
0 commit comments