1 // Copyright (c) 2012-2016 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.
5 #include "wallet/wallet.h"
7 #include "wallet/test/wallet_test_fixture.h"
11 #include <boost/test/unit_test.hpp>
13 extern CWallet
* pwalletMain
;
15 BOOST_FIXTURE_TEST_SUITE(accounting_tests
, WalletTestingSetup
)
18 GetResults(std::map
<CAmount
, CAccountingEntry
>& results
)
20 std::list
<CAccountingEntry
> aes
;
23 BOOST_CHECK(pwalletMain
->ReorderTransactions() == DB_LOAD_OK
);
24 pwalletMain
->ListAccountCreditDebit("", aes
);
25 for (CAccountingEntry
& ae
: aes
)
27 results
[ae
.nOrderPos
] = ae
;
31 BOOST_AUTO_TEST_CASE(acc_orderupgrade
)
33 std::vector
<CWalletTx
*> vpwtx
;
36 std::map
<CAmount
, CAccountingEntry
> results
;
38 LOCK(pwalletMain
->cs_wallet
);
42 ae
.nTime
= 1333333333;
43 ae
.strOtherAccount
= "b";
45 pwalletMain
->AddAccountingEntry(ae
);
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;
53 ae
.nTime
= 1333333336;
54 ae
.strOtherAccount
= "c";
55 pwalletMain
->AddAccountingEntry(ae
);
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");
68 ae
.nTime
= 1333333330;
69 ae
.strOtherAccount
= "d";
70 ae
.nOrderPos
= pwalletMain
->IncOrderPosNext();
71 pwalletMain
->AddAccountingEntry(ae
);
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());
84 wtx
.mapValue
["comment"] = "y";
86 CMutableTransaction
tx(wtx
);
87 --tx
.nLockTime
; // Just to change the hash :)
88 wtx
.SetTx(MakeTransactionRef(std::move(tx
)));
90 pwalletMain
->AddToWallet(wtx
);
91 vpwtx
.push_back(&pwalletMain
->mapWallet
[wtx
.GetHash()]);
92 vpwtx
[1]->nTimeReceived
= (unsigned int)1333333336;
94 wtx
.mapValue
["comment"] = "x";
96 CMutableTransaction
tx(wtx
);
97 --tx
.nLockTime
; // Just to change the hash :)
98 wtx
.SetTx(MakeTransactionRef(std::move(tx
)));
100 pwalletMain
->AddToWallet(wtx
);
101 vpwtx
.push_back(&pwalletMain
->mapWallet
[wtx
.GetHash()]);
102 vpwtx
[2]->nTimeReceived
= (unsigned int)1333333329;
103 vpwtx
[2]->nOrderPos
= -1;
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
);
118 ae
.nTime
= 1333333334;
119 ae
.strOtherAccount
= "e";
121 pwalletMain
->AddAccountingEntry(ae
);
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
);
138 BOOST_AUTO_TEST_SUITE_END()