Browse Source

make bitcoin include files more modular

tags/v0.15.1
Wladimir J. van der Laan 10 years ago
parent
commit
223b6f1ba4
25 changed files with 640 additions and 510 deletions
  1. 26
    19
      src/base58.h
  2. 7
    5
      src/bignum.h
  3. 7
    4
      src/db.cpp
  4. 54
    42
      src/db.h
  5. 2
    2
      src/headers.h
  6. 3
    8
      src/init.cpp
  7. 4
    0
      src/init.h
  8. 3
    0
      src/irc.cpp
  9. 5
    1
      src/irc.h
  10. 15
    8
      src/key.h
  11. 58
    60
      src/main.cpp
  12. 116
    104
      src/main.h
  13. 30
    27
      src/net.cpp
  14. 53
    41
      src/net.h
  15. 9
    4
      src/noui.h
  16. 24
    22
      src/rpc.cpp
  17. 7
    5
      src/script.cpp
  18. 31
    22
      src/script.h
  19. 61
    51
      src/serialize.h
  20. 3
    1
      src/strlcpy.h
  21. 22
    20
      src/ui.cpp
  22. 24
    20
      src/ui.h
  23. 12
    4
      src/uint256.h
  24. 5
    4
      src/util.cpp
  25. 59
    36
      src/util.h

+ 26
- 19
src/base58.h View File

@@ -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 <string>
#include <vector>
#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<unsigned char> vchTmp(pend-pbegin+1, 0);
std::vector<unsigned char> 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<unsigned char>& vch)
inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
{
return EncodeBase58(&vch[0], &vch[0] + vch.size());
}

inline bool DecodeBase58(const char* psz, vector<unsigned char>& vchRet)
inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
{
CAutoBN_CTX pctx;
vchRet.clear();
@@ -88,7 +93,7 @@ inline bool DecodeBase58(const char* psz, vector<unsigned char>& vchRet)
}

// Get bignum as little endian data
vector<unsigned char> vchTmp = bn.getvch();
std::vector<unsigned char> 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<unsigned char>& vchRet)
return true;
}

inline bool DecodeBase58(const string& str, vector<unsigned char>& vchRet)
inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
{
return DecodeBase58(str.c_str(), vchRet);
}
@@ -114,16 +119,16 @@ inline bool DecodeBase58(const string& str, vector<unsigned char>& vchRet)



inline string EncodeBase58Check(const vector<unsigned char>& vchIn)
inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
{
// add 4-byte hash check to the end
vector<unsigned char> vch(vchIn);
std::vector<unsigned char> 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<unsigned char>& vchRet)
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
{
if (!DecodeBase58(psz, vchRet))
return false;
@@ -142,7 +147,7 @@ inline bool DecodeBase58Check(const char* psz, vector<unsigned char>& vchRet)
return true;
}

inline bool DecodeBase58Check(const string& str, vector<unsigned char>& vchRet)
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
{
return DecodeBase58Check(str.c_str(), vchRet);
}
@@ -154,17 +159,17 @@ inline bool DecodeBase58Check(const string& str, vector<unsigned char>& 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<unsigned char> vch(1, ADDRESSVERSION);
std::vector<unsigned char> vch(1, ADDRESSVERSION);
vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160));
return EncodeBase58Check(vch);
}

inline bool AddressToHash160(const char* psz, uint160& hash160Ret)
{
vector<unsigned char> vch;
std::vector<unsigned char> 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<unsigned char>& vchPubKey)
inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey)
{
return Hash160ToAddress(Hash160(vchPubKey));
}

#endif

+ 7
- 5
src/bignum.h View File

@@ -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 <stdexcept>
#include <vector>
#include <openssl/bn.h>




#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<typename Stream>
void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
{
vector<unsigned char> vch;
std::vector<unsigned char> 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

+ 7
- 4
src/db.cpp View File

@@ -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<pair<int, CBlockIndex*> > 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);

+ 54
- 42
src/db.h View File

@@ -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 <map>
#include <string>
#include <vector>

#include <db_cxx.h>

class CTransaction;
class CTxIndex;
@@ -15,9 +25,9 @@ class CAccount;
class CAccountingEntry;
class CBlockLocator;

extern map<string, string> mapAddressBook;
extern std::map<std::string, std::string> mapAddressBook;
extern CCriticalSection cs_mapAddressBook;
extern vector<unsigned char> vchDefaultKey;
extern std::vector<unsigned char> vchDefaultKey;
extern bool fClient;
extern int nBestHeight;

@@ -27,7 +37,7 @@ extern DbEnv dbenv;


extern void DBFlush(bool fShutdown);
extern vector<unsigned char> GetKeyFromKeyPool();
extern std::vector<unsigned char> GetKeyFromKeyPool();
extern int64 GetOldestKeyPoolTime();


