Electroneum
Loading...
Searching...
No Matches
ban.cpp
Go to the documentation of this file.
1// Copyright (c) 2014-2019, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31#include "gtest/gtest.h"
33#include "p2p/net_node.h"
34#include "p2p/net_node.inl"
36#include "cryptonote_protocol/cryptonote_protocol_handler.inl"
37
38#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}
39
40namespace cryptonote {
41 class blockchain_storage;
42}
43
45{
46public:
48 void safesyncmode(const bool){}
51 bool init(const boost::program_options::variables_map& vm) {return true ;}
52 bool deinit(){return true;}
53 bool get_short_chain_history(std::list<crypto::hash>& ids) const { return true; }
54 bool get_stat_info(cryptonote::core_stat_info& st_inf) const {return true;}
55 bool have_block(const crypto::hash& id) const {return true;}
56 void get_blockchain_top(uint64_t& height, crypto::hash& top_id)const{height=0;top_id=crypto::null_hash;}
57 bool handle_incoming_tx(const cryptonote::blobdata& tx_blob, cryptonote::tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; }
58 bool handle_incoming_txs(const std::vector<cryptonote::blobdata>& tx_blob, std::vector<cryptonote::tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; }
59 bool handle_incoming_block(const cryptonote::blobdata& block_blob, const cryptonote::block *block, cryptonote::block_verification_context& bvc, bool update_miner_blocktemplate = true) { return true; }
60 void pause_mine(){}
61 void resume_mine(){}
62 bool on_idle(){return true;}
63 bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp){return true;}
65 cryptonote::blockchain_storage &get_blockchain_storage() { throw std::runtime_error("Called invalid member function: please never call get_blockchain_storage on the TESTING class test_core."); }
66 bool get_test_drop_download() const {return true;}
67 bool get_test_drop_download_height() const {return true;}
68 bool prepare_handle_incoming_blocks(const std::vector<cryptonote::block_complete_entry> &blocks_entry, std::vector<cryptonote::block> &blocks) { return true; }
69 bool cleanup_handle_incoming_blocks(bool force_sync = false) { return true; }
74 bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx_blob) const { return false; }
75 bool pool_has_tx(const crypto::hash &txid) const { return false; }
76 bool get_blocks(uint64_t start_offset, size_t count, std::vector<std::pair<cryptonote::blobdata, cryptonote::block>>& blocks, std::vector<cryptonote::blobdata>& txs) const { return false; }
77 bool get_transactions(const std::vector<crypto::hash>& txs_ids, std::vector<cryptonote::transaction>& txs, std::vector<crypto::hash>& missed_txs) const { return false; }
78 bool get_block_by_hash(const crypto::hash &h, cryptonote::block &blk, bool *orphan = NULL) const { return false; }
84 bool fluffy_blocks_enabled() const { return false; }
85 uint64_t prevalidate_block_hashes(uint64_t height, const std::vector<crypto::hash> &hashes) { return 0; }
86 bool pad_transactions() { return false; }
88 bool prune_blockchain(uint32_t pruning_seed = 0) { return true; }
89 void stop() {}
92 bool isValidatorsListValid() { return true; }
93 std::string get_validators_list() { return ""; }
94};
95
97
98static bool is_blocked(Server &server, const epee::net_utils::network_address &address, time_t *t = NULL)
99{
100 const std::string host = address.host_str();
101 std::map<std::string, time_t> hosts = server.get_blocked_hosts();
102 for (auto rec: hosts)
103 {
104 if (rec.first == host)
105 {
106 if (t)
107 *t = rec.second;
108 return true;
109 }
110 }
111 return false;
112}
113
114TEST(ban, add)
115{
116 test_core pr_core;
118 Server server(cprotocol);
119 cprotocol.set_p2p_endpoint(&server);
120
121 // starts empty
122 ASSERT_TRUE(server.get_blocked_hosts().empty());
123 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
124 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
125
126 // add an IP
127 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
128 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
129 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
130 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
131
132 // add the same, should not change
133 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4)));
134 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
135 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
136 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
137
138 // remove an unblocked IP, should not change
140 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
141 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
142 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
143
144 // remove the IP, ends up empty
146 ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
147 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
148 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
149
150 // remove the IP from an empty list, still empty
152 ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
153 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
154 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
155
156 // add two for known amounts of time, they're both blocked
157 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1));
158 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3));
159 ASSERT_TRUE(server.get_blocked_hosts().size() == 2);
160 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
161 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
164
165 // these tests would need to call is_remote_ip_allowed, which is private
166#if 0
167 // after two seconds, the first IP is unblocked, but not the second yet
168 sleep(2);
169 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
170 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
171 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
172
173 // after two more seconds, the second IP is also unblocked
174 sleep(2);
175 ASSERT_TRUE(server.get_blocked_hosts().size() == 0);
176 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
177 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
178#endif
179
180 // add an IP again, then re-ban for longer, then shorter
181 time_t t;
182 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2));
183 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
184 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
185 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
186 ASSERT_TRUE(t >= 1);
187 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9));
188 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
189 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
190 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
191 ASSERT_TRUE(t >= 8);
192 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5));
193 ASSERT_TRUE(server.get_blocked_hosts().size() == 1);
194 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t));
195 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5)));
196 ASSERT_TRUE(t >= 4);
197}
198
199TEST(ban, limit)
200{
201 test_core pr_core;
203 Server server(cprotocol);
204 cprotocol.set_p2p_endpoint(&server);
205
206 // starts empty
207 ASSERT_TRUE(server.get_blocked_hosts().empty());
208 ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
209 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), std::numeric_limits<time_t>::max() - 1));
210 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
211 ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1));
212 ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4)));
213}
214
216namespace cryptonote { template class t_cryptonote_protocol_handler<test_core>; }
#define MAKE_IPV4_ADDRESS(a, b, c, d)
Definition ban.cpp:38
nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< test_core > > Server
Definition ban.cpp:96
uint64_t height
uint8_t version
void set_p2p_endpoint(nodetool::i_p2p_endpoint< connection_context > *p2p)
virtual std::map< std::string, time_t > get_blocked_hosts()
Definition net_node.h:252
virtual bool unblock_host(const epee::net_utils::network_address &address)
virtual bool block_host(const epee::net_utils::network_address &adress, time_t seconds=P2P_IP_BLOCKTIME)
uint8_t get_ideal_hard_fork_version() const
Definition ban.cpp:79
bool init(const boost::program_options::variables_map &vm)
Definition ban.cpp:51
std::string get_validators_list()
Definition ban.cpp:93
void resume_mine()
Definition ban.cpp:61
bool get_blocks(uint64_t start_offset, size_t count, std::vector< std::pair< cryptonote::blobdata, cryptonote::block > > &blocks, std::vector< cryptonote::blobdata > &txs) const
Definition ban.cpp:76
bool handle_get_objects(cryptonote::NOTIFY_REQUEST_GET_OBJECTS::request &arg, cryptonote::NOTIFY_RESPONSE_GET_OBJECTS::request &rsp, cryptonote::cryptonote_connection_context &context)
Definition ban.cpp:64
cryptonote::network_type get_nettype() const
Definition ban.cpp:73
bool find_blockchain_supplement(const std::list< crypto::hash > &qblock_ids, cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request &resp)
Definition ban.cpp:63
void set_block_cumulative_difficulty(uint64_t height, cryptonote::difficulty_type diff)
Definition ban.cpp:90
bool handle_incoming_block(const cryptonote::blobdata &block_blob, const cryptonote::block *block, cryptonote::block_verification_context &bvc, bool update_miner_blocktemplate=true)
Definition ban.cpp:59
virtual void on_transaction_relayed(const cryptonote::blobdata &tx)
Definition ban.cpp:72
uint64_t get_current_blockchain_height() const
Definition ban.cpp:49
void safesyncmode(const bool)
Definition ban.cpp:48
void pause_mine()
Definition ban.cpp:60
uint8_t get_ideal_hard_fork_version(uint64_t height) const
Definition ban.cpp:80
bool get_short_chain_history(std::list< crypto::hash > &ids) const
Definition ban.cpp:53
void on_synchronized()
Definition ban.cpp:47
void get_blockchain_top(uint64_t &height, crypto::hash &top_id) const
Definition ban.cpp:56
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
Definition ban.cpp:82
bool on_idle()
Definition ban.cpp:62
bool handle_incoming_txs(const std::vector< cryptonote::blobdata > &tx_blob, std::vector< cryptonote::tx_verification_context > &tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
Definition ban.cpp:58
bool fluffy_blocks_enabled() const
Definition ban.cpp:84
bool prune_blockchain(uint32_t pruning_seed=0)
Definition ban.cpp:88
bool pad_transactions()
Definition ban.cpp:86
electroneum::basic::list_update_outcome set_validators_list(std::string v_list, bool isEmergencyUpdate=false)
Definition ban.cpp:91
bool isValidatorsListValid()
Definition ban.cpp:92
cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const
Definition ban.cpp:83
bool pool_has_tx(const crypto::hash &txid) const
Definition ban.cpp:75
bool deinit()
Definition ban.cpp:52
void stop()
Definition ban.cpp:89
bool get_block_by_hash(const crypto::hash &h, cryptonote::block &blk, bool *orphan=NULL) const
Definition ban.cpp:78
bool have_block(const crypto::hash &id) const
Definition ban.cpp:55
bool get_test_drop_download() const
Definition ban.cpp:66
uint64_t get_target_blockchain_height() const
Definition ban.cpp:70
bool get_test_drop_download_height() const
Definition ban.cpp:67
void set_target_blockchain_height(uint64_t)
Definition ban.cpp:50
bool get_stat_info(cryptonote::core_stat_info &st_inf) const
Definition ban.cpp:54
bool prepare_handle_incoming_blocks(const std::vector< cryptonote::block_complete_entry > &blocks_entry, std::vector< cryptonote::block > &blocks)
Definition ban.cpp:68
cryptonote::blockchain_storage & get_blockchain_storage()
Definition ban.cpp:65
uint32_t get_blockchain_pruning_seed() const
Definition ban.cpp:87
uint64_t prevalidate_block_hashes(uint64_t height, const std::vector< crypto::hash > &hashes)
Definition ban.cpp:85
bool handle_incoming_tx(const cryptonote::blobdata &tx_blob, cryptonote::tx_verification_context &tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
Definition ban.cpp:57
size_t get_block_sync_size(uint64_t height) const
Definition ban.cpp:71
bool cleanup_handle_incoming_blocks(bool force_sync=false)
Definition ban.cpp:69
bool get_transactions(const std::vector< crypto::hash > &txs_ids, std::vector< cryptonote::transaction > &txs, std::vector< crypto::hash > &missed_txs) const
Definition ban.cpp:77
uint8_t get_hard_fork_version(uint64_t height) const
Definition ban.cpp:81
bool get_pool_transaction(const crypto::hash &id, cryptonote::blobdata &tx_blob) const
Definition ban.cpp:74
#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT
This is the orginal cryptonote protocol network-events handler, modified by us.
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
POD_CLASS hash
Definition hash.h:50
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
boost::multiprecision::uint128_t difficulty_type
Definition difficulty.h:43
std::string blobdata
epee::misc_utils::struct_init< core_stat_info_t > core_stat_info
#define true
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
epee::misc_utils::struct_init< request_t > request
epee::misc_utils::struct_init< request_t > request
epee::misc_utils::struct_init< request_t > request
struct hash_func hashes[]
const char * address
Definition multisig.cpp:37