17 #include <boost/test/unit_test.hpp>
26 for (
int index=0; index < (int) txPrevOutputs.size(); index++) {
27 const CTxOut& prevOut = txPrevOutputs.at(index);
46 std::vector<CStakeableOutput> availableCoins;
54 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(pblocktemplate->block);
60 availableCoins.pop_back();
62 mtx.
vin.emplace_back(vin2);
65 std::vector<CTxOut> txPrevOutputs{prevOutput1, in2.
tx->
tx->vout[in2.
i]};
67 reSignTx(mtx, txPrevOutputs, pwalletMain.get());
68 pblock->vtx[1] = MakeTransactionRef(mtx);
74 pblock = std::make_shared<CBlock>(pblocktemplate->block);
76 for (
int i = 0; i < 999; ++i) {
77 mtx.
vout.emplace_back();
78 mtx.
vout.back().SetEmpty();
80 reSignTx(mtx, {prevOutput1}, pwalletMain.get());
81 pblock->vtx[1] = MakeTransactionRef(mtx);
87 pblock = std::make_shared<CBlock>(pblocktemplate->block);
97 std::string strFailReason;
120 for (
size_t i = 0; i < tx.
vout.size(); i++) {
121 if (tx.
vout[i].nValue == outpointValue) {
125 BOOST_ASSERT_MSG(
false,
"error in test, no output in tx for value");
129 static bool IsSpentOnFork(
const COutput& coin, std::initializer_list<std::shared_ptr<CBlock>> forkchain = {})
131 for (
const auto& block : forkchain) {
132 const auto& usedOutput = block->vtx[1]->vin.at(0).prevout;
133 if (coin.
tx->
GetHash() == usedOutput.hash && coin.
i == (
int)usedOutput.n) {
143 std::initializer_list<std::shared_ptr<CBlock>> forkchain = {})
145 std::vector<CStakeableOutput> availableCoins;
154 for (
auto it = availableCoins.begin(); it != availableCoins.end() ;) {
155 if (it->nDepth <= 120 || IsSpentOnFork(*it, forkchain)) {
156 it = availableCoins.erase(it);
171 BOOST_ASSERT(pblocktemplate);
172 auto pblock = std::make_shared<CBlock>(pblocktemplate->block);
174 for (
const auto& tx : txns) {
175 pblock->vtx.emplace_back(MakeTransactionRef(tx));
178 assert(
SignBlock(*pblock, *pwalletMain));
183 static COutput GetUnspentCoin(
CWallet* pwallet, std::initializer_list<std::shared_ptr<CBlock>> forkchain = {})
185 std::vector<COutput> availableCoins;
189 for (
const auto& coin : availableCoins) {
190 if (!IsSpentOnFork(coin, forkchain)) {
194 throw std::runtime_error(
"Unspent coin not found");
200 for (
int i=0; i<30; i++) {
267 auto e1Tx =
CreateAndCommitTx(pwalletMain.get(), *dest, d1Tx.vout[0].nValue - 0.1 * COIN, &coinControl);
270 std::shared_ptr<CBlock> pblockE1 =
CreateBlockInternal(pwalletMain.get(), {e1Tx}, pindexPrev, {pblockD1});
280 auto D4_tx1 =
CreateAndCommitTx(pwalletMain.get(), *dest, 199 * COIN, &coinControl);
290 coinControlE2.
Select(cTx_out, 249 * COIN);
291 auto E2_tx1 =
CreateAndCommitTx(pwalletMain.get(), *dest, 200 * COIN, &coinControlE2);
296 auto E2_tx2 =
CreateAndCommitTx(pwalletMain.get(), *dest, 199 * COIN, &coinControl);
298 std::shared_ptr<CBlock> pblockE2 =
CreateBlockInternal(pwalletMain.get(), {E2_tx1, E2_tx2},
299 pindexPrev, {pblockD1});
303 auto F2_tx1 =
CreateAndCommitTx(pwalletMain.get(), *dest, 199 * COIN, &coinControl);
307 pindexPrev, {pblockD1, pblockE2});
326 coinControl.
Select(dTxOutPoint, 249 * COIN);
328 auto E3_tx1 =
CreateAndCommitTx(pwalletMain.get(), *dest, 200 * COIN, &coinControl);
331 std::shared_ptr<CBlock> pblockE3 =
CreateBlockInternal(pwalletMain.get(), {E3_tx1}, pindexPrev, {pblockD3});
344 const auto& F_tx1 = E3_tx1;
356 std::shared_ptr<CBlock> pblockF3 =
CreateBlockInternal(pwalletMain.get(), {F_tx1}, pindexPrev, {pblockD3, pblockE3});
374 const COutput& input = GetUnspentCoin(pwalletMain.get(), {pblockD3, pblockE3});
381 auto gTx =
CreateAndCommitTx(pwalletMain.get(), *dest, 200 * COIN, &coinControl);
395 coinControl.
Select(gOut, 200 * COIN);
397 auto hTx =
CreateAndCommitTx(pwalletMain.get(), *dest, 199 * COIN, &coinControl);
407 {pblockD3, pblockE3, pblockF3, pblockG3});
418 {pblockD3, pblockE3, pblockF3, pblockG3, pblockH3});
432 std::shared_ptr<CBlock> pblockI = std::make_shared<CBlock>(*pblockI3);
434 pblockI->vtx.emplace_back(MakeTransactionRef(iTx));
436 assert(
SignBlock(*pblockI, *pwalletMain));
437 BOOST_CHECK(pblockI3->GetHash() != pblockI->GetHash());
int64_t CAmount
Amount in PIV (Can be negative)
bool SignBlock(CBlock &block, const CKeyStore &keystore)
void ProcessBlockAndCheckRejectionReason(std::shared_ptr< CBlock > &pblock, const std::string &blockRejectionReason, int expectedChainHeight)
const CChainParams & Params()
Return the currently selected parameters.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, CWallet *pwallet=nullptr, bool fProofOfStake=false, std::vector< CStakeableOutput > *availableCoins=nullptr, bool fNoMempoolTx=false, bool fTestValidity=true, CBlockIndex *prevBlock=nullptr, bool stopPoSOnNewBlock=true, bool fIncludeQfc=true)
Construct a new block template with coinbase to scriptPubKeyIn.
void setBlockHash(const uint256 &_hash)
BlockStateCatcher & get() const
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or nullptr if none.
void Select(const BaseOutPoint &output, CAmount value=0, bool isP2CS=false)
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
An outpoint - a combination of a transaction hash and an index n into its vout.
A key allocated from the key pool.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
SigVersion GetRequiredSigVersion() const
const uint256 & GetHash() const
std::vector< CTxOut > vout
An input of a transaction.
An output of a transaction.
std::string GetRejectReason() const
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
const uint256 & GetHash() const
CTxOut out
unspent transaction output
A signature creator for transactions.
RAII object to check and reserve a wallet rescan.
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
CWallet::CommitResult CommitTransaction(CTransactionRef tx, CReserveKey &opReservekey, CConnman *connman)
bool CreateTransaction(const std::vector< CRecipient > &vecSend, CTransactionRef &txRet, CReserveKey &reservekey, CAmount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, const CCoinControl *coinControl=nullptr, bool sign=true, CAmount nFeePay=0, bool fIncludeDelegated=false, bool *fStakeDelegationVoided=nullptr, int nExtraSize=0, int nMinDepth=0)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
bool AvailableCoins(std::vector< COutput > *pCoins, const CCoinControl *coinControl=nullptr, AvailableCoinsFilter coinsFilter=AvailableCoinsFilter()) const
populate vCoins with vector of available COutputs.
void LockCoin(const COutPoint &output)
bool StakeableCoins(std::vector< CStakeableOutput > *pCoins=nullptr)
Available coins (staking)
CallResult< CTxDestination > getNewAddress(const std::string &addressLabel, const std::string purpose, const CChainParams::Base58Type addrType=CChainParams::PUBKEY_ADDRESS)
const CWalletTx * GetWalletTx(const uint256 &hash) const
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver &reserver, bool update)
Scan active chain for relevant transactions after importing keys.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
RecursiveMutex cs_main
Global state.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
CTransaction CreateAndCommitTx(CWallet *pwalletMain, const CTxDestination &dest, CAmount destValue, CCoinControl *coinControl=nullptr)
void reSignTx(CMutableTransaction &mtx, const std::vector< CTxOut > &txPrevOutputs, CWallet *wallet)
COutPoint GetOutpointWithAmount(const CTransaction &tx, CAmount outpointValue)
BOOST_FIXTURE_TEST_CASE(coinstake_tests, TestPoSChainSetup)
std::shared_ptr< CBlock > CreateBlockInternal(CWallet *pwalletMain, const std::vector< CMutableTransaction > &txns={}, CBlockIndex *customPrevBlock=nullptr, std::initializer_list< std::shared_ptr< CBlock >> forkchain={})
void UpdateTransaction(CMutableTransaction &tx, unsigned int nIn, const SignatureData &data)
bool ProduceSignature(const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata, SigVersion sigversion, bool fColdStake, ScriptError *serror)
Produce a script signature using a generic signature creator.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
A mutable version of CTransaction.
std::vector< CTxOut > vout
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::shared_ptr< const CTransaction > CTransactionRef
void FlushStateToDisk()
Flush all state, indexes and buffers to disk.
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &pblock, const FlatFilePos *dbp)
Process an incoming block.
std::unique_ptr< CCoinsViewCache > pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos)
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...