@@ -37,8 +47,8 @@ class CDB
{
protected:
Db* pdb;
string strFile;
vector<DbTxn*> vTxn;
std::string strFile;
std::vector<DbTxn*> 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<CTransaction>& vtx);
bool ReadOwnerTxes(uint160 hash160, int nHeight, std::vector<CTransaction>& 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<unsigned char> vchPubKey;
std::vector<unsigned char> vchPubKey;

CKeyPool()
{
nTime = GetTime();
}

CKeyPool(const vector<unsigned char>& vchPubKeyIn)
CKeyPool(const std::vector<unsigned char>& 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<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
bool ReadKey(const std::vector<unsigned char>& 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<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
bool WriteKey(const std::vector<unsigned char>& 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<unsigned char>& vchPubKey)
bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
{
vchPubKey.clear();
return Read(string("defaultkey"), vchPubKey);
return Read(std::string("defaultkey"), vchPubKey);
}

bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)
bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
{
vchDefaultKey = vchPubKey;
nWalletDBUpdated++;
return Write(string("defaultkey"), vchPubKey);
return Write(std::string("defaultkey"), vchPubKey);
}

template<typename T>
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<typename T>
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<CAccountingEntry>& acentries);
int64 GetAccountCreditDebit(const std::string& strAccount);
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);

bool LoadWallet();
protected:
@@ -455,14 +465,14 @@ protected:
void KeepKey(int64 nIndex);
static void ReturnKey(int64 nIndex);
friend class CReserveKey;
friend vector<unsigned char> GetKeyFromKeyPool();
friend std::vector<unsigned char> 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<unsigned char> vchPubKey;
std::vector<unsigned char> vchPubKey;
public:
CReserveKey()
{
@@ -484,7 +494,7 @@ public:
ReturnKey();
}

vector<unsigned char> GetReservedKey()
std::vector<unsigned char> GetReservedKey()
{
if (nIndex == -1)
{
@@ -512,3 +522,5 @@ public:
vchPubKey.clear();
}
};

#endif

+ 2
- 2
src/headers.h View File

@@ -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

+ 3
- 8
src/init.cpp View File

@@ -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

+ 4
- 0
src/init.h View File

@@ -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

+ 3
- 0
src/irc.cpp View File

@@ -4,6 +4,9 @@

#include "headers.h"

using namespace std;
using namespace boost;

int nGotIRCAddresses = 0;
bool fGotExternalIP = false;


+ 5
- 1
src/irc.h View File

@@ -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

+ 15
- 8
src/key.h View File

@@ -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 <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>

// secp160k1
// const unsigned int PRIVATE_KEY_SIZE = 192;
@@ -36,7 +41,7 @@ public:


// secure_allocator is defined in serialize.h
typedef vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;



@@ -109,7 +114,7 @@ public:
return vchPrivKey;
}

bool SetPubKey(const vector<unsigned char>& vchPubKey)
bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
{
const unsigned char* pbegin = &vchPubKey[0];
if (!o2i_ECPublicKey(&pkey, &pbegin, vchPubKey.size()))
@@ -118,19 +123,19 @@ public:
return true;
}

vector<unsigned char> GetPubKey() const
std::vector<unsigned char> GetPubKey() const
{
unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
if (!nSize)
throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
vector<unsigned char> vchPubKey(nSize, 0);
std::vector<unsigned char> 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<unsigned char>& vchSig)
bool Sign(uint256 hash, std::vector<unsigned char>& vchSig)
{
vchSig.clear();
unsigned char pchSig[10000];
@@ -142,7 +147,7 @@ public:
return true;
}

bool Verify(uint256 hash, const vector<unsigned char>& vchSig)
bool Verify(uint256 hash, const std::vector<unsigned char>& 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<unsigned char>& vchSig)
static bool Sign(const CPrivKey& vchPrivKey, uint256 hash, std::vector<unsigned char>& vchSig)
{
CKey key;
if (!key.SetPrivKey(vchPrivKey))
@@ -158,7 +163,7 @@ public:
return key.Sign(hash, vchSig);
}

static bool Verify(const vector<unsigned char>& vchPubKey, uint256 hash, const vector<unsigned char>& vchSig)
static bool Verify(const std::vector<unsigned char>& vchPubKey, uint256 hash, const std::vector<unsigned char>& vchSig)
{
CKey key;
if (!key.SetPubKey(vchPubKey))
@@ -166,3 +171,5 @@ public:
return key.Verify(hash, vchSig);
}
};

#endif

+ 58
- 60
src/main.cpp View File

@@ -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<uint256, CDataStream*>::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<uint256> 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<CDiskTxPos> 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<unsigned int, CWalletTx*> 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<uint256, CTxIndex> 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<CTransaction> 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<uint256, CNode*> 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<unsigned char>, CAddress)& item, mapAddresses)
BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
{
const CAddress& addr = item.second;
if (addr.nTime > nSince)
nCount++;
}
foreach(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapAddresses)
BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, 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<CAddress> 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<pair<CWalletTx*,unsigned int> >& setCoi
bool CreateTransaction(const vector<pair<CScript, int64> >& 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<pair<CScript, int64> >& 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<pair<CScript, int64> >& 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<pair<CScript, int64> >& 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<CWalletTx*> 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);

+ 116
- 104
src/main.h View File

@@ -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 <list>

class COutPoint;
class CInPoint;
@@ -35,7 +45,7 @@ static const int fHaveUPnP = false;


