Skip to content

Commit

Permalink
Merge remote branch 'refs/remotes/svn/trunk' into svn
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinandresen committed Dec 16, 2010
2 parents bc530fe + 809ee79 commit 55c43da
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 153 deletions.
16 changes: 9 additions & 7 deletions db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
return Write(make_pair(string("acc"), strAccount), account);
}

bool CWalletDB::WriteAccountingEntry(const string& strAccount, const CAccountingEntry& acentry)
bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
{
return Write(make_tuple(string("acentry"), strAccount, ++nAccountingEntryNumber), acentry);
return Write(make_tuple(string("acentry"), acentry.strAccount, ++nAccountingEntryNumber), acentry);
}

int64 CWalletDB::GetAccountCreditDebit(const string& strAccount)
Expand All @@ -613,6 +613,8 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountin
{
int64 nCreditDebit = 0;

bool fAllAccounts = (strAccount == "*");

Dbc* pcursor = GetCursor();
if (!pcursor)
throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
Expand All @@ -622,7 +624,7 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountin
// Read next record
CDataStream ssKey;
if (fFlags == DB_SET_RANGE)
ssKey << make_tuple(string("acentry"), strAccount, uint64(0));
ssKey << make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64(0));
CDataStream ssValue;
int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
fFlags = DB_NEXT;
Expand All @@ -639,19 +641,19 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountin
ssKey >> strType;
if (strType != "acentry")
break;
string strAccountName;
ssKey >> strAccountName;
if (strAccountName != strAccount)
CAccountingEntry acentry;
ssKey >> acentry.strAccount;
if (!fAllAccounts && acentry.strAccount != strAccount)
break;

CAccountingEntry acentry;
ssValue >> acentry;
entries.push_back(acentry);
}

pcursor->close();
}


bool CWalletDB::LoadWallet()
{
vchDefaultKey.clear();
Expand Down
2 changes: 1 addition & 1 deletion db.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class CWalletDB : public CDB

bool ReadAccount(const string& strAccount, CAccount& account);
bool WriteAccount(const string& strAccount, const CAccount& account);
bool WriteAccountingEntry(const string& strAccount, const CAccountingEntry& acentry);
bool WriteAccountingEntry(const CAccountingEntry& acentry);
int64 GetAccountCreditDebit(const string& strAccount);
void ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& acentries);

Expand Down
10 changes: 7 additions & 3 deletions irc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "headers.h"

int nGotIRCAddresses = 0;
bool fGotExternalIP = false;

void ThreadIRCSeed2(void* parg);

Expand Down Expand Up @@ -223,6 +224,8 @@ bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet)
}
else
{
// Hybrid IRC used by lfnet always returns IP when you userhost yourself,
// but in case another IRC is ever used this should work.
printf("GetIPFromIRC() got userhost %s\n", strHost.c_str());
if (fUseProxy)
return false;
Expand Down Expand Up @@ -327,14 +330,15 @@ void ThreadIRCSeed2(void* parg)
}
Sleep(500);

// Get my external IP from IRC server
// Get our external IP from the IRC server and re-nick before joining the channel
CAddress addrFromIRC;
if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip))
{
// Just using it as a backup for now
printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str());
if (addrFromIRC.IsRoutable() && !fUseProxy && !addrLocalHost.IsRoutable())
if (!fUseProxy && addrFromIRC.IsRoutable())
{
// IRC lets you to re-nick
fGotExternalIP = true;
addrLocalHost.ip = addrFromIRC.ip;
strMyName = EncodeAddress(addrLocalHost);
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
Expand Down
1 change: 1 addition & 0 deletions irc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bool RecvLine(SOCKET hSocket, string& strLine);
void ThreadIRCSeed(void* parg);

extern int nGotIRCAddresses;
extern bool fGotExternalIP;
57 changes: 57 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,63 @@ int CWalletTx::GetRequestCount() const
return nRequests;
}

void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listReceived,
int64& nSent, int64& nFee, string& strSentAccount) const
{
nGenerated = nSent = nFee = 0;

if (IsCoinBase())
{
if (GetBlocksToMaturity() == 0)
nGenerated = GetCredit();
return;
}

// 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)
{
vector<unsigned char> vchPubKey;
if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
listReceived.push_back(make_pair(PubKeyToAddress(vchPubKey), txout.nValue));
}

// Sent
int64 nDebit = GetDebit();
if (nDebit > 0)
{
int64 nValueOut = GetValueOut();
nFee = nDebit - nValueOut;
nSent = nValueOut - GetChange();
strSentAccount = strFromAccount;
}
}

void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
int64& nSent, int64& nFee) const
{
nGenerated = nReceived = nSent = nFee = 0;

int64 allGenerated, allSent, allFee;
allGenerated = allSent = allFee = 0;
string strSentAccount;
list<pair<string, int64> > listReceived;
GetAmounts(allGenerated, listReceived, allSent, allFee, strSentAccount);

if (strAccount == "")
nGenerated = allGenerated;
if (strAccount == strSentAccount)
{
nSent = allSent;
nFee = allFee;
}
CRITICAL_BLOCK(cs_mapAddressBook)
{
foreach(const PAIRTYPE(string,int64)& r, listReceived)
if (mapAddressBook.count(r.first) && mapAddressBook[r.first] == strAccount)
nReceived += r.second;
}
}



