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 all 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
22 changes: 18 additions & 4 deletions mm2src/common/expirable_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,26 @@ pub struct ExpirableEntry<V> {
}

impl<V> ExpirableEntry<V> {
#[inline(always)]
pub fn new(v: V, exp: Duration) -> Self {
Self {
expires_at: Instant::now() + exp,
value: v,
}
}

#[inline(always)]
pub fn get_element(&self) -> &V { &self.value }

#[inline(always)]
pub fn update_value(&mut self, v: V) { self.value = v }

#[inline(always)]
pub fn update_expiration(&mut self, expires_at: Instant) { self.expires_at = expires_at }

/// Checks whether entry has longer ttl than the given one.
#[inline(always)]
pub fn has_longer_life_than(&self, min_ttl: Duration) -> bool { self.expires_at > Instant::now() + min_ttl }
}

impl<K: Eq + Hash, V> Default for ExpirableMap<K, V> {
Expand Down Expand Up @@ -47,10 +64,7 @@ impl<K: Eq + Hash, V> ExpirableMap<K, V> {
/// If a value already exists for the given key, it will be updated and then
/// the old one will be returned.
pub fn insert(&mut self, k: K, v: V, exp: Duration) -> Option<V> {
let entry = ExpirableEntry {
expires_at: Instant::now() + exp,
value: v,
};
let entry = ExpirableEntry::new(v, exp);

self.0.insert(k, entry).map(|v| v.value)
}
Expand Down
11 changes: 8 additions & 3 deletions mm2src/mm2_core/src/mm_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#[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};
Expand All @@ -30,7 +33,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 Down Expand Up @@ -142,6 +144,8 @@ pub struct MmCtx {
/// asynchronous handle for rusqlite connection.
#[cfg(not(target_arch = "wasm32"))]
pub async_sqlite_connection: Constructible<Arc<AsyncMutex<AsyncConnection>>>,
/// Links the RPC context to the P2P context to handle health check responses.
pub healthcheck_response_handler: AsyncMutex<ExpirableMap<String, oneshot::Sender<()>>>,
}

impl MmCtx {
Expand Down Expand Up @@ -191,6 +195,7 @@ impl MmCtx {
nft_ctx: Mutex::new(None),
#[cfg(not(target_arch = "wasm32"))]
async_sqlite_connection: Constructible::default(),
healthcheck_response_handler: AsyncMutex::new(ExpirableMap::default()),
}
}

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