From c039bb8e66c18b9a9a5b99a140b43a88a1093c1f Mon Sep 17 00:00:00 2001 From: Cobitcoin Date: Sat, 14 Jul 2018 11:10:27 -0300 Subject: [PATCH] blockhosts add block hosts add --- .gitattributes | 2 + .gitignore | 8 +++ README.md | 1 + ReleaseNotes.txt | 4 +- src/CryptoNoteConfig.h | 9 ++- .../CryptoNoteProtocolHandler.cpp | 1 + src/P2p/NetNode.cpp | 66 +++++++++++++++++++ src/P2p/NetNode.h | 8 +++ src/P2p/NetNodeCommon.h | 2 + src/version.h.in | 2 +- 10 files changed, 97 insertions(+), 6 deletions(-) create mode 100755 .gitattributes create mode 100755 .gitignore diff --git a/.gitattributes b/.gitattributes new file mode 100755 index 0000000..6afd357 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +.git* export-ignore +/CMakeLists.txt export-subst \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..0b14727 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/build +/tags +.idea +.ycm_extra_conf.py +.ycm_extra_conf.pyc +Release +Debug diff --git a/README.md b/README.md index 1d7b244..720fc9a 100755 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Due to changes in the security of the Nodes. We are updating their keys. ## Please update your nodes or your transactions will be invalid ! +changes will occur after block 12800 The change occurs automatically after the Daemons switch. The sources are available in Github: https://github.com/cobitcoin diff --git a/ReleaseNotes.txt b/ReleaseNotes.txt index 6d950f5..49bbd35 100755 --- a/ReleaseNotes.txt +++ b/ReleaseNotes.txt @@ -1,3 +1,3 @@ -Release notes 2.1.2 +Release notes 2.1.2.1252 -First Release with Cryptonote V-7 \ No newline at end of file +First Release with Cryptonote V-7 and blockhosts diff --git a/src/CryptoNoteConfig.h b/src/CryptoNoteConfig.h index d5db48f..a0b1fc2 100755 --- a/src/CryptoNoteConfig.h +++ b/src/CryptoNoteConfig.h @@ -36,7 +36,7 @@ const size_t BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW = 60; // MONEY_SUPPLY - total number coins to be generated const uint64_t MONEY_SUPPLY = UINT64_C(3000000000000000); const uint32_t KILL_HEIGHT = 0; -const uint32_t POW_CRYPTONIGHT_V7_BLOCK_INDEX = 12500; +const uint32_t POW_CRYPTONIGHT_V7_BLOCK_INDEX = 12800; const uint32_t POW_CRYPTONIGHT_V7_LAST_BLOCK = uint32_t(-1); const unsigned EMISSION_SPEED_FACTOR = 17; const uint64_t GENESIS_BLOCK_REWARD = UINT64_C(1000000000000000); @@ -95,7 +95,6 @@ const char CRYPTONOTE_POOLDATA_FILENAME[] = "poolstate.bin"; const char P2P_NET_DATA_FILENAME[] = "p2pstate.bin"; const char MINER_CONFIG_FILE_NAME[] = "miner_conf.json"; const char GENESIS_COINBASE_TX_HEX[] = "010a01ff000100020e2d7b7d04326b6185856316653f29160fe6d5b80450c8b9f72cf920915c3db52101f4335efca6efe73b6147961a845f097abaf073846e0f0acb42f239f499fe9761"; - } // parameters const char CRYPTONOTE_NAME[] = "cobitcoin"; @@ -129,6 +128,11 @@ const uint32_t P2P_DEFAULT_CONNECTION_TIMEOUT = 5000; // const uint32_t P2P_DEFAULT_PING_CONNECTION_TIMEOUT = 2000; // 2 seconds const uint64_t P2P_DEFAULT_INVOKE_TIMEOUT = 60 * 2 * 1000; // 2 minutes const size_t P2P_DEFAULT_HANDSHAKE_INVOKE_TIMEOUT = 5000; // 5 seconds +const uint32_t P2P_FAILED_ADDR_FORGET_SECONDS = (60*60); //1 hour +const uint32_t P2P_IP_BLOCKTIME = (60*60*24); //24 hour +const uint32_t P2P_IP_FAILS_BEFORE_BLOCK = 10; +const uint32_t P2P_IDLE_CONNECTION_KILL_INTERVAL = (5*60); //5 minutes + const char P2P_STAT_TRUSTED_PUB_KEY[] = "2018f9a5a434a9f1510d13336228c0b1ee9c918ce505efe225d8c94d045f3684"; const char* const SEED_NODES[] = { "217.182.199.62:18168", "139.99.106.13:18168", "74.208.10.18:18168" }; @@ -147,4 +151,3 @@ const std::initializer_list CHECKPOINTS = { }; - diff --git a/src/CryptoNoteProtocol/CryptoNoteProtocolHandler.cpp b/src/CryptoNoteProtocol/CryptoNoteProtocolHandler.cpp index 1cff981..ca6bd2e 100755 --- a/src/CryptoNoteProtocol/CryptoNoteProtocolHandler.cpp +++ b/src/CryptoNoteProtocol/CryptoNoteProtocolHandler.cpp @@ -335,6 +335,7 @@ int CryptoNoteProtocolHandler::handle_notify_new_block(int command, NOTIFY_NEW_B logger(Logging::TRACE) << context << "Block already exists"; } } else if (result == error::AddBlockErrorCondition::BLOCK_REJECTED) { + m_p2p->drop_connection(context, true); context.m_state = CryptoNoteConnectionContext::state_synchronizing; NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized(); r.block_ids = m_core.buildSparseChain(); diff --git a/src/P2p/NetNode.cpp b/src/P2p/NetNode.cpp index e01ca52..e4bab5f 100755 --- a/src/P2p/NetNode.cpp +++ b/src/P2p/NetNode.cpp @@ -345,6 +345,69 @@ std::string print_peerlist_to_string(const std::list& pl) { //----------------------------------------------------------------------------------- +bool NodeServer::block_host(const uint32_t address_ip, time_t seconds) +{ + std::unique_lock lock(mutex); + m_blocked_hosts[address_ip] = time(nullptr) + seconds; + + // drop any connection to that IP + std::list conns; + forEachConnection([&](P2pConnectionContext& cntxt) { + if (cntxt.m_remote_ip == address_ip) + { + conns.push_back(cntxt.m_connection_id); + } + return true; + }); + for (const auto &c_id: conns) { + auto c = m_connections.find(c_id); + if (c != m_connections.end()) + c->second.m_state = CryptoNoteConnectionContext::state_shutdown; + } + + logger(INFO) << "Host " << Common::ipAddressToString(address_ip) << " blocked."; + return true; +} +//----------------------------------------------------------------------------------- +bool NodeServer::unblock_host(const uint32_t address_ip) +{ + std::unique_lock lock(mutex); + auto i = m_blocked_hosts.find(address_ip); + if (i == m_blocked_hosts.end()) + return false; + m_blocked_hosts.erase(i); + logger(INFO) << "Host " << Common::ipAddressToString(address_ip) << " unblocked."; + return true; +} +//----------------------------------------------------------------------------------- +bool NodeServer::add_host_fail(const uint32_t address_ip) +{ + std::unique_lock lock(mutex); + uint64_t fails = ++m_host_fails_score[address_ip]; + logger(DEBUGGING) << "Host " << Common::ipAddressToString(address_ip) << " fail score=" << fails; + if(fails > P2P_IP_FAILS_BEFORE_BLOCK) + { + auto it = m_host_fails_score.find(address_ip); + if (it == m_host_fails_score.end()) { + logger(DEBUGGING) << "Internal error (add_host_fail)" << fails; + return false; + } + it->second = P2P_IP_FAILS_BEFORE_BLOCK/2; + block_host(address_ip); + } + return true; +} + +//----------------------------------------------------------------------------------- +void NodeServer::drop_connection(CryptoNoteConnectionContext& context, bool add_fail) +{ + if (add_fail) + add_host_fail(context.m_remote_ip); + + context.m_state = CryptoNoteConnectionContext::state_shutdown; +} + +//----------------------------------------------------------------------------------- bool NodeServer::handle_command_line(const boost::program_options::variables_map& vm) { m_bind_ip = command_line::get_arg(vm, arg_p2p_bind_ip); @@ -588,6 +651,7 @@ std::string print_peerlist_to_string(const std::list& pl) { } if (!handle_remote_peerlist(rsp.local_peerlist, rsp.node_data.local_time, context)) { + add_host_fail(context.m_remote_ip); logger(Logging::ERROR) << context << "COMMAND_HANDSHAKE: failed to handle_remote_peerlist(...), closing connection."; return false; } @@ -1143,12 +1207,14 @@ std::string print_peerlist_to_string(const std::list& pl) { context.version = arg.node_data.version; if (arg.node_data.network_id != m_network_id) { + add_host_fail(context.m_remote_ip); logger(Logging::INFO) << context << "WRONG NETWORK AGENT CONNECTED! id=" << arg.node_data.network_id; context.m_state = CryptoNoteConnectionContext::state_shutdown; return 1; } if(!context.m_is_income) { + add_host_fail(context.m_remote_ip); logger(Logging::ERROR) << context << "COMMAND_HANDSHAKE came not from incoming connection"; context.m_state = CryptoNoteConnectionContext::state_shutdown; return 1; diff --git a/src/P2p/NetNode.h b/src/P2p/NetNode.h index 6c84a11..2b5dff3 100755 --- a/src/P2p/NetNode.h +++ b/src/P2p/NetNode.h @@ -176,10 +176,14 @@ namespace CryptoNote //----------------- i_p2p_endpoint ------------------------------------------------------------- virtual void relay_notify_to_all(int command, const BinaryArray& data_buff, const net_connection_id* excludeConnection) override; virtual bool invoke_notify_to_peer(int command, const BinaryArray& req_buff, const CryptoNoteConnectionContext& context) override; + virtual void drop_connection(CryptoNoteConnectionContext& context, bool add_fail) override; virtual void for_each_connection(std::function f) override; virtual void externalRelayNotifyToAll(int command, const BinaryArray& data_buff) override; //----------------------------------------------------------------------------------------------- +bool block_host(const uint32_t address_ip, time_t seconds = P2P_IP_BLOCKTIME); +bool unblock_host(const uint32_t address_ip); +bool add_host_fail(const uint32_t address_ip); bool handle_command_line(const boost::program_options::variables_map& vm); bool handleConfig(const NetNodeConfig& config); bool append_net_address(std::vector& nodes, const std::string& addr); @@ -272,5 +276,9 @@ namespace CryptoNote std::list m_command_line_peers; uint64_t m_peer_livetime; boost::uuids::uuid m_network_id; + std::map m_blocked_hosts; + std::map m_host_fails_score; + + mutable std::mutex mutex; }; } diff --git a/src/P2p/NetNodeCommon.h b/src/P2p/NetNodeCommon.h index 765fcc7..60cea07 100755 --- a/src/P2p/NetNodeCommon.h +++ b/src/P2p/NetNodeCommon.h @@ -28,6 +28,7 @@ namespace CryptoNote { virtual void relay_notify_to_all(int command, const BinaryArray& data_buff, const net_connection_id* excludeConnection) = 0; virtual bool invoke_notify_to_peer(int command, const BinaryArray& req_buff, const CryptoNote::CryptoNoteConnectionContext& context) = 0; virtual uint64_t get_connections_count()=0; + virtual void drop_connection(CryptoNoteConnectionContext& context, bool add_fail) = 0; virtual void for_each_connection(std::function f) = 0; // can be called from external threads virtual void externalRelayNotifyToAll(int command, const BinaryArray& data_buff) = 0; @@ -36,6 +37,7 @@ namespace CryptoNote { struct p2p_endpoint_stub: public IP2pEndpoint { virtual void relay_notify_to_all(int command, const BinaryArray& data_buff, const net_connection_id* excludeConnection) override {} virtual bool invoke_notify_to_peer(int command, const BinaryArray& req_buff, const CryptoNote::CryptoNoteConnectionContext& context) override { return true; } + virtual void drop_connection(CryptoNoteConnectionContext& context, bool add_fail) override {} virtual void for_each_connection(std::function f) override {} virtual uint64_t get_connections_count() override { return 0; } virtual void externalRelayNotifyToAll(int command, const BinaryArray& data_buff) override {} diff --git a/src/version.h.in b/src/version.h.in index 8bffff2..8de814a 100755 --- a/src/version.h.in +++ b/src/version.h.in @@ -1,4 +1,4 @@ #define BUILD_COMMIT_ID "@VERSION@" #define PROJECT_VERSION "2.1.2" -#define PROJECT_VERSION_BUILD_NO "1251" +#define PROJECT_VERSION_BUILD_NO "1252" #define PROJECT_VERSION_LONG PROJECT_VERSION "." PROJECT_VERSION_BUILD_NO " (" BUILD_COMMIT_ID ")"