Merge pull request #341 from svost/c++11
[novacoin.git] / src / wallet.cpp
index 02650d7..b87b9f8 100644 (file)
@@ -24,6 +24,35 @@ extern int64_t nReserveBalance;
 // mapWallet
 //
 
+CWallet::CWallet()
+{
+    SetNull();
+}
+
+CWallet::CWallet(std::string strWalletFileIn)
+{
+    SetNull();
+
+    strWalletFile = strWalletFileIn;
+    fFileBacked = true;
+}
+
+void CWallet::SetNull()
+{
+    nWalletVersion = FEATURE_BASE;
+    nWalletMaxVersion = FEATURE_BASE;
+    fFileBacked = false;
+    nMasterKeyMaxID = 0;
+    pwalletdbEncryption = NULL;
+    pwalletdbDecryption = NULL;
+    nNextResend = 0;
+    nLastResend = 0;
+    nOrderPosNext = 0;
+    nKernelsTried = 0;
+    nCoinDaysTried = 0;
+    nTimeFirstKey = 0;
+}
+
 struct CompareValueOnly
 {
     bool operator()(const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1,
@@ -63,7 +92,7 @@ CPubKey CWallet::GenerateNewKey()
         nTimeFirstKey = nCreationTime;
 
     if (!AddKey(key))
-        throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
+        throw runtime_error("CWallet::GenerateNewKey() : AddKey failed");
     return key.GetPubKey();
 }
 
@@ -85,7 +114,7 @@ CMalleableKeyView CWallet::GenerateNewMalleableKey()
         nTimeFirstKey = nCreationTime;
 
     if (!AddKey(mKey))
-        throw std::runtime_error("CWallet::GenerateNewMalleableKey() : AddKey failed");
+        throw runtime_error("CWallet::GenerateNewMalleableKey() : AddKey failed");
     return CMalleableKeyView(mKey);
 }
 
@@ -114,7 +143,7 @@ bool CWallet::AddKey(const CMalleableKey& mKey)
     return true;
 }
 
-bool CWallet::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH)
+bool CWallet::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const vector<unsigned char> &vchCryptedSecretH)
 {
     if (!CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH))
         return false;
@@ -495,8 +524,8 @@ bool CWallet::DecryptWallet(const SecureString& strWalletPassphrase)
             auto mi2 = mapMalleableKeys.begin();
             while (mi2 != mapMalleableKeys.end())
             {
-                const CSecret &vchSecretH = mi2->second;
-                const CMalleableKeyView &keyView = mi2->first;
+                const auto &vchSecretH = mi2->second;
+                const auto &keyView = mi2->first;
                 pwalletdbDecryption->EraseCryptedMalleableKey(keyView);
                 pwalletdbDecryption->WriteMalleableKey(keyView, vchSecretH, mapKeyMetadata[CBitcoinAddress(keyView.GetMalleablePubKey())]);
                 mi2++;
@@ -526,7 +555,7 @@ bool CWallet::DecryptWallet(const SecureString& strWalletPassphrase)
     return true;
 }
 
-bool CWallet::GetPEM(const CKeyID &keyID, const std::string &fileName, const SecureString &strPassKey) const
+bool CWallet::GetPEM(const CKeyID &keyID, const string &fileName, const SecureString &strPassKey) const
 {
     BIO *pemOut = BIO_new_file(fileName.c_str(), "w");
     if (pemOut == NULL)
@@ -550,7 +579,7 @@ int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
     return nRet;
 }
 
-CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
+CWallet::TxItems CWallet::OrderedTxItems(list<CAccountingEntry>& acentries, string strAccount)
 {
     CWalletDB walletdb(strWalletFile);
 
@@ -562,13 +591,13 @@ CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries,
     for (auto it = mapWallet.begin(); it != mapWallet.end(); ++it)
     {
         CWalletTx* wtx = &((*it).second);
-        txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
+        txOrdered.insert({ wtx->nOrderPos, { wtx, (CAccountingEntry*)0 } });
     }
     acentries.clear();
     walletdb.ListAccountCreditDebit(strAccount, acentries);
     for(auto& entry : acentries)
     {
-        txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
+        txOrdered.insert({ entry.nOrderPos, { (CWalletTx*)0, &entry } });
     }
 
     return txOrdered;
@@ -636,7 +665,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
     {
         LOCK(cs_wallet);
         // Inserts only if not already there, returns tx inserted or tx found
-        auto ret = mapWallet.insert(make_pair(hash, wtxIn));
+        auto ret = mapWallet.insert({ hash, wtxIn });
         auto& wtx = (*ret.first).second;
         wtx.BindWallet(this);
         bool fInsertedNew = ret.second;
@@ -655,7 +684,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
                     {
                         // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
                         int64_t latestTolerated = latestNow + 300;
-                        std::list<CAccountingEntry> acentries;
+                        list<CAccountingEntry> acentries;
                         auto txOrdered = OrderedTxItems(acentries);
                         for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
                         {
@@ -683,7 +712,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
                     }
 
                     unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
-                    wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
+                    wtx.nTimeSmart = max(latestEntry, min(blocktime, latestNow));
                 }
                 else
                     printf("AddToWallet() : found %s in block %s not in index\n",
@@ -810,9 +839,62 @@ isminetype CWallet::IsMine(const CTxIn &txin) const
     return MINE_NO;
 }
 
+CWalletTx::CWalletTx()
+{
+    Init(NULL);
+}
+
+CWalletTx::CWalletTx(const CWallet* pwalletIn)
+{
+    Init(pwalletIn);
+}
+
+CWalletTx::CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
+{
+    Init(pwalletIn);
+}
+
+CWalletTx::CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
+{
+    Init(pwalletIn);
+}
+
+void CWalletTx::Init(const CWallet* pwalletIn)
+{
+    pwallet = pwalletIn;
+    vtxPrev.clear();
+    mapValue.clear();
+    vOrderForm.clear();
+    fTimeReceivedIsTxTime = false;
+    nTimeReceived = 0;
+    nTimeSmart = 0;
+    fFromMe = false;
+    strFromAccount.clear();
+    vfSpent.clear();
+    fDebitCached = false;
+    fWatchDebitCached = false;
+    fCreditCached = false;
+    fWatchCreditCached = false;
+    fAvailableCreditCached = false;
+    fAvailableWatchCreditCached = false;
+    fImmatureCreditCached = false;
+    fImmatureWatchCreditCached = false;
+    fChangeCached = false;
+    nDebitCached = 0;
+    nWatchDebitCached = 0;
+    nCreditCached = 0;
+    nWatchCreditCached = 0;
+    nAvailableCreditCached = 0;
+    nAvailableWatchCreditCached = 0;
+    nImmatureCreditCached = 0;
+    nImmatureWatchCreditCached = 0;
+    nChangeCached = 0;
+    nOrderPos = -1;
+}
+
 // marks certain txout's as spent
 // returns true if any update took place
-bool CWalletTx::UpdateSpent(const std::vector<char>& vfNewSpent)
+bool CWalletTx::UpdateSpent(const vector<char>& vfNewSpent)
 {
     bool fReturn = false;
     for (unsigned int i = 0; i < vfNewSpent.size(); i++)
@@ -848,7 +930,7 @@ void CWalletTx::BindWallet(CWallet *pwalletIn)
 void CWalletTx::MarkSpent(unsigned int nOut)
 {
     if (nOut >= vout.size())
-        throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
+        throw runtime_error("CWalletTx::MarkSpent() : nOut out of range");
     vfSpent.resize(vout.size());
     if (!vfSpent[nOut])
     {
@@ -860,7 +942,7 @@ void CWalletTx::MarkSpent(unsigned int nOut)
 void CWalletTx::MarkUnspent(unsigned int nOut)
 {
     if (nOut >= vout.size())
-        throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
+        throw runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
     vfSpent.resize(vout.size());
     if (vfSpent[nOut])
     {
@@ -872,7 +954,7 @@ void CWalletTx::MarkUnspent(unsigned int nOut)
 bool CWalletTx::IsSpent(unsigned int nOut) const
 {
     if (nOut >= vout.size())
-        throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
+        throw runtime_error("CWalletTx::IsSpent() : nOut out of range");
     if (nOut >= vfSpent.size())
         return false;
     return (!!vfSpent[nOut]);
@@ -902,7 +984,7 @@ isminetype CWallet::IsMine(const CTxOut& txout) const
 int64_t CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const
 {
     if (!MoneyRange(txout.nValue))
-        throw std::runtime_error("CWallet::GetCredit() : value out of range");
+        throw runtime_error("CWallet::GetCredit() : value out of range");
     return (IsMine(txout) & filter ? txout.nValue : 0);
 }
 
@@ -931,7 +1013,7 @@ bool CWallet::IsChange(const CTxOut& txout) const
 int64_t CWallet::GetChange(const CTxOut& txout) const
 {
     if (!MoneyRange(txout.nValue))
-        throw std::runtime_error("CWallet::GetChange() : value out of range");
+        throw runtime_error("CWallet::GetChange() : value out of range");
     return (IsChange(txout) ? txout.nValue : 0);
 }
 
@@ -955,7 +1037,7 @@ int64_t CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) co
     {
         nDebit += GetDebit(txin, filter);
         if (!MoneyRange(nDebit))
-            throw std::runtime_error("CWallet::GetDebit() : value out of range");
+            throw runtime_error("CWallet::GetDebit() : value out of range");
     }
     return nDebit;
 }
@@ -967,7 +1049,7 @@ int64_t CWallet::GetCredit(const CTransaction& tx, const isminefilter& filter) c
     {
         nCredit += GetCredit(txout, filter);
         if (!MoneyRange(nCredit))
-            throw std::runtime_error("CWallet::GetCredit() : value out of range");
+            throw runtime_error("CWallet::GetCredit() : value out of range");
     }
     return nCredit;
 }
@@ -979,7 +1061,7 @@ int64_t CWallet::GetChange(const CTransaction& tx) const
     {
         nChange += GetChange(txout);
         if (!MoneyRange(nChange))
-            throw std::runtime_error("CWallet::GetChange() : value out of range");
+            throw runtime_error("CWallet::GetChange() : value out of range");
     }
     return nChange;
 }
@@ -1180,7 +1262,7 @@ int64_t CWalletTx::GetAvailableCredit(bool fUseCache) const
             const CTxOut &txout = vout[i];
             nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
             if (!MoneyRange(nCredit))
-                throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
+                throw runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
         }
     }
 
@@ -1209,7 +1291,7 @@ int64_t CWalletTx::GetAvailableWatchCredit(bool fUseCache) const
             const CTxOut &txout = vout[i];
             nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
             if (!MoneyRange(nCredit))
-                throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
+                throw runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
         }
     }
 
