aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/init.cpp70
-rw-r--r--src/irc.cpp17
-rw-r--r--src/main.cpp21
-rw-r--r--src/net.cpp61
-rw-r--r--src/script.cpp2
-rw-r--r--src/test/script_op_eval_tests.cpp72
-rw-r--r--src/util.cpp17
-rw-r--r--src/util.h25
8 files changed, 189 insertions, 96 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 1e389b226b..0eb5b529f1 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -194,6 +194,7 @@ bool AppInit2(int argc, char* argv[])
" -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" +
" -addnode=<ip> \t " + _("Add a node to connect to") + "\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" +
" -nodnsseed \t " + _("Don't bootstrap list of peers using DNS") + "\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");
- fAllowDNS = GetBoolArg("-dns");
#ifndef WIN32
fDaemon = GetBoolArg("-daemon");
@@ -267,10 +267,6 @@ bool AppInit2(int argc, char* argv[])
#endif
fPrintToConsole = GetBoolArg("-printtoconsole");
fPrintToDebugger = GetBoolArg("-printtodebugger");
-
- fTestNet = GetBoolArg("-testnet");
- bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
- fNoListen = GetBoolArg("-nolisten") || fTOR;
fLogTimestamps = GetBoolArg("-logtimestamps");
#ifndef QT_GUI
@@ -332,17 +328,6 @@ bool AppInit2(int argc, char* argv[])
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;
//
// 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
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
//
@@ -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"))
{
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);
}
- if (fHaveUPnP)
- {
-#if USE_UPNP
- if (GetBoolArg("-noupnp"))
- fUseUPnP = false;
-#else
- if (GetBoolArg("-upnp"))
- fUseUPnP = true;
-#endif
- }
-
//
// Start the node
//
diff --git a/src/irc.cpp b/src/irc.cpp
index 5278488dcd..fe96a90a1c 100644
--- a/src/irc.cpp
+++ b/src/irc.cpp
@@ -264,19 +264,14 @@ void ThreadIRCSeed2(void* parg)
int nErrorWait = 10;
int nRetryWait = 10;
bool fNameInUse = false;
- bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
while (!fShutdown)
{
- //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
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);
- if (addrIRC.IsValid())
- addrConnect = addrIRC;
- }
+
+ CAddress addrIRC("irc.lfnet.org", 6667, true);
+ if (addrIRC.IsValid())
+ addrConnect = addrIRC;
SOCKET hSocket;
if (!ConnectSocket(addrConnect, hSocket))
@@ -406,10 +401,6 @@ void ThreadIRCSeed2(void* parg)
closesocket(hSocket);
hSocket = INVALID_SOCKET;
- // IRC usually blocks TOR, so only try once
- if (fTOR)
- return;
-
if (GetTime() - nStart > 20 * 60)
{
nErrorWait /= 3;
diff --git a/src/main.cpp b/src/main.cpp
index bef76f7374..15f047bf7b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -35,7 +35,6 @@ map<COutPoint, CInPoint> mapNextTx;
map<uint256, CBlockIndex*> mapBlockIndex;
uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
static CBigNum bnProofOfWorkLimit(~uint256(0) >> 32);
-const int nInitialBlockThreshold = 120; // Regard blocks up until N-threshold as "initial download"
CBlockIndex* pindexGenesisBlock = NULL;
int nBestHeight = -1;
CBigNum bnBestChainWork = 0;
@@ -285,6 +284,7 @@ bool CTransaction::AreInputsStandard(std::map<uint256, std::pair<CTxIndex, CTran
COutPoint prevout = vin[i].prevout;
assert(mapInputs.count(prevout.hash) > 0);
CTransaction& txPrev = mapInputs[prevout.hash].second;
+ assert(prevout.n < txPrev.vout.size());
vector<vector<unsigned char> > vSolutions;
txnouttype whichType;
@@ -826,7 +826,7 @@ int GetNumBlocksOfPeers()
bool IsInitialBlockDownload()
{
- if (pindexBest == NULL || nBestHeight < (Checkpoints::GetTotalBlocksEstimate()-nInitialBlockThreshold))
+ if (pindexBest == NULL || nBestHeight < Checkpoints::GetTotalBlocksEstimate())
return true;
static int64 nLastUpdate;
static CBlockIndex* pindexLastBest;
@@ -946,6 +946,17 @@ bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTes
return error("FetchInputs() : %s ReadFromDisk prev tx %s failed", GetHash().ToString().substr(0,10).c_str(), prevout.hash.ToString().substr(0,10).c_str());
}
}
+
+ // Make sure all prevout.n's are valid:
+ for (int i = 0; i < vin.size(); i++)
+ {
+ const COutPoint prevout = vin[i].prevout;
+ const CTxIndex& txindex = inputsRet[prevout.hash].first;
+ const CTransaction& txPrev = inputsRet[prevout.hash].second;
+ if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
+ return DoS(100, error("FetchInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
+ }
+
return true;
}
@@ -976,10 +987,10 @@ bool CTransaction::ConnectInputs(map<uint256, pair<CTxIndex, CTransaction> > inp
if (pindex->nBlockPos == txindex.pos.nBlockPos && pindex->nFile == txindex.pos.nFile)
return error("ConnectInputs() : tried to spend coinbase at depth %d", pindexBlock->nHeight - pindex->nHeight);
- // Skip ECDSA signature verification when connecting blocks (fBlock=true) during initial download
- // (before the last blockchain checkpoint). This is safe because block merkle hashes are
+ // Skip ECDSA signature verification when connecting blocks (fBlock=true)
+ // before the last blockchain checkpoint. This is safe because block merkle hashes are
// still computed and checked, and any change will be caught at the next checkpoint.
- if (!(fBlock && IsInitialBlockDownload()))
+ if (!(fBlock && (nBestHeight < Checkpoints::GetTotalBlocksEstimate())))
{
bool fStrictOpEval = true;
// This code should be removed when OP_EVAL has
diff --git a/src/net.cpp b/src/net.cpp
index 9b8dbe11e8..6dab7e8c11 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -496,21 +496,25 @@ bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
void AddressCurrentlyConnected(const CAddress& addr)
{
+ CAddress *paddrFound = NULL;
+
CRITICAL_BLOCK(cs_mapAddresses)
{
// Only if it's been published already
map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
if (it != mapAddresses.end())
+ paddrFound = &(*it).second;
+ }
+
+ if (paddrFound)
+ {
+ int64 nUpdateInterval = 20 * 60;
+ if (paddrFound->nTime < GetAdjustedTime() - nUpdateInterval)
{
- CAddress& addrFound = (*it).second;
- int64 nUpdateInterval = 20 * 60;
- if (addrFound.nTime < GetAdjustedTime() - nUpdateInterval)
- {
- // Periodically update most recently seen time
- addrFound.nTime = GetAdjustedTime();
- CAddrDB addrdb;
- addrdb.WriteAddress(addrFound);
- }
+ // Periodically update most recently seen time
+ paddrFound->nTime = GetAdjustedTime();
+ CAddrDB addrdb;
+ addrdb.WriteAddress(*paddrFound);
}
}
}
@@ -1271,13 +1275,13 @@ void ThreadDNSAddressSeed2(void* parg)
if (!fTestNet)
{
printf("Loading addresses from DNS seeds (could take a while)\n");
- CAddrDB addrDB;
- addrDB.TxnBegin();
for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
vector<CAddress> vaddr;
if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true))
{
+ CAddrDB addrDB;
+ addrDB.TxnBegin();
BOOST_FOREACH (CAddress& addr, vaddr)
{
if (addr.GetByte(3) != 127)
@@ -1287,10 +1291,9 @@ void ThreadDNSAddressSeed2(void* parg)
found++;
}
}
+ addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
}
}
-
- addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
}
printf("%d addresses found from DNS seeds\n", found);
@@ -1462,28 +1465,32 @@ void ThreadOpenConnections2(void* parg)
if (fShutdown)
return;
+ bool fAddSeeds = false;
+
CRITICAL_BLOCK(cs_mapAddresses)
{
// Add seed nodes if IRC isn't working
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;
+ }
+
+ if (fAddSeeds)
+ {
+ for (int i = 0; i < ARRAYLEN(pnSeed); i++)
{
- for (int i = 0; i < ARRAYLEN(pnSeed); i++)
- {
- // It'll only connect to one or two seed nodes because once it connects,
- // it'll get a pile of addresses with newer timestamps.
- // Seed nodes are given a random 'last seen time' of between one and two
- // weeks ago.
- const int64 nOneWeek = 7*24*60*60;
- CAddress addr;
- addr.ip = pnSeed[i];
- addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
- AddAddress(addr);
- }
+ // It'll only connect to one or two seed nodes because once it connects,
+ // it'll get a pile of addresses with newer timestamps.
+ // Seed nodes are given a random 'last seen time' of between one and two
+ // weeks ago.
+ const int64 nOneWeek = 7*24*60*60;
+ CAddress addr;
+ addr.ip = pnSeed[i];
+ addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
+ AddAddress(addr);
}
}
-
//
// Choose an address to connect to based on most recently seen
//
diff --git a/src/script.cpp b/src/script.cpp
index 0a1b3bb309..f576e14a8b 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -1033,7 +1033,7 @@ bool EvalScriptInner(vector<vector<unsigned char> >& stack, const CScript& scrip
return false;
if (!EvalScriptInner(stack, subscript, txTo, nIn, nHashType,
- pbegincodehash, pendcodehash, nOpCount, nSigOpCount, fStrictOpEval, nRecurseDepth++))
+ pbegincodehash, pendcodehash, nOpCount, nSigOpCount, fStrictOpEval, nRecurseDepth+1))
return false;
}
break;
diff --git a/src/test/script_op_eval_tests.cpp b/src/test/script_op_eval_tests.cpp
index c44642c6e9..b1bd52c5f2 100644
--- a/src/test/script_op_eval_tests.cpp
+++ b/src/test/script_op_eval_tests.cpp
@@ -93,31 +93,65 @@ BOOST_AUTO_TEST_CASE(script_op_eval2)
{
// Test OP_EVAL edge cases
- CScript recurse;
- recurse << OP_DUP << OP_EVAL;
+ // Make sure infinite recursion fails to validate:
+ CScript infiniteRecurse;
+ infiniteRecurse << OP_DUP << OP_EVAL;
- uint160 recurseHash = Hash160(recurse);
+ uint160 infiniteRecurseHash = Hash160(infiniteRecurse);
- CScript fund;
- fund << OP_DUP << OP_HASH160 << recurseHash << OP_EQUALVERIFY << OP_EVAL;
+ CScript fund1;
+ fund1 << OP_DUP << OP_HASH160 << infiniteRecurseHash << OP_EQUALVERIFY << OP_EVAL;
- CTransaction txFrom; // Funding transaction:
- txFrom.vout.resize(1);
- txFrom.vout[0].scriptPubKey = fund;
+ CTransaction txFrom1; // Funding transaction:
+ txFrom1.vout.resize(1);
+ txFrom1.vout[0].scriptPubKey = fund1;
- BOOST_CHECK(txFrom.IsStandard()); // Looks like a standard transaction until you try to spend it
+ BOOST_CHECK(txFrom1.IsStandard()); // Looks like a standard transaction until you try to spend it
- CTransaction txTo;
- txTo.vin.resize(1);
- txTo.vout.resize(1);
- txTo.vin[0].prevout.n = 0;
- txTo.vin[0].prevout.hash = txFrom.GetHash();
- txTo.vin[0].scriptSig = CScript() << static_cast<std::vector<unsigned char> >(recurse);
- txTo.vout[0].nValue = 1;
+ std::vector<unsigned char> infiniteRecurseSerialized(infiniteRecurse);
- int nUnused = 0;
- BOOST_CHECK(!VerifyScript(txTo.vin[0].scriptSig, txFrom.vout[0].scriptPubKey, txTo, 0, nUnused, 0, true));
- BOOST_CHECK(!VerifySignature(txFrom, txTo, 0, nUnused, true));
+ CTransaction txTo1;
+ txTo1.vin.resize(1);
+ txTo1.vout.resize(1);
+ txTo1.vin[0].prevout.n = 0;
+ txTo1.vin[0].prevout.hash = txFrom1.GetHash();
+ txTo1.vin[0].scriptSig = CScript() << infiniteRecurseSerialized << infiniteRecurseSerialized;
+ txTo1.vout[0].nValue = 1;
+
+ int nUnused1 = 0;
+ BOOST_CHECK(!VerifyScript(txTo1.vin[0].scriptSig, txFrom1.vout[0].scriptPubKey, txTo1, 0, nUnused1, 0, true));
+ BOOST_CHECK(!VerifySignature(txFrom1, txTo1, 0, nUnused1, true));
+
+ // Make sure 3-level-deep recursion fails to validate:
+ CScript recurse3;
+ recurse3 << OP_EVAL;
+
+ uint160 recurse3Hash = Hash160(recurse3);
+
+ CScript fund2;
+ fund2 << OP_DUP << OP_HASH160 << recurse3Hash << OP_EQUALVERIFY << OP_EVAL;
+
+ CTransaction txFrom2; // Funding transaction:
+ txFrom2.vout.resize(1);
+ txFrom2.vout[0].scriptPubKey = fund2;
+
+ BOOST_CHECK(txFrom2.IsStandard()); // Looks like a standard transaction until you try to spend it
+
+ std::vector<unsigned char> recurse3Serialized(recurse3);
+ CScript op1Script = CScript() << OP_1;
+ std::vector<unsigned char> op1Serialized(op1Script);
+
+ CTransaction txTo2;
+ txTo2.vin.resize(1);
+ txTo2.vout.resize(1);
+ txTo2.vin[0].prevout.n = 0;
+ txTo2.vin[0].prevout.hash = txFrom2.GetHash();
+ txTo2.vin[0].scriptSig = CScript() << op1Serialized << recurse3Serialized << recurse3Serialized;
+ txTo2.vout[0].nValue = 1;
+
+ int nUnused2 = 0;
+ BOOST_CHECK(!VerifyScript(txTo2.vin[0].scriptSig, txFrom2.vout[0].scriptPubKey, txTo2, 0, nUnused2, 0, true));
+ BOOST_CHECK(!VerifySignature(txFrom2, txTo2, 0, nUnused2, true));
}
BOOST_AUTO_TEST_CASE(script_op_eval3)
diff --git a/src/util.cpp b/src/util.cpp
index a45ce33a1c..3805e077a1 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -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)
{
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
diff --git a/src/util.h b/src/util.h
index bb90869962..48fea023bf 100644
--- a/src/util.h
+++ b/src/util.h
@@ -439,7 +439,7 @@ inline int64 GetArg(const std::string& strArg, int64 nDefault)
return nDefault;
}
-inline bool GetBoolArg(const std::string& strArg)
+inline bool GetBoolArg(const std::string& strArg, bool fDefault=false)
{
if (mapArgs.count(strArg))
{
@@ -447,9 +447,26 @@ inline bool GetBoolArg(const std::string& strArg)
return true;
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);