Skip to content

Commit

Permalink
make bitcoin include files more modular
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj authored and Witchspace committed May 15, 2011
1 parent c22feee commit 223b6f1
Show file tree
Hide file tree
Showing 25 changed files with 640 additions and 510 deletions.
45 changes: 26 additions & 19 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@
// - 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;
CBigNum bn0 = 0;

// 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;
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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())
Expand All @@ -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);
}
Expand All @@ -187,15 +192,17 @@ 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());
}




inline string PubKeyToAddress(const vector<unsigned char>& vchPubKey)
inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey)
{
return Hash160ToAddress(Hash160(vchPubKey));
}

#endif
12 changes: 7 additions & 5 deletions src/bignum.h
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -308,7 +308,7 @@ class CBigNum : public BIGNUM
CAutoBN_CTX pctx;
CBigNum bnBase = nBase;
CBigNum bn0 = 0;
string str;
std::string str;
CBigNum bn = *this;
BN_set_negative(&bn, false);
CBigNum dv;
Expand Down Expand Up @@ -348,7 +348,7 @@ class CBigNum : public BIGNUM
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);
}
Expand Down Expand Up @@ -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
11 changes: 7 additions & 4 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include "headers.h"

using namespace std;
using namespace boost;

void ThreadFlushWalletDB(void* parg);


Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 223b6f1

Please sign in to comment.