Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new-RPC): connection healthcheck implementation for peers #2194

Merged
merged 38 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4c23204
create bare RPC structure
onur-ozkan Aug 19, 2024
ad1cdb9
implement `PeerConnectionHealth` p2p command
onur-ozkan Aug 20, 2024
838b5a5
add new p2p message topic "hcheck"
onur-ozkan Aug 26, 2024
886d13e
fix p2p race condition
onur-ozkan Aug 26, 2024
9ee8485
WIP: `HealtCheckMsg`
onur-ozkan Aug 26, 2024
db49d0d
implement signing and verification logic
onur-ozkan Aug 27, 2024
a5d7f81
simulate request-response behaviour
onur-ozkan Aug 27, 2024
90e5599
improve the performance and logging
onur-ozkan Aug 28, 2024
ecf9cea
remove redundant crates
onur-ozkan Aug 28, 2024
1d9d874
add debug logs for invalid messages
onur-ozkan Aug 28, 2024
493585a
improve response status, error logs and types
onur-ozkan Aug 28, 2024
f5c98da
inline various functions
onur-ozkan Aug 28, 2024
0aaa011
prevent brute-force attacks
onur-ozkan Aug 28, 2024
b73a0f2
handle panics
onur-ozkan Sep 2, 2024
e6f29c7
add unit test coverage for healthcheck implementation
onur-ozkan Sep 2, 2024
8a20b8c
extend healthcheck coverage to WASM
onur-ozkan Sep 2, 2024
f550550
add integration test coverage for `peer_connection_healthcheck` RPC
onur-ozkan Sep 2, 2024
9774f15
add global configuration interface for healthchecks
onur-ozkan Sep 2, 2024
6286299
add doc-comments
onur-ozkan Sep 3, 2024
14eeaee
handle our address
onur-ozkan Sep 4, 2024
b5275d1
update time related logics
onur-ozkan Sep 18, 2024
5b39e75
remove manual default value handling
onur-ozkan Sep 18, 2024
7fccb15
pack healthcheck related `Ctx` fields
onur-ozkan Sep 18, 2024
1777559
make safer ser and deser functions for bytes and peer addresses
onur-ozkan Sep 18, 2024
13fe925
separate the healthcheck processing logic and improve the performance
onur-ozkan Sep 19, 2024
1a3bb84
rename `bruteforce_shield` into `ddos_shield`
onur-ozkan Sep 19, 2024
5605a00
keep `init_p2p_context` private
onur-ozkan Sep 24, 2024
0d46b36
Merge branch 'dev' of github.com:KomodoPlatform/komodo-defi-framework…
onur-ozkan Sep 24, 2024
74c8bea
nit fixes
onur-ozkan Sep 25, 2024
de61cc2
switch to sync `Mutex`
onur-ozkan Sep 25, 2024
93efaa2
set max limit for expiration time logic
onur-ozkan Sep 25, 2024
9b08695
fix WASM lint
onur-ozkan Sep 25, 2024
ad61f31
run ddos protection logic after message verification
onur-ozkan Sep 25, 2024
b4d63f6
create `PeerAddress` as a wrapper of `libp2p::PeerId`
onur-ozkan Sep 26, 2024
8727e52
switch back to async mutexa again
onur-ozkan Sep 26, 2024
d2d9fff
implement reusable messages
onur-ozkan Sep 30, 2024
b98832c
reduce the healthcheck overhead
onur-ozkan Sep 30, 2024
3169081
use more precise error type and remove sender_peer from hc message
onur-ozkan Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#[cfg(feature = "track-ctx-pointer")]
use common::executor::Timer;
use common::executor::{abortable_queue::{AbortableQueue, WeakSpawner},
graceful_shutdown, AbortSettings, AbortableSystem, SpawnAbortable, SpawnFuture};
use common::log::{self, LogLevel, LogOnError, LogState};
use common::{cfg_native, cfg_wasm32, small_rng};
use common::{executor::{abortable_queue::{AbortableQueue, WeakSpawner},
graceful_shutdown, AbortSettings, AbortableSystem, SpawnAbortable, SpawnFuture},
expirable_map::ExpirableMap};
use futures::channel::oneshot;
use futures::lock::Mutex as AsyncMutex;
use gstuff::{try_s, Constructible, ERR, ERRL};
use lazy_static::lazy_static;
use mm2_event_stream::{controller::Controller, Event, EventStreamConfiguration};
use mm2_metrics::{MetricsArc, MetricsOps};
use primitives::hash::H160;
use rand::Rng;
use serde::Deserialize;
use serde_json::{self as json, Value as Json};
use shared_ref_counter::{SharedRc, WeakRc};
use std::any::Any;
Expand All @@ -30,7 +34,6 @@ cfg_wasm32! {
cfg_native! {
use db_common::async_sql_conn::AsyncConnection;
use db_common::sqlite::rusqlite::Connection;
use futures::lock::Mutex as AsyncMutex;
use rustls::ServerName;
use mm2_metrics::prometheus;
use mm2_metrics::MmMetricsError;
Expand All @@ -42,6 +45,36 @@ cfg_native! {
/// Default interval to export and record metrics to log.
const EXPORT_METRICS_INTERVAL: f64 = 5. * 60.;

pub struct HealthChecker {
/// Links the RPC context to the P2P context to handle health check responses.
pub response_handler: AsyncMutex<ExpirableMap<String, oneshot::Sender<()>>>,
/// This is used to record healthcheck sender peers in an expirable manner to prevent DDoS attacks.
pub ddos_shield: AsyncMutex<ExpirableMap<String, ()>>,
pub config: HealthcheckConfig,
}

#[derive(Debug, Deserialize)]
#[serde(default)]
pub struct HealthcheckConfig {
/// Required time (millisecond) to wait before processing another healthcheck request from the same peer.
pub blocking_ms_for_per_address: u64,
/// Lifetime of the message.
/// Do not change this unless you know what you are doing.
pub message_expiration_secs: u64,
/// Maximum time (milliseconds) to wait for healthcheck response.
pub timeout_secs: u64,
}

impl Default for HealthcheckConfig {
fn default() -> Self {
Self {
blocking_ms_for_per_address: 750,
message_expiration_secs: 10,
timeout_secs: 10,
}
}
}

/// MarketMaker state, shared between the various MarketMaker threads.
///
/// Every MarketMaker has one and only one instance of `MmCtx`.
Expand Down Expand Up @@ -142,6 +175,7 @@ pub struct MmCtx {
/// asynchronous handle for rusqlite connection.
#[cfg(not(target_arch = "wasm32"))]
pub async_sqlite_connection: Constructible<Arc<AsyncMutex<AsyncConnection>>>,
pub health_checker: HealthChecker,
}

impl MmCtx {
Expand Down Expand Up @@ -191,6 +225,11 @@ impl MmCtx {
nft_ctx: Mutex::new(None),
#[cfg(not(target_arch = "wasm32"))]
async_sqlite_connection: Constructible::default(),
health_checker: HealthChecker {
response_handler: AsyncMutex::new(ExpirableMap::default()),
ddos_shield: AsyncMutex::new(ExpirableMap::default()),
config: HealthcheckConfig::default(),
},
}
}

Expand Down Expand Up @@ -776,6 +815,13 @@ impl MmCtxBuilder {
.expect("Invalid json value in 'event_stream_configuration'.");
ctx.event_stream_configuration = Some(event_stream_configuration);
}

let healthcheck_config = &ctx.conf["healthcheck_config"];
if !healthcheck_config.is_null() {
let healthcheck_config: HealthcheckConfig =
json::from_value(healthcheck_config.clone()).expect("Invalid json value in 'healthcheck_config'.");
borngraced marked this conversation as resolved.
Show resolved Hide resolved
ctx.health_checker.config = healthcheck_config;
}
}

#[cfg(target_arch = "wasm32")]
Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bitcrypto = { path = "../mm2_bitcoin/crypto" }
blake2 = "0.10.6"
bytes = "0.4"
chain = { path = "../mm2_bitcoin/chain" }
chrono = "0.4"
cfg-if = "1.0"
coins = { path = "../coins" }
coins_activation = { path = "../coins_activation" }
Expand Down
Loading
Loading