From a3e500db12191ce3cd96190116202eabc8ecb626 Mon Sep 17 00:00:00 2001 From: Mykhailo Kremniov Date: Wed, 24 Jan 2024 10:50:17 +0200 Subject: [PATCH] Discouraged peers are now disconnected automatically; Peers are no longer automatically banned; Ban duration is now always specified explicitly by the user; Time formatting improvements; Wallet & rpc commands for printing reserved and discouraged addresses were added; Printing banned and discouraged addresses now also prints the expiration time; --- Cargo.lock | 8 + Cargo.toml | 1 + chainstate/src/detail/mod.rs | 2 +- common/src/chain/transaction/printout.rs | 5 +- common/src/primitives/time.rs | 67 +++++- dns-server/src/main.rs | 4 +- node-lib/src/config_files/mod.rs | 8 +- node-lib/src/config_files/p2p.rs | 8 - node-lib/src/options.rs | 10 +- node-lib/tests/cli.rs | 23 +- p2p/benches/benches.rs | 7 +- p2p/src/ban_config.rs | 25 +- p2p/src/error.rs | 9 - p2p/src/interface/p2p_interface.rs | 11 +- p2p/src/interface/p2p_interface_impl.rs | 30 ++- .../p2p_interface_impl_delegation.rs | 21 +- p2p/src/peer_manager/config.rs | 2 +- p2p/src/peer_manager/mod.rs | 81 ++++--- p2p/src/peer_manager/peerdb/mod.rs | 28 ++- p2p/src/peer_manager/peerdb/tests.rs | 214 +++++++++++++++++- p2p/src/peer_manager/peers_eviction/mod.rs | 4 + p2p/src/peer_manager/tests/ban.rs | 88 +++---- p2p/src/peer_manager/tests/discouragement.rs | 159 ++++++++++++- p2p/src/peer_manager/tests/utils.rs | 20 +- p2p/src/peer_manager/tests/whitelist.rs | 6 +- p2p/src/peer_manager_event.rs | 18 +- p2p/src/rpc.rs | 33 ++- p2p/src/sync/peer_v2/block_manager.rs | 4 +- p2p/src/sync/tests/block_response.rs | 7 +- p2p/src/sync/tests/helpers/mod.rs | 14 +- p2p/src/sync/tests/helpers/test_node_group.rs | 4 +- p2p/src/tests/correct_handshake.rs | 4 +- p2p/src/tests/helpers/test_node.rs | 5 +- p2p/src/tests/incorrect_handshake.rs | 19 +- wallet/src/wallet/mod.rs | 2 +- wallet/wallet-cli-lib/Cargo.toml | 2 + .../src/commands/helper_types.rs | 17 ++ wallet/wallet-cli-lib/src/commands/mod.rs | 52 ++++- .../wallet-controller/src/sync/tests/mod.rs | 21 +- .../src/handles_client/mod.rs | 32 ++- wallet/wallet-node-client/src/node_traits.rs | 19 +- .../src/rpc_client/client_impl.rs | 29 ++- .../src/rpc_client/cold_wallet_client.rs | 25 +- wallet/wallet-rpc-lib/src/rpc/interface.rs | 20 +- wallet/wallet-rpc-lib/src/rpc/mod.rs | 26 ++- wallet/wallet-rpc-lib/src/rpc/server_impl.rs | 25 +- 46 files changed, 915 insertions(+), 304 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0dd4ad8d58..879254118a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2714,6 +2714,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.28" @@ -7517,6 +7523,8 @@ dependencies = [ "directories", "futures", "hex", + "humantime", + "itertools 0.12.0", "logging", "mempool", "node-comm", diff --git a/Cargo.toml b/Cargo.toml index 3251bc066c..2d8d42eafb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -139,6 +139,7 @@ criterion = "0.5" crossterm = "0.27" derive_more = "0.99" directories = "5.0" +humantime = "2.1" dyn-clone = "1.0" enum-iterator = "1.4" expect-test = "1.3" diff --git a/chainstate/src/detail/mod.rs b/chainstate/src/detail/mod.rs index 261af811f2..eb396a3368 100644 --- a/chainstate/src/detail/mod.rs +++ b/chainstate/src/detail/mod.rs @@ -537,7 +537,7 @@ impl Chainstate bi.block_id(), bi.block_height(), bi.block_timestamp(), - bi.block_timestamp().into_time().as_standard_printable_time(), + bi.block_timestamp().into_time(), ); self.update_initial_block_download_flag() diff --git a/common/src/chain/transaction/printout.rs b/common/src/chain/transaction/printout.rs index 2eb3e52272..96b04751cf 100644 --- a/common/src/chain/transaction/printout.rs +++ b/common/src/chain/transaction/printout.rs @@ -61,10 +61,7 @@ pub fn transaction_summary(tx: &Transaction, chain_config: &ChainConfig) -> Stri }; let fmt_timelock = |tl: &OutputTimeLock| match tl { OutputTimeLock::UntilHeight(h) => format!("OutputTimeLock::UntilHeight({h})"), - OutputTimeLock::UntilTime(t) => format!( - "OutputTimeLock::UntilTime({})", - t.into_time().as_standard_printable_time() - ), + OutputTimeLock::UntilTime(t) => format!("OutputTimeLock::UntilTime({})", t.into_time()), OutputTimeLock::ForBlockCount(n) => format!("OutputTimeLock::ForBlockCount({n} blocks)"), OutputTimeLock::ForSeconds(secs) => { format!("OutputTimeLock::ForSeconds({secs} seconds)") diff --git a/common/src/primitives/time.rs b/common/src/primitives/time.rs index 34d268271f..1d87a891ba 100644 --- a/common/src/primitives/time.rs +++ b/common/src/primitives/time.rs @@ -13,9 +13,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fmt::{Debug, Display}; use std::sync::atomic::{AtomicU64, Ordering}; use std::time::{Duration, SystemTime}; +use chrono::TimeZone; +use serde::{Deserialize, Serialize}; + pub fn duration_to_int(d: &Duration) -> Result { let r = d.as_millis().try_into()?; Ok(r) @@ -61,7 +65,7 @@ pub fn get_time() -> Time { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct Time { /// Time, stored as duration since SystemTime::UNIX_EPOCH time: Duration, @@ -102,13 +106,13 @@ impl Time { self.time.saturating_sub(t.time) } - pub fn as_absolute_time(&self) -> SystemTime { - SystemTime::UNIX_EPOCH + self.time - } - - pub fn as_standard_printable_time(&self) -> String { - let datetime: chrono::DateTime = self.as_absolute_time().into(); - format!("{}", datetime.format("%Y-%m-%d %H:%M:%S")) + pub fn as_absolute_time(&self) -> Option> { + TryInto::::try_into(self.time.as_secs()).ok().and_then(|secs| { + // Note: chrono::DateTime supports time values up to about 262,000 years away + // from the common era, which is still way below i64::MAX; i.e. timestamp_opt + // may still return None here. + chrono::Utc.timestamp_opt(secs, self.time.subsec_nanos()).single() + }) } } @@ -136,6 +140,33 @@ impl std::ops::Sub