You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

accounting_tests.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2012-2014 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. #include "wallet/wallet.h"
  5. #include "wallet/walletdb.h"
  6. #include "test/test_bitcoin.h"
  7. #include <stdint.h>
  8. #include <boost/foreach.hpp>
  9. #include <boost/test/unit_test.hpp>
  10. extern CWallet* pwalletMain;
  11. BOOST_FIXTURE_TEST_SUITE(accounting_tests, TestingSetup)
  12. static void
  13. GetResults(CWalletDB& walletdb, std::map<CAmount, CAccountingEntry>& results)
  14. {
  15. std::list<CAccountingEntry> aes;
  16. results.clear();
  17. BOOST_CHECK(walletdb.ReorderTransactions(pwalletMain) == DB_LOAD_OK);
  18. walletdb.ListAccountCreditDebit("", aes);
  19. BOOST_FOREACH(CAccountingEntry& ae, aes)
  20. {
  21. results[ae.nOrderPos] = ae;
  22. }
  23. }
  24. BOOST_AUTO_TEST_CASE(acc_orderupgrade)
  25. {
  26. CWalletDB walletdb(pwalletMain->strWalletFile);
  27. std::vector<CWalletTx*> vpwtx;
  28. CWalletTx wtx;
  29. CAccountingEntry ae;
  30. std::map<CAmount, CAccountingEntry> results;
  31. LOCK(pwalletMain->cs_wallet);
  32. ae.strAccount = "";
  33. ae.nCreditDebit = 1;
  34. ae.nTime = 1333333333;
  35. ae.strOtherAccount = "b";
  36. ae.strComment = "";
  37. walletdb.WriteAccountingEntry(ae);
  38. wtx.mapValue["comment"] = "z";
  39. pwalletMain->AddToWallet(wtx, false, &walletdb);
  40. vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
  41. vpwtx[0]->nTimeReceived = (unsigned int)1333333335;
  42. vpwtx[0]->nOrderPos = -1;
  43. ae.nTime = 1333333336;
  44. ae.strOtherAccount = "c";
  45. walletdb.WriteAccountingEntry(ae);
  46. GetResults(walletdb, results);
  47. BOOST_CHECK(pwalletMain->nOrderPosNext == 3);
  48. BOOST_CHECK(2 == results.size());
  49. BOOST_CHECK(results[0].nTime == 1333333333);
  50. BOOST_CHECK(results[0].strComment.empty());
  51. BOOST_CHECK(1 == vpwtx[0]->nOrderPos);
  52. BOOST_CHECK(results[2].nTime == 1333333336);
  53. BOOST_CHECK(results[2].strOtherAccount == "c");
  54. ae.nTime = 1333333330;
  55. ae.strOtherAccount = "d";
  56. ae.nOrderPos = pwalletMain->IncOrderPosNext();
  57. walletdb.WriteAccountingEntry(ae);
  58. GetResults(walletdb, results);
  59. BOOST_CHECK(results.size() == 3);
  60. BOOST_CHECK(pwalletMain->nOrderPosNext == 4);
  61. BOOST_CHECK(results[0].nTime == 1333333333);
  62. BOOST_CHECK(1 == vpwtx[0]->nOrderPos);
  63. BOOST_CHECK(results[2].nTime == 1333333336);
  64. BOOST_CHECK(results[3].nTime == 1333333330);
  65. BOOST_CHECK(results[3].strComment.empty());
  66. wtx.mapValue["comment"] = "y";
  67. {
  68. CMutableTransaction tx(wtx);
  69. --tx.nLockTime; // Just to change the hash :)
  70. *static_cast<CTransaction*>(&wtx) = CTransaction(tx);
  71. }
  72. pwalletMain->AddToWallet(wtx, false, &walletdb);
  73. vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
  74. vpwtx[1]->nTimeReceived = (unsigned int)1333333336;
  75. wtx.mapValue["comment"] = "x";
  76. {
  77. CMutableTransaction tx(wtx);
  78. --tx.nLockTime; // Just to change the hash :)
  79. *static_cast<CTransaction*>(&wtx) = CTransaction(tx);
  80. }
  81. pwalletMain->AddToWallet(wtx, false, &walletdb);
  82. vpwtx.push_back(&pwalletMain->mapWallet[wtx.GetHash()]);
  83. vpwtx[2]->nTimeReceived = (unsigned int)1333333329;
  84. vpwtx[2]->nOrderPos = -1;
  85. GetResults(walletdb, results);
  86. BOOST_CHECK(results.size() == 3);
  87. BOOST_CHECK(pwalletMain->nOrderPosNext == 6);
  88. BOOST_CHECK(0 == vpwtx[2]->nOrderPos);
  89. BOOST_CHECK(results[1].nTime == 1333333333);
  90. BOOST_CHECK(2 == vpwtx[0]->nOrderPos);
  91. BOOST_CHECK(results[3].nTime == 1333333336);
  92. BOOST_CHECK(results[4].nTime == 1333333330);
  93. BOOST_CHECK(results[4].strComment.empty());
  94. BOOST_CHECK(5 == vpwtx[1]->nOrderPos);
  95. ae.nTime = 1333333334;
  96. ae.strOtherAccount = "e";
  97. ae.nOrderPos = -1;
  98. walletdb.WriteAccountingEntry(ae);
  99. GetResults(walletdb, results);
  100. BOOST_CHECK(results.size() == 4);
  101. BOOST_CHECK(pwalletMain->nOrderPosNext == 7);
  102. BOOST_CHECK(0 == vpwtx[2]->nOrderPos);
  103. BOOST_CHECK(results[1].nTime == 1333333333);
  104. BOOST_CHECK(2 == vpwtx[0]->nOrderPos);
  105. BOOST_CHECK(results[3].nTime == 1333333336);
  106. BOOST_CHECK(results[3].strComment.empty());
  107. BOOST_CHECK(results[4].nTime == 1333333330);
  108. BOOST_CHECK(results[4].strComment.empty());
  109. BOOST_CHECK(results[5].nTime == 1333333334);
  110. BOOST_CHECK(6 == vpwtx[1]->nOrderPos);
  111. }
  112. BOOST_AUTO_TEST_SUITE_END()