Browse Source

integration of core bitcoin

pull/1/head
Wladimir J. van der Laan 12 years ago
parent
commit
69d605f410
  1. 26
      TODO
  2. 34
      bitcoin.pro
  3. 526
      core/include/db.h
  4. 147
      core/include/headers.h
  5. 11
      core/include/init.h
  6. 13
      core/include/irc.h
  7. 18
      core/include/key.h
  8. 204
      core/include/main.h
  9. 67
      core/include/noui.h
  10. 6
      core/include/rpc.h
  11. 718
      core/include/script.h
  12. 1026
      core/src/db.cpp
  13. 522
      core/src/init.cpp
  14. 438
      core/src/irc.cpp
  15. 4024
      core/src/main.cpp
  16. 1665
      core/src/net.cpp
  17. 2195
      core/src/rpc.cpp
  18. 1208
      core/src/script.cpp
  19. 21
      core/src/util.cpp
  20. 18
      json/include/json/json_spirit.h
  21. 54
      json/include/json/json_spirit_error_position.h
  22. 62
      json/include/json/json_spirit_reader.h
  23. 612
      json/include/json/json_spirit_reader_template.h
  24. 70
      json/include/json/json_spirit_stream_reader.h
  25. 61
      json/include/json/json_spirit_utils.h
  26. 534
      json/include/json/json_spirit_value.h
  27. 50
      json/include/json/json_spirit_writer.h
  28. 248
      json/include/json/json_spirit_writer_template.h
  29. 137
      json/src/json_spirit_reader.cpp
  30. 8
      json/src/json_spirit_value.cpp
  31. 95
      json/src/json_spirit_writer.cpp

26
TODO

@ -62,3 +62,29 @@ AboutDialog @@ -62,3 +62,29 @@ AboutDialog
- Make icon blend into taskbar and maybe silver/grey
Same for the other icons?
Done:
Compatibility with Qt, and some more modularity
- Put guard statements around header files.
- Proper include between header files; remove central "header.h" file.
- Removed macro foreach: conflicts with Qt keyword foreach, replaced back with BOOST_FOREACH
- Prefix stdlib structures and functions with std:: in headers; "using namespace" in header files is
generally frowned upon
Todo:
- Repeated in all files?
#define __STDC_LIMIT_MACROS // to enable UINT64_MAX from stdint.h
#include "main.h"
#ifndef GUI
#include "noui.h"
#endif
- Check windows support / cross platform

34
bitcoin.pro

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += gui/include core/include cryptopp/include
unix:LIBS += -lssl
INCLUDEPATH += gui/include core/include cryptopp/include json/include
unix:LIBS += -lssl -lboost_system -lboost_filesystem -lboost_program_options -lboost_thread -ldb_cxx
# Input
HEADERS += gui/include/bitcoingui.h \
@ -34,7 +34,23 @@ HEADERS += gui/include/bitcoingui.h \ @@ -34,7 +34,23 @@ HEADERS += gui/include/bitcoingui.h \
core/include/strlcpy.h \
core/include/main.h \
core/include/net.h \
core/include/key.h
core/include/key.h \
core/include/db.h \
core/include/script.h \
core/include/noui.h \
core/include/init.h \
core/include/headers.h \
core/include/irc.h \
json/include/json/json_spirit_writer_template.h \
json/include/json/json_spirit_writer.h \
json/include/json/json_spirit_value.h \
json/include/json/json_spirit_utils.h \
json/include/json/json_spirit_stream_reader.h \
json/include/json/json_spirit_reader_template.h \
json/include/json/json_spirit_reader.h \
json/include/json/json_spirit_error_position.h \
json/include/json/json_spirit.h \
core/include/rpc.h
SOURCES += gui/src/bitcoin.cpp gui/src/bitcoingui.cpp \
gui/src/transactiontablemodel.cpp \
gui/src/addresstablemodel.cpp \
@ -47,7 +63,17 @@ SOURCES += gui/src/bitcoin.cpp gui/src/bitcoingui.cpp \ @@ -47,7 +63,17 @@ SOURCES += gui/src/bitcoin.cpp gui/src/bitcoingui.cpp \
gui/src/bitcoinaddressvalidator.cpp \
cryptopp/src/sha.cpp \
cryptopp/src/cpu.cpp \
core/src/util.cpp
core/src/util.cpp \
core/src/script.cpp \
core/src/main.cpp \
core/src/init.cpp \
core/src/rpc.cpp \
core/src/net.cpp \
core/src/irc.cpp \
core/src/db.cpp \
json/src/json_spirit_writer.cpp \
json/src/json_spirit_value.cpp \
json/src/json_spirit_reader.cpp
RESOURCES += \
gui/bitcoin.qrc

