Fix issue #659, and cleanup wallet/command-line argument handling a bit

This commit is contained in:
Gavin Andresen 2012-01-03 10:14:22 -05:00
parent 4231eb217c
commit 0fcf91ea1e
5 changed files with 84 additions and 43 deletions

View File

@ -194,6 +194,7 @@ bool AppInit2(int argc, char* argv[])
" -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" + " -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" +
" -addnode=<ip> \t " + _("Add a node to connect to") + "\n" + " -addnode=<ip> \t " + _("Add a node to connect to") + "\n" +
" -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" + " -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" +
" -noirc \t " + _("Don't find peers using internet relay chat") + "\n" +
" -nolisten \t " + _("Don't accept connections from outside") + "\n" + " -nolisten \t " + _("Don't accept connections from outside") + "\n" +
" -nodnsseed \t " + _("Don't bootstrap list of peers using DNS") + "\n" + " -nodnsseed \t " + _("Don't bootstrap list of peers using DNS") + "\n" +
" -banscore=<n> \t " + _("Threshold for disconnecting misbehaving peers (default: 100)") + "\n" + " -banscore=<n> \t " + _("Threshold for disconnecting misbehaving peers (default: 100)") + "\n" +
@ -248,7 +249,6 @@ bool AppInit2(int argc, char* argv[])
} }
fDebug = GetBoolArg("-debug"); fDebug = GetBoolArg("-debug");
fAllowDNS = GetBoolArg("-dns");
#ifndef WIN32 #ifndef WIN32
fDaemon = GetBoolArg("-daemon"); fDaemon = GetBoolArg("-daemon");
@ -267,10 +267,6 @@ bool AppInit2(int argc, char* argv[])
#endif #endif
fPrintToConsole = GetBoolArg("-printtoconsole"); fPrintToConsole = GetBoolArg("-printtoconsole");
fPrintToDebugger = GetBoolArg("-printtodebugger"); fPrintToDebugger = GetBoolArg("-printtodebugger");
fTestNet = GetBoolArg("-testnet");
bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
fNoListen = GetBoolArg("-nolisten") || fTOR;
fLogTimestamps = GetBoolArg("-logtimestamps"); fLogTimestamps = GetBoolArg("-logtimestamps");
#ifndef QT_GUI #ifndef QT_GUI
@ -332,17 +328,6 @@ bool AppInit2(int argc, char* argv[])
return false; return false;
} }
// Bind to the port early so we can tell if another instance is already running.
if (!fNoListen)
{
std::string strError;
if (!BindListenPort(strError))
{
wxMessageBox(strError, "Bitcoin");
return false;
}
}
std::ostringstream strErrors; std::ostringstream strErrors;
// //
// Load data files // Load data files
@ -428,6 +413,10 @@ bool AppInit2(int argc, char* argv[])
// Add wallet transactions that aren't already in a block to mapTransactions // Add wallet transactions that aren't already in a block to mapTransactions
pwalletMain->ReacceptWalletTransactions(); pwalletMain->ReacceptWalletTransactions();
// Note: Bitcoin-QT stores several settings in the wallet, so we want
// to load the wallet BEFORE parsing command-line arguments, so
// the command-line/bitcoin.conf settings override GUI setting.
// //
// Parameters // Parameters
// //
@ -480,6 +469,44 @@ bool AppInit2(int argc, char* argv[])
} }
} }
fTestNet = GetBoolArg("-testnet");
bool fTor = (fUseProxy && addrProxy.port == htons(9050));
if (fTor)
{
// Use SoftSetArg here so user can override any of these if they wish.
// Note: the GetBoolArg() calls for all of these must happen later.
SoftSetArg("-nolisten", true);
SoftSetArg("-noirc", true);
SoftSetArg("-nodnsseed", true);
SoftSetArg("-noupnp", true);
SoftSetArg("-upnp", false);
SoftSetArg("-dns", false);
}
fAllowDNS = GetBoolArg("-dns");
fNoListen = GetBoolArg("-nolisten");
if (fHaveUPnP)
{
#if USE_UPNP
if (GetBoolArg("-noupnp"))
fUseUPnP = false;
#else
if (GetBoolArg("-upnp"))
fUseUPnP = true;
#endif
}
if (!fNoListen)
{
std::string strError;
if (!BindListenPort(strError))
{
wxMessageBox(strError, "Bitcoin");
return false;
}
}
if (mapArgs.count("-addnode")) if (mapArgs.count("-addnode"))
{ {
BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"]) BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
@ -502,17 +529,6 @@ bool AppInit2(int argc, char* argv[])
wxMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), "Bitcoin", wxOK | wxICON_EXCLAMATION); wxMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), "Bitcoin", wxOK | wxICON_EXCLAMATION);
} }
if (fHaveUPnP)
{
#if USE_UPNP
if (GetBoolArg("-noupnp"))
fUseUPnP = false;
#else
if (GetBoolArg("-upnp"))
fUseUPnP = true;
#endif
}
// //
// Start the node // Start the node
// //

