From 223b6f1ba4819e9a146e7aa451d546726d0bc714 Mon Sep 17 00:00:00 2001 From: Wladimir J. van der Laan Date: Sun, 15 May 2011 09:11:04 +0200 Subject: [PATCH] make bitcoin include files more modular --- src/base58.h | 45 +++++++----- src/bignum.h | 12 ++- src/db.cpp | 11 ++- src/db.h | 96 ++++++++++++++----------- src/headers.h | 4 +- src/init.cpp | 11 +-- src/init.h | 4 + src/irc.cpp | 3 + src/irc.h | 6 +- src/key.h | 23 ++++-- src/main.cpp | 118 +++++++++++++++--------------- src/main.h | 220 +++++++++++++++++++++++++++++-------------------------- src/net.cpp | 57 ++++++++------- src/net.h | 94 +++++++++++++---------- src/noui.h | 13 ++- src/rpc.cpp | 46 ++++++------ src/script.cpp | 12 ++- src/script.h | 53 ++++++++------ src/serialize.h | 112 +++++++++++++++------------- src/strlcpy.h | 4 +- src/ui.cpp | 42 ++++++----- src/ui.h | 44 ++++++----- src/uint256.h | 16 +++- src/util.cpp | 9 +- src/util.h | 95 +++++++++++++++--------- 25 files changed, 640 insertions(+), 510 deletions(-) diff --git a/src/base58.h b/src/base58.h index 828f8d5..580bd3f 100644 --- a/src/base58.h +++ b/src/base58.h @@ -11,12 +11,17 @@ // - E-mail usually won't line-break if there's no punctuation to break at. // - Doubleclicking selects the whole number as one word if it's all alphanumeric. // +#ifndef BITCOIN_BASE58_H +#define BITCOIN_BASE58_H +#include +#include +#include "bignum.h" static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; -inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) +inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) { CAutoBN_CTX pctx; CBigNum bn58 = 58; @@ -24,15 +29,15 @@ inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pen // Convert big endian data to little endian // Extra zero at the end make sure bignum will interpret as a positive number - vector vchTmp(pend-pbegin+1, 0); + std::vector vchTmp(pend-pbegin+1, 0); reverse_copy(pbegin, pend, vchTmp.begin()); // Convert little endian data to bignum CBigNum bn; bn.setvch(vchTmp); - // Convert bignum to string - string str; + // Convert bignum to std::string + std::string str; str.reserve((pend - pbegin) * 138 / 100 + 1); CBigNum dv; CBigNum rem; @@ -49,17 +54,17 @@ inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pen for (const unsigned char* p = pbegin; p < pend && *p == 0; p++) str += pszBase58[0]; - // Convert little endian string to big endian + // Convert little endian std::string to big endian reverse(str.begin(), str.end()); return str; } -inline string EncodeBase58(const vector& vch) +inline std::string EncodeBase58(const std::vector& vch) { return EncodeBase58(&vch[0], &vch[0] + vch.size()); } -inline bool DecodeBase58(const char* psz, vector& vchRet) +inline bool DecodeBase58(const char* psz, std::vector& vchRet) { CAutoBN_CTX pctx; vchRet.clear(); @@ -88,7 +93,7 @@ inline bool DecodeBase58(const char* psz, vector& vchRet) } // Get bignum as little endian data - vector vchTmp = bn.getvch(); + std::vector vchTmp = bn.getvch(); // Trim off sign byte if present if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80) @@ -105,7 +110,7 @@ inline bool DecodeBase58(const char* psz, vector& vchRet) return true; } -inline bool DecodeBase58(const string& str, vector& vchRet) +inline bool DecodeBase58(const std::string& str, std::vector& vchRet) { return DecodeBase58(str.c_str(), vchRet); } @@ -114,16 +119,16 @@ inline bool DecodeBase58(const string& str, vector& vchRet) -inline string EncodeBase58Check(const vector& vchIn) +inline std::string EncodeBase58Check(const std::vector& vchIn) { // add 4-byte hash check to the end - vector vch(vchIn); + std::vector vch(vchIn); uint256 hash = Hash(vch.begin(), vch.end()); vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4); return EncodeBase58(vch); } -inline bool DecodeBase58Check(const char* psz, vector& vchRet) +inline bool DecodeBase58Check(const char* psz, std::vector& vchRet) { if (!DecodeBase58(psz, vchRet)) return false; @@ -142,7 +147,7 @@ inline bool DecodeBase58Check(const char* psz, vector& vchRet) return true; } -inline bool DecodeBase58Check(const string& str, vector& vchRet) +inline bool DecodeBase58Check(const std::string& str, std::vector& vchRet) { return DecodeBase58Check(str.c_str(), vchRet); } @@ -154,17 +159,17 @@ inline bool DecodeBase58Check(const string& str, vector& vchRet) #define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0)) -inline string Hash160ToAddress(uint160 hash160) +inline std::string Hash160ToAddress(uint160 hash160) { // add 1-byte version number to the front - vector vch(1, ADDRESSVERSION); + std::vector vch(1, ADDRESSVERSION); vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160)); return EncodeBase58Check(vch); } inline bool AddressToHash160(const char* psz, uint160& hash160Ret) { - vector vch; + std::vector vch; if (!DecodeBase58Check(psz, vch)) return false; if (vch.empty()) @@ -176,7 +181,7 @@ inline bool AddressToHash160(const char* psz, uint160& hash160Ret) return (nVersion <= ADDRESSVERSION); } -inline bool AddressToHash160(const string& str, uint160& hash160Ret) +inline bool AddressToHash160(const std::string& str, uint160& hash160Ret) { return AddressToHash160(str.c_str(), hash160Ret); } @@ -187,7 +192,7 @@ inline bool IsValidBitcoinAddress(const char* psz) return AddressToHash160(psz, hash160); } -inline bool IsValidBitcoinAddress(const string& str) +inline bool IsValidBitcoinAddress(const std::string& str) { return IsValidBitcoinAddress(str.c_str()); } @@ -195,7 +200,9 @@ inline bool IsValidBitcoinAddress(const string& str) -inline string PubKeyToAddress(const vector& vchPubKey) +inline std::string PubKeyToAddress(const std::vector& vchPubKey) { return Hash160ToAddress(Hash160(vchPubKey)); } + +#endif diff --git a/src/bignum.h b/src/bignum.h index 450e809..5b4c78e 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -1,14 +1,14 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_BIGNUM_H +#define BITCOIN_BIGNUM_H #include #include #include - - - +#include "util.h" class bignum_error : public std::runtime_error { @@ -308,7 +308,7 @@ public: CAutoBN_CTX pctx; CBigNum bnBase = nBase; CBigNum bn0 = 0; - string str; + std::string str; CBigNum bn = *this; BN_set_negative(&bn, false); CBigNum dv; @@ -348,7 +348,7 @@ public: template void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) { - vector vch; + std::vector vch; ::Unserialize(s, vch, nType, nVersion); setvch(vch); } @@ -530,3 +530,5 @@ inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); } inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); } inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); } + +#endif diff --git a/src/db.cpp b/src/db.cpp index 071231c..52c0f5b 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -4,6 +4,9 @@ #include "headers.h" +using namespace std; +using namespace boost; + void ThreadFlushWalletDB(void* parg); @@ -434,13 +437,13 @@ bool CTxDB::LoadBlockIndex() // Calculate bnChainWork vector > vSortedByHeight; vSortedByHeight.reserve(mapBlockIndex.size()); - foreach(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex) + BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex) { CBlockIndex* pindex = item.second; vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex)); } sort(vSortedByHeight.begin(), vSortedByHeight.end()); - foreach(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight) + BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight) { CBlockIndex* pindex = item.second; pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork(); @@ -603,7 +606,7 @@ int64 CWalletDB::GetAccountCreditDebit(const string& strAccount) ListAccountCreditDebit(strAccount, entries); int64 nCreditDebit = 0; - foreach (const CAccountingEntry& entry, entries) + BOOST_FOREACH (const CAccountingEntry& entry, entries) nCreditDebit += entry.nCreditDebit; return nCreditDebit; @@ -796,7 +799,7 @@ bool CWalletDB::LoadWallet() pcursor->close(); } - foreach(uint256 hash, vWalletUpgrade) + BOOST_FOREACH(uint256 hash, vWalletUpgrade) WriteTx(hash, mapWallet[hash]); printf("nFileVersion = %d\n", nFileVersion); diff --git a/src/db.h b/src/db.h index 290981c..9826194 100644 --- a/src/db.h +++ b/src/db.h @@ -1,6 +1,16 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_DB_H +#define BITCOIN_DB_H + +#include "key.h" + +#include +#include +#include + +#include class CTransaction; class CTxIndex; @@ -15,9 +25,9 @@ class CAccount; class CAccountingEntry; class CBlockLocator; -extern map mapAddressBook; +extern std::map mapAddressBook; extern CCriticalSection cs_mapAddressBook; -extern vector vchDefaultKey; +extern std::vector vchDefaultKey; extern bool fClient; extern int nBestHeight; @@ -27,7 +37,7 @@ extern DbEnv dbenv; extern void DBFlush(bool fShutdown); -extern vector GetKeyFromKeyPool(); +extern std::vector GetKeyFromKeyPool(); extern int64 GetOldestKeyPoolTime(); @@ -37,8 +47,8 @@ class CDB { protected: Db* pdb; - string strFile; - vector vTxn; + std::string strFile; + std::vector vTxn; bool fReadOnly; explicit CDB(const char* pszFile, const char* pszMode="r+"); @@ -247,12 +257,12 @@ public: bool ReadVersion(int& nVersion) { nVersion = 0; - return Read(string("version"), nVersion); + return Read(std::string("version"), nVersion); } bool WriteVersion(int nVersion) { - return Write(string("version"), nVersion); + return Write(std::string("version"), nVersion); } }; @@ -276,7 +286,7 @@ public: bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight); bool EraseTxIndex(const CTransaction& tx); bool ContainsTx(uint256 hash); - bool ReadOwnerTxes(uint160 hash160, int nHeight, vector& vtx); + bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector& vtx); bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex); bool ReadDiskTx(uint256 hash, CTransaction& tx); bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex); @@ -318,14 +328,14 @@ class CKeyPool { public: int64 nTime; - vector vchPubKey; + std::vector vchPubKey; CKeyPool() { nTime = GetTime(); } - CKeyPool(const vector& vchPubKeyIn) + CKeyPool(const std::vector& vchPubKeyIn) { nTime = GetTime(); vchPubKey = vchPubKeyIn; @@ -353,101 +363,101 @@ private: CWalletDB(const CWalletDB&); void operator=(const CWalletDB&); public: - bool ReadName(const string& strAddress, string& strName) + bool ReadName(const std::string& strAddress, std::string& strName) { strName = ""; - return Read(make_pair(string("name"), strAddress), strName); + return Read(std::make_pair(std::string("name"), strAddress), strName); } - bool WriteName(const string& strAddress, const string& strName) + bool WriteName(const std::string& strAddress, const std::string& strName) { CRITICAL_BLOCK(cs_mapAddressBook) mapAddressBook[strAddress] = strName; nWalletDBUpdated++; - return Write(make_pair(string("name"), strAddress), strName); + return Write(std::make_pair(std::string("name"), strAddress), strName); } - bool EraseName(const string& strAddress) + bool EraseName(const std::string& strAddress) { // This should only be used for sending addresses, never for receiving addresses, // receiving addresses must always have an address book entry if they're not change return. CRITICAL_BLOCK(cs_mapAddressBook) mapAddressBook.erase(strAddress); nWalletDBUpdated++; - return Erase(make_pair(string("name"), strAddress)); + return Erase(std::make_pair(std::string("name"), strAddress)); } bool ReadTx(uint256 hash, CWalletTx& wtx) { - return Read(make_pair(string("tx"), hash), wtx); + return Read(std::make_pair(std::string("tx"), hash), wtx); } bool WriteTx(uint256 hash, const CWalletTx& wtx) { nWalletDBUpdated++; - return Write(make_pair(string("tx"), hash), wtx); + return Write(std::make_pair(std::string("tx"), hash), wtx); } bool EraseTx(uint256 hash) { nWalletDBUpdated++; - return Erase(make_pair(string("tx"), hash)); + return Erase(std::make_pair(std::string("tx"), hash)); } - bool ReadKey(const vector& vchPubKey, CPrivKey& vchPrivKey) + bool ReadKey(const std::vector& vchPubKey, CPrivKey& vchPrivKey) { vchPrivKey.clear(); - return Read(make_pair(string("key"), vchPubKey), vchPrivKey); + return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey); } - bool WriteKey(const vector& vchPubKey, const CPrivKey& vchPrivKey) + bool WriteKey(const std::vector& vchPubKey, const CPrivKey& vchPrivKey) { nWalletDBUpdated++; - return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false); + return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false); } bool WriteBestBlock(const CBlockLocator& locator) { nWalletDBUpdated++; - return Write(string("bestblock"), locator); + return Write(std::string("bestblock"), locator); } bool ReadBestBlock(CBlockLocator& locator) { - return Read(string("bestblock"), locator); + return Read(std::string("bestblock"), locator); } - bool ReadDefaultKey(vector& vchPubKey) + bool ReadDefaultKey(std::vector& vchPubKey) { vchPubKey.clear(); - return Read(string("defaultkey"), vchPubKey); + return Read(std::string("defaultkey"), vchPubKey); } - bool WriteDefaultKey(const vector& vchPubKey) + bool WriteDefaultKey(const std::vector& vchPubKey) { vchDefaultKey = vchPubKey; nWalletDBUpdated++; - return Write(string("defaultkey"), vchPubKey); + return Write(std::string("defaultkey"), vchPubKey); } template - bool ReadSetting(const string& strKey, T& value) + bool ReadSetting(const std::string& strKey, T& value) { - return Read(make_pair(string("setting"), strKey), value); + return Read(std::make_pair(std::string("setting"), strKey), value); } template - bool WriteSetting(const string& strKey, const T& value) + bool WriteSetting(const std::string& strKey, const T& value) { nWalletDBUpdated++; - return Write(make_pair(string("setting"), strKey), value); + return Write(std::make_pair(std::string("setting"), strKey), value); } - bool ReadAccount(const string& strAccount, CAccount& account); - bool WriteAccount(const string& strAccount, const CAccount& account); + bool ReadAccount(const std::string& strAccount, CAccount& account); + bool WriteAccount(const std::string& strAccount, const CAccount& account); bool WriteAccountingEntry(const CAccountingEntry& acentry); - int64 GetAccountCreditDebit(const string& strAccount); - void ListAccountCreditDebit(const string& strAccount, list& acentries); + int64 GetAccountCreditDebit(const std::string& strAccount); + void ListAccountCreditDebit(const std::string& strAccount, std::list& acentries); bool LoadWallet(); protected: @@ -455,14 +465,14 @@ protected: void KeepKey(int64 nIndex); static void ReturnKey(int64 nIndex); friend class CReserveKey; - friend vector GetKeyFromKeyPool(); + friend std::vector GetKeyFromKeyPool(); friend int64 GetOldestKeyPoolTime(); }; bool LoadWallet(bool& fFirstRunRet); -void BackupWallet(const string& strDest); +void BackupWallet(const std::string& strDest); -inline bool SetAddressBookName(const string& strAddress, const string& strName) +inline bool SetAddressBookName(const std::string& strAddress, const std::string& strName) { return CWalletDB().WriteName(strAddress, strName); } @@ -471,7 +481,7 @@ class CReserveKey { protected: int64 nIndex; - vector vchPubKey; + std::vector vchPubKey; public: CReserveKey() { @@ -484,7 +494,7 @@ public: ReturnKey(); } - vector GetReservedKey() + std::vector GetReservedKey() { if (nIndex == -1) { @@ -512,3 +522,5 @@ public: vchPubKey.clear(); } }; + +#endif diff --git a/src/headers.h b/src/headers.h index 6a08cb7..d40c5ed 100644 --- a/src/headers.h +++ b/src/headers.h @@ -109,8 +109,6 @@ #pragma hdrstop -using namespace std; -using namespace boost; #include "strlcpy.h" #include "serialize.h" @@ -133,6 +131,7 @@ using namespace boost; #endif #include "init.h" +#ifdef GUI #include "xpm/addressbook16.xpm" #include "xpm/addressbook20.xpm" #include "xpm/bitcoin16.xpm" @@ -145,3 +144,4 @@ using namespace boost; #include "xpm/send16noshadow.xpm" #include "xpm/send20.xpm" #include "xpm/about.xpm" +#endif diff --git a/src/init.cpp b/src/init.cpp index 7e84675..431c533 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1,14 +1,10 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. - #include "headers.h" - - - - - +using namespace std; +using namespace boost; ////////////////////////////////////////////////////////////////////////////// // @@ -71,7 +67,6 @@ void HandleSIGTERM(int) // // Start // - #ifndef GUI int main(int argc, char* argv[]) { @@ -458,7 +453,7 @@ bool AppInit2(int argc, char* argv[]) if (mapArgs.count("-addnode")) { - foreach(string strAddr, mapMultiArgs["-addnode"]) + BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"]) { CAddress addr(strAddr, fAllowDNS); addr.nTime = 0; // so it won't relay unless successfully connected diff --git a/src/init.h b/src/init.h index 265ddb8..61b2728 100644 --- a/src/init.h +++ b/src/init.h @@ -1,7 +1,11 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_INIT_H +#define BITCOIN_INIT_H void Shutdown(void* parg); bool AppInit(int argc, char* argv[]); bool AppInit2(int argc, char* argv[]); + +#endif diff --git a/src/irc.cpp b/src/irc.cpp index 4e39889..099d9e0 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -4,6 +4,9 @@ #include "headers.h" +using namespace std; +using namespace boost; + int nGotIRCAddresses = 0; bool fGotExternalIP = false; diff --git a/src/irc.h b/src/irc.h index 4bc8129..18e5359 100644 --- a/src/irc.h +++ b/src/irc.h @@ -1,9 +1,13 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_IRC_H +#define BITCOIN_IRC_H -bool RecvLine(SOCKET hSocket, string& strLine); +bool RecvLine(SOCKET hSocket, std::string& strLine); void ThreadIRCSeed(void* parg); extern int nGotIRCAddresses; extern bool fGotExternalIP; + +#endif diff --git a/src/key.h b/src/key.h index 06f88cc..c973d6e 100644 --- a/src/key.h +++ b/src/key.h @@ -1,7 +1,12 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_KEY_H +#define BITCOIN_KEY_H +#include +#include +#include // secp160k1 // const unsigned int PRIVATE_KEY_SIZE = 192; @@ -36,7 +41,7 @@ public: // secure_allocator is defined in serialize.h -typedef vector > CPrivKey; +typedef std::vector > CPrivKey; @@ -109,7 +114,7 @@ public: return vchPrivKey; } - bool SetPubKey(const vector& vchPubKey) + bool SetPubKey(const std::vector& vchPubKey) { const unsigned char* pbegin = &vchPubKey[0]; if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size())) @@ -118,19 +123,19 @@ public: return true; } - vector GetPubKey() const + std::vector GetPubKey() const { unsigned int nSize = i2o_ECPublicKey(pkey, NULL); if (!nSize) throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed"); - vector vchPubKey(nSize, 0); + std::vector vchPubKey(nSize, 0); unsigned char* pbegin = &vchPubKey[0]; if (i2o_ECPublicKey(pkey, &pbegin) != nSize) throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size"); return vchPubKey; } - bool Sign(uint256 hash, vector& vchSig) + bool Sign(uint256 hash, std::vector& vchSig) { vchSig.clear(); unsigned char pchSig[10000]; @@ -142,7 +147,7 @@ public: return true; } - bool Verify(uint256 hash, const vector& vchSig) + bool Verify(uint256 hash, const std::vector& vchSig) { // -1 = error, 0 = bad sig, 1 = good if (ECDSA_verify(0, (unsigned char*)&hash, sizeof(hash), &vchSig[0], vchSig.size(), pkey) != 1) @@ -150,7 +155,7 @@ public: return true; } - static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, vector& vchSig) + static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, std::vector& vchSig) { CKey key; if (!key.SetPrivKey(vchPrivKey)) @@ -158,7 +163,7 @@ public: return key.Sign(hash, vchSig); } - static bool Verify(const vector& vchPubKey, uint256 hash, const vector& vchSig) + static bool Verify(const std::vector& vchPubKey, uint256 hash, const std::vector& vchSig) { CKey key; if (!key.SetPubKey(vchPubKey)) @@ -166,3 +171,5 @@ public: return key.Verify(hash, vchSig); } }; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 4f9ceac..68b6b4e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,11 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. - #include "headers.h" #include "cryptopp/sha.h" - - - +using namespace std; +using namespace boost; // // Global state @@ -156,7 +154,7 @@ bool AddToWallet(const CWalletTx& wtxIn) // If default receiving address gets used, replace it with a new one CScript scriptDefaultKey; scriptDefaultKey.SetBitcoinAddress(vchDefaultKey); - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.scriptPubKey == scriptDefaultKey) { @@ -244,7 +242,7 @@ void AddOrphanTx(const CDataStream& vMsg) if (mapOrphanTransactions.count(hash)) return; CDataStream* pvMsg = mapOrphanTransactions[hash] = new CDataStream(vMsg); - foreach(const CTxIn& txin, tx.vin) + BOOST_FOREACH(const CTxIn& txin, tx.vin) mapOrphanTransactionsByPrev.insert(make_pair(txin.prevout.hash, pvMsg)); } @@ -255,7 +253,7 @@ void EraseOrphanTx(uint256 hash) const CDataStream* pvMsg = mapOrphanTransactions[hash]; CTransaction tx; CDataStream(*pvMsg) >> tx; - foreach(const CTxIn& txin, tx.vin) + BOOST_FOREACH(const CTxIn& txin, tx.vin) { for (multimap::iterator mi = mapOrphanTransactionsByPrev.lower_bound(txin.prevout.hash); mi != mapOrphanTransactionsByPrev.upper_bound(txin.prevout.hash);) @@ -426,7 +424,7 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l // Sent/received. Standard client will never generate a send-to-multiple-recipients, // but non-standard clients might (so return a list of address/amount pairs) - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) { string address; uint160 hash160; @@ -471,13 +469,13 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i nGenerated = allGeneratedMature; if (strAccount == strSentAccount) { - foreach(const PAIRTYPE(string,int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(string,int64)& s, listSent) nSent += s.second; nFee = allFee; } CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string,int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) { if (mapAddressBook.count(r.first)) { @@ -557,7 +555,7 @@ void CWalletTx::AddSupportingTransactions(CTxDB& txdb) if (SetMerkleBranch() < COPY_DEPTH) { vector vWorkQueue; - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) vWorkQueue.push_back(txin.prevout.hash); // This critsect is OK because txdb is already open @@ -576,7 +574,7 @@ void CWalletTx::AddSupportingTransactions(CTxDB& txdb) if (mapWallet.count(hash)) { tx = mapWallet[hash]; - foreach(const CMerkleTx& txWalletPrev, mapWallet[hash].vtxPrev) + BOOST_FOREACH(const CMerkleTx& txWalletPrev, mapWallet[hash].vtxPrev) mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev; } else if (mapWalletPrev.count(hash)) @@ -597,7 +595,7 @@ void CWalletTx::AddSupportingTransactions(CTxDB& txdb) vtxPrev.push_back(tx); if (nDepth < COPY_DEPTH) - foreach(const CTxIn& txin, tx.vin) + BOOST_FOREACH(const CTxIn& txin, tx.vin) vWorkQueue.push_back(txin.prevout.hash); } } @@ -628,7 +626,7 @@ bool CTransaction::CheckTransaction() const // Check for negative or overflow output values int64 nValueOut = 0; - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) { if (txout.nValue < 0) return error("CTransaction::CheckTransaction() : txout.nValue negative"); @@ -646,7 +644,7 @@ bool CTransaction::CheckTransaction() const } else { - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) if (txin.prevout.IsNull()) return error("CTransaction::CheckTransaction() : prevout is null"); } @@ -804,7 +802,7 @@ bool CTransaction::RemoveFromMemoryPool() // Remove transaction from memory pool CRITICAL_BLOCK(cs_mapTransactions) { - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) mapNextTx.erase(txin.prevout); mapTransactions.erase(GetHash()); nTransactionsUpdated++; @@ -872,7 +870,7 @@ bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs) CRITICAL_BLOCK(cs_mapTransactions) { // Add previous supporting transactions first - foreach(CMerkleTx& tx, vtxPrev) + BOOST_FOREACH(CMerkleTx& tx, vtxPrev) { if (!tx.IsCoinBase()) { @@ -897,7 +895,7 @@ int ScanForWalletTransactions(CBlockIndex* pindexStart) { CBlock block; block.ReadFromDisk(pindex, true); - foreach(CTransaction& tx, block.vtx) + BOOST_FOREACH(CTransaction& tx, block.vtx) { if (AddToWalletIfInvolvingMe(tx, &block)) ret++; @@ -916,7 +914,7 @@ void ReacceptWalletTransactions() { fRepeat = false; vector vMissingTx; - foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) + BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) { CWalletTx& wtx = item.second; if (wtx.IsCoinBase() && wtx.IsSpent(0)) @@ -969,7 +967,7 @@ void ReacceptWalletTransactions() void CWalletTx::RelayWalletTransaction(CTxDB& txdb) { - foreach(const CMerkleTx& tx, vtxPrev) + BOOST_FOREACH(const CMerkleTx& tx, vtxPrev) { if (!tx.IsCoinBase()) { @@ -1014,7 +1012,7 @@ void ResendWalletTransactions() { // Sort them in chronological order multimap mapSorted; - foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) + BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) { CWalletTx& wtx = item.second; // Don't rebroadcast until it's had plenty of time that @@ -1022,7 +1020,7 @@ void ResendWalletTransactions() if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60) mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx)); } - foreach(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted) + BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted) { CWalletTx& wtx = *item.second; wtx.RelayWalletTransaction(txdb); @@ -1198,7 +1196,7 @@ bool CTransaction::DisconnectInputs(CTxDB& txdb) // Relinquish previous transactions' spent pointers if (!IsCoinBase()) { - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) { COutPoint prevout = txin.prevout; @@ -1421,7 +1419,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex) map mapUnused; int64 nFees = 0; - foreach(CTransaction& tx, vtx) + BOOST_FOREACH(CTransaction& tx, vtx) { CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos); nTxPos += ::GetSerializeSize(tx, SER_DISK); @@ -1444,7 +1442,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex) } // Watch for transactions paying to me - foreach(CTransaction& tx, vtx) + BOOST_FOREACH(CTransaction& tx, vtx) AddToWalletIfInvolvingMe(tx, this, true); return true; @@ -1481,7 +1479,7 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) // Disconnect shorter branch vector vResurrect; - foreach(CBlockIndex* pindex, vDisconnect) + BOOST_FOREACH(CBlockIndex* pindex, vDisconnect) { CBlock block; if (!block.ReadFromDisk(pindex)) @@ -1490,7 +1488,7 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) return error("Reorganize() : DisconnectBlock failed"); // Queue memory transactions to resurrect - foreach(const CTransaction& tx, block.vtx) + BOOST_FOREACH(const CTransaction& tx, block.vtx) if (!tx.IsCoinBase()) vResurrect.push_back(tx); } @@ -1511,7 +1509,7 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) } // Queue memory transactions to delete - foreach(const CTransaction& tx, block.vtx) + BOOST_FOREACH(const CTransaction& tx, block.vtx) vDelete.push_back(tx); } if (!txdb.WriteHashBestChain(pindexNew->GetBlockHash())) @@ -1522,21 +1520,21 @@ bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) return error("Reorganize() : TxnCommit failed"); // Disconnect shorter branch - foreach(CBlockIndex* pindex, vDisconnect) + BOOST_FOREACH(CBlockIndex* pindex, vDisconnect) if (pindex->pprev) pindex->pprev->pnext = NULL; // Connect longer branch - foreach(CBlockIndex* pindex, vConnect) + BOOST_FOREACH(CBlockIndex* pindex, vConnect) if (pindex->pprev) pindex->pprev->pnext = pindex; // Resurrect memory transactions that were in the disconnected branch - foreach(CTransaction& tx, vResurrect) + BOOST_FOREACH(CTransaction& tx, vResurrect) tx.AcceptToMemoryPool(txdb, false); // Delete redundant memory transactions that are in the connected branch - foreach(CTransaction& tx, vDelete) + BOOST_FOREACH(CTransaction& tx, vDelete) tx.RemoveFromMemoryPool(); return true; @@ -1571,7 +1569,7 @@ bool CBlock::SetBestChain(CTxDB& txdb, CBlockIndex* pindexNew) pindexNew->pprev->pnext = pindexNew; // Delete redundant memory transactions - foreach(CTransaction& tx, vtx) + BOOST_FOREACH(CTransaction& tx, vtx) tx.RemoveFromMemoryPool(); } else @@ -1682,7 +1680,7 @@ bool CBlock::CheckBlock() const return error("CheckBlock() : more than one coinbase"); // Check transactions - foreach(const CTransaction& tx, vtx) + BOOST_FOREACH(const CTransaction& tx, vtx) if (!tx.CheckTransaction()) return error("CheckBlock() : CheckTransaction failed"); @@ -1720,7 +1718,7 @@ bool CBlock::AcceptBlock() return error("AcceptBlock() : block's timestamp is too early"); // Check that all transactions are finalized - foreach(const CTransaction& tx, vtx) + BOOST_FOREACH(const CTransaction& tx, vtx) if (!tx.IsFinal(nHeight, GetBlockTime())) return error("AcceptBlock() : contains a non-final transaction"); @@ -1748,7 +1746,7 @@ bool CBlock::AcceptBlock() // Relay inventory, but don't relay old inventory during initial block download if (hashBestChain == hash) CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (nBestHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 118000)) pnode->PushInventory(CInv(MSG_BLOCK, hash)); @@ -2120,7 +2118,7 @@ string GetWarnings(string strFor) // Alerts CRITICAL_BLOCK(cs_mapAlerts) { - foreach(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) + BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) { const CAlert& alert = item.second; if (alert.AppliesToMe() && alert.nPriority > nPriority) @@ -2167,7 +2165,7 @@ bool CAlert::ProcessAlert() } // Check if this alert has been cancelled - foreach(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) + BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) { const CAlert& alert = item.second; if (alert.Cancels(*this)) @@ -2431,7 +2429,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) // Relay alerts CRITICAL_BLOCK(cs_mapAlerts) - foreach(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) + BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts) item.second.RelayTo(pfrom); pfrom->fSuccessfullyConnected = true; @@ -2469,7 +2467,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) // Store the new addresses int64 nNow = GetAdjustedTime(); int64 nSince = nNow - 10 * 60; - foreach(CAddress& addr, vAddr) + BOOST_FOREACH(CAddress& addr, vAddr) { if (fShutdown) return true; @@ -2493,7 +2491,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) uint256 hashRand = hashSalt ^ (((int64)addr.ip)<<32) ^ ((GetTime()+addr.ip)/(24*60*60)); hashRand = Hash(BEGIN(hashRand), END(hashRand)); multimap mapMix; - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) { if (pnode->nVersion < 31402) continue; @@ -2522,7 +2520,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) return error("message inv size() = %d", vInv.size()); CTxDB txdb("r"); - foreach(const CInv& inv, vInv) + BOOST_FOREACH(const CInv& inv, vInv) { if (fShutdown) return true; @@ -2554,7 +2552,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) if (vInv.size() > 50000) return error("message getdata size() = %d", vInv.size()); - foreach(const CInv& inv, vInv) + BOOST_FOREACH(const CInv& inv, vInv) { if (fShutdown) return true; @@ -2717,7 +2715,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) } } - foreach(uint256 hash, vWorkQueue) + BOOST_FOREACH(uint256 hash, vWorkQueue) EraseOrphanTx(hash); } else if (fMissingInputs) @@ -2752,13 +2750,13 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) CRITICAL_BLOCK(cs_mapAddresses) { unsigned int nCount = 0; - foreach(const PAIRTYPE(vector, CAddress)& item, mapAddresses) + BOOST_FOREACH(const PAIRTYPE(vector, CAddress)& item, mapAddresses) { const CAddress& addr = item.second; if (addr.nTime > nSince) nCount++; } - foreach(const PAIRTYPE(vector, CAddress)& item, mapAddresses) + BOOST_FOREACH(const PAIRTYPE(vector, CAddress)& item, mapAddresses) { const CAddress& addr = item.second; if (addr.nTime > nSince && GetRand(nCount) < 2500) @@ -2861,7 +2859,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) // Relay pfrom->setKnown.insert(alert.GetHash()); CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) alert.RelayTo(pnode); } } @@ -2912,7 +2910,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) nLastRebroadcast = GetTime(); CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) { // Periodically clear setAddrKnown to allow refresh broadcasts pnode->setAddrKnown.clear(); @@ -2964,7 +2962,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) { vector vAddr; vAddr.reserve(pto->vAddrToSend.size()); - foreach(const CAddress& addr, pto->vAddrToSend) + BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend) { // returns true if wasn't already contained in the set if (pto->setAddrKnown.insert(addr).second) @@ -2993,7 +2991,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle) { vInv.reserve(pto->vInventoryToSend.size()); vInvWait.reserve(pto->vInventoryToSend.size()); - foreach(const CInv& inv, pto->vInventoryToSend) + BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend) { if (pto->setInventoryKnown.count(inv)) continue; @@ -3220,7 +3218,7 @@ public: void print() const { printf("COrphan(hash=%s, dPriority=%.1f)\n", ptx->GetHash().ToString().substr(0,10).c_str(), dPriority); - foreach(uint256 hash, setDependsOn) + BOOST_FOREACH(uint256 hash, setDependsOn) printf(" setDependsOn %s\n", hash.ToString().substr(0,10).c_str()); } }; @@ -3264,7 +3262,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey) COrphan* porphan = NULL; double dPriority = 0; - foreach(const CTxIn& txin, tx.vin) + BOOST_FOREACH(const CTxIn& txin, tx.vin) { // Read prev transaction CTransaction txPrev; @@ -3349,7 +3347,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey) uint256 hash = tx.GetHash(); if (mapDependers.count(hash)) { - foreach(COrphan* porphan, mapDependers[hash]) + BOOST_FOREACH(COrphan* porphan, mapDependers[hash]) { if (!porphan->setDependsOn.empty()) { @@ -3679,7 +3677,7 @@ bool SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, set< vCoins.push_back(&(*it).second); random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt); - foreach(CWalletTx* pcoin, vCoins) + BOOST_FOREACH(CWalletTx* pcoin, vCoins) { if (!pcoin->IsFinal() || !pcoin->IsConfirmed()) continue; @@ -3817,7 +3815,7 @@ bool SelectCoins(int64 nTargetValue, set >& setCoi bool CreateTransaction(const vector >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet) { int64 nValue = 0; - foreach (const PAIRTYPE(CScript, int64)& s, vecSend) + BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend) { if (nValue < 0) return false; @@ -3842,7 +3840,7 @@ bool CreateTransaction(const vector >& vecSend, CWalletTx& int64 nTotalValue = nValue + nFeeRet; double dPriority = 0; // vouts to the payees - foreach (const PAIRTYPE(CScript, int64)& s, vecSend) + BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend) wtxNew.vout.push_back(CTxOut(s.second, s.first)); // Choose coins to use @@ -3850,7 +3848,7 @@ bool CreateTransaction(const vector >& vecSend, CWalletTx& int64 nValueIn = 0; if (!SelectCoins(nTotalValue, setCoins, nValueIn)) return false; - foreach(PAIRTYPE(CWalletTx*, unsigned int) pcoin, setCoins) + BOOST_FOREACH(PAIRTYPE(CWalletTx*, unsigned int) pcoin, setCoins) { int64 nCredit = pcoin.first->vout[pcoin.second].nValue; dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain(); @@ -3886,12 +3884,12 @@ bool CreateTransaction(const vector >& vecSend, CWalletTx& reservekey.ReturnKey(); // Fill vin - foreach(const PAIRTYPE(CWalletTx*,unsigned int)& coin, setCoins) + BOOST_FOREACH(const PAIRTYPE(CWalletTx*,unsigned int)& coin, setCoins) wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second)); // Sign int nIn = 0; - foreach(const PAIRTYPE(CWalletTx*,unsigned int)& coin, setCoins) + BOOST_FOREACH(const PAIRTYPE(CWalletTx*,unsigned int)& coin, setCoins) if (!SignSignature(*coin.first, wtxNew, nIn++)) return false; @@ -3951,7 +3949,7 @@ bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey) // Mark old coins as spent set setCoins; - foreach(const CTxIn& txin, wtxNew.vin) + BOOST_FOREACH(const CTxIn& txin, wtxNew.vin) { CWalletTx &pcoin = mapWallet[txin.prevout.hash]; pcoin.MarkSpent(txin.prevout.n); diff --git a/src/main.h b/src/main.h index d1201e6..92b73fe 100644 --- a/src/main.h +++ b/src/main.h @@ -1,6 +1,16 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_MAIN_H +#define BITCOIN_MAIN_H + +#include "bignum.h" +#include "net.h" +#include "key.h" +#include "db.h" +#include "script.h" + +#include class COutPoint; class CInPoint; @@ -35,7 +45,7 @@ static const int fHaveUPnP = false; extern CCriticalSection cs_main; -extern map mapBlockIndex; +extern std::map mapBlockIndex; extern uint256 hashGenesisBlock; extern CBigNum bnProofOfWorkLimit; extern CBlockIndex* pindexGenesisBlock; @@ -45,11 +55,11 @@ extern CBigNum bnBestInvalidWork; extern uint256 hashBestChain; extern CBlockIndex* pindexBest; extern unsigned int nTransactionsUpdated; -extern map mapRequestCount; +extern std::map mapRequestCount; extern CCriticalSection cs_mapRequestCount; -extern map mapAddressBook; +extern std::map mapAddressBook; extern CCriticalSection cs_mapAddressBook; -extern vector vchDefaultKey; +extern std::vector vchDefaultKey; extern double dHashesPerSec; extern int64 nHPSTimerStart; @@ -73,7 +83,7 @@ bool CheckDiskSpace(uint64 nAdditionalBytes=0); FILE* OpenBlockFile(unsigned int nFile, unsigned int nBlockPos, const char* pszMode="rb"); FILE* AppendBlockFile(unsigned int& nFileRet); bool AddKey(const CKey& key); -vector GenerateNewKey(); +std::vector GenerateNewKey(); bool AddToWallet(const CWalletTx& wtxIn); void WalletUpdateSpent(const COutPoint& prevout); int ScanForWalletTransactions(CBlockIndex* pindexStart); @@ -81,15 +91,15 @@ void ReacceptWalletTransactions(); bool LoadBlockIndex(bool fAllowNew=true); void PrintBlockTree(); bool ProcessMessages(CNode* pfrom); -bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv); +bool ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vRecv); bool SendMessages(CNode* pto, bool fSendTrickle); int64 GetBalance(); -bool CreateTransaction(const vector >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet); +bool CreateTransaction(const std::vector >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet); bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet); bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey); bool BroadcastTransaction(CWalletTx& wtxNew); -string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); -string SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); +std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); +std::string SendMoneyToBitcoinAddress(std::string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); void GenerateBitcoins(bool fGenerate); void ThreadBitcoinMiner(void* parg); CBlock* CreateNewBlock(CReserveKey& reservekey); @@ -99,7 +109,7 @@ bool CheckWork(CBlock* pblock, CReserveKey& reservekey); void BitcoinMiner(); bool CheckProofOfWork(uint256 hash, unsigned int nBits); bool IsInitialBlockDownload(); -string GetWarnings(string strFor); +std::string GetWarnings(std::string strFor); @@ -147,7 +157,7 @@ public: return !(a == b); } - string ToString() const + std::string ToString() const { if (IsNull()) return strprintf("null"); @@ -206,7 +216,7 @@ public: return !(a == b); } - string ToString() const + std::string ToString() const { return strprintf("COutPoint(%s, %d)", hash.ToString().substr(0,10).c_str(), n); } @@ -275,9 +285,9 @@ public: return !(a == b); } - string ToString() const + std::string ToString() const { - string str; + std::string str; str += strprintf("CTxIn("); str += prevout.ToString(); if (prevout.IsNull()) @@ -353,14 +363,14 @@ public: int64 GetCredit() const { if (!MoneyRange(nValue)) - throw runtime_error("CTxOut::GetCredit() : value out of range"); + throw std::runtime_error("CTxOut::GetCredit() : value out of range"); return (IsMine() ? nValue : 0); } bool IsChange() const { // On a debit transaction, a txout that's mine but isn't in the address book is change - vector vchPubKey; + std::vector vchPubKey; if (ExtractPubKey(scriptPubKey, true, vchPubKey)) CRITICAL_BLOCK(cs_mapAddressBook) if (!mapAddressBook.count(PubKeyToAddress(vchPubKey))) @@ -371,7 +381,7 @@ public: int64 GetChange() const { if (!MoneyRange(nValue)) - throw runtime_error("CTxOut::GetChange() : value out of range"); + throw std::runtime_error("CTxOut::GetChange() : value out of range"); return (IsChange() ? nValue : 0); } @@ -386,7 +396,7 @@ public: return !(a == b); } - string ToString() const + std::string ToString() const { if (scriptPubKey.size() < 6) return "CTxOut(error)"; @@ -410,8 +420,8 @@ class CTransaction { public: int nVersion; - vector vin; - vector vout; + std::vector vin; + std::vector vout; unsigned int nLockTime; @@ -458,7 +468,7 @@ public: nBlockTime = GetAdjustedTime(); if ((int64)nLockTime < (nLockTime < 500000000 ? (int64)nBlockHeight : nBlockTime)) return true; - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) if (!txin.IsFinal()) return false; return true; @@ -501,19 +511,19 @@ public: int GetSigOpCount() const { int n = 0; - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) n += txin.scriptSig.GetSigOpCount(); - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) n += txout.scriptPubKey.GetSigOpCount(); return n; } bool IsStandard() const { - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) if (!txin.scriptSig.IsPushOnly()) return error("nonstandard txin: %s", txin.scriptSig.ToString().c_str()); - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) if (!::IsStandard(txout.scriptPubKey)) return error("nonstandard txout: %s", txout.scriptPubKey.ToString().c_str()); return true; @@ -521,7 +531,7 @@ public: bool IsMine() const { - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) if (txout.IsMine()) return true; return false; @@ -535,11 +545,11 @@ public: int64 GetDebit() const { int64 nDebit = 0; - foreach(const CTxIn& txin, vin) + BOOST_FOREACH(const CTxIn& txin, vin) { nDebit += txin.GetDebit(); if (!MoneyRange(nDebit)) - throw runtime_error("CTransaction::GetDebit() : value out of range"); + throw std::runtime_error("CTransaction::GetDebit() : value out of range"); } return nDebit; } @@ -547,11 +557,11 @@ public: int64 GetCredit() const { int64 nCredit = 0; - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) { nCredit += txout.GetCredit(); if (!MoneyRange(nCredit)) - throw runtime_error("CTransaction::GetCredit() : value out of range"); + throw std::runtime_error("CTransaction::GetCredit() : value out of range"); } return nCredit; } @@ -561,11 +571,11 @@ public: if (IsCoinBase()) return 0; int64 nChange = 0; - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) { nChange += txout.GetChange(); if (!MoneyRange(nChange)) - throw runtime_error("CTransaction::GetChange() : value out of range"); + throw std::runtime_error("CTransaction::GetChange() : value out of range"); } return nChange; } @@ -573,11 +583,11 @@ public: int64 GetValueOut() const { int64 nValueOut = 0; - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) { nValueOut += txout.nValue; if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut)) - throw runtime_error("CTransaction::GetValueOut() : value out of range"); + throw std::runtime_error("CTransaction::GetValueOut() : value out of range"); } return nValueOut; } @@ -615,7 +625,7 @@ public: // To limit dust spam, require MIN_TX_FEE if any output is less than 0.01 if (nMinFee < MIN_TX_FEE) - foreach(const CTxOut& txout, vout) + BOOST_FOREACH(const CTxOut& txout, vout) if (txout.nValue < CENT) nMinFee = MIN_TX_FEE; @@ -668,9 +678,9 @@ public: } - string ToString() const + std::string ToString() const { - string str; + std::string str; str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%d)\n", GetHash().ToString().substr(0,10).c_str(), nVersion, @@ -694,7 +704,7 @@ public: bool ReadFromDisk(CTxDB& txdb, COutPoint prevout); bool ReadFromDisk(COutPoint prevout); bool DisconnectInputs(CTxDB& txdb); - bool ConnectInputs(CTxDB& txdb, map& mapTestPool, CDiskTxPos posThisTx, + bool ConnectInputs(CTxDB& txdb, std::map& mapTestPool, CDiskTxPos posThisTx, CBlockIndex* pindexBlock, int64& nFees, bool fBlock, bool fMiner, int64 nMinFee=0); bool ClientConnectInputs(); bool CheckTransaction() const; @@ -721,7 +731,7 @@ class CMerkleTx : public CTransaction { public: uint256 hashBlock; - vector vMerkleBranch; + std::vector vMerkleBranch; int nIndex; // memory only @@ -776,14 +786,14 @@ public: class CWalletTx : public CMerkleTx { public: - vector vtxPrev; - map mapValue; - vector > vOrderForm; + std::vector vtxPrev; + std::map mapValue; + std::vector > vOrderForm; unsigned int fTimeReceivedIsTxTime; unsigned int nTimeReceived; // time received by this node char fFromMe; - string strFromAccount; - vector vfSpent; + std::string strFromAccount; + std::vector vfSpent; // memory only mutable char fDebitCached; @@ -850,8 +860,8 @@ public: { pthis->mapValue["fromaccount"] = pthis->strFromAccount; - string str; - foreach(char f, vfSpent) + std::string str; + BOOST_FOREACH(char f, vfSpent) { str += (f ? '1' : '0'); if (f) @@ -874,7 +884,7 @@ public: pthis->strFromAccount = pthis->mapValue["fromaccount"]; if (mapValue.count("spent")) - foreach(char c, pthis->mapValue["spent"]) + BOOST_FOREACH(char c, pthis->mapValue["spent"]) pthis->vfSpent.push_back(c != '0'); else pthis->vfSpent.assign(vout.size(), fSpent); @@ -887,7 +897,7 @@ public: // marks certain txout's as spent // returns true if any update took place - bool UpdateSpent(const vector& vfNewSpent) + bool UpdateSpent(const std::vector& vfNewSpent) { bool fReturn = false; for (int i=0; i < vfNewSpent.size(); i++) @@ -916,7 +926,7 @@ public: void MarkSpent(unsigned int nOut) { if (nOut >= vout.size()) - throw runtime_error("CWalletTx::MarkSpent() : nOut out of range"); + throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range"); vfSpent.resize(vout.size()); if (!vfSpent[nOut]) { @@ -928,7 +938,7 @@ public: bool IsSpent(unsigned int nOut) const { if (nOut >= vout.size()) - throw runtime_error("CWalletTx::IsSpent() : nOut out of range"); + throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range"); if (nOut >= vfSpent.size()) return false; return (!!vfSpent[nOut]); @@ -976,7 +986,7 @@ public: const CTxOut &txout = vout[i]; nCredit += txout.GetCredit(); if (!MoneyRange(nCredit)) - throw runtime_error("CWalletTx::GetAvailableCredit() : value out of range"); + throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range"); } } @@ -995,10 +1005,10 @@ public: return nChangeCached; } - void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list >& listReceived, - list >& listSent, int64& nFee, string& strSentAccount) const; + void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list >& listReceived, + std::list >& listSent, int64& nFee, std::string& strSentAccount) const; - void GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived, + void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived, int64& nSent, int64& nFee) const; bool IsFromMe() const @@ -1018,8 +1028,8 @@ public: // If no confirmations but it's from us, we can still // consider it confirmed if all dependencies are confirmed - map mapPrev; - vector vWorkQueue; + std::map mapPrev; + std::vector vWorkQueue; vWorkQueue.reserve(vtxPrev.size()+1); vWorkQueue.push_back(this); for (int i = 0; i < vWorkQueue.size(); i++) @@ -1034,10 +1044,10 @@ public: return false; if (mapPrev.empty()) - foreach(const CMerkleTx& tx, vtxPrev) + BOOST_FOREACH(const CMerkleTx& tx, vtxPrev) mapPrev[tx.GetHash()] = &tx; - foreach(const CTxIn& txin, ptx->vin) + BOOST_FOREACH(const CTxIn& txin, ptx->vin) { if (!mapPrev.count(txin.prevout.hash)) return false; @@ -1077,7 +1087,7 @@ class CTxIndex { public: CDiskTxPos pos; - vector vSpent; + std::vector vSpent; CTxIndex() { @@ -1149,10 +1159,10 @@ public: unsigned int nNonce; // network and disk - vector vtx; + std::vector vtx; // memory only - mutable vector vMerkleTree; + mutable std::vector vMerkleTree; CBlock() @@ -1207,7 +1217,7 @@ public: int GetSigOpCount() const { int n = 0; - foreach(const CTransaction& tx, vtx) + BOOST_FOREACH(const CTransaction& tx, vtx) n += tx.GetSigOpCount(); return n; } @@ -1216,14 +1226,14 @@ public: uint256 BuildMerkleTree() const { vMerkleTree.clear(); - foreach(const CTransaction& tx, vtx) + BOOST_FOREACH(const CTransaction& tx, vtx) vMerkleTree.push_back(tx.GetHash()); int j = 0; for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { for (int i = 0; i < nSize; i += 2) { - int i2 = min(i+1, nSize-1); + int i2 = std::min(i+1, nSize-1); vMerkleTree.push_back(Hash(BEGIN(vMerkleTree[j+i]), END(vMerkleTree[j+i]), BEGIN(vMerkleTree[j+i2]), END(vMerkleTree[j+i2]))); } @@ -1232,15 +1242,15 @@ public: return (vMerkleTree.empty() ? 0 : vMerkleTree.back()); } - vector GetMerkleBranch(int nIndex) const + std::vector GetMerkleBranch(int nIndex) const { if (vMerkleTree.empty()) BuildMerkleTree(); - vector vMerkleBranch; + std::vector vMerkleBranch; int j = 0; for (int nSize = vtx.size(); nSize > 1; nSize = (nSize + 1) / 2) { - int i = min(nIndex^1, nSize-1); + int i = std::min(nIndex^1, nSize-1); vMerkleBranch.push_back(vMerkleTree[j+i]); nIndex >>= 1; j += nSize; @@ -1248,11 +1258,11 @@ public: return vMerkleBranch; } - static uint256 CheckMerkleBranch(uint256 hash, const vector& vMerkleBranch, int nIndex) + static uint256 CheckMerkleBranch(uint256 hash, const std::vector& vMerkleBranch, int nIndex) { if (nIndex == -1) return 0; - foreach(const uint256& otherside, vMerkleBranch) + BOOST_FOREACH(const uint256& otherside, vMerkleBranch) { if (nIndex & 1) hash = Hash(BEGIN(otherside), END(otherside), BEGIN(hash), END(hash)); @@ -1483,7 +1493,7 @@ public: for (int i = 0; i < nMedianTimeSpan && pindex; i++, pindex = pindex->pprev) *(--pbegin) = pindex->GetBlockTime(); - sort(pbegin, pend); + std::sort(pbegin, pend); return pbegin[(pend - pbegin)/2]; } @@ -1501,7 +1511,7 @@ public: - string ToString() const + std::string ToString() const { return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nHeight=%d, merkle=%s, hashBlock=%s)", pprev, pnext, nFile, nBlockPos, nHeight, @@ -1570,9 +1580,9 @@ public: } - string ToString() const + std::string ToString() const { - string str = "CDiskBlockIndex("; + std::string str = "CDiskBlockIndex("; str += CBlockIndex::ToString(); str += strprintf("\n hashBlock=%s, hashPrev=%s, hashNext=%s)", GetBlockHash().ToString().c_str(), @@ -1602,7 +1612,7 @@ public: class CBlockLocator { protected: - vector vHave; + std::vector vHave; public: CBlockLocator() @@ -1616,7 +1626,7 @@ public: explicit CBlockLocator(uint256 hashBlock) { - map::iterator mi = mapBlockIndex.find(hashBlock); + std::map::iterator mi = mapBlockIndex.find(hashBlock); if (mi != mapBlockIndex.end()) Set((*mi).second); } @@ -1660,9 +1670,9 @@ public: // Retrace how far back it was in the sender's branch int nDistance = 0; int nStep = 1; - foreach(const uint256& hash, vHave) + BOOST_FOREACH(const uint256& hash, vHave) { - map::iterator mi = mapBlockIndex.find(hash); + std::map::iterator mi = mapBlockIndex.find(hash); if (mi != mapBlockIndex.end()) { CBlockIndex* pindex = (*mi).second; @@ -1679,9 +1689,9 @@ public: CBlockIndex* GetBlockIndex() { // Find the first block the caller has in the main chain - foreach(const uint256& hash, vHave) + BOOST_FOREACH(const uint256& hash, vHave) { - map::iterator mi = mapBlockIndex.find(hash); + std::map::iterator mi = mapBlockIndex.find(hash); if (mi != mapBlockIndex.end()) { CBlockIndex* pindex = (*mi).second; @@ -1695,9 +1705,9 @@ public: uint256 GetBlockHash() { // Find the first block the caller has in the main chain - foreach(const uint256& hash, vHave) + BOOST_FOREACH(const uint256& hash, vHave) { - map::iterator mi = mapBlockIndex.find(hash); + std::map::iterator mi = mapBlockIndex.find(hash); if (mi != mapBlockIndex.end()) { CBlockIndex* pindex = (*mi).second; @@ -1731,7 +1741,7 @@ public: CPrivKey vchPrivKey; int64 nTimeCreated; int64 nTimeExpires; - string strComment; + std::string strComment; //// todo: add something to note what created it (user, getnewaddress, change) //// maybe should have a map property map @@ -1764,7 +1774,7 @@ public: class CAccount { public: - vector vchPubKey; + std::vector vchPubKey; CAccount() { @@ -1793,11 +1803,11 @@ public: class CAccountingEntry { public: - string strAccount; + std::string strAccount; int64 nCreditDebit; int64 nTime; - string strOtherAccount; - string strComment; + std::string strOtherAccount; + std::string strComment; CAccountingEntry() { @@ -1848,16 +1858,16 @@ public: int64 nExpiration; int nID; int nCancel; - set setCancel; + std::set setCancel; int nMinVer; // lowest version inclusive int nMaxVer; // highest version inclusive - set setSubVer; // empty matches all + std::set setSubVer; // empty matches all int nPriority; // Actions - string strComment; - string strStatusBar; - string strReserved; + std::string strComment; + std::string strStatusBar; + std::string strReserved; IMPLEMENT_SERIALIZE ( @@ -1896,13 +1906,13 @@ public: strReserved.clear(); } - string ToString() const + std::string ToString() const { - string strSetCancel; - foreach(int n, setCancel) + std::string strSetCancel; + BOOST_FOREACH(int n, setCancel) strSetCancel += strprintf("%d ", n); - string strSetSubVer; - foreach(string str, setSubVer) + std::string strSetSubVer; + BOOST_FOREACH(std::string str, setSubVer) strSetSubVer += "\"" + str + "\" "; return strprintf( "CAlert(\n" @@ -1942,8 +1952,8 @@ public: class CAlert : public CUnsignedAlert { public: - vector vchMsg; - vector vchSig; + std::vector vchMsg; + std::vector vchSig; CAlert() { @@ -1985,7 +1995,7 @@ public: return (alert.nID <= nCancel || setCancel.count(alert.nID)); } - bool AppliesTo(int nVersion, string strSubVerIn) const + bool AppliesTo(int nVersion, std::string strSubVerIn) const { return (IsInEffect() && nMinVer <= nVersion && nVersion <= nMaxVer && @@ -2041,11 +2051,13 @@ public: -extern map mapTransactions; -extern map mapWallet; -extern vector vWalletUpdated; +extern std::map mapTransactions; +extern std::map mapWallet; +extern std::vector vWalletUpdated; extern CCriticalSection cs_mapWallet; -extern map, CPrivKey> mapKeys; -extern map > mapPubKeys; +extern std::map, CPrivKey> mapKeys; +extern std::map > mapPubKeys; extern CCriticalSection cs_mapKeys; extern CKey keyUser; + +#endif diff --git a/src/net.cpp b/src/net.cpp index 018afc4..1320781 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -11,6 +11,9 @@ #include #endif +using namespace std; +using namespace boost; + static const int MAX_OUTBOUND_CONNECTIONS = 8; void ThreadMessageHandler2(void* parg); @@ -330,7 +333,7 @@ void ThreadGetMyExternalIP(void* parg) CAddress addr(addrLocalHost); addr.nTime = GetAdjustedTime(); CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) pnode->PushAddress(addr); } } @@ -414,7 +417,7 @@ void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1) // call this in the destructor so it doesn't get called after it's deleted. CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) { CRITICAL_BLOCK(pnode->cs_mapRequests) { @@ -451,7 +454,7 @@ bool AnySubscribed(unsigned int nChannel) if (pnodeLocalHost->IsSubscribed(nChannel)) return true; CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode->IsSubscribed(nChannel)) return true; return false; @@ -473,7 +476,7 @@ void CNode::Subscribe(unsigned int nChannel, unsigned int nHops) { // Relay subscribe CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode != this) pnode->PushMessage("subscribe", nChannel, nHops); } @@ -495,7 +498,7 @@ void CNode::CancelSubscribe(unsigned int nChannel) { // Relay subscription cancel CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode != this) pnode->PushMessage("sub-cancel", nChannel); } @@ -513,7 +516,7 @@ CNode* FindNode(unsigned int ip) { CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode->addr.ip == ip) return (pnode); } @@ -524,7 +527,7 @@ CNode* FindNode(CAddress addr) { CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode->addr == addr) return (pnode); } @@ -661,7 +664,7 @@ void ThreadSocketHandler2(void* parg) { // Disconnect unused nodes vector vNodesCopy = vNodes; - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) { if (pnode->fDisconnect || (pnode->GetRefCount() <= 0 && pnode->vRecv.empty() && pnode->vSend.empty())) @@ -683,7 +686,7 @@ void ThreadSocketHandler2(void* parg) // Delete disconnected nodes list vNodesDisconnectedCopy = vNodesDisconnected; - foreach(CNode* pnode, vNodesDisconnectedCopy) + BOOST_FOREACH(CNode* pnode, vNodesDisconnectedCopy) { // wait until threads are done using it if (pnode->GetRefCount() <= 0) @@ -729,7 +732,7 @@ void ThreadSocketHandler2(void* parg) hSocketMax = max(hSocketMax, hListenSocket); CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) { if (pnode->hSocket == INVALID_SOCKET || pnode->hSocket < 0) continue; @@ -771,7 +774,7 @@ void ThreadSocketHandler2(void* parg) int nInbound = 0; CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode->fInbound) nInbound++; if (hSocket == INVALID_SOCKET) @@ -801,10 +804,10 @@ void ThreadSocketHandler2(void* parg) CRITICAL_BLOCK(cs_vNodes) { vNodesCopy = vNodes; - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) pnode->AddRef(); } - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) { if (fShutdown) return; @@ -921,7 +924,7 @@ void ThreadSocketHandler2(void* parg) } CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) pnode->Release(); } @@ -1057,7 +1060,7 @@ void DNSAddressSeed() vector vaddr; if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, true)) { - foreach (CAddress& addr, vaddr) + BOOST_FOREACH (CAddress& addr, vaddr) { if (addr.GetByte(3) != 127) { @@ -1148,7 +1151,7 @@ void ThreadOpenConnections2(void* parg) { for (int64 nLoop = 0;; nLoop++) { - foreach(string strAddr, mapMultiArgs["-connect"]) + BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"]) { CAddress addr(strAddr, fAllowDNS); if (addr.IsValid()) @@ -1166,7 +1169,7 @@ void ThreadOpenConnections2(void* parg) // Connect to manually added nodes first if (mapArgs.count("-addnode")) { - foreach(string strAddr, mapMultiArgs["-addnode"]) + BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"]) { CAddress addr(strAddr, fAllowDNS); if (addr.IsValid()) @@ -1190,7 +1193,7 @@ void ThreadOpenConnections2(void* parg) { int nOutbound = 0; CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (!pnode->fInbound) nOutbound++; int nMaxOutboundConnections = MAX_OUTBOUND_CONNECTIONS; @@ -1233,7 +1236,7 @@ void ThreadOpenConnections2(void* parg) { nSeedDisconnected = GetTime(); CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (setSeed.count(pnode->addr.ip)) pnode->fDisconnect = true; } @@ -1241,7 +1244,7 @@ void ThreadOpenConnections2(void* parg) // Keep setting timestamps to 0 so they won't reconnect if (GetTime() - nSeedDisconnected < 60 * 60) { - foreach(PAIRTYPE(const vector, CAddress)& item, mapAddresses) + BOOST_FOREACH(PAIRTYPE(const vector, CAddress)& item, mapAddresses) { if (setSeed.count(item.second.ip) && item.second.nTime != 0) { @@ -1264,12 +1267,12 @@ void ThreadOpenConnections2(void* parg) // Do this here so we don't have to critsect vNodes inside mapAddresses critsect. set setConnected; CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) setConnected.insert(pnode->addr.ip & 0x0000ffff); CRITICAL_BLOCK(cs_mapAddresses) { - foreach(const PAIRTYPE(vector, CAddress)& item, mapAddresses) + BOOST_FOREACH(const PAIRTYPE(vector, CAddress)& item, mapAddresses) { const CAddress& addr = item.second; if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.ip & 0x0000ffff)) @@ -1385,7 +1388,7 @@ void ThreadMessageHandler2(void* parg) CRITICAL_BLOCK(cs_vNodes) { vNodesCopy = vNodes; - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) pnode->AddRef(); } @@ -1393,7 +1396,7 @@ void ThreadMessageHandler2(void* parg) CNode* pnodeTrickle = NULL; if (!vNodesCopy.empty()) pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())]; - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) { // Receive messages TRY_CRITICAL_BLOCK(pnode->cs_vRecv) @@ -1410,7 +1413,7 @@ void ThreadMessageHandler2(void* parg) CRITICAL_BLOCK(cs_vNodes) { - foreach(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) pnode->Release(); } @@ -1527,7 +1530,7 @@ void StartNode(void* parg) { vector vaddr; if (Lookup(pszHostName, vaddr, nLocalServices, -1, true)) - foreach (const CAddress &addr, vaddr) + BOOST_FOREACH (const CAddress &addr, vaddr) if (addr.GetByte(3) != 127) { addrLocalHost = addr; @@ -1648,7 +1651,7 @@ public: ~CNetCleanup() { // Close sockets - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode->hSocket != INVALID_SOCKET) closesocket(pnode->hSocket); if (hListenSocket != INVALID_SOCKET) diff --git a/src/net.h b/src/net.h index ea12b98..6bbcd64 100644 --- a/src/net.h +++ b/src/net.h @@ -1,6 +1,16 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_NET_H +#define BITCOIN_NET_H + +#include +#include +#include + +#ifndef __WXMSW__ +#include +#endif class CMessageHeader; class CAddress; @@ -23,7 +33,7 @@ enum bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet); -bool Lookup(const char *pszName, vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); +bool Lookup(const char *pszName, std::vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); bool GetMyExternalIP(unsigned int& ipRet); bool AddAddress(CAddress addr, int64 nTimePenalty=0); @@ -34,7 +44,7 @@ void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1); bool AnySubscribed(unsigned int nChannel); void MapPort(bool fMapPort); void DNSAddressSeed(); -bool BindListenPort(string& strError=REF(string())); +bool BindListenPort(std::string& strError=REF(std::string())); void StartNode(void* parg); bool StopNode(); @@ -89,12 +99,12 @@ public: READWRITE(nChecksum); ) - string GetCommand() + std::string GetCommand() { if (pchCommand[COMMAND_SIZE-1] == 0) - return string(pchCommand, pchCommand + strlen(pchCommand)); + return std::string(pchCommand, pchCommand + strlen(pchCommand)); else - return string(pchCommand, pchCommand + COMMAND_SIZE); + return std::string(pchCommand, pchCommand + COMMAND_SIZE); } bool IsValid() @@ -182,13 +192,13 @@ public: Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true); } - explicit CAddress(string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) + explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { Init(); Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn); } - explicit CAddress(string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) + explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { Init(); Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true); @@ -245,16 +255,16 @@ public: return false; } - vector GetKey() const + std::vector GetKey() const { CDataStream ss; ss.reserve(18); ss << FLATDATA(pchReserved) << ip << port; #if defined(_MSC_VER) && _MSC_VER < 1300 - return vector((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]); + return std::vector((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]); #else - return vector(ss.begin(), ss.end()); + return std::vector(ss.begin(), ss.end()); #endif } @@ -301,22 +311,22 @@ public: return ((unsigned char*)&ip)[3-n]; } - string ToStringIPPort() const + std::string ToStringIPPort() const { return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); } - string ToStringIP() const + std::string ToStringIP() const { return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0)); } - string ToStringPort() const + std::string ToStringPort() const { return strprintf("%u", ntohs(port)); } - string ToString() const + std::string ToString() const { return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); } @@ -364,7 +374,7 @@ public: hash = hashIn; } - CInv(const string& strType, const uint256& hashIn) + CInv(const std::string& strType, const uint256& hashIn) { int i; for (i = 1; i < ARRAYLEN(ppszTypeName); i++) @@ -403,7 +413,7 @@ public: return ppszTypeName[type]; } - string ToString() const + std::string ToString() const { return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str()); } @@ -446,17 +456,17 @@ extern uint64 nLocalServices; extern CAddress addrLocalHost; extern CNode* pnodeLocalHost; extern uint64 nLocalHostNonce; -extern array vnThreadsRunning; +extern boost::array vnThreadsRunning; extern SOCKET hListenSocket; -extern vector vNodes; +extern std::vector vNodes; extern CCriticalSection cs_vNodes; -extern map, CAddress> mapAddresses; +extern std::map, CAddress> mapAddresses; extern CCriticalSection cs_mapAddresses; -extern map mapRelay; -extern deque > vRelayExpiration; +extern std::map mapRelay; +extern std::deque > vRelayExpiration; extern CCriticalSection cs_mapRelay; -extern map mapAlreadyAskedFor; +extern std::map mapAlreadyAskedFor; // Settings extern int fUseProxy; @@ -485,7 +495,7 @@ public: unsigned int nMessageStart; CAddress addr; int nVersion; - string strSubVer; + std::string strSubVer; bool fClient; bool fInbound; bool fNetworkNode; @@ -495,7 +505,7 @@ protected: int nRefCount; public: int64 nReleaseTime; - map mapRequests; + std::map mapRequests; CCriticalSection cs_mapRequests; uint256 hashContinue; CBlockIndex* pindexLastGetBlocksBegin; @@ -503,19 +513,19 @@ public: int nStartingHeight; // flood relay - vector vAddrToSend; - set setAddrKnown; + std::vector vAddrToSend; + std::set setAddrKnown; bool fGetAddr; - set setKnown; + std::set setKnown; // inventory based relay - set setInventoryKnown; - vector vInventoryToSend; + std::set setInventoryKnown; + std::vector vInventoryToSend; CCriticalSection cs_inventory; - multimap mapAskFor; + std::multimap mapAskFor; // publish and subscription - vector vfSubscribe; + std::vector vfSubscribe; CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false) @@ -577,13 +587,13 @@ public: int GetRefCount() { - return max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0); + return std::max(nRefCount, 0) + (GetTime() < nReleaseTime ? 1 : 0); } CNode* AddRef(int64 nTimeout=0) { if (nTimeout != 0) - nReleaseTime = max(nReleaseTime, GetTime() + nTimeout); + nReleaseTime = std::max(nReleaseTime, GetTime() + nTimeout); else nRefCount++; return this; @@ -634,11 +644,11 @@ public: // Make sure not to reuse time indexes to keep things in the same order int64 nNow = (GetTime() - 1) * 1000000; static int64 nLastTime; - nLastTime = nNow = max(nNow, ++nLastTime); + nLastTime = nNow = std::max(nNow, ++nLastTime); // Each retry is 2 minutes after the last - nRequestTime = max(nRequestTime + 2 * 60 * 1000000, nNow); - mapAskFor.insert(make_pair(nRequestTime, inv)); + nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow); + mapAskFor.insert(std::make_pair(nRequestTime, inv)); } @@ -722,7 +732,7 @@ public: CAddress addrMe = (fUseProxy ? CAddress("0.0.0.0") : addrLocalHost); RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); PushMessage("version", VERSION, nLocalServices, nTime, addrYou, addrMe, - nLocalHostNonce, string(pszSubVer), nBestHeight); + nLocalHostNonce, std::string(pszSubVer), nBestHeight); } @@ -948,7 +958,7 @@ inline void RelayInventory(const CInv& inv) { // Put on lists to offer to the other nodes CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) pnode->PushInventory(inv); } @@ -975,7 +985,7 @@ inline void RelayMessage<>(const CInv& inv, const CDataStream& ss) // Save original serialized message so newer versions are preserved mapRelay[inv] = ss; - vRelayExpiration.push_back(make_pair(GetTime() + 15 * 60, inv)); + vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv)); } RelayInventory(inv); @@ -1007,7 +1017,7 @@ void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, // Relay CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) pnode->PushMessage("publish", nChannel, nHops, obj); } @@ -1018,7 +1028,7 @@ void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, uint256 hash = obj.GetHash(); CRITICAL_BLOCK(cs_vNodes) - foreach(CNode* pnode, vNodes) + BOOST_FOREACH(CNode* pnode, vNodes) if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) pnode->PushMessage("pub-cancel", nChannel, nHops, hash); @@ -1035,3 +1045,5 @@ void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, if (obj.setSources.empty()) AdvertStopPublish(pfrom, nChannel, nHops, obj); } + +#endif diff --git a/src/noui.h b/src/noui.h index d108184..afb1952 100644 --- a/src/noui.h +++ b/src/noui.h @@ -1,7 +1,10 @@ // Copyright (c) 2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_NOUI_H +#define BITCOIN_NOUI_H +#include typedef void wxWindow; #define wxYES 0x00000002 @@ -31,7 +34,7 @@ typedef void wxWindow; #define wxMORE 0x00010000 #define wxSETUP 0x00020000 -inline int MyMessageBox(const string& message, const string& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1) +inline int MyMessageBox(const std::string& message, const std::string& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1) { printf("%s: %s\n", caption.c_str(), message.c_str()); fprintf(stderr, "%s: %s\n", caption.c_str(), message.c_str()); @@ -39,17 +42,17 @@ inline int MyMessageBox(const string& message, const string& caption="Message", } #define wxMessageBox MyMessageBox -inline int ThreadSafeMessageBox(const string& message, const string& caption, int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1) +inline int ThreadSafeMessageBox(const std::string& message, const std::string& caption, int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1) { return MyMessageBox(message, caption, style, parent, x, y); } -inline bool ThreadSafeAskFee(int64 nFeeRequired, const string& strCaption, wxWindow* parent) +inline bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent) { return true; } -inline void CalledSetStatusBar(const string& strText, int nField) +inline void CalledSetStatusBar(const std::string& strText, int nField) { } @@ -60,3 +63,5 @@ inline void UIThreadCall(boost::function0 fn) inline void MainFrameRepaint() { } + +#endif diff --git a/src/rpc.cpp b/src/rpc.cpp index df5f123..9efcbbb 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -21,6 +21,8 @@ typedef boost::asio::ssl::stream SSLStream; // a certain size around 145MB. If we need access to json_spirit outside this // file, we could use the compiled json_spirit option. +using namespace std; +using namespace boost; using namespace boost::asio; using namespace json_spirit; @@ -81,7 +83,7 @@ void WalletTxToJSON(const CWalletTx& wtx, Object& entry) entry.push_back(Pair("confirmations", wtx.GetDepthInMainChain())); entry.push_back(Pair("txid", wtx.GetHash().GetHex())); entry.push_back(Pair("time", (boost::int64_t)wtx.GetTxTime())); - foreach(const PAIRTYPE(string,string)& item, wtx.mapValue) + BOOST_FOREACH(const PAIRTYPE(string,string)& item, wtx.mapValue) entry.push_back(Pair(item.first, item.second)); } @@ -336,7 +338,7 @@ string GetAccountAddress(string strAccount, bool bForceNew=false) ++it) { const CWalletTx& wtx = (*it).second; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.scriptPubKey == scriptPubKey) account.vchPubKey.clear(); } @@ -449,7 +451,7 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) Array ret; CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) { const string& strAddress = item.first; const string& strName = item.second; @@ -541,7 +543,7 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) if (wtx.IsCoinBase() || !wtx.IsFinal()) continue; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.scriptPubKey == scriptPubKey) if (wtx.GetDepthInMainChain() >= nMinDepth) nAmount += txout.nValue; @@ -556,7 +558,7 @@ void GetAccountPubKeys(string strAccount, set& setPubKey) { CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) { const string& strAddress = item.first; const string& strName = item.second; @@ -600,7 +602,7 @@ Value getreceivedbyaccount(const Array& params, bool fHelp) if (wtx.IsCoinBase() || !wtx.IsFinal()) continue; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (setPubKey.count(txout.scriptPubKey)) if (wtx.GetDepthInMainChain() >= nMinDepth) nAmount += txout.nValue; @@ -678,9 +680,9 @@ Value getbalance(const Array& params, bool fHelp) list > listSent; wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount); if (wtx.GetDepthInMainChain() >= nMinDepth) - foreach(const PAIRTYPE(string,int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) nBalance += r.second; - foreach(const PAIRTYPE(string,int64)& r, listSent) + BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listSent) nBalance -= r.second; nBalance -= allFee; nBalance += allGeneratedMature; @@ -804,7 +806,7 @@ Value sendmany(const Array& params, bool fHelp) vector > vecSend; int64 totalAmount = 0; - foreach(const Pair& s, sendTo) + BOOST_FOREACH(const Pair& s, sendTo) { uint160 hash160; string strAddress = s.name_; @@ -885,7 +887,7 @@ Value ListReceived(const Array& params, bool fByAccounts) if (nDepth < nMinDepth) continue; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { // Only counting our own bitcoin addresses and not ip addresses uint160 hash160 = txout.scriptPubKey.GetBitcoinAddressHash160(); @@ -904,7 +906,7 @@ Value ListReceived(const Array& params, bool fByAccounts) map mapAccountTally; CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) { const string& strAddress = item.first; const string& strAccount = item.second; @@ -1024,7 +1026,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe // Sent if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount)) { - foreach(const PAIRTYPE(string, int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) { Object entry; entry.push_back(Pair("account", strSentAccount)); @@ -1042,7 +1044,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string, int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) { string account; if (mapAddressBook.count(r.first)) @@ -1114,7 +1116,7 @@ Value listtransactions(const Array& params, bool fHelp) } list acentries; walletdb.ListAccountCreditDebit(strAccount, acentries); - foreach(CAccountingEntry& entry, acentries) + BOOST_FOREACH(CAccountingEntry& entry, acentries) { txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry))); } @@ -1162,7 +1164,7 @@ Value listaccounts(const Array& params, bool fHelp) CRITICAL_BLOCK(cs_mapWallet) CRITICAL_BLOCK(cs_mapAddressBook) { - foreach(const PAIRTYPE(string, string)& entry, mapAddressBook) { + BOOST_FOREACH(const PAIRTYPE(string, string)& entry, mapAddressBook) { uint160 hash160; if(AddressToHash160(entry.first, hash160) && mapPubKeys.count(hash160)) // This address belongs to me mapAccountBalances[entry.second] = 0; @@ -1177,12 +1179,12 @@ Value listaccounts(const Array& params, bool fHelp) list > listSent; wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); mapAccountBalances[strSentAccount] -= nFee; - foreach(const PAIRTYPE(string, int64)& s, listSent) + BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) mapAccountBalances[strSentAccount] -= s.second; if (wtx.GetDepthInMainChain() >= nMinDepth) { mapAccountBalances[""] += nGeneratedMature; - foreach(const PAIRTYPE(string, int64)& r, listReceived) + BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) if (mapAddressBook.count(r.first)) mapAccountBalances[mapAddressBook[r.first]] += r.second; else @@ -1193,11 +1195,11 @@ Value listaccounts(const Array& params, bool fHelp) list acentries; CWalletDB().ListAccountCreditDebit("*", acentries); - foreach(const CAccountingEntry& entry, acentries) + BOOST_FOREACH(const CAccountingEntry& entry, acentries) mapAccountBalances[entry.strAccount] += entry.nCreditDebit; Object ret; - foreach(const PAIRTYPE(string, int64)& accountBalance, mapAccountBalances) { + BOOST_FOREACH(const PAIRTYPE(string, int64)& accountBalance, mapAccountBalances) { ret.push_back(Pair(accountBalance.first, ValueFromAmount(accountBalance.second))); } return ret; @@ -1320,7 +1322,7 @@ Value getwork(const Array& params, bool fHelp) { // Deallocate old blocks since they're obsolete now mapNewBlock.clear(); - foreach(CBlock* pblock, vNewBlock) + BOOST_FOREACH(CBlock* pblock, vNewBlock) delete pblock; vNewBlock.clear(); } @@ -1490,7 +1492,7 @@ string HTTPPost(const string& strMsg, const map& mapRequestHeader << "Content-Type: application/json\r\n" << "Content-Length: " << strMsg.size() << "\r\n" << "Accept: application/json\r\n"; - foreach(const PAIRTYPE(string, string)& item, mapRequestHeaders) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapRequestHeaders) s << item.first << ": " << item.second << "\r\n"; s << "\r\n" << strMsg; @@ -1710,7 +1712,7 @@ bool ClientAllowed(const string& strAddress) if (strAddress == asio::ip::address_v4::loopback().to_string()) return true; const vector& vAllow = mapMultiArgs["-rpcallowip"]; - foreach(string strAllow, vAllow) + BOOST_FOREACH(string strAllow, vAllow) if (WildcardMatch(strAddress, strAllow)) return true; return false; diff --git a/src/script.cpp b/src/script.cpp index a85c371..97334ca 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1,9 +1,11 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. - #include "headers.h" +using namespace std; +using namespace boost; + bool CheckSig(vector vchSig, vector vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType); @@ -974,7 +976,7 @@ bool Solver(const CScript& scriptPubKey, vector >& vSo // Scan templates const CScript& script1 = scriptPubKey; - foreach(const CScript& script2, vTemplates) + BOOST_FOREACH(const CScript& script2, vTemplates) { vSolutionRet.clear(); opcodetype opcode1, opcode2; @@ -1030,7 +1032,7 @@ bool Solver(const CScript& scriptPubKey, uint256 hash, int nHashType, CScript& s // Compile solution CRITICAL_BLOCK(cs_mapKeys) { - foreach(PAIRTYPE(opcodetype, valtype)& item, vSolution) + BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) { if (item.first == OP_PUBKEY) { @@ -1100,7 +1102,7 @@ bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector +#include class CTransaction; @@ -310,7 +317,7 @@ inline const char* GetOpName(opcodetype opcode) -inline string ValueString(const vector& vch) +inline std::string ValueString(const std::vector& vch) { if (vch.size() <= 4) return strprintf("%d", CBigNum(vch).getint()); @@ -318,10 +325,10 @@ inline string ValueString(const vector& vch) return HexStr(vch); } -inline string StackString(const vector >& vStack) +inline std::string StackString(const std::vector >& vStack) { - string str; - foreach(const vector& vch, vStack) + std::string str; + BOOST_FOREACH(const std::vector& vch, vStack) { if (!str.empty()) str += " "; @@ -338,7 +345,7 @@ inline string StackString(const vector >& vStack) -class CScript : public vector +class CScript : public std::vector { protected: CScript& push_int64(int64 n) @@ -371,10 +378,10 @@ protected: public: CScript() { } - CScript(const CScript& b) : vector(b.begin(), b.end()) { } - CScript(const_iterator pbegin, const_iterator pend) : vector(pbegin, pend) { } + CScript(const CScript& b) : std::vector(b.begin(), b.end()) { } + CScript(const_iterator pbegin, const_iterator pend) : std::vector(pbegin, pend) { } #ifndef _MSC_VER - CScript(const unsigned char* pbegin, const unsigned char* pend) : vector(pbegin, pend) { } + CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector(pbegin, pend) { } #endif CScript& operator+=(const CScript& b) @@ -405,7 +412,7 @@ public: explicit CScript(opcodetype b) { operator<<(b); } explicit CScript(const uint256& b) { operator<<(b); } explicit CScript(const CBigNum& b) { operator<<(b); } - explicit CScript(const vector& b) { operator<<(b); } + explicit CScript(const std::vector& b) { operator<<(b); } CScript& operator<<(char b) { return push_int64(b); } @@ -422,7 +429,7 @@ public: CScript& operator<<(opcodetype opcode) { if (opcode < 0 || opcode > 0xff) - throw runtime_error("CScript::operator<<() : invalid opcode"); + throw std::runtime_error("CScript::operator<<() : invalid opcode"); insert(end(), (unsigned char)opcode); return *this; } @@ -447,7 +454,7 @@ public: return *this; } - CScript& operator<<(const vector& b) + CScript& operator<<(const std::vector& b) { if (b.size() < OP_PUSHDATA1) { @@ -483,7 +490,7 @@ public: } - bool GetOp(iterator& pc, opcodetype& opcodeRet, vector& vchRet) + bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector& vchRet) { // Wrapper so it can be called with either iterator or const_iterator const_iterator pc2 = pc; @@ -500,7 +507,7 @@ public: return fRet; } - bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector& vchRet) const + bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector& vchRet) const { return GetOp2(pc, opcodeRet, &vchRet); } @@ -510,7 +517,7 @@ public: return GetOp2(pc, opcodeRet, NULL); } - bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, vector* pvchRet) const + bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector* pvchRet) const { opcodeRet = OP_INVALIDOPCODE; if (pvchRet) @@ -617,7 +624,7 @@ public: uint160 GetBitcoinAddressHash160() const { opcodetype opcode; - vector vch; + std::vector vch; CScript::const_iterator pc = begin(); if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0; if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0; @@ -629,7 +636,7 @@ public: return hash160; } - string GetBitcoinAddress() const + std::string GetBitcoinAddress() const { uint160 hash160 = GetBitcoinAddressHash160(); if (hash160 == 0) @@ -643,12 +650,12 @@ public: *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; } - void SetBitcoinAddress(const vector& vchPubKey) + void SetBitcoinAddress(const std::vector& vchPubKey) { SetBitcoinAddress(Hash160(vchPubKey)); } - bool SetBitcoinAddress(const string& strAddress) + bool SetBitcoinAddress(const std::string& strAddress) { this->clear(); uint160 hash160; @@ -664,11 +671,11 @@ public: printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str()); } - string ToString() const + std::string ToString() const { - string str; + std::string str; opcodetype opcode; - vector vch; + std::vector vch; const_iterator pc = begin(); while (pc < end()) { @@ -703,7 +710,9 @@ public: uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType); bool IsStandard(const CScript& scriptPubKey); bool IsMine(const CScript& scriptPubKey); -bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector& vchPubKeyRet); +bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, std::vector& vchPubKeyRet); bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret); bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript()); bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0); + +#endif diff --git a/src/serialize.h b/src/serialize.h index ee39c07..8e7677a 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1,15 +1,23 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_SERIALIZE_H +#define BITCOIN_SERIALIZE_H #include #include #include #include +#include +#include +#include +#include + #include #include #include #include + #if defined(_MSC_VER) || defined(__BORLANDC__) typedef __int64 int64; typedef unsigned __int64 uint64; @@ -277,11 +285,11 @@ template class CFixedFieldString { protected: - const string* pcstr; - string* pstr; + const std::string* pcstr; + std::string* pstr; public: - explicit CFixedFieldString(const string& str) : pcstr(&str), pstr(NULL) { } - explicit CFixedFieldString(string& str) : pcstr(&str), pstr(&str) { } + explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { } + explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { } unsigned int GetSerializeSize(int, int=0) const { @@ -317,9 +325,9 @@ public: // // string -template unsigned int GetSerializeSize(const basic_string& str, int, int=0); -template void Serialize(Stream& os, const basic_string& str, int, int=0); -template void Unserialize(Stream& is, basic_string& str, int, int=0); +template unsigned int GetSerializeSize(const std::basic_string& str, int, int=0); +template void Serialize(Stream& os, const std::basic_string& str, int, int=0); +template void Unserialize(Stream& is, std::basic_string& str, int, int=0); // vector template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::true_type&); @@ -398,13 +406,13 @@ inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION) // string // template -unsigned int GetSerializeSize(const basic_string& str, int, int) +unsigned int GetSerializeSize(const std::basic_string& str, int, int) { return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]); } template -void Serialize(Stream& os, const basic_string& str, int, int) +void Serialize(Stream& os, const std::basic_string& str, int, int) { WriteCompactSize(os, str.size()); if (!str.empty()) @@ -412,7 +420,7 @@ void Serialize(Stream& os, const basic_string& str, int, int) } template -void Unserialize(Stream& is, basic_string& str, int, int) +void Unserialize(Stream& is, std::basic_string& str, int, int) { unsigned int nSize = ReadCompactSize(is); str.resize(nSize); @@ -483,7 +491,7 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, unsigned int i = 0; while (i < nSize) { - unsigned int blk = min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); + unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); v.resize(i + blk); is.read((char*)&v[i], blk * sizeof(T)); i += blk; @@ -526,19 +534,19 @@ inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersio // inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion) { - return GetSerializeSize((const vector&)v, nType, nVersion); + return GetSerializeSize((const std::vector&)v, nType, nVersion); } template void Serialize(Stream& os, const CScript& v, int nType, int nVersion) { - Serialize(os, (const vector&)v, nType, nVersion); + Serialize(os, (const std::vector&)v, nType, nVersion); } template void Unserialize(Stream& is, CScript& v, int nType, int nVersion) { - Unserialize(is, (vector&)v, nType, nVersion); + Unserialize(is, (std::vector&)v, nType, nVersion); } @@ -575,26 +583,26 @@ template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(get<0>(item), nType, nVersion); - nSize += GetSerializeSize(get<1>(item), nType, nVersion); - nSize += GetSerializeSize(get<2>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); return nSize; } template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) { - Serialize(os, get<0>(item), nType, nVersion); - Serialize(os, get<1>(item), nType, nVersion); - Serialize(os, get<2>(item), nType, nVersion); + Serialize(os, boost::get<0>(item), nType, nVersion); + Serialize(os, boost::get<1>(item), nType, nVersion); + Serialize(os, boost::get<2>(item), nType, nVersion); } template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) { - Unserialize(is, get<0>(item), nType, nVersion); - Unserialize(is, get<1>(item), nType, nVersion); - Unserialize(is, get<2>(item), nType, nVersion); + Unserialize(is, boost::get<0>(item), nType, nVersion); + Unserialize(is, boost::get<1>(item), nType, nVersion); + Unserialize(is, boost::get<2>(item), nType, nVersion); } @@ -606,29 +614,29 @@ template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(get<0>(item), nType, nVersion); - nSize += GetSerializeSize(get<1>(item), nType, nVersion); - nSize += GetSerializeSize(get<2>(item), nType, nVersion); - nSize += GetSerializeSize(get<3>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion); return nSize; } template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) { - Serialize(os, get<0>(item), nType, nVersion); - Serialize(os, get<1>(item), nType, nVersion); - Serialize(os, get<2>(item), nType, nVersion); - Serialize(os, get<3>(item), nType, nVersion); + Serialize(os, boost::get<0>(item), nType, nVersion); + Serialize(os, boost::get<1>(item), nType, nVersion); + Serialize(os, boost::get<2>(item), nType, nVersion); + Serialize(os, boost::get<3>(item), nType, nVersion); } template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) { - Unserialize(is, get<0>(item), nType, nVersion); - Unserialize(is, get<1>(item), nType, nVersion); - Unserialize(is, get<2>(item), nType, nVersion); - Unserialize(is, get<3>(item), nType, nVersion); + Unserialize(is, boost::get<0>(item), nType, nVersion); + Unserialize(is, boost::get<1>(item), nType, nVersion); + Unserialize(is, boost::get<2>(item), nType, nVersion); + Unserialize(is, boost::get<3>(item), nType, nVersion); } @@ -661,7 +669,7 @@ void Unserialize(Stream& is, std::map& m, int nType, int nVersion typename std::map::iterator mi = m.begin(); for (unsigned int i = 0; i < nSize; i++) { - pair item; + std::pair item; Unserialize(is, item, nType, nVersion); mi = m.insert(mi, item); } @@ -773,7 +781,7 @@ struct secure_allocator : public std::allocator { if (p != NULL) memset(p, 0, sizeof(T) * n); - allocator::deallocate(p, n); + std::allocator::deallocate(p, n); } }; @@ -787,7 +795,7 @@ struct secure_allocator : public std::allocator class CDataStream { protected: - typedef vector > vector_type; + typedef std::vector > vector_type; vector_type vch; unsigned int nReadPos; short state; @@ -828,12 +836,12 @@ public: Init(nTypeIn, nVersionIn); } - CDataStream(const vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) { Init(nTypeIn, nVersionIn); } - CDataStream(const vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) { Init(nTypeIn, nVersionIn); } @@ -844,7 +852,7 @@ public: nType = nTypeIn; nVersion = nVersionIn; state = 0; - exceptmask = ios::badbit | ios::failbit; + exceptmask = std::ios::badbit | std::ios::failbit; } CDataStream& operator+=(const CDataStream& b) @@ -860,9 +868,9 @@ public: return (ret); } - string str() const + std::string str() const { - return (string(begin(), end())); + return (std::string(begin(), end())); } @@ -895,7 +903,7 @@ public: vch.insert(it, first, last); } - void insert(iterator it, vector::const_iterator first, vector::const_iterator last) + void insert(iterator it, std::vector::const_iterator first, std::vector::const_iterator last) { if (it == vch.begin() + nReadPos && last - first <= nReadPos) { @@ -985,7 +993,7 @@ public: } bool eof() const { return size() == 0; } - bool fail() const { return state & (ios::badbit | ios::failbit); } + bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } bool good() const { return !eof() && (state == 0); } void clear(short n) { state = n; } // name conflict with vector clear() short exceptions() { return exceptmask; } @@ -1009,7 +1017,7 @@ public: { if (nReadPosNext > vch.size()) { - setstate(ios::failbit, "CDataStream::read() : end of data"); + setstate(std::ios::failbit, "CDataStream::read() : end of data"); memset(pch, 0, nSize); nSize = vch.size() - nReadPos; } @@ -1032,7 +1040,7 @@ public: { if (nReadPosNext > vch.size()) { - setstate(ios::failbit, "CDataStream::ignore() : end of data"); + setstate(std::ios::failbit, "CDataStream::ignore() : end of data"); nSize = vch.size() - nReadPos; } nReadPos = 0; @@ -1167,7 +1175,7 @@ public: nType = nTypeIn; nVersion = nVersionIn; state = 0; - exceptmask = ios::badbit | ios::failbit; + exceptmask = std::ios::badbit | std::ios::failbit; } ~CAutoFile() @@ -1201,7 +1209,7 @@ public: throw std::ios_base::failure(psz); } - bool fail() const { return state & (ios::badbit | ios::failbit); } + bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } bool good() const { return state == 0; } void clear(short n = 0) { state = n; } short exceptions() { return exceptmask; } @@ -1219,7 +1227,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::read : file handle is NULL"); if (fread(pch, 1, nSize, file) != nSize) - setstate(ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); + setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); return (*this); } @@ -1228,7 +1236,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::write : file handle is NULL"); if (fwrite(pch, 1, nSize, file) != nSize) - setstate(ios::failbit, "CAutoFile::write : write failed"); + setstate(std::ios::failbit, "CAutoFile::write : write failed"); return (*this); } @@ -1259,3 +1267,5 @@ public: return (*this); } }; + +#endif diff --git a/src/strlcpy.h b/src/strlcpy.h index dc0560b..d4d1908 100644 --- a/src/strlcpy.h +++ b/src/strlcpy.h @@ -13,7 +13,8 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - +#ifndef BITCOIN_STRLCPY_H +#define BITCOIN_STRLCPY_H /* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). @@ -82,3 +83,4 @@ inline size_t strlcat(char *dst, const char *src, size_t siz) return(dlen + (s - src)); /* count does not include NUL */ } +#endif diff --git a/src/ui.cpp b/src/ui.cpp index 962a268..6e28435 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -7,6 +7,8 @@ #include #endif +using namespace std; +using namespace boost; DEFINE_EVENT_TYPE(wxEVT_UITHREADCALL) @@ -294,7 +296,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent) dResize -= 0.01; #endif wxListCtrl* pplistCtrl[] = {m_listCtrlAll, m_listCtrlSentReceived, m_listCtrlSent, m_listCtrlReceived}; - foreach(wxListCtrl* p, pplistCtrl) + BOOST_FOREACH(wxListCtrl* p, pplistCtrl) { p->InsertColumn(0, "", wxLIST_FORMAT_LEFT, dResize * 0); p->InsertColumn(1, "", wxLIST_FORMAT_LEFT, dResize * 0); @@ -528,7 +530,7 @@ string SingleLine(const string& strIn) { string strOut; bool fOneSpace = false; - foreach(unsigned char c, strIn) + BOOST_FOREACH(unsigned char c, strIn) { if (isspace(c)) { @@ -609,7 +611,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) if (nCredit == 0) { int64 nUnmatured = 0; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) nUnmatured += txout.GetCredit(); if (wtx.IsInMainChain()) { @@ -644,7 +646,7 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) // Received by Bitcoin Address if (!fShowReceived) return false; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) { @@ -687,11 +689,11 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) else { bool fAllFromMe = true; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllFromMe = fAllFromMe && txin.IsMine(); bool fAllToMe = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllToMe = fAllToMe && txout.IsMine(); if (fAllFromMe && fAllToMe) @@ -776,9 +778,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex) // Mixed debit transaction, can't break down payees // bool fAllMine = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllMine = fAllMine && txout.IsMine(); - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllMine = fAllMine && txin.IsMine(); InsertLine(fNew, nIndex, hash, strSort, colour, @@ -1006,7 +1008,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event) string strTop; if (m_listCtrl->GetItemCount()) strTop = (string)m_listCtrl->GetItemText(0); - foreach(uint256 hash, vWalletUpdated) + BOOST_FOREACH(uint256 hash, vWalletUpdated) { map::iterator mi = mapWallet.find(hash); if (mi != mapWallet.end()) @@ -1265,7 +1267,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails if (nNet > 0) { // Credit - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) { @@ -1316,7 +1318,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // Coinbase // int64 nUnmatured = 0; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) nUnmatured += txout.GetCredit(); strHTML += _("Credit: "); if (wtx.IsInMainChain()) @@ -1335,11 +1337,11 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails else { bool fAllFromMe = true; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) fAllFromMe = fAllFromMe && txin.IsMine(); bool fAllToMe = true; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) fAllToMe = fAllToMe && txout.IsMine(); if (fAllFromMe) @@ -1347,7 +1349,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // // Debit // - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) { if (txout.IsMine()) continue; @@ -1388,10 +1390,10 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails // // Mixed debit transaction // - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) if (txin.IsMine()) strHTML += _("Debit: ") + FormatMoney(-txin.GetDebit()) + "
"; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.IsMine()) strHTML += _("Credit: ") + FormatMoney(txout.GetCredit()) + "
"; } @@ -1418,10 +1420,10 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails if (fDebug) { strHTML += "

