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 BOOST_FIXTURE_TEST_SUITE(accounting_tests
, WalletTestingSetup
)
16 GetResults(CWallet
*wallet
, std::map
<CAmount
, CAccountingEntry
>& results
)
18 std::list
<CAccountingEntry
> aes
;
21 BOOST_CHECK(wallet
->ReorderTransactions() == DB_LOAD_OK
);
22 wallet
->ListAccountCreditDebit("", aes
);
23 for (CAccountingEntry
& ae
: aes
)
25 results
[ae
.nOrderPos
] = ae
;
29 BOOST_AUTO_TEST_CASE(acc_orderupgrade
)
31 std::vector
<CWalletTx
*> vpwtx
;
34 std::map
<CAmount
, CAccountingEntry
> results
;
36 LOCK(pwalletMain
->cs_wallet
);
40 ae
.nTime
= 1333333333;
41 ae
.strOtherAccount
= "b";
43 pwalletMain
->AddAccountingEntry(ae
);
45 wtx
.mapValue
["comment"] = "z";
46 pwalletMain
->AddToWallet(wtx
);
47 vpwtx
.push_back(&pwalletMain
->mapWallet
[wtx
.GetHash()]);
48 vpwtx
[0]->nTimeReceived
= (unsigned int)1333333335;
49 vpwtx
[0]->nOrderPos
= -1;
51 ae
.nTime
= 1333333336;
52 ae
.strOtherAccount
= "c";
53 pwalletMain
->AddAccountingEntry(ae
);
55 GetResults(pwalletMain
.get(), results
);
57 BOOST_CHECK(pwalletMain
->nOrderPosNext
== 3);
58 BOOST_CHECK(2 == results
.size());
59 BOOST_CHECK(results
[0].nTime
== 1333333333);
60 BOOST_CHECK(results
[0].strComment
.empty());
61 BOOST_CHECK(1 == vpwtx
[0]->nOrderPos
);
62 BOOST_CHECK(results
[2].nTime
== 1333333336);
63 BOOST_CHECK(results
[2].strOtherAccount
== "c");
66 ae
.nTime
= 1333333330;
67 ae
.strOtherAccount
= "d";
68 ae
.nOrderPos
= pwalletMain
->IncOrderPosNext();
69 pwalletMain
->AddAccountingEntry(ae
);
71 GetResults(pwalletMain
.get(), results
);
73 BOOST_CHECK(results
.size() == 3);
74 BOOST_CHECK(pwalletMain
->nOrderPosNext
== 4);
75 BOOST_CHECK(results
[0].nTime
== 1333333333);
76 BOOST_CHECK(1 == vpwtx
[0]->nOrderPos
);
77 BOOST_CHECK(results
[2].nTime
== 1333333336);
78 BOOST_CHECK(results
[3].nTime
== 1333333330);
79 BOOST_CHECK(results
[3].strComment
.empty());
82 wtx
.mapValue
["comment"] = "y";
84 CMutableTransaction
tx(*wtx
.tx
);
85 --tx
.nLockTime
; // Just to change the hash :)
86 wtx
.SetTx(MakeTransactionRef(std::move(tx
)));
88 pwalletMain
->AddToWallet(wtx
);
89 vpwtx
.push_back(&pwalletMain
->mapWallet
[wtx
.GetHash()]);
90 vpwtx
[1]->nTimeReceived
= (unsigned int)1333333336;
92 wtx
.mapValue
["comment"] = "x";
94 CMutableTransaction
tx(*wtx
.tx
);
95 --tx
.nLockTime
; // Just to change the hash :)
96 wtx
.SetTx(MakeTransactionRef(std::move(tx
)));
98 pwalletMain
->AddToWallet(wtx
);
99 vpwtx
.push_back(&pwalletMain
->mapWallet
[wtx
.GetHash()]);
100 vpwtx
[2]->nTimeReceived
= (unsigned int)1333333329;
101 vpwtx
[2]->nOrderPos
= -1;
103 GetResults(pwalletMain
.get(), results
);
105 BOOST_CHECK(results
.size() == 3);
106 BOOST_CHECK(pwalletMain
->nOrderPosNext
== 6);
107 BOOST_CHECK(0 == vpwtx
[2]->nOrderPos
);
108 BOOST_CHECK(results
[1].nTime
== 1333333333);
109 BOOST_CHECK(2 == vpwtx
[0]->nOrderPos
);
110 BOOST_CHECK(results
[3].nTime
== 1333333336);
111 BOOST_CHECK(results
[4].nTime
== 1333333330);
112 BOOST_CHECK(results
[4].strComment
.empty());
113 BOOST_CHECK(5 == vpwtx
[1]->nOrderPos
);
116 ae
.nTime
= 1333333334;
117 ae
.strOtherAccount
= "e";
119 pwalletMain
->AddAccountingEntry(ae
);
121 GetResults(pwalletMain
.get(), results
);
123 BOOST_CHECK(results
.size() == 4);
124 BOOST_CHECK(pwalletMain
->nOrderPosNext
== 7);
125 BOOST_CHECK(0 == vpwtx
[2]->nOrderPos
);
126 BOOST_CHECK(results
[1].nTime
== 1333333333);
127 BOOST_CHECK(2 == vpwtx
[0]->nOrderPos
);
128 BOOST_CHECK(results
[3].nTime
== 1333333336);
129 BOOST_CHECK(results
[3].strComment
.empty());
130 BOOST_CHECK(results
[4].nTime
== 1333333330);
131 BOOST_CHECK(results
[4].strComment
.empty());
132 BOOST_CHECK(results
[5].nTime
== 1333333334);
133 BOOST_CHECK(6 == vpwtx
[1]->nOrderPos
);
136 BOOST_AUTO_TEST_SUITE_END()