X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fnet.cpp;h=cd826d688964c08b17df50ea9b887725cae53ba5;hp=e767fa3f758692666e1c341802161f59bc967ce5;hb=28f9882707d389250e307ebf58dcf981340f1381;hpb=726b753c71c1ec24fd0f55a7badef4ef5a00769a diff --git a/src/net.cpp b/src/net.cpp index e767fa3..cd826d6 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -55,6 +55,7 @@ static map mapLocalHost; static bool vfReachable[NET_MAX] = {}; static bool vfLimited[NET_MAX] = {}; static CNode* pnodeLocalHost = NULL; +static CNode* pnodeSync = NULL; CAddress addrSeenByPeer(CService("0.0.0.0", 0), nLocalServices); uint64 nLocalHostNonce = 0; array vnThreadsRunning; @@ -140,7 +141,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer) bool RecvLine(SOCKET hSocket, string& strLine) { strLine = ""; - loop + while (true) { char c; int nBytes = recv(hSocket, &c, 1, 0); @@ -313,7 +314,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha { if (strLine.empty()) // HTTP response is separated from headers by blank line { - loop + while (true) { if (!RecvLine(hSocket, strLine)) { @@ -409,7 +410,7 @@ bool GetMyExternalIP(CNetAddr& ipRet) void ThreadGetMyExternalIP(void* parg) { // Make this thread recognisable as the external IP detection thread - RenameThread("bitcoin-ext-ip"); + RenameThread("novacoin-ext-ip"); CNetAddr addrLocalHost; if (GetMyExternalIP(addrLocalHost)) @@ -539,6 +540,15 @@ void CNode::CloseSocketDisconnect() hSocket = INVALID_SOCKET; vRecv.clear(); } + + // in case this fails, we'll empty the recv buffer when the CNode is deleted + TRY_LOCK(cs_vRecv, lockRecv); + if (lockRecv) + vRecv.clear(); + + // if this was the sync node, we'll need a new one + if (this == pnodeSync) + pnodeSync = NULL; } void CNode::Cleanup() @@ -641,7 +651,7 @@ void CNode::copyStats(CNodeStats &stats) void ThreadSocketHandler(void* parg) { // Make this thread recognisable as the networking thread - RenameThread("bitcoin-net"); + RenameThread("novacoin-net"); try { @@ -665,7 +675,7 @@ void ThreadSocketHandler2(void* parg) list vNodesDisconnected; unsigned int nPrevNodeCount = 0; - loop + while (true) { // // Disconnect nodes @@ -1000,7 +1010,7 @@ void ThreadSocketHandler2(void* parg) void ThreadMapPort(void* parg) { // Make this thread recognisable as the UPnP thread - RenameThread("bitcoin-UPnP"); + RenameThread("novacoin-UPnP"); try { @@ -1078,7 +1088,8 @@ void ThreadMapPort2(void* parg) else printf("UPnP Port Mapping successful.\n"); int i = 1; - loop { + while (true) + { if (fShutdown || !fUseUPnP) { r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0); @@ -1113,7 +1124,8 @@ void ThreadMapPort2(void* parg) freeUPNPDevlist(devlist); devlist = 0; if (r != 0) FreeUPNPUrls(&urls); - loop { + while (true) + { if (fShutdown || !fUseUPnP) return; Sleep(2000); @@ -1149,13 +1161,15 @@ void MapPort() // The first name is used as information source for addrman. // The second name should resolve to a list of seed addresses. static const char *strDNSSeed[][2] = { - {"novacoin.su", "seed.novacoin.su"}, + {"novacoin.su", "dnsseed.novacoin.su"}, + {"novacoin.ru", "dnsseed.novacoin.ru"}, + {"novaco.in", "dnsseed.novaco.in"}, }; void ThreadDNSAddressSeed(void* parg) { // Make this thread recognisable as the DNS seeding thread - RenameThread("bitcoin-dnsseed"); + RenameThread("novacoin-dnsseed"); try { @@ -1241,7 +1255,7 @@ void ThreadDumpAddress2(void* parg) { DumpAddresses(); vnThreadsRunning[THREAD_DUMPADDRESS]--; - Sleep(100000); + Sleep(600000); vnThreadsRunning[THREAD_DUMPADDRESS]++; } vnThreadsRunning[THREAD_DUMPADDRESS]--; @@ -1250,7 +1264,7 @@ void ThreadDumpAddress2(void* parg) void ThreadDumpAddress(void* parg) { // Make this thread recognisable as the address dumping thread - RenameThread("bitcoin-adrdump"); + RenameThread("novacoin-adrdump"); try { @@ -1265,7 +1279,7 @@ void ThreadDumpAddress(void* parg) void ThreadOpenConnections(void* parg) { // Make this thread recognisable as the connection opening thread - RenameThread("bitcoin-opencon"); + RenameThread("novacoin-opencon"); try { @@ -1309,7 +1323,7 @@ void static ThreadStakeMinter(void* parg) try { vnThreadsRunning[THREAD_MINTER]++; - BitcoinMiner(pwallet, true); + StakeMiner(pwallet); vnThreadsRunning[THREAD_MINTER]--; } catch (std::exception& e) { @@ -1349,7 +1363,7 @@ void ThreadOpenConnections2(void* parg) // Initiate network connections int64 nStart = GetTime(); - loop + while (true) { ProcessOneShot(); @@ -1408,7 +1422,7 @@ void ThreadOpenConnections2(void* parg) int64 nANow = GetAdjustedTime(); int nTries = 0; - loop + while (true) { // use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections) CAddress addr = addrman.Select(10 + min(nOutbound,8)*10); @@ -1447,7 +1461,7 @@ void ThreadOpenConnections2(void* parg) void ThreadOpenAddedConnections(void* parg) { // Make this thread recognisable as the connection opening thread - RenameThread("bitcoin-opencon"); + RenameThread("novacoin-opencon"); try { @@ -1501,7 +1515,7 @@ void ThreadOpenAddedConnections2(void* parg) } } } - loop + while (true) { vector > vservConnectAddresses = vservAddressesToAdd; // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry @@ -1568,17 +1582,42 @@ bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOu return true; } +// for now, use a very simple selection metric: the node from which we received +// most recently +double static NodeSyncScore(const CNode *pnode) { + return -pnode->nLastRecv; +} - - - - - +void static StartSync(const vector &vNodes) { + CNode *pnodeNewSync = NULL; + double dBestScore = 0; + + // Iterate over all nodes + BOOST_FOREACH(CNode* pnode, vNodes) { + // check preconditions for allowing a sync + if (!pnode->fClient && !pnode->fOneShot && + !pnode->fDisconnect && pnode->fSuccessfullyConnected && + (pnode->nStartingHeight > (nBestHeight - 144)) && + (pnode->nVersion < NOBLKS_VERSION_START || pnode->nVersion >= NOBLKS_VERSION_END)) { + // if ok, compare node's score with the best so far + double dScore = NodeSyncScore(pnode); + if (pnodeNewSync == NULL || dScore > dBestScore) { + pnodeNewSync = pnode; + dBestScore = dScore; + } + } + } + // if a new sync candidate was found, start sync! + if (pnodeNewSync) { + pnodeNewSync->fStartSync = true; + pnodeSync = pnodeNewSync; + } +} void ThreadMessageHandler(void* parg) { // Make this thread recognisable as the message handling thread - RenameThread("bitcoin-msghand"); + RenameThread("novacoin-msghand"); try { @@ -1602,14 +1641,21 @@ void ThreadMessageHandler2(void* parg) SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL); while (!fShutdown) { + bool fHaveSyncNode = false; vector vNodesCopy; { LOCK(cs_vNodes); vNodesCopy = vNodes; - BOOST_FOREACH(CNode* pnode, vNodesCopy) + BOOST_FOREACH(CNode* pnode, vNodesCopy) { pnode->AddRef(); + if (pnode == pnodeSync) + fHaveSyncNode = true; + } } + if (!fHaveSyncNode) + StartSync(vNodesCopy); + // Poll the connected nodes for messages CNode* pnodeTrickle = NULL; if (!vNodesCopy.empty()) @@ -1727,8 +1773,12 @@ bool BindListenPort(const CService &addrBind, string& strError) // and enable it by default or not. Try to enable it, if possible. if (addrBind.IsIPv6()) { #ifdef IPV6_V6ONLY +#ifdef WIN32 + setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int)); +#else setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int)); #endif +#endif #ifdef WIN32 int nProtLevel = 10 /* PROTECTION_LEVEL_UNRESTRICTED */; int nParameterId = 23 /* IPV6_PROTECTION_LEVEl */; @@ -1825,7 +1875,7 @@ void static Discover() void StartNode(void* parg) { // Make this thread recognisable as the startup thread - RenameThread("bitcoin-start"); + RenameThread("novacoin-start"); if (semOutbound == NULL) { // initialize semaphore @@ -1842,18 +1892,11 @@ void StartNode(void* parg) // Start threads // -/* if (!GetBoolArg("-dnsseed", true)) printf("DNS seeding disabled\n"); else if (!NewThread(ThreadDNSAddressSeed, NULL)) printf("Error: NewThread(ThreadDNSAddressSeed) failed\n"); -*/ - - if (!GetBoolArg("-dnsseed", false)) - printf("DNS seeding disabled\n"); - if (GetBoolArg("-dnsseed", false)) - printf("DNS seeding NYI\n"); // Map ports with UPnP if (fUseUPnP) @@ -1894,6 +1937,10 @@ bool StopNode() fShutdown = true; nTransactionsUpdated++; int64 nStart = GetTime(); + { + LOCK(cs_main); + ThreadScriptCheckQuit(); + } if (semOutbound) for (int i=0; ipost(); @@ -1920,7 +1967,8 @@ bool StopNode() if (vnThreadsRunning[THREAD_ADDEDCONNECTIONS] > 0) printf("ThreadOpenAddedConnections still running\n"); if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n"); if (vnThreadsRunning[THREAD_MINTER] > 0) printf("ThreadStakeMinter still running\n"); - while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0) + if (vnThreadsRunning[THREAD_SCRIPTCHECK] > 0) printf("ThreadScriptCheck still running\n"); + while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0 || vnThreadsRunning[THREAD_SCRIPTCHECK] > 0) Sleep(20); Sleep(50); DumpAddresses(); @@ -1951,3 +1999,31 @@ public: } } instance_of_cnetcleanup; + +void RelayTransaction(const CTransaction& tx, const uint256& hash) +{ + CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); + ss.reserve(10000); + ss << tx; + RelayTransaction(tx, hash, ss); +} + +void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss) +{ + CInv inv(MSG_TX, hash); + { + LOCK(cs_mapRelay); + // Expire old relay messages + while (!vRelayExpiration.empty() && vRelayExpiration.front().first < GetTime()) + { + mapRelay.erase(vRelayExpiration.front().second); + vRelayExpiration.pop_front(); + } + + // Save original serialized message so newer versions are preserved + mapRelay.insert(std::make_pair(inv, ss)); + vRelayExpiration.push_back(std::make_pair(GetTime() + 15 * 60, inv)); + } + + RelayInventory(inv); +}