From 7676468780f61f238ac5c02eb2e13a81ecbf656f Mon Sep 17 00:00:00 2001 From: Intel Date: Sun, 11 May 2014 14:00:28 -0400 Subject: [PATCH 1/3] Limit packet size --- src/server/poolserver/Stratum/Client.cpp | 4 ++++ src/server/poolserver/Stratum/Server.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/server/poolserver/Stratum/Client.cpp b/src/server/poolserver/Stratum/Client.cpp index 4942d59..0ad8f23 100644 --- a/src/server/poolserver/Stratum/Client.cpp +++ b/src/server/poolserver/Stratum/Client.cpp @@ -388,6 +388,10 @@ namespace Stratum } else _recvMessage += c; } + + // Check if message doesn't exceed maximum packet size + if (_recvMessage.size() > MAX_PACKET) + Disconnect(); StartRead(); } else { diff --git a/src/server/poolserver/Stratum/Server.h b/src/server/poolserver/Stratum/Server.h index a77246a..208b10d 100644 --- a/src/server/poolserver/Stratum/Server.h +++ b/src/server/poolserver/Stratum/Server.h @@ -18,6 +18,8 @@ #include #include +#define MAX_PACKET 4096 + using namespace boost; using namespace boost::asio::ip; From 50097be937d30972e9194c33f03cdca74eeb7380 Mon Sep 17 00:00:00 2001 From: Intel Date: Sun, 11 May 2014 14:01:35 -0400 Subject: [PATCH 2/3] Tab2Space --- src/server/poolserver/Stratum/Client.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/poolserver/Stratum/Client.cpp b/src/server/poolserver/Stratum/Client.cpp index 0ad8f23..c50bab7 100644 --- a/src/server/poolserver/Stratum/Client.cpp +++ b/src/server/poolserver/Stratum/Client.cpp @@ -388,10 +388,10 @@ namespace Stratum } else _recvMessage += c; } - - // Check if message doesn't exceed maximum packet size - if (_recvMessage.size() > MAX_PACKET) - Disconnect(); + + // Check if message doesn't exceed maximum packet size + if (_recvMessage.size() > MAX_PACKET) + Disconnect(); StartRead(); } else { From a31dee09e0f1ebf99efeb32897c6b78c3493291c Mon Sep 17 00:00:00 2001 From: Intel Date: Sun, 11 May 2014 15:25:16 -0400 Subject: [PATCH 3/3] Improved server logging --- src/server/poolserver/DataMgr/DataMgr.cpp | 4 +--- .../poolserver/NetworkMgr/NetworkMgr.cpp | 2 +- src/server/poolserver/Stratum/Client.cpp | 20 ++++++++++--------- src/server/poolserver/Stratum/Server.cpp | 2 +- src/server/shared/Logging/Log.cpp | 18 +++++++++-------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/server/poolserver/DataMgr/DataMgr.cpp b/src/server/poolserver/DataMgr/DataMgr.cpp index af5734e..e2d56c7 100644 --- a/src/server/poolserver/DataMgr/DataMgr.cpp +++ b/src/server/poolserver/DataMgr/DataMgr.cpp @@ -7,14 +7,12 @@ DataMgr* DataMgr::singleton = 0; void DataMgr::Upload() { - sLog.Info(LOG_SERVER, "We have %u shares", Size()); + sLog.Info(LOG_SERVER, "Uploading %u shares", Size()); uint32 bulkCount = sConfig.Get("ShareUploadBulkCount"); while (Size() > sConfig.Get("ShareUploadMinCount")) { - sLog.Info(LOG_SERVER, "Uploading %u shares to database", Size()); - std::string query("INSERT INTO `shares` (`rem_host`, `username`, `our_result`, `upstream_result`, `reason`, `time`, `difficulty`) VALUES "); for (int i = 0; i < bulkCount; ++i) { diff --git a/src/server/poolserver/NetworkMgr/NetworkMgr.cpp b/src/server/poolserver/NetworkMgr/NetworkMgr.cpp index a40fe21..ff54a0c 100644 --- a/src/server/poolserver/NetworkMgr/NetworkMgr.cpp +++ b/src/server/poolserver/NetworkMgr/NetworkMgr.cpp @@ -78,7 +78,7 @@ void NetworkMgr::UpdateBlockTemplate() // Set _curBlockTmpl = block; - sLog.Debug(LOG_SERVER, "Fetched block template from rpc #%u", i); + sLog.Info(LOG_SERVER, "Fetched block template from rpc #%u", i); // Break from loop break; diff --git a/src/server/poolserver/Stratum/Client.cpp b/src/server/poolserver/Stratum/Client.cpp index c50bab7..c31868c 100644 --- a/src/server/poolserver/Stratum/Client.cpp +++ b/src/server/poolserver/Stratum/Client.cpp @@ -90,7 +90,7 @@ namespace Stratum // check username std::string username = params[0].GetString(); if (!_workers.count(username)) { - sLog.Error(LOG_STRATUM, "%s: Worker not authenticated", username.c_str()); + sLog.Warn(LOG_STRATUM, "%s: Worker not authenticated", username.c_str()); JSON response; response["id"] = msg["id"]; response["result"]; @@ -138,7 +138,7 @@ namespace Stratum BinaryData extranonce2 = Util::ASCIIToBin(params[2].GetString()); if (extranonce2.size() != 4) { - sLog.Error(LOG_STRATUM, "Wrong extranonce size"); + sLog.Warn(LOG_STRATUM, "Wrong extranonce size"); JSON response; response["id"] = msg["id"]; response["result"]; @@ -151,7 +151,7 @@ namespace Stratum ByteBuffer timebuf(Util::Reverse(Util::ASCIIToBin(params[3].GetString()))); if (timebuf.Size() != 4) { - sLog.Error(LOG_STRATUM, "Wrong ntime size"); + sLog.Warn(LOG_STRATUM, "Wrong ntime size"); JSON response; response["id"] = msg["id"]; response["result"]; @@ -164,7 +164,7 @@ namespace Stratum ByteBuffer noncebuf(Util::Reverse(Util::ASCIIToBin(params[4].GetString()))); if (noncebuf.Size() != 4) { - sLog.Error(LOG_STRATUM, "Wrong nonce size"); + sLog.Warn(LOG_STRATUM, "Wrong nonce size"); JSON response; response["id"] = msg["id"]; response["result"]; @@ -180,9 +180,11 @@ namespace Stratum sharebuf << noncebuf << extranonce2; uint64 share; sharebuf >> share; - sLog.Debug(LOG_STRATUM, "Job::SubmitShare: Nonce: %s, Extranonse: %s, Share: %u", Util::BinToASCII(noncebuf.Binary()).c_str(), Util::BinToASCII(extranonce2).c_str(), share); + + sLog.Debug(LOG_STRATUM, "Job::SubmitShare: Nonce: %s, Extranonce: %s, Share: %u", Util::BinToASCII(noncebuf.Binary()).c_str(), Util::BinToASCII(extranonce2).c_str(), share); + if (!job.SubmitShare(share)) { - sLog.Error(LOG_STRATUM, "%s: Duplicate share", username.c_str()); + sLog.Warn(LOG_STRATUM, "%s: Duplicate share", username.c_str()); DataMgr::Instance()->Push(Share(_ip, username, false, "Duplicate share", Util::Date(), job.diff)); _shareLimiter.LogBad(); @@ -223,7 +225,7 @@ namespace Stratum // Check if difficulty meets job diff if (target > job.jobTarget) { - sLog.Error(LOG_STRATUM, "%s: Share above target", username.c_str()); + sLog.Warn(LOG_STRATUM, "%s: Share above target", username.c_str()); DataMgr::Instance()->Push(Share(_ip, username, false, "Share above target", Util::Date(), job.diff)); _shareLimiter.LogBad(); @@ -239,7 +241,7 @@ namespace Stratum // Check if block meets criteria if (target <= job.blockTarget) { - sLog.Info(LOG_SERVER, "We have found a block candidate!"); + sLog.Info(LOG_STRATUM, "We have found a block candidate!"); if (_server->SubmitBlock(block)) { std::string query("INSERT INTO `shares` (`rem_host`, `username`, `our_result`, `upstream_result`, `reason`, `solution`, `time`, `difficulty`) VALUES "); @@ -381,7 +383,7 @@ namespace Stratum try { OnMessage(JSON::FromString(_recvMessage)); } catch (std::exception& e) { - sLog.Error(LOG_SERVER, "Exception caught while parsing json: %s", e.what()); + sLog.Debug(LOG_STRATUM, "Exception caught while parsing json: %s", e.what()); } _recvMessage.clear(); diff --git a/src/server/poolserver/Stratum/Server.cpp b/src/server/poolserver/Stratum/Server.cpp index 1a70e88..c9527d5 100644 --- a/src/server/poolserver/Stratum/Server.cpp +++ b/src/server/poolserver/Stratum/Server.cpp @@ -11,7 +11,7 @@ namespace Stratum _StartAccept(); - sLog.Debug(LOG_STRATUM, "Stratum server started"); + sLog.Info(LOG_STRATUM, "Stratum server started"); } void Server::SendToAll(JSON msg) diff --git a/src/server/shared/Logging/Log.cpp b/src/server/shared/Logging/Log.cpp index e41aebc..a2af123 100644 --- a/src/server/shared/Logging/Log.cpp +++ b/src/server/shared/Logging/Log.cpp @@ -74,36 +74,38 @@ void Log::Write(LogLevel level, LogType type, std::string msg) { boost::lock_guard lock(_mutex); + std::string timestamp = Util::Date("%b-%d %H:%M:%S"); + switch(level) { case LOG_LEVEL_ERROR: if (sConfig.Get("LogConsoleLevel") >= level) - std::cout << "[ERROR] " << msg << std::endl; + std::cout << timestamp << " [ERROR] " << msg << std::endl; if (sConfig.Get("LogFileLevel") >= level) - AppendFile("[ERROR] " + msg); + AppendFile(timestamp + " [ERROR] " + msg); break; case LOG_LEVEL_WARN: if (sConfig.Get("LogConsoleLevel") >= level) - std::cout << "[WARN] " << msg << std::endl; + std::cout << timestamp << " [WARN] " << msg << std::endl; if (sConfig.Get("LogFileLevel") >= level) - AppendFile("[WARN] " + msg); + AppendFile(timestamp + " [WARN] " + msg); break; case LOG_LEVEL_INFO: if (sConfig.Get("LogConsoleLevel") >= level) - std::cout << "[INFO] " << msg << std::endl; + std::cout << timestamp << " [INFO] " << msg << std::endl; if (sConfig.Get("LogFileLevel") >= level) - AppendFile("[INFO] " + msg); + AppendFile(timestamp + " [INFO] " + msg); break; case LOG_LEVEL_DEBUG: if (sConfig.Get("LogConsoleLevel") >= level) { uint32_t debugmask = sConfig.Get("LogConsoleDebugMask"); if (debugmask & uint32_t(pow(2, type))) - std::cout << "[DEBUG] " << msg << std::endl; + std::cout << timestamp << " [DEBUG] " << msg << std::endl; } if (sConfig.Get("LogFileLevel") >= level) { uint32_t debugmask = sConfig.Get("LogFileDebugMask"); if (debugmask & uint32_t(pow(2, type))) - AppendFile("[DEBUG] " + msg); + AppendFile(timestamp + " [DEBUG] " + msg); } break; }