Raven Core  3.0.0
P2P Digital Currency
accounting_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "wallet/wallet.h"
7 
9 
10 #include <stdint.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 extern CWallet *pwalletMain;
15 
17 
18  static void
19  GetResults(std::map<CAmount, CAccountingEntry> &results)
20  {
21  std::list<CAccountingEntry> aes;
22 
23  results.clear();
24  BOOST_CHECK(pwalletMain->ReorderTransactions() == DB_LOAD_OK);
25  pwalletMain->ListAccountCreditDebit("", aes);
26  for (CAccountingEntry &ae : aes)
27  {
28  results[ae.nOrderPos] = ae;
29  }
30  }
31 
32  BOOST_AUTO_TEST_CASE(acc_orderupgrade_test)
33  {
34  BOOST_TEST_MESSAGE("Running ACC OrderUpgrade Test");
35 
36  std::vector<CWalletTx *> vpwtx;
37  CWalletTx wtx;
39  std::map<CAmount, CAccountingEntry> results;
40 
41  LOCK(pwalletMain->cs_wallet);
42 
43  ae.strAccount = "";
44  ae.nCreditDebit = 1;
45  ae.nTime = 1333333333;
46  ae.strOtherAccount = "b";
47  ae.strComment = "";
48  pwalletMain->AddAccountingEntry(ae);
49 
50  wtx.mapValue["comment"] = "z";
51  pwalletMain->AddToWallet(wtx);
52  vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
53  vpwtx[0]->nTimeReceived = (unsigned int) 1333333335;
54  vpwtx[0]->nOrderPos = -1;
55 
56  ae.nTime = 1333333336;
57  ae.strOtherAccount = "c";
58  pwalletMain->AddAccountingEntry(ae);
59 
60  GetResults(results);
61 
62  BOOST_CHECK(pwalletMain->nOrderPosNext == 3);
63  BOOST_CHECK(2 == results.size());
64  BOOST_CHECK(results[0].nTime == 1333333333);
65  BOOST_CHECK(results[0].strComment.empty());
66  BOOST_CHECK(1 == vpwtx[0]->nOrderPos);
67  BOOST_CHECK(results[2].nTime == 1333333336);
68  BOOST_CHECK(results[2].strOtherAccount == "c");
69 
70 
71  ae.nTime = 1333333330;
72  ae.strOtherAccount = "d";
73  ae.nOrderPos = pwalletMain->IncOrderPosNext();
74  pwalletMain->AddAccountingEntry(ae);
75 
76  GetResults(results);
77 
78  BOOST_CHECK(results.size() == 3);
79  BOOST_CHECK(pwalletMain->nOrderPosNext == 4);
80  BOOST_CHECK(results[0].nTime == 1333333333);
81  BOOST_CHECK(1 == vpwtx[0]->nOrderPos);
82  BOOST_CHECK(results[2].nTime == 1333333336);
83  BOOST_CHECK(results[3].nTime == 1333333330);
84  BOOST_CHECK(results[3].strComment.empty());
85 
86 
87  wtx.mapValue["comment"] = "y";
88  {
89  CMutableTransaction tx(wtx);
90  --tx.nLockTime; // Just to change the hash :)
91  wtx.SetTx(MakeTransactionRef(std::move(tx)));
92  }
93  pwalletMain->AddToWallet(wtx);
94  vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
95  vpwtx[1]->nTimeReceived = (unsigned int) 1333333336;
96 
97  wtx.mapValue["comment"] = "x";
98  {
99  CMutableTransaction tx(wtx);
100  --tx.nLockTime; // Just to change the hash :)
101  wtx.SetTx(MakeTransactionRef(std::move(tx)));
102  }
103  pwalletMain->AddToWallet(wtx);
104  vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
105  vpwtx[2]->nTimeReceived = (unsigned int) 1333333329;
106  vpwtx[2]->nOrderPos = -1;
107 
108  GetResults(results);
109 
110  BOOST_CHECK(results.size() == 3);
111  BOOST_CHECK(pwalletMain->nOrderPosNext == 6);
112  BOOST_CHECK(0 == vpwtx[2]->nOrderPos);
113  BOOST_CHECK(results[1].nTime == 1333333333);
114  BOOST_CHECK(2 == vpwtx[0]->nOrderPos);
115  BOOST_CHECK(results[3].nTime == 1333333336);
116  BOOST_CHECK(results[4].nTime == 1333333330);
117  BOOST_CHECK(results[4].strComment.empty());
118  BOOST_CHECK(5 == vpwtx[1]->nOrderPos);
119 
120 
121  ae.nTime = 1333333334;
122  ae.strOtherAccount = "e";
123  ae.nOrderPos = -1;
124  pwalletMain->AddAccountingEntry(ae);
125 
126  GetResults(results);
127 
128  BOOST_CHECK(results.size() == 4);
129  BOOST_CHECK(pwalletMain->nOrderPosNext == 7);
130  BOOST_CHECK(0 == vpwtx[2]->nOrderPos);
131  BOOST_CHECK(results[1].nTime == 1333333333);
132  BOOST_CHECK(2 == vpwtx[0]->nOrderPos);
133  BOOST_CHECK(results[3].nTime == 1333333336);
134  BOOST_CHECK(results[3].strComment.empty());
135  BOOST_CHECK(results[4].nTime == 1333333330);
136  BOOST_CHECK(results[4].strComment.empty());
137  BOOST_CHECK(results[5].nTime == 1333333334);
138  BOOST_CHECK(6 == vpwtx[1]->nOrderPos);
139  }
140 
void SetTx(CTransactionRef arg)
Definition: wallet.h:243
int64_t nOrderPos
position in ordered transaction list
Definition: wallet.h:599
BOOST_AUTO_TEST_CASE(acc_orderupgrade_test)
CCriticalSection cs_wallet
Definition: wallet.h:751
const uint256 & GetHash() const
Definition: wallet.h:277
int64_t IncOrderPosNext(CWalletDB *pwalletdb=nullptr)
Increment the next transaction order id.
Definition: wallet.cpp:762
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: wallet.h:316
std::string strComment
Definition: wallet.h:597
DBErrors ReorderTransactions()
Definition: wallet.cpp:685
#define LOCK(cs)
Definition: sync.h:176
CWallet * pwalletMain
bool AddToWallet(const CWalletTx &wtxIn, bool fFlushOnClose=true)
Definition: wallet.cpp:884
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:285
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:15
Testing setup and teardown for wallet.
int64_t nOrderPosNext
Definition: wallet.h:825
void ListAccountCreditDebit(const std::string &strAccount, std::list< CAccountingEntry > &entries)
Definition: wallet.cpp:3724
Internal transfers.
Definition: wallet.h:590
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:17
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:673
std::map< uint256, CWalletTx > mapWallet
Definition: wallet.h:818
A mutable version of CTransaction.
Definition: transaction.h:389
bool AddAccountingEntry(const CAccountingEntry &)
Definition: wallet.cpp:3729
std::string strOtherAccount
Definition: wallet.h:596
CAmount nCreditDebit
Definition: wallet.h:594
int64_t nTime
Definition: wallet.h:595
#define BOOST_CHECK(expr)
Definition: object.cpp:18
std::string strAccount
Definition: wallet.h:593