526
core/include/db.h

@ -0,0 +1,526 @@ @@ -0,0 +1,526 @@
// 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;
class CDiskBlockIndex;
class CDiskTxPos;
class COutPoint;
class CUser;
class CReview;
class CAddress;
class CWalletTx;
class CAccount;
class CAccountingEntry;
class CBlockLocator;
extern std::map<std::string, std::string> mapAddressBook;
extern CCriticalSection cs_mapAddressBook;
extern std::vector<unsigned char> vchDefaultKey;
extern bool fClient;
extern int nBestHeight;
extern unsigned int nWalletDBUpdated;
extern DbEnv dbenv;
extern void DBFlush(bool fShutdown);
extern std::vector<unsigned char> GetKeyFromKeyPool();
extern int64 GetOldestKeyPoolTime();
class CDB
{
protected:
Db* pdb;
std::string strFile;
std::vector<DbTxn*> vTxn;
bool fReadOnly;
explicit CDB(const char* pszFile, const char* pszMode="r+");
~CDB() { Close(); }
public:
void Close();
private:
CDB(const CDB&);
void operator=(const CDB&);
protected:
template<typename K, typename T>
bool Read(const K& key, T& value)
{
if (!pdb)
return false;
// Key
CDataStream ssKey(SER_DISK);
ssKey.reserve(1000);
ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size());
// Read
Dbt datValue;
datValue.set_flags(DB_DBT_MALLOC);
int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);
memset(datKey.get_data(), 0, datKey.get_size());
if (datValue.get_data() == NULL)
return false;
// Unserialize value
CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);
ssValue >> value;
// Clear and free memory
memset(datValue.get_data(), 0, datValue.get_size());
free(datValue.get_data());
return (ret == 0);
}
template<typename K, typename T>
bool Write(const K& key, const T& value, bool fOverwrite=true)
{
if (!pdb)
return false;
if (fReadOnly)
assert(("Write called on database in read-only mode", false));
// Key
CDataStream ssKey(SER_DISK);
ssKey.reserve(1000);
ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size());
// Value
CDataStream ssValue(SER_DISK);
ssValue.reserve(10000);
ssValue << value;
Dbt datValue(&ssValue[0], ssValue.size());
// Write
int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
// Clear memory in case it was a private key
memset(datKey.get_data(), 0, datKey.get_size());
memset(datValue.get_data(), 0, datValue.get_size());
return (ret == 0);
}
template<typename K>
bool Erase(const K& key)
{
if (!pdb)
return false;
if (fReadOnly)
assert(("Erase called on database in read-only mode", false));
// Key
CDataStream ssKey(SER_DISK);
ssKey.reserve(1000);
ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size());
// Erase
int ret = pdb->del(GetTxn(), &datKey, 0);
// Clear memory
memset(datKey.get_data(), 0, datKey.get_size());
return (ret == 0 || ret == DB_NOTFOUND);
}
template<typename K>
bool Exists(const K& key)
{
if (!pdb)
return false;
// Key
CDataStream ssKey(SER_DISK);
ssKey.reserve(1000);
ssKey << key;
Dbt datKey(&ssKey[0], ssKey.size());
// Exists
int ret = pdb->exists(GetTxn(), &datKey, 0);
// Clear memory
memset(datKey.get_data(), 0, datKey.get_size());
return (ret == 0);
}
Dbc* GetCursor()
{
if (!pdb)
return NULL;
Dbc* pcursor = NULL;
int ret = pdb->cursor(NULL, &pcursor, 0);
if (ret != 0)
return NULL;
return pcursor;
}
int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
{
// Read at cursor
Dbt datKey;
if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
{
datKey.set_data(&ssKey[0]);
datKey.set_size(ssKey.size());
}
Dbt datValue;
if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
{
datValue.set_data(&ssValue[0]);
datValue.set_size(ssValue.size());
}
datKey.set_flags(DB_DBT_MALLOC);
datValue.set_flags(DB_DBT_MALLOC);
int ret = pcursor->get(&datKey, &datValue, fFlags);
if (ret != 0)
return ret;
else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
return 99999;
// Convert to streams
ssKey.SetType(SER_DISK);
ssKey.clear();
ssKey.write((char*)datKey.get_data(), datKey.get_size());
ssValue.SetType(SER_DISK);
ssValue.clear();
ssValue.write((char*)datValue.get_data(), datValue.get_size());
// Clear and free memory
memset(datKey.get_data(), 0, datKey.get_size());
memset(datValue.get_data(), 0, datValue.get_size());
free(datKey.get_data());
free(datValue.get_data());
return 0;
}
DbTxn* GetTxn()
{
if (!vTxn.empty())
return vTxn.back();
else
return NULL;
}
public:
bool TxnBegin()
{
if (!pdb)
return false;
DbTxn* ptxn = NULL;
int ret = dbenv.txn_begin(GetTxn(), &ptxn, DB_TXN_NOSYNC);
if (!ptxn || ret != 0)
return false;
vTxn.push_back(ptxn);
return true;
}
bool TxnCommit()
{
if (!pdb)
return false;
if (vTxn.empty())
return false;
int ret = vTxn.back()->commit(0);
vTxn.pop_back();
return (ret == 0);
}
bool TxnAbort()
{
if (!pdb)
return false;
if (vTxn.empty())
return false;
int ret = vTxn.back()->abort();
vTxn.pop_back();
return (ret == 0);
}
bool ReadVersion(int& nVersion)
{
nVersion = 0;
return Read(std::string("version"), nVersion);
}
bool WriteVersion(int nVersion)
{
return Write(std::string("version"), nVersion);
}
};
class CTxDB : public CDB
{
public:
CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
private:
CTxDB(const CTxDB&);
void operator=(const CTxDB&);
public:
bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
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, 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);
bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
bool EraseBlockIndex(uint256 hash);
bool ReadHashBestChain(uint256& hashBestChain);
bool WriteHashBestChain(uint256 hashBestChain);
bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork);
bool WriteBestInvalidWork(CBigNum bnBestInvalidWork);
bool LoadBlockIndex();
};
class CAddrDB : public CDB
{
public:
CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }
private:
CAddrDB(const CAddrDB&);
void operator=(const CAddrDB&);
public:
bool WriteAddress(const CAddress& addr);
bool EraseAddress(const CAddress& addr);
bool LoadAddresses();
};
bool LoadAddresses();
class CKeyPool
{
public:
int64 nTime;
std::vector<unsigned char> vchPubKey;
CKeyPool()
{
nTime = GetTime();
}
CKeyPool(const std::vector<unsigned char>& vchPubKeyIn)
{
nTime = GetTime();
vchPubKey = vchPubKeyIn;
}
IMPLEMENT_SERIALIZE
(
if (!(nType & SER_GETHASH))
READWRITE(nVersion);
READWRITE(nTime);
READWRITE(vchPubKey);
)
};
class CWalletDB : public CDB
{
public:
CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode)
{
}
private:
CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);
public:
bool ReadName(const std::string& strAddress, std::string& strName)
{
strName = "";
return Read(std::make_pair(std::string("name"), strAddress), strName);
}
bool WriteName(const std::string& strAddress, const std::string& strName)
{
CRITICAL_BLOCK(cs_mapAddressBook)
mapAddressBook[strAddress] = strName;
nWalletDBUpdated++;
return Write(std::make_pair(std::string("name"), strAddress), strName);
}
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(std::make_pair(std::string("name"), strAddress));
}
bool ReadTx(uint256 hash, CWalletTx& wtx)
{
return Read(std::make_pair(std::string("tx"), hash), wtx);
}
bool WriteTx(uint256 hash, const CWalletTx& wtx)
{
nWalletDBUpdated++;
return Write(std::make_pair(std::string("tx"), hash), wtx);
}
bool EraseTx(uint256 hash)
{
nWalletDBUpdated++;
return Erase(std::make_pair(std::string("tx"), hash));
}
bool ReadKey(const std::vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)
{
vchPrivKey.clear();
return Read(std::make_pair(std::string("key"), vchPubKey), vchPrivKey);
}
bool WriteKey(const std::vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)
{
nWalletDBUpdated++;
return Write(std::make_pair(std::string("key"), vchPubKey), vchPrivKey, false);
}
bool WriteBestBlock(const CBlockLocator& locator)
{
nWalletDBUpdated++;
return Write(std::string("bestblock"), locator);
}
bool ReadBestBlock(CBlockLocator& locator)
{
return Read(std::string("bestblock"), locator);
}
bool ReadDefaultKey(std::vector<unsigned char>& vchPubKey)
{
vchPubKey.clear();
return Read(std::string("defaultkey"), vchPubKey);
}
bool WriteDefaultKey(const std::vector<unsigned char>& vchPubKey)
{
vchDefaultKey = vchPubKey;
nWalletDBUpdated++;
return Write(std::string("defaultkey"), vchPubKey);
}
template<typename T>
bool ReadSetting(const std::string& strKey, T& value)
{
return Read(std::make_pair(std::string("setting"), strKey), value);
}
template<typename T>
bool WriteSetting(const std::string& strKey, const T& value)
{
nWalletDBUpdated++;
return Write(std::make_pair(std::string("setting"), strKey), value);
}
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 std::string& strAccount);
void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
bool LoadWallet();
protected:
void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
void KeepKey(int64 nIndex);
static void ReturnKey(int64 nIndex);
friend class CReserveKey;
friend std::vector<unsigned char> GetKeyFromKeyPool();
friend int64 GetOldestKeyPoolTime();
};
bool LoadWallet(bool& fFirstRunRet);
void BackupWallet(const std::string& strDest);
inline bool SetAddressBookName(const std::string& strAddress, const std::string& strName)
{
return CWalletDB().WriteName(strAddress, strName);
}
class CReserveKey
{
protected:
int64 nIndex;
std::vector<unsigned char> vchPubKey;
public:
CReserveKey()
{
nIndex = -1;
}
~CReserveKey()
{
if (!fShutdown)
ReturnKey();
}
std::vector<unsigned char> GetReservedKey()
{
if (nIndex == -1)
{
CKeyPool keypool;
CWalletDB().ReserveKeyFromKeyPool(nIndex, keypool);
vchPubKey = keypool.vchPubKey;
}
assert(!vchPubKey.empty());
return vchPubKey;
}
void KeepKey()
{
if (nIndex != -1)
CWalletDB().KeepKey(nIndex);
nIndex = -1;
vchPubKey.clear();
}
void ReturnKey()
{
if (nIndex != -1)
CWalletDB::ReturnKey(nIndex);
nIndex = -1;
vchPubKey.clear();
}
};
#endif

