Skip to content

Commit

Permalink
In order to handle bitcoin#9 a better separation of uint160/Address a…
Browse files Browse the repository at this point in the history
…nd BitcoinAddress/ChainAddress has been introduced. The CBitcoinAddress class has been redesigned and made chain agnostic. It is now called ChainAddress. Further uint160 has a typedef of Address making it the chain agnostic address. The change from CBitcoinAddress to ChainAddress touched on a great number of files - most of the changes are trivial though... Only non-trivial comments are mentioned below. This commit also closes bitcoin#9, as we don't require the chain id anymore.

base58: CBase58 and CBitcoinAddress merged into ChainAddress. typedef of Address = uint160
uint256: added const versions of begin / end
bitcoind: fixed issue with wrong datadir in e.g. testnet.
BlockChain: removed some out-commented code
wallet: removed the out-commented dispatch functions
  • Loading branch information
Michael Gronager authored and Michael Gronager committed Jan 20, 2012
1 parent 6f47e4e commit 0887f48
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 542 deletions.
12 changes: 9 additions & 3 deletions applications/bitcoind/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char* argv[])
("help,?", "Show help messages")
("version,v", "print version string")
("conf,c", value<string>(&config_file)->default_value("bitcoin.conf"), "Specify configuration file")
("datadir", value<string>(&data_dir)->default_value(CDB::dataDir(bitcoin.dataDirSuffix())), "Specify data directory")
("datadir", "Specify non default data directory")
;

options_description config("Config options");
Expand Down Expand Up @@ -173,7 +173,7 @@ int main(int argc, char* argv[])
options_description config_file_options;
config_file_options.add(config);

options_description visible("Allowed options");
options_description visible;
visible.add(generic).add(config);

positional_options_description pos;
Expand All @@ -196,6 +196,9 @@ int main(int argc, char* argv[])
cout << argv[0] << " version is: " << FormatFullVersion() << "\n";
return 1;
}

if(!args.count("datadir"))
data_dir = CDB::dataDir(bitcoin.dataDirSuffix());

// if present, parse the config file - if no data dir is specified we always assume bitcoin chain at this stage
string config_path = data_dir + "/" + config_file;
Expand Down Expand Up @@ -235,14 +238,17 @@ int main(int argc, char* argv[])
}

// Else we start the bitcoin node and server!

const Chain* chain_chooser;
if(args.count("testnet"))
chain_chooser = &testnet;
else
chain_chooser = &bitcoin;
const Chain& chain(*chain_chooser);

if(!args.count("datadir"))
data_dir = CDB::dataDir(chain.dataDirSuffix());

logfile = data_dir + "/debug.log";