View File

@ -264,19 +264,14 @@ void ThreadIRCSeed2(void* parg)
int nErrorWait = 10; int nErrorWait = 10;
int nRetryWait = 10; int nRetryWait = 10;
bool fNameInUse = false; bool fNameInUse = false;
bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
while (!fShutdown) while (!fShutdown)
{ {
//CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org
if (!fTOR)
{
//struct hostent* phostent = gethostbyname("chat.freenode.net");
CAddress addrIRC("irc.lfnet.org", 6667, true); CAddress addrIRC("irc.lfnet.org", 6667, true);
if (addrIRC.IsValid()) if (addrIRC.IsValid())
addrConnect = addrIRC; addrConnect = addrIRC;
}
SOCKET hSocket; SOCKET hSocket;
if (!ConnectSocket(addrConnect, hSocket)) if (!ConnectSocket(addrConnect, hSocket))
@ -406,10 +401,6 @@ void ThreadIRCSeed2(void* parg)
closesocket(hSocket); closesocket(hSocket);
hSocket = INVALID_SOCKET; hSocket = INVALID_SOCKET;
// IRC usually blocks TOR, so only try once
if (fTOR)
return;
if (GetTime() - nStart > 20 * 60) if (GetTime() - nStart > 20 * 60)
{ {
nErrorWait /= 3; nErrorWait /= 3;

View File

@ -1471,7 +1471,7 @@ void ThreadOpenConnections2(void* parg)
{ {
// Add seed nodes if IRC isn't working // Add seed nodes if IRC isn't working
bool fTOR = (fUseProxy && addrProxy.port == htons(9050)); bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
if (mapAddresses.empty() && (GetTime() - nStart > 60 || fTOR) && !fTestNet) if (mapAddresses.empty() && (GetTime() - nStart > 60 || fUseProxy) && !fTestNet)
fAddSeeds = true; fAddSeeds = true;
} }

View File

@ -470,6 +470,23 @@ void ParseParameters(int argc, char* argv[])
} }
} }
bool SoftSetArg(const std::string& strArg, const std::string& strValue)
{
if (mapArgs.count(strArg))
return false;
mapArgs[strArg] = strValue;
return true;
}
bool SoftSetArg(const std::string& strArg, bool fValue)
{
if (fValue)
return SoftSetArg(strArg, std::string("1"));
else
return SoftSetArg(strArg, std::string("0"));
}
string EncodeBase64(const unsigned char* pch, size_t len) string EncodeBase64(const unsigned char* pch, size_t len)
{ {
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

View File

@ -439,7 +439,7 @@ inline int64 GetArg(const std::string& strArg, int64 nDefault)
return nDefault; return nDefault;
} }
inline bool GetBoolArg(const std::string& strArg) inline bool GetBoolArg(const std::string& strArg, bool fDefault=false)
{ {
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
{ {
@ -447,9 +447,26 @@ inline bool GetBoolArg(const std::string& strArg)
return true; return true;
return (atoi(mapArgs[strArg]) != 0); return (atoi(mapArgs[strArg]) != 0);
} }
return false; return fDefault;
} }
/**
* Set an argument if it doesn't already have a value
*
* @param strArg Argument to set (e.g. "-foo")
* @param strValue Value (e.g. "1")
* @return true if argument gets set, false if it already had a value
*/
bool SoftSetArg(const std::string& strArg, const std::string& strValue);
/**
* Set a boolean argument if it doesn't already have a value
*
* @param strArg Argument to set (e.g. "-foo")
* @param fValue Value (e.g. false)
* @return true if argument gets set, false if it already had a value
*/
bool SoftSetArg(const std::string& strArg, bool fValue);