147
core/include/headers.h

@ -0,0 +1,147 @@ @@ -0,0 +1,147 @@
// 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.
#ifdef _MSC_VER
#pragma warning(disable:4786)
#pragma warning(disable:4804)
#pragma warning(disable:4805)
#pragma warning(disable:4717)
#endif
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0500
#ifdef _WIN32_IE
#undef _WIN32_IE
#endif
#define _WIN32_IE 0x0400
#define WIN32_LEAN_AND_MEAN 1
#define __STDC_LIMIT_MACROS // to enable UINT64_MAX from stdint.h
#if (defined(__unix__) || defined(unix)) && !defined(USG)
#include <sys/param.h> // to get BSD define
#endif
#ifdef __WXMAC_OSX__
#ifndef BSD
#define BSD 1
#endif
#endif
#ifdef GUI
#include <wx/wx.h>
#include <wx/stdpaths.h>
#include <wx/snglinst.h>
#include <wx/utils.h>
#include <wx/clipbrd.h>
#include <wx/taskbar.h>
#endif
#include <openssl/buffer.h>
#include <openssl/ecdsa.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include <db_cxx.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include <assert.h>
#include <memory>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/thread.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/config.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
#ifdef __WXMSW__
#include <windows.h>
#include <winsock2.h>
#include <mswsock.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <io.h>
#include <process.h>
#include <malloc.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <fcntl.h>
#include <signal.h>
#endif
#ifdef BSD
#include <netinet/in.h>
#endif
#pragma hdrstop
#include "strlcpy.h"
#include "serialize.h"
#include "uint256.h"
#include "util.h"
#include "key.h"
#include "bignum.h"
#include "base58.h"
#include "script.h"
#include "db.h"
#include "net.h"
#include "irc.h"
#include "main.h"
#include "rpc.h"
#ifdef GUI
#include "uibase.h"
#include "ui.h"
#else
#include "noui.h"
#endif
#include "init.h"
#ifdef GUI
#include "xpm/addressbook16.xpm"
#include "xpm/addressbook20.xpm"
#include "xpm/bitcoin16.xpm"
#include "xpm/bitcoin20.xpm"
#include "xpm/bitcoin32.xpm"
#include "xpm/bitcoin48.xpm"
#include "xpm/bitcoin80.xpm"
#include "xpm/check.xpm"
#include "xpm/send16.xpm"
#include "xpm/send16noshadow.xpm"
#include "xpm/send20.xpm"
#include "xpm/about.xpm"
#endif