@@ -1280,11 +1362,11 @@ void CWalletTx::GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMatur
 
         // If we are debited by the transaction, add the output as a "sent" entry
         if (nDebit > 0)
-            listSent.push_back(make_pair(address, txout.nValue));
+            listSent.push_back({ address, txout.nValue });
 
         // If we are receiving the output, add it as a "received" entry
         if (fIsMine & filter)
-            listReceived.push_back(make_pair(address, txout.nValue));
+            listReceived.push_back({ address, txout.nValue });
     }
 
 }
@@ -1514,9 +1596,9 @@ bool CWalletTx::RelayWalletTransaction()
    return RelayWalletTransaction(txdb);
 }
 
-std::vector<uint256> CWallet::ResendWalletTransactionsBefore(int64_t nTime)
+vector<uint256> CWallet::ResendWalletTransactionsBefore(int64_t nTime)
 {
-    std::vector<uint256> result;
+    vector<uint256> result;
 
     LOCK(cs_wallet);
     // Sort them in chronological order
@@ -1527,7 +1609,7 @@ std::vector<uint256> CWallet::ResendWalletTransactionsBefore(int64_t nTime)
         // Don't rebroadcast if newer than nTime:
         if (wtx.nTimeReceived > nTime)
             continue;
-        mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
+        mapSorted.insert({ wtx.nTimeReceived, &wtx });
     }
     for(auto& item : mapSorted)
     {
@@ -1558,7 +1640,7 @@ void CWallet::ResendWalletTransactions(int64_t nBestBlockTime)
 
     // Rebroadcast unconfirmed txes older than 5 minutes before the last
     // block was found:
-    std::vector<uint256> relayed = ResendWalletTransactionsBefore(nBestBlockTime - 5*60);
+    auto relayed = ResendWalletTransactionsBefore(nBestBlockTime - 5*60);
     if (!relayed.empty())
         printf("CWallet::ResendWalletTransactions: rebroadcast %" PRIszu " unconfirmed transactions\n", relayed.size());
 }