extern CCriticalSection cs_main;
extern map<uint256, CBlockIndex*> mapBlockIndex;
extern std::map<uint256, CBlockIndex*> 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<uint256, int> mapRequestCount;
extern std::map<uint256, int> mapRequestCount;
extern CCriticalSection cs_mapRequestCount;
extern map<string, string> mapAddressBook;
extern std::map<std::string, std::string> mapAddressBook;
extern CCriticalSection cs_mapAddressBook;
extern vector<unsigned char> vchDefaultKey;
extern std::vector<unsigned char> 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<unsigned char> GenerateNewKey();
std::vector<unsigned char> 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<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
bool CreateTransaction(const std::vector<std::pair<CScript, int64> >& 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<unsigned char> vchPubKey;
std::vector<unsigned char> 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<CTxIn> vin;
vector<CTxOut> vout;
std::vector<CTxIn> vin;
std::vector<CTxOut> 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<uint256, CTxIndex>& mapTestPool, CDiskTxPos posThisTx,
bool ConnectInputs(CTxDB& txdb, std::map<uint256, CTxIndex>& 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<uint256> vMerkleBranch;
std::vector<uint256> vMerkleBranch;
int nIndex;

// memory only
@@ -776,14 +786,14 @@ public:
class CWalletTx : public CMerkleTx
{
public:
vector<CMerkleTx> vtxPrev;
map<string, string> mapValue;
vector<pair<string, string> > vOrderForm;
std::vector<CMerkleTx> vtxPrev;
std::map<std::string, std::string> mapValue;
std::vector<std::pair<std::string, std::string> > vOrderForm;
unsigned int fTimeReceivedIsTxTime;
unsigned int nTimeReceived; // time received by this node
char fFromMe;
string strFromAccount;
vector<char> vfSpent;
std::string strFromAccount;
std::vector<char> 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<char>& vfNewSpent)
bool UpdateSpent(const std::vector<char>& 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<pair<string /* address */, int64> >& listReceived,
list<pair<string /* address */, int64> >& listSent, int64& nFee, string& strSentAccount) const;
void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<std::string /* address */, int64> >& listReceived,
std::list<std::pair<std::string /* address */, int64> >& 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<uint256, const CMerkleTx*> mapPrev;
vector<const CMerkleTx*> vWorkQueue;
std::map<uint256, const CMerkleTx*> mapPrev;
std::vector<const CMerkleTx*> 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<CDiskTxPos> vSpent;
std::vector<CDiskTxPos> vSpent;

CTxIndex()
{
@@ -1149,10 +1159,10 @@ public:
unsigned int nNonce;

// network and disk
vector<CTransaction> vtx;
std::vector<CTransaction> vtx;

// memory only
mutable vector<uint256> vMerkleTree;
mutable std::vector<uint256> 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<uint256> GetMerkleBranch(int nIndex) const
std::vector<uint256> GetMerkleBranch(int nIndex) const
{
if (vMerkleTree.empty())
BuildMerkleTree();
vector<uint256> vMerkleBranch;
std::vector<uint256> 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<uint256>& vMerkleBranch, int nIndex)
static uint256 CheckMerkleBranch(uint256 hash, const std::vector<uint256>& 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<uint256> vHave;
std::vector<uint256> vHave;
public:

CBlockLocator()
@@ -1616,7 +1626,7 @@ public:

explicit CBlockLocator(uint256 hashBlock)
{
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
std::map<uint256, CBlockIndex*>::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<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
std::map<uint256, CBlockIndex*>::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<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
std::map<uint256, CBlockIndex*>::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<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
std::map<uint256, CBlockIndex*>::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<string, string> property map

@@ -1764,7 +1774,7 @@ public:
class CAccount
{
public:
vector<unsigned char> vchPubKey;
std::vector<unsigned char> 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<int> setCancel;
std::set<int> setCancel;
int nMinVer; // lowest version inclusive
int nMaxVer; // highest version inclusive
set<string> setSubVer; // empty matches all
std::set<std::string> 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<unsigned char> vchMsg;
vector<unsigned char> vchSig;
std::vector<unsigned char> vchMsg;
std::vector<unsigned char> 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<uint256, CTransaction> mapTransactions;
extern map<uint256, CWalletTx> mapWallet;
extern vector<uint256> vWalletUpdated;
extern std::map<uint256, CTransaction> mapTransactions;
extern std::map<uint256, CWalletTx> mapWallet;
extern std::vector<uint256> vWalletUpdated;
extern CCriticalSection cs_mapWallet;
extern map<vector<unsigned char>, CPrivKey> mapKeys;
extern map<uint160, vector<unsigned char> > mapPubKeys;
extern std::map<std::vector<unsigned char>, CPrivKey> mapKeys;
extern std::map<uint160, std::vector<unsigned char> > mapPubKeys;
extern CCriticalSection cs_mapKeys;
extern CKey keyUser;

#endif

+ 30
- 27
src/net.cpp View File

@@ -11,6 +11,9 @@
#include <miniupnpc/upnperrors.h>
#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<CNode*> 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<CNode*> 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<CAddress> 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"])
{