11
core/include/init.h

@ -0,0 +1,11 @@ @@ -0,0 +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

13
core/include/irc.h

@ -0,0 +1,13 @@ @@ -0,0 +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, std::string& strLine);
void ThreadIRCSeed(void* parg);
extern int nGotIRCAddresses;
extern bool fGotExternalIP;
#endif

18
core/include/key.h

@ -4,6 +4,10 @@ @@ -4,6 +4,10 @@
#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;
// const unsigned int PUBLIC_KEY_SIZE = 41;
@ -110,7 +114,7 @@ public: @@ -110,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()))
@ -119,19 +123,19 @@ public: @@ -119,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];
@ -143,7 +147,7 @@ public: @@ -143,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)
@ -151,7 +155,7 @@ public: @@ -151,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))
@ -159,7 +163,7 @@ public: @@ -159,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))

204
core/include/main.h

@ -7,6 +7,10 @@ @@ -7,6 +7,10 @@
#include "bignum.h"
#include "net.h"
#include "key.h"
#include "db.h"
#include "script.h"
#include <list>
class COutPoint;
class CInPoint;
@ -79,7 +83,7 @@ bool CheckDiskSpace(uint64 nAdditionalBytes=0); @@ -79,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);
@ -87,15 +91,15 @@ void ReacceptWalletTransactions(); @@ -87,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);
@ -105,7 +109,7 @@ bool CheckWork(CBlock* pblock, CReserveKey& reservekey); @@ -105,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);
@ -153,7 +157,7 @@ public: @@ -153,7 +157,7 @@ public:
return !(a == b);
}
string ToString() const
std::string ToString() const
{
if (IsNull())
return strprintf("null");
@ -212,7 +216,7 @@ public: @@ -212,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);
}
@ -281,9 +285,9 @@ public: @@ -281,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())
@ -359,14 +363,14 @@ public: @@ -359,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)))
@ -377,7 +381,7 @@ public: @@ -377,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);
}
@ -392,7 +396,7 @@ public: @@ -392,7 +396,7 @@ public:
return !(a == b);
}
string ToString() const
std::string ToString() const
{
if (scriptPubKey.size() < 6)
return "CTxOut(error)";
@ -416,8 +420,8 @@ class CTransaction @@ -416,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;
@ -464,7 +468,7 @@ public: @@ -464,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;
@ -507,19 +511,19 @@ public: @@ -507,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;
@ -527,7 +531,7 @@ public: @@ -527,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;
@ -541,11 +545,11 @@ public: @@ -541,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;
}
@ -553,11 +557,11 @@ public: @@ -553,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;
}
@ -567,11 +571,11 @@ public: @@ -567,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;
}
@ -579,11 +583,11 @@ public: @@ -579,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;
}
@ -621,7 +625,7 @@ public: @@ -621,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;
@ -674,9 +678,9 @@ public: @@ -674,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,
@ -700,7 +704,7 @@ public: @@ -700,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;
@ -727,7 +731,7 @@ class CMerkleTx : public CTransaction @@ -727,7 +731,7 @@ class CMerkleTx : public CTransaction
{
public:
uint256 hashBlock;
vector<uint256> vMerkleBranch;
std::vector<uint256> vMerkleBranch;
int nIndex;
// memory only
@ -782,14 +786,14 @@ public: @@ -782,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;
@ -856,8 +860,8 @@ public: @@ -856,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)
@ -880,7 +884,7 @@ public: @@ -880,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);
@ -893,7 +897,7 @@ public: @@ -893,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++)
@ -922,7 +926,7 @@ public: @@ -922,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])
{
@ -934,7 +938,7 @@ public: @@ -934,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]);
@ -982,7 +986,7 @@ public: @@ -982,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");
}
}
@ -1001,10 +1005,10 @@ public: @@ -1001,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
@ -1024,8 +1028,8 @@ public: @@ -1024,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++)
@ -1040,10 +1044,10 @@ public: @@ -1040,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;
@ -1083,7 +1087,7 @@ class CTxIndex @@ -1083,7 +1087,7 @@ class CTxIndex
{
public:
CDiskTxPos pos;
vector<CDiskTxPos> vSpent;
std::vector<CDiskTxPos> vSpent;
CTxIndex()
{
@ -1155,10 +1159,10 @@ public: @@ -1155,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()
@ -1213,7 +1217,7 @@ public: @@ -1213,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;
}
@ -1222,14 +1226,14 @@ public: @@ -1222,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])));
}
@ -1238,15 +1242,15 @@ public: @@ -1238,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;
@ -1254,11 +1258,11 @@ public: @@ -1254,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));
@ -1489,7 +1493,7 @@ public: @@ -1489,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];
}
@ -1507,7 +1511,7 @@ public: @@ -1507,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,
@ -1576,9 +1580,9 @@ public: @@ -1576,9 +1580,9 @@ public:
}
string ToString() const
std::string ToString() const
{
string str = "CDiskBlockIndex(";
std::string str = "CDiskBlockIndex(";
str += CBlockIndex::ToString();
str += strprintf