Node node(chain, data_dir, args.count("nolisten") ? "" : "0.0.0.0"); // it is also here we specify the use of a proxy!
Expand Down
60 changes: 30 additions & 30 deletions applications/btc/btc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class CRPCAssetSyncronizer : public CAssetSyncronizer
virtual void getCreditCoins(uint160 btc, Coins& coins)
{
Array p;
p.push_back(CBitcoinAddress(btc).toString());
p.push_back(ChainAddress(btc).toString());

Object reply = callRPC("getcredit", p);

Expand All @@ -122,7 +122,7 @@ class CRPCAssetSyncronizer : public CAssetSyncronizer
virtual void getDebitCoins(uint160 btc, Coins& coins)
{
Array p;
p.push_back(CBitcoinAddress(btc).toString());
p.push_back(ChainAddress(btc).toString());

Object reply = callRPC("getdebit", p);

Expand All @@ -135,7 +135,7 @@ class CRPCAssetSyncronizer : public CAssetSyncronizer
virtual void getCoins(uint160 btc, Coins& coins)
{
Array p;
p.push_back(CBitcoinAddress(btc).toString());
p.push_back(ChainAddress(btc).toString());

Object reply = callRPC("getcoins", p);

Expand Down Expand Up @@ -231,7 +231,7 @@ uint160 getNewAddress(string asset_name)
std::vector<unsigned char> newKey;
if (!pwalletMain->GetKeyFromPool(newKey, false)) // Error: Keypool ran out, please call keypoolrefill first
return 0;
CBitcoinAddress address(newKey);
ChainAddress address(newKey);

pwalletMain->SetAddressBookName(address, asset_name);

Expand Down Expand Up @@ -269,16 +269,16 @@ uint160 getAccountAddress(string asset_name, bool bForceNew=false)
if (!pwalletMain->GetKeyFromPool(account.vchPubKey, false)) // Error: Keypool ran out, please call keypoolrefill first
return 0;

pwalletMain->SetAddressBookName(CBitcoinAddress(account.vchPubKey), asset_name);
pwalletMain->SetAddressBookName(ChainAddress(account.vchPubKey), asset_name);
walletdb.WriteAccount(asset_name, account);
}

return CBitcoinAddress(account.vchPubKey).GetHash160();
return ChainAddress(account.vchPubKey).GetHash160();
}

void setAccount(string btcaddr, string asset_name)
{
CBitcoinAddress address(btcaddr);
ChainAddress address(btcaddr);
if (!address.IsValid())
return; // Invalid bitcoin address

Expand All @@ -295,12 +295,12 @@ void setAccount(string btcaddr, string asset_name)

string getAccount(string btcaddr)
{
CBitcoinAddress address(btcaddr);
ChainAddress address(btcaddr);
if (!address.IsValid())
return ""; // Invalid bitcoin address

string asset_name;
map<CBitcoinAddress, string>::iterator mi = pwalletMain->mapAddressBook.find(address);
map<ChainAddress, string>::iterator mi = pwalletMain->mapAddressBook.find(address);
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
asset_name = (*mi).second;
return asset_name;
Expand All @@ -326,7 +326,7 @@ Value getrecvbyaddress(const Array& params, bool fHelp)
"Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.");

// Bitcoin address
CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
ChainAddress address = ChainAddress(params[0].get_str());
CScript scriptPubKey;
if (!address.IsValid())
throw JSONRPCError(-5, "Invalid bitcoin address");
Expand Down Expand Up @@ -369,11 +369,11 @@ Value getrecvbyaddress(const Array& params, bool fHelp)
return sum;
}

void getAccountAddresses(string strAccount, set<CBitcoinAddress>& setAddress)
void getAccountAddresses(string strAccount, set<ChainAddress>& setAddress)
{
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const ChainAddress& address = item.first;
const string& strName = item.second;
if (strName == strAccount)
setAddress.insert(address);
Expand All @@ -395,12 +395,12 @@ Value getrecvbyaccount(const Array& params, bool fHelp)

// Get the set of pub keys that have the label
string account = AccountFromValue(params[0]);
set<CBitcoinAddress> addresses;
set<ChainAddress> addresses;
getAccountAddresses(account, addresses);

// now we need to call the server... - first create the input
Array txlists;
for(set<CBitcoinAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
for(set<ChainAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
{
Array p;
p.push_back(it->toString());
Expand Down Expand Up @@ -448,14 +448,14 @@ Value getaccountbalance(const Array& params, bool fHelp)
if (params.size() > 1)
nMinDepth = params[1].get_int();

set<CBitcoinAddress> addresses;
set<ChainAddress> addresses;
if (params.size() == 0 || params[0].get_str() == "*") {
// Calculate total balance a different way from GetBalance()
// (GetBalance() sums up all unspent TxOuts)
// getbalance and getbalance '*' should always return the same number.
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const ChainAddress& address = item.first;
CScript scriptPubKey;
scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey))
Expand All @@ -467,9 +467,9 @@ Value getaccountbalance(const Array& params, bool fHelp)
else
{
string account = params[0].get_str();
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const ChainAddress& address = item.first;
CScript scriptPubKey;
scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey))
Expand All @@ -483,7 +483,7 @@ Value getaccountbalance(const Array& params, bool fHelp)

// trying to use the CAssetSyncronizer and CAsset instead...
CAsset asset;
for(set<CBitcoinAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
for(set<ChainAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
asset.addAddress(it->GetHash160());

CRPCAssetSyncronizer rpc;
Expand All @@ -494,7 +494,7 @@ Value getaccountbalance(const Array& params, bool fHelp)
return balance*1.0/COIN;

int64 sum = 0;
for(set<CBitcoinAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
for(set<ChainAddress>::iterator it = addresses.begin(); it!= addresses.end(); ++it)
{
Array p;
p.push_back(it->toString());
Expand All @@ -520,7 +520,7 @@ Value listaccnts(const Array& params, bool fHelp)
nMinDepth = params[0].get_int();

map<string, int64> mapAccountBalances;
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& entry, pwalletMain->mapAddressBook) {
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& entry, pwalletMain->mapAddressBook) {
if (pwalletMain->HaveKey(entry.first)) // This address belongs to me
{
Array p;
Expand Down Expand Up @@ -588,9 +588,9 @@ Value getprivatekeys(const Array& params, bool fHelp)
"requires wallet passphrase to be set with walletpassphrase first");
Array result;

BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const ChainAddress& address = item.first;
CScript scriptPubKey;
scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey))
Expand Down Expand Up @@ -626,7 +626,7 @@ Value sendtoaddr(const Array& params, bool fHelp)
"sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\n"
"<amount> is a real and is rounded to the nearest 0.00000001");

CBitcoinAddress to_address(params[0].get_str());
ChainAddress to_address(params[0].get_str());
if (!to_address.IsValid())
throw JSONRPCError(-5, "Invalid bitcoin address");

Expand All @@ -638,7 +638,7 @@ Value sendtoaddr(const Array& params, bool fHelp)

// first we need to load an asset with the right addresses - my addresses, from the wallet, further, we need to ensure that the addresses are all loaded with a private key (making them spendable)

set<CBitcoinAddress> addresses;
set<ChainAddress> addresses;
string account;
// if (params.size() == 0 || params[0].get_str() == "*")
account.clear();
Expand All @@ -647,9 +647,9 @@ Value sendtoaddr(const Array& params, bool fHelp)

CAsset asset;

BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
BOOST_FOREACH(const PAIRTYPE(ChainAddress, string)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const ChainAddress& address = item.first;
CScript scriptPubKey;
scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey))
Expand All @@ -670,7 +670,7 @@ Value sendtoaddr(const Array& params, bool fHelp)
Transaction tx;
set<CAsset::Payment> payments;
payments.insert(CAsset::Payment(to_address.GetHash160(), amount));
CBitcoinAddress btcbroker("19bvWMvxddxbDrrN6kXZxqhZsApfVFDxB6");
ChainAddress btcbroker("19bvWMvxddxbDrrN6kXZxqhZsApfVFDxB6");
payments.insert(CAsset::Payment(btcbroker.GetHash160(), amount/10)); // 10% in brokerfee!
// tx = asset.generateTx(to_address.GetHash160(), amount);
tx = asset.generateTx(payments);
Expand Down
14 changes: 7 additions & 7 deletions applications/btcd/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Value getvalue(const Array& params, bool fHelp)
"getvalue <btcaddr>\n"
"Get value of <btcaddr>");
uint160 hash160 = CBitcoinAddress(params[0].get_str()).GetHash160();
uint160 hash160 = ChainAddress(params[0].get_str()).GetHash160();
CAsset asset;
asset.addAddress(hash160);
Expand All @@ -195,7 +195,7 @@ Value checkvalue(const Array& params, bool fHelp)
"check the value of a bitcoin address, <within> seconds, get confirmations of specific <amount> or get a true/false of a minimum confirmation level");
// parse the input
CBitcoinAddress addr = CBitcoinAddress(params[0].get_str());
ChainAddress addr = ChainAddress(params[0].get_str());
if(!addr.IsValid())
throw runtime_error("getdebit <btcaddr>\n"
"btcaddr invalid!");
Expand Down Expand Up @@ -271,12 +271,12 @@ Value GetDebit::operator()(const Array& params, bool fHelp) {
throw runtime_error(
"getdebit <btcaddr>\n"
"Get debit coins of <btcaddr>");
CBitcoinAddress addr = CBitcoinAddress(params[0].get_str());
ChainAddress addr = ChainAddress(params[0].get_str());
if(!addr.IsValid(_blockChain.chain().networkId()))
throw runtime_error("getdebit <btcaddr>\n"
"btcaddr invalid!");

uint160 hash160 = CBitcoinAddress(params[0].get_str()).GetHash160();
uint160 hash160 = ChainAddress(params[0].get_str()).GetHash160();

set<Coin> debit;

Expand All @@ -302,7 +302,7 @@ Value GetCredit::operator()(const Array& params, bool fHelp) {
throw runtime_error("getcredit <btcaddr>\n"
"Get credit coins of <btcaddr>");

CBitcoinAddress addr = CBitcoinAddress(params[0].get_str());
ChainAddress addr = ChainAddress(params[0].get_str());
if(!addr.IsValid(_blockChain.chain().networkId()))
throw runtime_error("getcredit <btcaddr>\n"
"btcaddr invalid!");
Expand Down Expand Up @@ -334,7 +334,7 @@ Value getcoins(const Array& params, bool fHelp)
"getcoins <btcaddr>\n"
"Get non spend coins of <btcaddr>");
uint160 hash160 = CBitcoinAddress(params[0].get_str()).GetHash160();
uint160 hash160 = ChainAddress(params[0].get_str()).GetHash160();
CAsset asset;
asset.addAddress(hash160);
Expand Down Expand Up @@ -479,7 +479,7 @@ Value posttx(const Array& params, bool fHelp)
}
// check that the transaction contains a fee to me, the broker
uint160 btcbroker = CBitcoinAddress(BTCBROKER).GetHash160();
uint160 btcbroker = ChainAddress(BTCBROKER).GetHash160();
if(tx.paymentTo(btcbroker) < tx.GetMinFee())
{
ostringstream err;
Expand Down
6 changes: 3 additions & 3 deletions include/btc/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class CAsset : public CKeyStore

virtual bool AddKey(const CKey& key);

virtual bool HaveKey(const CBitcoinAddress &address) const;
virtual bool HaveKey(const ChainAddress &address) const;

virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
virtual bool GetKey(const ChainAddress &address, CKey& keyOut) const;

// we might need to override these as well... Depending how well we want to support having only keys with pub/160 part
// virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
// virtual bool GetPubKey(const ChainAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
// virtual std::vector<unsigned char> GenerateNewKey();

const Transaction& getTx(uint256 hash) const;
Expand Down

0 comments on commit 0887f48

Please sign in to comment.