debug print

"; - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) if (txin.IsMine()) strHTML += "Debit: " + FormatMoney(-txin.GetDebit()) + "
"; - foreach(const CTxOut& txout, wtx.vout) + BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (txout.IsMine()) strHTML += "Credit: " + FormatMoney(txout.GetCredit()) + "
"; @@ -1431,7 +1433,7 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails strHTML += "
Inputs:
"; CRITICAL_BLOCK(cs_mapWallet) { - foreach(const CTxIn& txin, wtx.vin) + BOOST_FOREACH(const CTxIn& txin, wtx.vin) { COutPoint prevout = txin.prevout; map::iterator mi = mapWallet.find(prevout.hash); @@ -2341,7 +2343,7 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit CRITICAL_BLOCK(cs_mapAddressBook) { string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); - foreach(const PAIRTYPE(string, string)& item, mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(string, string)& item, mapAddressBook) { string strAddress = item.first; string strName = item.second; diff --git a/src/ui.h b/src/ui.h index aff1f1e..16643db 100644 --- a/src/ui.h +++ b/src/ui.h @@ -1,6 +1,8 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_UI_H +#define BITCOIN_UI_H DECLARE_EVENT_TYPE(wxEVT_UITHREADCALL, -1) @@ -12,9 +14,9 @@ extern wxLocale g_locale; void HandleCtrlA(wxKeyEvent& event); void UIThreadCall(boost::function0); -int ThreadSafeMessageBox(const string& message, const string& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1); -bool ThreadSafeAskFee(int64 nFeeRequired, const string& strCaption, wxWindow* parent); -void CalledSetStatusBar(const string& strText, int nField); +int ThreadSafeMessageBox(const std::string& message, const std::string& caption="Message", int style=wxOK, wxWindow* parent=NULL, int x=-1, int y=-1); +bool ThreadSafeAskFee(int64 nFeeRequired, const std::string& strCaption, wxWindow* parent); +void CalledSetStatusBar(const std::string& strText, int nField); void MainFrameRepaint(); void CreateMainWindow(); void SetStartOnSystemStartup(bool fAutoStart); @@ -28,8 +30,8 @@ inline int MyMessageBox(const wxString& message, const wxString& caption="Messag if (!fDaemon) return wxMessageBox(message, caption, style, parent, x, y); #endif - printf("wxMessageBox %s: %s\n", string(caption).c_str(), string(message).c_str()); - fprintf(stderr, "%s: %s\n", string(caption).c_str(), string(message).c_str()); + printf("wxMessageBox %s: %s\n", std::string(caption).c_str(), std::string(message).c_str()); + fprintf(stderr, "%s: %s\n", std::string(caption).c_str(), std::string(message).c_str()); return wxOK; } #define wxMessageBox MyMessageBox @@ -93,8 +95,8 @@ public: bool fRefresh; void OnUIThreadCall(wxCommandEvent& event); - int GetSortIndex(const string& strSort); - void InsertLine(bool fNew, int nIndex, uint256 hashKey, string strSort, const wxColour& colour, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4, const wxString& str5); + int GetSortIndex(const std::string& strSort); + void InsertLine(bool fNew, int nIndex, uint256 hashKey, std::string strSort, const wxColour& colour, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4, const wxString& str5); bool DeleteLine(uint256 hashKey); bool InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex=-1); void RefreshListCtrl(); @@ -176,8 +178,8 @@ public: // Custom bool fEnabledPrev; - string strFromSave; - string strMessageSave; + std::string strFromSave; + std::string strMessageSave; }; @@ -211,8 +213,8 @@ public: void Close(); void Repaint(); bool Status(); - bool Status(const string& str); - bool Error(const string& str); + bool Status(const std::string& str); + bool Error(const std::string& str); void StartTransfer(); void OnReply2(CDataStream& vRecv); void OnReply3(CDataStream& vRecv); @@ -257,7 +259,7 @@ public: wxString GetSelectedAddress(); wxString GetSelectedSendingAddress(); wxString GetSelectedReceivingAddress(); - bool CheckIfMine(const string& strAddress, const string& strTitle); + bool CheckIfMine(const std::string& strAddress, const std::string& strTitle); }; @@ -281,11 +283,11 @@ protected: public: /** Constructor */ CGetTextFromUserDialog(wxWindow* parent, - const string& strCaption, - const string& strMessage1, - const string& strValue1="", - const string& strMessage2="", - const string& strValue2="") : CGetTextFromUserDialogBase(parent, wxID_ANY, strCaption) + const std::string& strCaption, + const std::string& strMessage1, + const std::string& strValue1="", + const std::string& strMessage2="", + const std::string& strValue2="") : CGetTextFromUserDialogBase(parent, wxID_ANY, strCaption) { int x = GetSize().GetWidth(); int y = GetSize().GetHeight(); @@ -308,9 +310,9 @@ public: } // Custom - string GetValue() { return (string)m_textCtrl1->GetValue(); } - string GetValue1() { return (string)m_textCtrl1->GetValue(); } - string GetValue2() { return (string)m_textCtrl2->GetValue(); } + std::string GetValue() { return (std::string)m_textCtrl1->GetValue(); } + std::string GetValue1() { return (std::string)m_textCtrl1->GetValue(); } + std::string GetValue2() { return (std::string)m_textCtrl2->GetValue(); } }; @@ -341,3 +343,5 @@ public: DECLARE_EVENT_TABLE() }; + +#endif diff --git a/src/uint256.h b/src/uint256.h index c4f391a..14feb16 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -1,9 +1,15 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_UINT256_H +#define BITCOIN_UINT256_H + +#include "serialize.h" #include #include +#include + #if defined(_MSC_VER) || defined(__BORLANDC__) typedef __int64 int64; typedef unsigned __int64 uint64; @@ -16,7 +22,7 @@ typedef unsigned long long uint64; #endif -inline int Testuint256AdHoc(vector vArg); +inline int Testuint256AdHoc(std::vector vArg); @@ -296,7 +302,7 @@ public: char psz[sizeof(pn)*2 + 1]; for (int i = 0; i < sizeof(pn); i++) sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]); - return string(psz, psz + sizeof(pn)*2); + return std::string(psz, psz + sizeof(pn)*2); } void SetHex(const char* psz) @@ -377,7 +383,7 @@ public: friend class uint160; friend class uint256; - friend inline int Testuint256AdHoc(vector vArg); + friend inline int Testuint256AdHoc(std::vector vArg); }; typedef base_uint<160> base_uint160; @@ -626,7 +632,7 @@ inline const uint256 operator-(const uint256& a, const uint256& b) { return -inline int Testuint256AdHoc(vector vArg) +inline int Testuint256AdHoc(std::vector vArg) { uint256 g(0); @@ -755,3 +761,5 @@ inline int Testuint256AdHoc(vector vArg) return (0); } + +#endif diff --git a/src/util.cpp b/src/util.cpp index 2359616..4e93f62 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,9 +1,10 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. - #include "headers.h" +using namespace std; +using namespace boost; map mapArgs; map > mapMultiArgs; @@ -704,7 +705,7 @@ void GetDataDir(char* pszDir) if (!pfMkdir[nVariation]) { pfMkdir[nVariation] = true; - filesystem::create_directory(pszDir); + boost::filesystem::create_directory(pszDir); } } @@ -855,7 +856,7 @@ void AddTimeData(unsigned int ip, int64 nTime) { // If nobody has a time different than ours but within 5 minutes of ours, give a warning bool fMatch = false; - foreach(int64 nOffset, vTimeOffsets) + BOOST_FOREACH(int64 nOffset, vTimeOffsets) if (nOffset != 0 && abs64(nOffset) < 5 * 60) fMatch = true; @@ -869,7 +870,7 @@ void AddTimeData(unsigned int ip, int64 nTime) } } } - foreach(int64 n, vTimeOffsets) + BOOST_FOREACH(int64 n, vTimeOffsets) printf("%+"PRI64d" ", n); printf("| nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); } diff --git a/src/util.h b/src/util.h index 44afffc..b1eabd5 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,28 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_UTIL_H +#define BITCOIN_UTIL_H + +#include "uint256.h" + +#ifndef __WXMSW__ +#include +#include +#include +#endif +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include #if defined(_MSC_VER) || defined(__BORLANDC__) @@ -17,7 +39,6 @@ typedef unsigned long long uint64; #define __forceinline inline #endif -#define foreach BOOST_FOREACH #define loop for (;;) #define BEGIN(a) ((char*)&(a)) #define END(a) ((char*)&((&(a))[1])) @@ -134,8 +155,8 @@ inline const char* _(const char* psz) -extern map mapArgs; -extern map > mapMultiArgs; +extern std::map mapArgs; +extern std::map > mapMultiArgs; extern bool fDebug; extern bool fPrintToConsole; extern bool fPrintToDebugger; @@ -145,7 +166,7 @@ extern bool fShutdown; extern bool fDaemon; extern bool fServer; extern bool fCommandLine; -extern string strMiscWarning; +extern std::string strMiscWarning; extern bool fTestNet; extern bool fNoListen; extern bool fLogTimestamps; @@ -154,39 +175,39 @@ void RandAddSeed(); void RandAddSeedPerfmon(); int OutputDebugStringF(const char* pszFormat, ...); int my_snprintf(char* buffer, size_t limit, const char* format, ...); -string strprintf(const char* format, ...); +std::string strprintf(const char* format, ...); bool error(const char* format, ...); void LogException(std::exception* pex, const char* pszThread); void PrintException(std::exception* pex, const char* pszThread); void PrintExceptionContinue(std::exception* pex, const char* pszThread); -void ParseString(const string& str, char c, vector& v); -string FormatMoney(int64 n, bool fPlus=false); -bool ParseMoney(const string& str, int64& nRet); +void ParseString(const std::string& str, char c, std::vector& v); +std::string FormatMoney(int64 n, bool fPlus=false); +bool ParseMoney(const std::string& str, int64& nRet); bool ParseMoney(const char* pszIn, int64& nRet); -vector ParseHex(const char* psz); -vector ParseHex(const string& str); +std::vector ParseHex(const char* psz); +std::vector ParseHex(const std::string& str); void ParseParameters(int argc, char* argv[]); const char* wxGetTranslation(const char* psz); bool WildcardMatch(const char* psz, const char* mask); -bool WildcardMatch(const string& str, const string& mask); +bool WildcardMatch(const std::string& str, const std::string& mask); int GetFilesize(FILE* file); void GetDataDir(char* pszDirRet); -string GetConfigFile(); -string GetPidFile(); -void CreatePidFile(string pidFile, pid_t pid); -void ReadConfigFile(map& mapSettingsRet, map >& mapMultiSettingsRet); +std::string GetConfigFile(); +std::string GetPidFile(); +void CreatePidFile(std::string pidFile, pid_t pid); +void ReadConfigFile(std::map& mapSettingsRet, std::map >& mapMultiSettingsRet); #ifdef __WXMSW__ string MyGetSpecialFolderPath(int nFolder, bool fCreate); #endif -string GetDefaultDataDir(); -string GetDataDir(); +std::string GetDefaultDataDir(); +std::string GetDataDir(); void ShrinkDebugFile(); int GetRandInt(int nMax); uint64 GetRand(uint64 nMax); int64 GetTime(); int64 GetAdjustedTime(); void AddTimeData(unsigned int ip, int64 nTime); -string FormatFullVersion(); +std::string FormatFullVersion(); @@ -268,12 +289,12 @@ public: -inline string i64tostr(int64 n) +inline std::string i64tostr(int64 n) { return strprintf("%"PRI64d, n); } -inline string itostr(int n) +inline std::string itostr(int n) { return strprintf("%d", n); } @@ -287,7 +308,7 @@ inline int64 atoi64(const char* psz) #endif } -inline int64 atoi64(const string& str) +inline int64 atoi64(const std::string& str) { #ifdef _MSC_VER return _atoi64(str.c_str()); @@ -296,7 +317,7 @@ inline int64 atoi64(const string& str) #endif } -inline int atoi(const string& str) +inline int atoi(const std::string& str) { return atoi(str.c_str()); } @@ -317,39 +338,39 @@ inline int64 abs64(int64 n) } template -string HexStr(const T itbegin, const T itend, bool fSpaces=false) +std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) { if (itbegin == itend) return ""; const unsigned char* pbegin = (const unsigned char*)&itbegin[0]; const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]); - string str; + std::string str; str.reserve((pend-pbegin) * (fSpaces ? 3 : 2)); for (const unsigned char* p = pbegin; p != pend; p++) str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p); return str; } -inline string HexStr(const vector& vch, bool fSpaces=false) +inline std::string HexStr(const std::vector& vch, bool fSpaces=false) { return HexStr(vch.begin(), vch.end(), fSpaces); } template -string HexNumStr(const T itbegin, const T itend, bool f0x=true) +std::string HexNumStr(const T itbegin, const T itend, bool f0x=true) { if (itbegin == itend) return ""; const unsigned char* pbegin = (const unsigned char*)&itbegin[0]; const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]); - string str = (f0x ? "0x" : ""); + std::string str = (f0x ? "0x" : ""); str.reserve(str.size() + (pend-pbegin) * 2); for (const unsigned char* p = pend-1; p >= pbegin; p--) str += strprintf("%02x", *p); return str; } -inline string HexNumStr(const vector& vch, bool f0x=true) +inline std::string HexNumStr(const std::vector& vch, bool f0x=true) { return HexNumStr(vch.begin(), vch.end(), f0x); } @@ -360,7 +381,7 @@ void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSp printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str()); } -inline void PrintHex(const vector& vch, const char* pszFormat="%s", bool fSpaces=true) +inline void PrintHex(const std::vector& vch, const char* pszFormat="%s", bool fSpaces=true) { printf(pszFormat, HexStr(vch, fSpaces).c_str()); } @@ -380,11 +401,11 @@ inline int64 GetPerformanceCounter() inline int64 GetTimeMillis() { - return (posix_time::ptime(posix_time::microsec_clock::universal_time()) - - posix_time::ptime(gregorian::date(1970,1,1))).total_milliseconds(); + return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - + boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds(); } -inline string DateTimeStrFormat(const char* pszFormat, int64 nTime) +inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime) { time_t n = nTime; struct tm* ptmTime = gmtime(&n); @@ -409,21 +430,21 @@ inline bool IsSwitchChar(char c) #endif } -inline string GetArg(const string& strArg, const string& strDefault) +inline std::string GetArg(const std::string& strArg, const std::string& strDefault) { if (mapArgs.count(strArg)) return mapArgs[strArg]; return strDefault; } -inline int64 GetArg(const string& strArg, int64 nDefault) +inline int64 GetArg(const std::string& strArg, int64 nDefault) { if (mapArgs.count(strArg)) return atoi64(mapArgs[strArg]); return nDefault; } -inline bool GetBoolArg(const string& strArg) +inline bool GetBoolArg(const std::string& strArg) { if (mapArgs.count(strArg)) { @@ -538,7 +559,7 @@ uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION) return Hash(ss.begin(), ss.end()); } -inline uint160 Hash160(const vector& vch) +inline uint160 Hash160(const std::vector& vch) { uint256 hash1; SHA256(&vch[0], vch.size(), (unsigned char*)&hash1); @@ -655,3 +676,5 @@ inline bool AffinityBugWorkaround(void(*pfn)(void*)) #endif return false; } + +#endif -- 1.7.1