Expand Down
38 changes: 9 additions & 29 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,36 +873,11 @@ class CWalletTx : public CMerkleTx
return nChangeCached;
}

void GetAccountAmounts(string strAccount, const set<CScript>& setPubKey,
int64& nGenerated, int64& nReceived, int64& nSent, int64& nFee) const
{
nGenerated = nReceived = nSent = nFee = 0;

// Generated blocks count to account ""
if (IsCoinBase())
{
if (strAccount == "" && GetBlocksToMaturity() == 0)
nGenerated = GetCredit();
return;
}

// Received
foreach(const CTxOut& txout, vout)
if (setPubKey.count(txout.scriptPubKey))
nReceived += txout.nValue;
void GetAmounts(int64& nGenerated, list<pair<string /* address */, int64> >& listReceived,
int64& nSent, int64& nFee, string& strSentAccount) const;

// Sent
if (strFromAccount == strAccount)
{
int64 nDebit = GetDebit();
if (nDebit > 0)
{
int64 nValueOut = GetValueOut();
nFee = nDebit - nValueOut;
nSent = nValueOut - GetChange();
}
}
}
void GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
int64& nSent, int64& nFee) const;

bool IsFromMe() const
{
Expand Down Expand Up @@ -1695,6 +1670,7 @@ class CAccount
class CAccountingEntry
{
public:
string strAccount;
int64 nCreditDebit;
int64 nTime;
string strOtherAccount;
Expand All @@ -1709,6 +1685,7 @@ class CAccountingEntry
{
nCreditDebit = 0;
nTime = 0;
strAccount.clear();
strOtherAccount.clear();
strComment.clear();
}
Expand All @@ -1717,6 +1694,7 @@ class CAccountingEntry
(
if (!(nType & SER_GETHASH))
READWRITE(nVersion);
// Note: strAccount is serialized as part of the key, not here.
READWRITE(nCreditDebit);
READWRITE(nTime);
READWRITE(strOtherAccount);
Expand All @@ -1733,6 +1711,8 @@ class CAccountingEntry


//
// Alerts are for notifying old versions if they become too obsolete and
// need to upgrade. The message is displayed in the status bar.
// Alert messages are broadcast as a vector of signed data. Unserializing may
// not read the entire buffer if the alert is for a newer version, but older
// versions can still relay the original data.
Expand Down
49 changes: 37 additions & 12 deletions net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const cha
return error("GetMyExternalIP() : connection closed");
}


// We now get our external IP from the IRC server first and only use this as a backup
bool GetMyExternalIP(unsigned int& ipRet)
{
CAddress addrConnect;
Expand All @@ -176,6 +176,10 @@ bool GetMyExternalIP(unsigned int& ipRet)
for (int nLookup = 0; nLookup <= 1; nLookup++)
for (int nHost = 1; nHost <= 2; nHost++)
{
// We should be phasing out our use of sites like these. If we need
// replacements, we should ask for volunteers to put this simple
// php file on their webserver that prints the client IP:
// <?php echo $_SERVER["REMOTE_ADDR"]; ?>
if (nHost == 1)
{
addrConnect = CAddress("91.198.22.70:80"); // checkip.dyndns.org
Expand Down Expand Up @@ -222,6 +226,36 @@ bool GetMyExternalIP(unsigned int& ipRet)
return false;
}

void ThreadGetMyExternalIP(void* parg)
{
// Wait for IRC to get it first
if (!GetBoolArg("-noirc"))
{
for (int i = 0; i < 2 * 60; i++)
{
Sleep(1000);
if (fGotExternalIP || fShutdown)
return;
}
}

// Fallback in case IRC fails to get it
if (GetMyExternalIP(addrLocalHost.ip))
{
printf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP().c_str());
if (addrLocalHost.IsRoutable())
{
// If we already connected to a few before we had our IP, go back and addr them.
// setAddrKnown automatically filters any duplicate sends.
CAddress addr(addrLocalHost);
addr.nTime = GetAdjustedTime();
CRITICAL_BLOCK(cs_vNodes)
foreach(CNode* pnode, vNodes)
pnode->PushAddress(addr);
}
}
}




Expand Down Expand Up @@ -1310,24 +1344,15 @@ void StartNode(void* parg)
#endif
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());

// Get our external IP address for incoming connections
if (fUseProxy)
if (fUseProxy || mapArgs.count("-connect"))
{
// Proxies can't take incoming connections
addrLocalHost.ip = CAddress("0.0.0.0").ip;
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
}
else
{
if (addrIncoming.IsValid())
addrLocalHost.ip = addrIncoming.ip;

if (GetMyExternalIP(addrLocalHost.ip))
{
addrIncoming = addrLocalHost;
CWalletDB().WriteSetting("addrIncoming", addrIncoming);
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
}
CreateThread(ThreadGetMyExternalIP, NULL);
}

//
Expand Down
Loading

0 comments on commit 55c43da

Please sign in to comment.