@@ -1824,7 +1906,7 @@ bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, unsigned int nSpendTime,
 
     // List of values less than target
     pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
-    coinLowestLarger.first = std::numeric_limits<int64_t>::max();
+    coinLowestLarger.first = numeric_limits<int64_t>::max();
     coinLowestLarger.second.first = NULL;
     vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue;
     int64_t nTotalLower = 0;
@@ -1848,7 +1930,7 @@ bool CWallet::SelectCoinsMinConf(int64_t nTargetValue, unsigned int nSpendTime,
             continue;
 
         auto n = pcoin->vout[i].nValue;
-        auto coin = make_pair(n,make_pair(pcoin, i));
+        auto coin = make_pair(n, make_pair(pcoin, i));
 
         if (n == nTargetValue)
         {
@@ -1938,7 +2020,7 @@ bool CWallet::SelectCoins(int64_t nTargetValue, unsigned int nSpendTime, set<pai
             if(!out.fSpendable)
                 continue;
             nValueRet += out.tx->vout[out.i].nValue;
-            setCoinsRet.insert(make_pair(out.tx, out.i));
+            setCoinsRet.insert({ out.tx, out.i });
         }
         return (nValueRet >= nTargetValue);
     }
@@ -1977,7 +2059,7 @@ bool CWallet::SelectCoinsSimple(int64_t nTargetValue, int64_t nMinValue, int64_t
             continue;
 
         auto n = pcoin->vout[i].nValue;
-        auto coin = make_pair(n,make_pair(pcoin, i));
+        auto coin = make_pair(n, make_pair(pcoin, i));
 
         if (n >= nTargetValue)
         {
@@ -2116,7 +2198,7 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
 bool CWallet::CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl* coinControl)
 {
     vector< pair<CScript, int64_t> > vecSend;
-    vecSend.push_back(make_pair(scriptPubKey, nValue));
+    vecSend.push_back({ scriptPubKey, nValue });
     return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, coinControl);
 }
 
@@ -2834,7 +2916,7 @@ int64_t CWallet::GetOldestKeyPoolTime()
     return keypool.nTime;
 }
 
-std::map<CBitcoinAddress, int64_t> CWallet::GetAddressBalances()
+map<CBitcoinAddress, int64_t> CWallet::GetAddressBalances()
 {
     map<CBitcoinAddress, int64_t> balances;
 
@@ -3107,7 +3189,7 @@ void CWallet::UpdatedTransaction(const uint256 &hashTx)
     }
 }
 
-void CWallet::GetAddresses(std::map<CBitcoinAddress, int64_t> &mapAddresses) const {
+void CWallet::GetAddresses(map<CBitcoinAddress, int64_t> &mapAddresses) const {
     mapAddresses.clear();
 
     // get birth times for keys with metadata
@@ -3131,7 +3213,7 @@ void CWallet::GetAddresses(std::map<CBitcoinAddress, int64_t> &mapAddresses) con
             }
             else {
                 // multisig output affects more than one key
-                std::vector<CKeyID> vAffected;
+                vector<CKeyID> vAffected;
                 ::ExtractAffectedKeys(*this, out.scriptPubKey, vAffected);
 
                 for(auto it3 = vAffected.begin(); it3 != vAffected.end(); it3++) {