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