From 35af84a0f47bba2d350d0a34ae01a3460f2f2312 Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Mon, 7 Oct 2024 09:53:48 +0530 Subject: [PATCH 1/6] added writing peer state to file --- .../core/consensus/blockchain_sync_state.rs | 4 +- saito-core/src/core/consensus/limit/mod.rs | 2 - saito-core/src/core/consensus/mod.rs | 5 +- .../block_depth_limit_checker.rs | 0 saito-core/src/core/consensus/peers/mod.rs | 6 +++ .../src/core/consensus/{ => peers}/peer.rs | 8 +-- .../consensus/{ => peers}/peer_collection.rs | 4 +- .../consensus/{ => peers}/peer_service.rs | 2 +- .../core/consensus/peers/peer_state_writer.rs | 51 +++++++++++++++++++ .../{limit => peers}/rate_limiter.rs | 2 +- saito-core/src/core/io/interface_io.rs | 2 +- saito-core/src/core/io/network.rs | 12 ++--- saito-core/src/core/msg/handshake.rs | 2 +- saito-core/src/core/msg/message.rs | 2 +- saito-core/src/core/routing_thread.rs | 19 +++++-- saito-core/src/core/util/test/node_tester.rs | 3 +- .../src/core/util/test/test_io_handler.rs | 6 +-- saito-core/src/core/util/test/test_manager.rs | 2 +- saito-core/src/core/verification_thread.rs | 2 +- saito-rust/src/main.rs | 3 +- saito-rust/src/network_controller.rs | 2 +- saito-rust/src/rust_io_handler.rs | 2 +- saito-spammer/src/main.rs | 3 +- saito-spammer/src/spammer.rs | 2 +- saito-spammer/src/transaction_generator.rs | 2 +- saito-wasm/src/saitowasm.rs | 3 +- saito-wasm/src/wasm_io_handler.rs | 2 +- saito-wasm/src/wasm_peer.rs | 2 +- saito-wasm/src/wasm_peer_service.rs | 2 +- 29 files changed, 114 insertions(+), 43 deletions(-) delete mode 100644 saito-core/src/core/consensus/limit/mod.rs rename saito-core/src/core/consensus/{limit => peers}/block_depth_limit_checker.rs (100%) create mode 100644 saito-core/src/core/consensus/peers/mod.rs rename saito-core/src/core/consensus/{ => peers}/peer.rs (98%) rename saito-core/src/core/consensus/{ => peers}/peer_collection.rs (93%) rename saito-core/src/core/consensus/{ => peers}/peer_service.rs (98%) create mode 100644 saito-core/src/core/consensus/peers/peer_state_writer.rs rename saito-core/src/core/consensus/{limit => peers}/rate_limiter.rs (98%) diff --git a/saito-core/src/core/consensus/blockchain_sync_state.rs b/saito-core/src/core/consensus/blockchain_sync_state.rs index f77f785ca..3117591cd 100644 --- a/saito-core/src/core/consensus/blockchain_sync_state.rs +++ b/saito-core/src/core/consensus/blockchain_sync_state.rs @@ -3,7 +3,7 @@ use std::collections::VecDeque; use std::sync::Arc; use crate::core::consensus::blockchain::Blockchain; -use crate::core::consensus::peer_collection::PeerCollection; +use crate::core::consensus::peers::peer_collection::PeerCollection; use ahash::HashMap; use log::{debug, error, info, trace, warn}; use tokio::sync::RwLock; @@ -173,7 +173,7 @@ impl BlockchainSyncState { let mut allowed_quota = self.batch_size - fetching_count; for block_data in deq.iter_mut() { - // we limit concurrent fetches to this amount + // we peers concurrent fetches to this amount if allowed_quota == 0 { // we have reached allowed concurrent fetches quota. break; diff --git a/saito-core/src/core/consensus/limit/mod.rs b/saito-core/src/core/consensus/limit/mod.rs deleted file mode 100644 index dfd9284d8..000000000 --- a/saito-core/src/core/consensus/limit/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod block_depth_limit_checker; -pub mod rate_limiter; diff --git a/saito-core/src/core/consensus/mod.rs b/saito-core/src/core/consensus/mod.rs index 83eef9cbb..28360e771 100644 --- a/saito-core/src/core/consensus/mod.rs +++ b/saito-core/src/core/consensus/mod.rs @@ -6,12 +6,9 @@ pub mod burnfee; pub mod context; pub mod golden_ticket; pub mod hop; -pub mod limit; pub mod mempool; pub mod merkle; -pub mod peer; -pub mod peer_collection; -pub mod peer_service; +pub mod peers; pub mod ringitem; pub mod slip; pub mod transaction; diff --git a/saito-core/src/core/consensus/limit/block_depth_limit_checker.rs b/saito-core/src/core/consensus/peers/block_depth_limit_checker.rs similarity index 100% rename from saito-core/src/core/consensus/limit/block_depth_limit_checker.rs rename to saito-core/src/core/consensus/peers/block_depth_limit_checker.rs diff --git a/saito-core/src/core/consensus/peers/mod.rs b/saito-core/src/core/consensus/peers/mod.rs new file mode 100644 index 000000000..de16ed8c3 --- /dev/null +++ b/saito-core/src/core/consensus/peers/mod.rs @@ -0,0 +1,6 @@ +pub mod block_depth_limit_checker; +pub mod peer; +pub mod peer_collection; +pub mod peer_service; +pub mod peer_state_writer; +pub mod rate_limiter; diff --git a/saito-core/src/core/consensus/peer.rs b/saito-core/src/core/consensus/peers/peer.rs similarity index 98% rename from saito-core/src/core/consensus/peer.rs rename to saito-core/src/core/consensus/peers/peer.rs index ba3d6d90e..2954cd2f6 100644 --- a/saito-core/src/core/consensus/peer.rs +++ b/saito-core/src/core/consensus/peers/peer.rs @@ -1,6 +1,6 @@ -use crate::core::consensus::limit::block_depth_limit_checker::BlockDepthLimitChecker; -use crate::core::consensus::limit::rate_limiter::RateLimiter; -use crate::core::consensus::peer_service::PeerService; +use crate::core::consensus::peers::block_depth_limit_checker::BlockDepthLimitChecker; +use crate::core::consensus::peers::peer_service::PeerService; +use crate::core::consensus::peers::rate_limiter::RateLimiter; use crate::core::consensus::wallet::Wallet; use crate::core::defs::{ PeerIndex, PrintForLog, SaitoHash, SaitoPublicKey, Timestamp, WS_KEEP_ALIVE_PERIOD, @@ -426,7 +426,7 @@ impl Peer { #[cfg(test)] mod tests { - use crate::core::consensus::peer::{Peer, PeerStatus}; + use crate::core::consensus::peers::peer::{Peer, PeerStatus}; use crate::core::process::version::Version; use std::cmp::Ordering; diff --git a/saito-core/src/core/consensus/peer_collection.rs b/saito-core/src/core/consensus/peers/peer_collection.rs similarity index 93% rename from saito-core/src/core/consensus/peer_collection.rs rename to saito-core/src/core/consensus/peers/peer_collection.rs index 41f9f2935..092facf84 100644 --- a/saito-core/src/core/consensus/peer_collection.rs +++ b/saito-core/src/core/consensus/peers/peer_collection.rs @@ -1,4 +1,5 @@ -use crate::core::consensus::peer::{Peer, PeerStatus}; +use crate::core::consensus::peers::peer::{Peer, PeerStatus}; +use crate::core::consensus::peers::peer_state_writer::PeerStateWriter; use crate::core::defs::{PeerIndex, SaitoPublicKey, Timestamp}; use std::collections::HashMap; use std::time::Duration; @@ -21,6 +22,7 @@ pub struct PeerCollection { pub index_to_peers: HashMap, pub address_to_peers: HashMap, pub peer_counter: PeerCounter, + pub peer_state_writer: PeerStateWriter, } impl PeerCollection { diff --git a/saito-core/src/core/consensus/peer_service.rs b/saito-core/src/core/consensus/peers/peer_service.rs similarity index 98% rename from saito-core/src/core/consensus/peer_service.rs rename to saito-core/src/core/consensus/peers/peer_service.rs index 1c3d6f9c6..b7144fa72 100644 --- a/saito-core/src/core/consensus/peer_service.rs +++ b/saito-core/src/core/consensus/peers/peer_service.rs @@ -98,7 +98,7 @@ impl PeerService { #[cfg(test)] mod tests { - use crate::core::consensus::peer_service::PeerService; + use crate::core::consensus::peers::peer_service::PeerService; #[test] fn test_serialize() { diff --git a/saito-core/src/core/consensus/peers/peer_state_writer.rs b/saito-core/src/core/consensus/peers/peer_state_writer.rs new file mode 100644 index 000000000..474ef4a20 --- /dev/null +++ b/saito-core/src/core/consensus/peers/peer_state_writer.rs @@ -0,0 +1,51 @@ +use crate::core::defs::Timestamp; +use crate::core::io::interface_io::InterfaceIO; +use std::io::Error; +use std::net::IpAddr; +use std::time::Duration; + +const PEER_STATE_FILENAME: &str = "./data/peer_state.txt"; + +pub(crate) const PEER_STATE_WRITE_PERIOD: Timestamp = + Duration::from_secs(5).as_millis() as Timestamp; + +#[derive(Clone, Debug, Default)] +pub(crate) struct PeerStateWriter { + lines: Vec, +} + +impl PeerStateWriter { + /// Writes peer state data to the file and clears collected state + /// + /// # Arguments + /// + /// * `io_handler`: + /// + /// returns: Result<(), Error> + /// + /// # Examples + /// + /// ``` + /// + /// ``` + pub(crate) async fn write_state( + &mut self, + io_handler: &mut Box, + ) -> Result<(), Error> { + io_handler.write_value(PEER_STATE_FILENAME, &[]).await?; + + for line in self.lines.drain(..) { + io_handler + .append_value(PEER_STATE_FILENAME, (line + "\r\n").as_bytes()) + .await? + } + + io_handler.flush_data(PEER_STATE_FILENAME).await?; + + Ok(()) + } + + pub(crate) fn add_line(&mut self, line: String) { + self.lines.push(line); + } +} diff --git a/saito-core/src/core/consensus/limit/rate_limiter.rs b/saito-core/src/core/consensus/peers/rate_limiter.rs similarity index 98% rename from saito-core/src/core/consensus/limit/rate_limiter.rs rename to saito-core/src/core/consensus/peers/rate_limiter.rs index ecf9253d7..eabcebb56 100644 --- a/saito-core/src/core/consensus/limit/rate_limiter.rs +++ b/saito-core/src/core/consensus/peers/rate_limiter.rs @@ -20,7 +20,7 @@ impl RateLimiter { } pub fn has_limit_exceeded(&mut self, current_time: Timestamp) -> bool { - // TODO : current implementation allows twice the limit from spikes. a sliding window implementation would be better I think. + // TODO : current implementation allows twice the peers from spikes. a sliding window implementation would be better I think. if current_time.saturating_sub(self.last_request_time) > self.window { self.request_count = 0; self.last_request_time = current_time; diff --git a/saito-core/src/core/io/interface_io.rs b/saito-core/src/core/io/interface_io.rs index 3dc8fcd6c..c9659e1c1 100644 --- a/saito-core/src/core/io/interface_io.rs +++ b/saito-core/src/core/io/interface_io.rs @@ -3,7 +3,7 @@ use std::io::Error; use async_trait::async_trait; -use crate::core::consensus::peer_service::PeerService; +use crate::core::consensus::peers::peer_service::PeerService; use crate::core::consensus::wallet::Wallet; use crate::core::defs::{BlockId, PeerIndex, SaitoHash, SaitoPublicKey}; use crate::core::process::version::Version; diff --git a/saito-core/src/core/io/network.rs b/saito-core/src/core/io/network.rs index 257e72bcf..f870ec553 100644 --- a/saito-core/src/core/io/network.rs +++ b/saito-core/src/core/io/network.rs @@ -7,8 +7,8 @@ use tokio::sync::RwLock; use crate::core::consensus::block::Block; use crate::core::consensus::blockchain::Blockchain; use crate::core::consensus::mempool::Mempool; -use crate::core::consensus::peer::{Peer, PeerStatus}; -use crate::core::consensus::peer_collection::PeerCollection; +use crate::core::consensus::peers::peer::{Peer, PeerStatus}; +use crate::core::consensus::peers::peer_collection::PeerCollection; use crate::core::consensus::transaction::{Transaction, TransactionType}; use crate::core::consensus::wallet::Wallet; use crate::core::defs::{BlockId, PeerIndex, PrintForLog, SaitoHash, SaitoPublicKey, Timestamp}; @@ -198,7 +198,7 @@ impl Network { peer.handshake_limiter.increase(); if peer.has_handshake_limit_exceeded(current_time) { warn!( - "peer {:?} exceeded rate limit for handshake challenge", + "peer {:?} exceeded rate peers for handshake challenge", peer_index ); return; @@ -237,7 +237,7 @@ impl Network { peer.handshake_limiter.increase(); if peer.has_handshake_limit_exceeded(current_time) { warn!( - "peer {:?} exceeded rate limit for handshake challenge", + "peer {:?} exceeded rate peers for handshake challenge", peer_index ); return; @@ -304,10 +304,10 @@ impl Network { let peer = peers.index_to_peers.get_mut(&peer_index); if let Some(peer) = peer { - // Check rate limit + // Check rate peers peer.key_list_limiter.increase(); if peer.has_key_list_limit_exceeded(current_time) { - debug!("peer {:?} exceeded rate limit for key list", peer_index); + debug!("peer {:?} exceeded rate peers for key list", peer_index); return Err(Error::from(ErrorKind::Other)); } else { debug!("can make request") diff --git a/saito-core/src/core/msg/handshake.rs b/saito-core/src/core/msg/handshake.rs index ad6890da0..aab183183 100644 --- a/saito-core/src/core/msg/handshake.rs +++ b/saito-core/src/core/msg/handshake.rs @@ -2,7 +2,7 @@ use std::io::{Error, ErrorKind}; use log::{trace, warn}; -use crate::core::consensus::peer_service::PeerService; +use crate::core::consensus::peers::peer_service::PeerService; use crate::core::defs::{SaitoHash, SaitoPublicKey, SaitoSignature}; use crate::core::process::version::Version; use crate::core::util::serialize::Serialize; diff --git a/saito-core/src/core/msg/message.rs b/saito-core/src/core/msg/message.rs index 44627d5c7..63e953ded 100644 --- a/saito-core/src/core/msg/message.rs +++ b/saito-core/src/core/msg/message.rs @@ -4,7 +4,7 @@ use std::io::{Error, ErrorKind}; use log::{error, warn}; use crate::core::consensus::block::{Block, BlockType}; -use crate::core::consensus::peer_service::PeerService; +use crate::core::consensus::peers::peer_service::PeerService; use crate::core::consensus::transaction::Transaction; use crate::core::defs::{BlockHash, BlockId, ForkId, SaitoPublicKey}; use crate::core::msg::api_message::ApiMessage; diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index 55e2eea95..21caae0a5 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -9,7 +9,8 @@ use tokio::sync::{RwLock, RwLockReadGuard}; use crate::core::consensus::blockchain::Blockchain; use crate::core::consensus::blockchain_sync_state::BlockchainSyncState; use crate::core::consensus::mempool::Mempool; -use crate::core::consensus::peer_service::PeerService; +use crate::core::consensus::peers::peer_service::PeerService; +use crate::core::consensus::peers::peer_state_writer::PEER_STATE_WRITE_PERIOD; use crate::core::consensus::wallet::Wallet; use crate::core::consensus_thread::ConsensusEvent; use crate::core::defs::{ @@ -89,6 +90,7 @@ pub struct RoutingThread { pub network: Network, pub reconnection_timer: Timestamp, pub peer_removal_timer: Timestamp, + pub peer_file_write_timer: Timestamp, pub stats: RoutingStats, pub senders_to_verification: Vec>, pub last_verification_thread_index: usize, @@ -538,7 +540,7 @@ impl ProcessEvent for RoutingThread { let time = self.timer.get_timestamp_in_ms(); peer.message_limiter.increase(); if peer.has_message_limit_exceeded(time) { - info!("limit exceeded for messages from peer : {:?}", peer_index); + info!("peers exceeded for messages from peer : {:?}", peer_index); return None; } } @@ -590,7 +592,7 @@ impl ProcessEvent for RoutingThread { peer.invalid_block_limiter.increase(); if peer.has_invalid_block_limit_exceeded(time) { info!( - "limit exceeded for invalid blocks from peer : {:?}. disconnecting peer...", + "peers exceeded for invalid blocks from peer : {:?}. disconnecting peer...", peer_index ); self.network @@ -654,6 +656,17 @@ impl ProcessEvent for RoutingThread { self.peer_removal_timer = 0; } + self.peer_file_write_timer += duration_value; + if self.peer_file_write_timer >= PEER_STATE_WRITE_PERIOD { + let mut peers = self.network.peer_lock.write().await; + peers + .peer_state_writer + .write_state(&mut self.network.io_interface) + .await + .unwrap(); + self.peer_file_write_timer = 0; + } + if work_done { return Some(()); } diff --git a/saito-core/src/core/util/test/node_tester.rs b/saito-core/src/core/util/test/node_tester.rs index 628949419..160030111 100644 --- a/saito-core/src/core/util/test/node_tester.rs +++ b/saito-core/src/core/util/test/node_tester.rs @@ -5,7 +5,7 @@ pub mod test { use crate::core::consensus::blockchain_sync_state::BlockchainSyncState; use crate::core::consensus::context::Context; use crate::core::consensus::mempool::Mempool; - use crate::core::consensus::peer_collection::PeerCollection; + use crate::core::consensus::peers::peer_collection::PeerCollection; use crate::core::consensus::transaction::Transaction; use crate::core::consensus::wallet::Wallet; @@ -185,6 +185,7 @@ pub mod test { ), reconnection_timer: 0, peer_removal_timer: 0, + peer_file_write_timer: 0, stats: RoutingStats::new(sender_to_stat.clone()), senders_to_verification: vec![sender_to_verification.clone()], last_verification_thread_index: 0, diff --git a/saito-core/src/core/util/test/test_io_handler.rs b/saito-core/src/core/util/test/test_io_handler.rs index 2b68b7059..b5d92f573 100644 --- a/saito-core/src/core/util/test/test_io_handler.rs +++ b/saito-core/src/core/util/test/test_io_handler.rs @@ -9,7 +9,7 @@ pub mod test { use tokio::fs::File; use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use crate::core::consensus::peer_service::PeerService; + use crate::core::consensus::peers::peer_service::PeerService; use crate::core::consensus::wallet::Wallet; use crate::core::defs::{BlockId, PeerIndex, SaitoHash, BLOCK_FILE_EXTENSION}; use crate::core::io::interface_io::{InterfaceEvent, InterfaceIO}; @@ -86,11 +86,11 @@ pub mod test { } async fn append_value(&mut self, _key: &str, _value: &[u8]) -> Result<(), Error> { - todo!() + Ok(()) } async fn flush_data(&mut self, _key: &str) -> Result<(), Error> { - todo!() + Ok(()) } async fn read_value(&self, key: &str) -> Result, Error> { diff --git a/saito-core/src/core/util/test/test_manager.rs b/saito-core/src/core/util/test/test_manager.rs index 4503aca32..f0b3fcde2 100644 --- a/saito-core/src/core/util/test/test_manager.rs +++ b/saito-core/src/core/util/test/test_manager.rs @@ -44,7 +44,7 @@ pub mod test { use crate::core::consensus::blockchain::{AddBlockResult, Blockchain, DEFAULT_SOCIAL_STAKE}; use crate::core::consensus::golden_ticket::GoldenTicket; use crate::core::consensus::mempool::Mempool; - use crate::core::consensus::peer_collection::PeerCollection; + use crate::core::consensus::peers::peer_collection::PeerCollection; use crate::core::consensus::slip::Slip; use crate::core::consensus::transaction::{Transaction, TransactionType}; use crate::core::consensus::wallet::Wallet; diff --git a/saito-core/src/core/verification_thread.rs b/saito-core/src/core/verification_thread.rs index 04efb9e16..3effa561d 100644 --- a/saito-core/src/core/verification_thread.rs +++ b/saito-core/src/core/verification_thread.rs @@ -10,7 +10,7 @@ use tokio::sync::RwLock; use crate::core::consensus::block::Block; use crate::core::consensus::blockchain::Blockchain; -use crate::core::consensus::peer_collection::PeerCollection; +use crate::core::consensus::peers::peer_collection::PeerCollection; use crate::core::consensus::transaction::Transaction; use crate::core::consensus::wallet::Wallet; use crate::core::consensus_thread::ConsensusEvent; diff --git a/saito-rust/src/main.rs b/saito-rust/src/main.rs index 4862fed6c..71f5830c3 100644 --- a/saito-rust/src/main.rs +++ b/saito-rust/src/main.rs @@ -26,7 +26,7 @@ use tracing_subscriber::Layer; use saito_core::core::consensus::blockchain::Blockchain; use saito_core::core::consensus::blockchain_sync_state::BlockchainSyncState; use saito_core::core::consensus::context::Context; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::consensus_thread::{ConsensusEvent, ConsensusStats, ConsensusThread}; use saito_core::core::defs::{ @@ -366,6 +366,7 @@ async fn run_routing_event_processor( ), reconnection_timer: 0, peer_removal_timer: 0, + peer_file_write_timer: 0, stats: RoutingStats::new(sender_to_stat.clone()), senders_to_verification: senders, last_verification_thread_index: 0, diff --git a/saito-rust/src/network_controller.rs b/saito-rust/src/network_controller.rs index d2f9379c4..24b0f0a49 100644 --- a/saito-rust/src/network_controller.rs +++ b/saito-rust/src/network_controller.rs @@ -24,7 +24,7 @@ use warp::Filter; use saito_core::core::consensus::block::{Block, BlockType}; use saito_core::core::consensus::blockchain::Blockchain; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::defs::{ BlockId, PeerIndex, PrintForLog, SaitoHash, SaitoPublicKey, StatVariable, BLOCK_FILE_EXTENSION, STAT_BIN_COUNT, diff --git a/saito-rust/src/rust_io_handler.rs b/saito-rust/src/rust_io_handler.rs index f318cdd63..a39c77a96 100644 --- a/saito-rust/src/rust_io_handler.rs +++ b/saito-rust/src/rust_io_handler.rs @@ -11,7 +11,7 @@ use tokio::fs::{File, OpenOptions}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::sync::mpsc::Sender; -use saito_core::core::consensus::peer_service::PeerService; +use saito_core::core::consensus::peers::peer_service::PeerService; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::defs::{BlockId, PeerIndex, SaitoHash, BLOCK_FILE_EXTENSION}; use saito_core::core::io::interface_io::{InterfaceEvent, InterfaceIO}; diff --git a/saito-spammer/src/main.rs b/saito-spammer/src/main.rs index 3f7035b9a..d939646e0 100644 --- a/saito-spammer/src/main.rs +++ b/saito-spammer/src/main.rs @@ -18,7 +18,7 @@ use tracing_subscriber::Layer; use saito_core::core::consensus::blockchain::Blockchain; use saito_core::core::consensus::blockchain_sync_state::BlockchainSyncState; use saito_core::core::consensus::context::Context; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::consensus_thread::{ConsensusEvent, ConsensusStats, ConsensusThread}; use saito_core::core::defs::{ @@ -319,6 +319,7 @@ async fn run_routing_event_processor( ), reconnection_timer: 0, peer_removal_timer: 0, + peer_file_write_timer: 0, stats: RoutingStats::new(sender_to_stat.clone()), senders_to_verification: senders, last_verification_thread_index: 0, diff --git a/saito-spammer/src/spammer.rs b/saito-spammer/src/spammer.rs index b731dfc28..c772dd4bc 100644 --- a/saito-spammer/src/spammer.rs +++ b/saito-spammer/src/spammer.rs @@ -7,7 +7,7 @@ use tokio::sync::mpsc::{Receiver, Sender}; use tokio::sync::RwLock; use saito_core::core::consensus::blockchain::Blockchain; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::consensus::transaction::Transaction; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::defs::Currency; diff --git a/saito-spammer/src/transaction_generator.rs b/saito-spammer/src/transaction_generator.rs index 54e6afd93..3142ce61c 100644 --- a/saito-spammer/src/transaction_generator.rs +++ b/saito-spammer/src/transaction_generator.rs @@ -8,7 +8,7 @@ use tokio::sync::mpsc::Sender; use tokio::sync::RwLock; use saito_core::core::consensus::blockchain::Blockchain; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::consensus::slip::{Slip, SLIP_SIZE}; use saito_core::core::consensus::transaction::Transaction; use saito_core::core::consensus::wallet::Wallet; diff --git a/saito-wasm/src/saitowasm.rs b/saito-wasm/src/saitowasm.rs index 46c906d13..55b919d38 100644 --- a/saito-wasm/src/saitowasm.rs +++ b/saito-wasm/src/saitowasm.rs @@ -10,7 +10,7 @@ use saito_core::core::consensus::blockchain::Blockchain; use saito_core::core::consensus::blockchain_sync_state::BlockchainSyncState; use saito_core::core::consensus::context::Context; use saito_core::core::consensus::mempool::Mempool; -use saito_core::core::consensus::peer_collection::PeerCollection; +use saito_core::core::consensus::peers::peer_collection::PeerCollection; use saito_core::core::consensus::transaction::Transaction; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::consensus_thread::{ConsensusEvent, ConsensusStats, ConsensusThread}; @@ -121,6 +121,7 @@ pub fn new(haste_multiplier: u64, enable_stats: bool) -> SaitoWasm { ), reconnection_timer: 0, peer_removal_timer: 0, + peer_file_write_timer: 0, stats: RoutingStats::new(sender_to_stat.clone()), senders_to_verification: vec![sender_to_verification.clone()], last_verification_thread_index: 0, diff --git a/saito-wasm/src/wasm_io_handler.rs b/saito-wasm/src/wasm_io_handler.rs index 7316460e6..b3b5f9db1 100644 --- a/saito-wasm/src/wasm_io_handler.rs +++ b/saito-wasm/src/wasm_io_handler.rs @@ -7,7 +7,7 @@ use log::{error, trace}; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; -use saito_core::core::consensus::peer_service::PeerService; +use saito_core::core::consensus::peers::peer_service::PeerService; use saito_core::core::consensus::wallet::Wallet; use saito_core::core::defs::{BlockId, PeerIndex, PrintForLog, SaitoHash}; use saito_core::core::io::interface_io::{InterfaceEvent, InterfaceIO}; diff --git a/saito-wasm/src/wasm_peer.rs b/saito-wasm/src/wasm_peer.rs index 9b159b7bc..f69deaff1 100644 --- a/saito-wasm/src/wasm_peer.rs +++ b/saito-wasm/src/wasm_peer.rs @@ -4,7 +4,7 @@ use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; use crate::wasm_peer_service::WasmPeerService; -use saito_core::core::consensus::peer::Peer; +use saito_core::core::consensus::peers::peer::Peer; use saito_core::core::defs::{PeerIndex, PrintForLog}; #[wasm_bindgen] diff --git a/saito-wasm/src/wasm_peer_service.rs b/saito-wasm/src/wasm_peer_service.rs index 0b7e4baab..db0eaa1ca 100644 --- a/saito-wasm/src/wasm_peer_service.rs +++ b/saito-wasm/src/wasm_peer_service.rs @@ -1,5 +1,5 @@ use js_sys::JsString; -use saito_core::core::consensus::peer_service::PeerService; +use saito_core::core::consensus::peers::peer_service::PeerService; use serde::{Deserialize, Serialize}; use wasm_bindgen::prelude::wasm_bindgen; From a01f1c2b2f2a6095857b0d9bd4c3a9ef2f32abaf Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Tue, 8 Oct 2024 03:15:36 +0530 Subject: [PATCH 2/6] Write peer state and stats to a human readable file for analytical purposes #828 --- saito-core/src/core/consensus/peers/peer.rs | 9 ++++ .../core/consensus/peers/peer_state_writer.rs | 44 +++++++++++++------ saito-core/src/core/routing_thread.rs | 30 ++++++++++--- saito-core/src/core/util/crypto.rs | 2 +- .../src/core/util/test/test_io_handler.rs | 20 ++------- 5 files changed, 69 insertions(+), 36 deletions(-) diff --git a/saito-core/src/core/consensus/peers/peer.rs b/saito-core/src/core/consensus/peers/peer.rs index 2954cd2f6..1ebd83a9b 100644 --- a/saito-core/src/core/consensus/peers/peer.rs +++ b/saito-core/src/core/consensus/peers/peer.rs @@ -93,6 +93,15 @@ impl Peer { pub fn has_invalid_block_limit_exceeded(&mut self, current_time: Timestamp) -> bool { self.invalid_block_limiter.has_limit_exceeded(current_time) } + pub fn get_limited_till(&mut self, current_time: Timestamp) -> Option { + let mut result = None; + + if self.has_key_list_limit_exceeded(current_time) { + if self.key_list_limiter.has_limit_exceeded(current_time) {} + } + + result + } pub fn get_url(&self) -> String { if let Some(config) = self.static_peer_config.as_ref() { diff --git a/saito-core/src/core/consensus/peers/peer_state_writer.rs b/saito-core/src/core/consensus/peers/peer_state_writer.rs index 474ef4a20..27a57e87c 100644 --- a/saito-core/src/core/consensus/peers/peer_state_writer.rs +++ b/saito-core/src/core/consensus/peers/peer_state_writer.rs @@ -1,7 +1,7 @@ -use crate::core::defs::Timestamp; +use crate::core::defs::{PrintForLog, SaitoPublicKey, Timestamp}; use crate::core::io::interface_io::InterfaceIO; +use ahash::HashMap; use std::io::Error; -use std::net::IpAddr; use std::time::Duration; const PEER_STATE_FILENAME: &str = "./data/peer_state.txt"; @@ -9,11 +9,18 @@ const PEER_STATE_FILENAME: &str = "./data/peer_state.txt"; pub(crate) const PEER_STATE_WRITE_PERIOD: Timestamp = Duration::from_secs(5).as_millis() as Timestamp; -#[derive(Clone, Debug, Default)] -pub(crate) struct PeerStateWriter { - lines: Vec, +#[derive(Default, Debug, Clone)] +pub(crate) struct PeerStateEntry { + pub msg_limit_exceeded: bool, + pub invalid_blocks_received: bool, + pub same_depth_blocks_received: bool, + pub too_far_blocks_received: bool, + pub limited_till: Option, } +#[derive(Clone, Debug, Default)] +pub(crate) struct PeerStateWriter {} + impl PeerStateWriter { /// Writes peer state data to the file and clears collected state /// @@ -29,14 +36,29 @@ impl PeerStateWriter { /// /// ``` pub(crate) async fn write_state( - &mut self, + &self, + data: HashMap, io_handler: &mut Box, ) -> Result<(), Error> { - io_handler.write_value(PEER_STATE_FILENAME, &[]).await?; + let line = + "key,limited_till,msg_limit,invalid_blocks_limit,same_depth_limit,too_far_block_limit\r\n" + .to_string(); + io_handler + .write_value(PEER_STATE_FILENAME, line.as_bytes()) + .await?; - for line in self.lines.drain(..) { + for (public_key, data) in data.iter() { + let line = format!( + "{:?},{:?},{:?},{:?},{:?},{:?}\r\n", + public_key.to_base58(), + data.limited_till.unwrap_or(0), + data.msg_limit_exceeded, + data.invalid_blocks_received, + data.same_depth_blocks_received, + data.too_far_blocks_received, + ); io_handler - .append_value(PEER_STATE_FILENAME, (line + "\r\n").as_bytes()) + .append_value(PEER_STATE_FILENAME, line.as_bytes()) .await? } @@ -44,8 +66,4 @@ impl PeerStateWriter { Ok(()) } - - pub(crate) fn add_line(&mut self, line: String) { - self.lines.push(line); - } } diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index 21caae0a5..6b6162259 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -1,8 +1,8 @@ -use std::sync::Arc; -use std::time::Duration; - +use ahash::HashMap; use async_trait::async_trait; use log::{debug, info, trace, warn}; +use std::sync::Arc; +use std::time::Duration; use tokio::sync::mpsc::Sender; use tokio::sync::{RwLock, RwLockReadGuard}; @@ -10,7 +10,7 @@ use crate::core::consensus::blockchain::Blockchain; use crate::core::consensus::blockchain_sync_state::BlockchainSyncState; use crate::core::consensus::mempool::Mempool; use crate::core::consensus::peers::peer_service::PeerService; -use crate::core::consensus::peers::peer_state_writer::PEER_STATE_WRITE_PERIOD; +use crate::core::consensus::peers::peer_state_writer::{PeerStateEntry, PEER_STATE_WRITE_PERIOD}; use crate::core::consensus::wallet::Wallet; use crate::core::consensus_thread::ConsensusEvent; use crate::core::defs::{ @@ -659,9 +659,29 @@ impl ProcessEvent for RoutingThread { self.peer_file_write_timer += duration_value; if self.peer_file_write_timer >= PEER_STATE_WRITE_PERIOD { let mut peers = self.network.peer_lock.write().await; + let mut data: HashMap = Default::default(); + + let current_time = self.timer.get_timestamp_in_ms(); + + for (peer_index, peer) in peers.index_to_peers.iter_mut() { + if peer.public_key.is_none() { + continue; + } + data.insert( + peer.public_key.unwrap(), + PeerStateEntry { + msg_limit_exceeded: peer.has_message_limit_exceeded(current_time), + invalid_blocks_received: peer + .has_invalid_block_limit_exceeded(current_time), + same_depth_blocks_received: false, + too_far_blocks_received: false, + limited_till: None, + }, + ); + } peers .peer_state_writer - .write_state(&mut self.network.io_interface) + .write_state(data, &mut self.network.io_interface) .await .unwrap(); self.peer_file_write_timer = 0; diff --git a/saito-core/src/core/util/crypto.rs b/saito-core/src/core/util/crypto.rs index d3a319c17..f9f3fd3a4 100644 --- a/saito-core/src/core/util/crypto.rs +++ b/saito-core/src/core/util/crypto.rs @@ -8,7 +8,7 @@ use rand::{thread_rng, Rng}; use secp256k1::ecdsa; pub use secp256k1::{Message, PublicKey, SecretKey, SECP256K1}; -type Aes128Cbc = Cbc; +// type Aes128Cbc = Cbc; pub const PARALLEL_HASH_BYTE_THRESHOLD: usize = 128_000; diff --git a/saito-core/src/core/util/test/test_io_handler.rs b/saito-core/src/core/util/test/test_io_handler.rs index b5d92f573..c9c399468 100644 --- a/saito-core/src/core/util/test/test_io_handler.rs +++ b/saito-core/src/core/util/test/test_io_handler.rs @@ -94,16 +94,7 @@ pub mod test { } async fn read_value(&self, key: &str) -> Result, Error> { - let result = File::open(key).await; - if result.is_err() { - error!( - "error : path {:?} \r\n : {:?}", - key.to_string(), - result.err().unwrap() - ); - unreachable!(); - } - let mut file = result.unwrap(); + let mut file = File::open(key).await?; let mut encoded = Vec::::new(); let result = file.read_to_end(&mut encoded).await; @@ -113,13 +104,8 @@ pub mod test { Ok(encoded) } async fn load_block_file_list(&self) -> Result, Error> { - info!("current dir = {:?}", std::env::current_dir().unwrap()); - let result = fs::read_dir(self.get_block_dir()); - if result.is_err() { - return Err(result.err().unwrap()); - } - let mut paths: Vec<_> = result - .unwrap() + info!("current dir = {:?}", std::env::current_dir()?); + let mut paths: Vec<_> = fs::read_dir(self.get_block_dir())? .map(|r| r.unwrap()) .filter(|r| { r.file_name() From 80c9d3bd4c76c98629a518fe8d45b909d327ffde Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Tue, 8 Oct 2024 04:03:21 +0530 Subject: [PATCH 3/6] Write peer state and stats to a human readable file for analytical purposes #828 --- saito-core/src/core/consensus/peers/peer_state_writer.rs | 6 ++++-- saito-core/src/core/routing_thread.rs | 4 +++- saito-core/src/core/verification_thread.rs | 2 +- saito-js/lib/custom/shared_methods.web.ts | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/saito-core/src/core/consensus/peers/peer_state_writer.rs b/saito-core/src/core/consensus/peers/peer_state_writer.rs index 27a57e87c..01596ae2a 100644 --- a/saito-core/src/core/consensus/peers/peer_state_writer.rs +++ b/saito-core/src/core/consensus/peers/peer_state_writer.rs @@ -7,7 +7,7 @@ use std::time::Duration; const PEER_STATE_FILENAME: &str = "./data/peer_state.txt"; pub(crate) const PEER_STATE_WRITE_PERIOD: Timestamp = - Duration::from_secs(5).as_millis() as Timestamp; + Duration::from_secs(1).as_millis() as Timestamp; #[derive(Default, Debug, Clone)] pub(crate) struct PeerStateEntry { @@ -62,7 +62,9 @@ impl PeerStateWriter { .await? } - io_handler.flush_data(PEER_STATE_FILENAME).await?; + if !data.is_empty() { + io_handler.flush_data(PEER_STATE_FILENAME).await?; + } Ok(()) } diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index 6b6162259..4996a696a 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -589,7 +589,6 @@ impl ProcessEvent for RoutingThread { let mut peers = self.network.peer_lock.write().await; let peer = peers.find_peer_by_index_mut(peer_index)?; let time = self.timer.get_timestamp_in_ms(); - peer.invalid_block_limiter.increase(); if peer.has_invalid_block_limit_exceeded(time) { info!( "peers exceeded for invalid blocks from peer : {:?}. disconnecting peer...", @@ -654,6 +653,7 @@ impl ProcessEvent for RoutingThread { let mut peers = self.network.peer_lock.write().await; peers.remove_disconnected_peers(current_time); self.peer_removal_timer = 0; + work_done = true; } self.peer_file_write_timer += duration_value; @@ -665,6 +665,7 @@ impl ProcessEvent for RoutingThread { for (peer_index, peer) in peers.index_to_peers.iter_mut() { if peer.public_key.is_none() { + info!("public key not set yet for peer : {:?}", peer_index); continue; } data.insert( @@ -685,6 +686,7 @@ impl ProcessEvent for RoutingThread { .await .unwrap(); self.peer_file_write_timer = 0; + work_done = true; } if work_done { diff --git a/saito-core/src/core/verification_thread.rs b/saito-core/src/core/verification_thread.rs index 3effa561d..8922af0c4 100644 --- a/saito-core/src/core/verification_thread.rs +++ b/saito-core/src/core/verification_thread.rs @@ -139,7 +139,7 @@ impl VerificationThread { ); let mut peers = self.peer_lock.write().await; if let Some(peer) = peers.find_peer_by_index_mut(peer_index) { - // NOTE : this means if we cannot deserialize a block from the buffer we mark it as blacklisted. + // NOTE : this means if we receive an invalid block, peer is blacklisted. peer.invalid_block_limiter.increase(); } return; diff --git a/saito-js/lib/custom/shared_methods.web.ts b/saito-js/lib/custom/shared_methods.web.ts index 362e9e5fc..5aed40be3 100644 --- a/saito-js/lib/custom/shared_methods.web.ts +++ b/saito-js/lib/custom/shared_methods.web.ts @@ -144,11 +144,11 @@ export default class WebSharedMethods extends CustomSharedMethods { } appendValue(key: string, value: Uint8Array): void { - throw new Error("Method not implemented."); + // TODO : check if this needs implementing. might be not needed for web } flushData(key: string): void { - throw new Error("Method not implemented."); + // TODO : check if this needs implementing. might be not needed for web } sendInterfaceEvent(event: String, peerIndex: bigint, public_key: string) { From 92d9eb3264908dcb4c76c19c758414cbbbc2eb44 Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Tue, 8 Oct 2024 10:54:20 +0530 Subject: [PATCH 4/6] Write peer state and stats to a human readable file for analytical purposes #828 --- .../core/consensus/peers/peer_state_writer.rs | 32 +++++++-- saito-core/src/core/routing_thread.rs | 67 ++++++++++--------- 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/saito-core/src/core/consensus/peers/peer_state_writer.rs b/saito-core/src/core/consensus/peers/peer_state_writer.rs index 01596ae2a..8d26947a1 100644 --- a/saito-core/src/core/consensus/peers/peer_state_writer.rs +++ b/saito-core/src/core/consensus/peers/peer_state_writer.rs @@ -1,5 +1,6 @@ -use crate::core::defs::{PrintForLog, SaitoPublicKey, Timestamp}; +use crate::core::defs::{PeerIndex, PrintForLog, SaitoPublicKey, Timestamp}; use crate::core::io::interface_io::InterfaceIO; +use crate::core::routing_thread::PeerState; use ahash::HashMap; use std::io::Error; use std::time::Duration; @@ -9,8 +10,10 @@ const PEER_STATE_FILENAME: &str = "./data/peer_state.txt"; pub(crate) const PEER_STATE_WRITE_PERIOD: Timestamp = Duration::from_secs(1).as_millis() as Timestamp; -#[derive(Default, Debug, Clone)] +#[derive(Debug, Clone)] pub(crate) struct PeerStateEntry { + pub peer_index: PeerIndex, + pub public_key: SaitoPublicKey, pub msg_limit_exceeded: bool, pub invalid_blocks_received: bool, pub same_depth_blocks_received: bool, @@ -18,6 +21,20 @@ pub(crate) struct PeerStateEntry { pub limited_till: Option, } +impl Default for PeerStateEntry { + fn default() -> Self { + Self { + peer_index: 0, + public_key: [0; 33], + msg_limit_exceeded: false, + invalid_blocks_received: false, + same_depth_blocks_received: false, + too_far_blocks_received: false, + limited_till: None, + } + } +} + #[derive(Clone, Debug, Default)] pub(crate) struct PeerStateWriter {} @@ -37,20 +54,21 @@ impl PeerStateWriter { /// ``` pub(crate) async fn write_state( &self, - data: HashMap, + data: Vec, io_handler: &mut Box, ) -> Result<(), Error> { let line = - "key,limited_till,msg_limit,invalid_blocks_limit,same_depth_limit,too_far_block_limit\r\n" + "peer_index,public_key,limited_till,msg_limit,invalid_blocks_limit,same_depth_limit,too_far_block_limit\r\n" .to_string(); io_handler .write_value(PEER_STATE_FILENAME, line.as_bytes()) .await?; - for (public_key, data) in data.iter() { + for data in data.iter() { let line = format!( - "{:?},{:?},{:?},{:?},{:?},{:?}\r\n", - public_key.to_base58(), + "{:?},{:?},{:?},{:?},{:?},{:?},{:?}\r\n", + data.peer_index, + data.public_key.to_base58(), data.limited_till.unwrap_or(0), data.msg_limit_exceeded, data.invalid_blocks_received, diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index 4996a696a..d7116cf11 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -527,6 +527,39 @@ impl RoutingThread { warn!("peer {:?} not found to update services", peer_index); } } + + async fn write_peer_state_data(&mut self, duration_value: Timestamp, work_done: &mut bool) { + self.peer_file_write_timer += duration_value; + if self.peer_file_write_timer >= PEER_STATE_WRITE_PERIOD { + let mut peers = self.network.peer_lock.write().await; + let mut data: Vec = Default::default(); + + let current_time = self.timer.get_timestamp_in_ms(); + + for (peer_index, peer) in peers.index_to_peers.iter_mut() { + if peer.public_key.is_none() { + info!("public key not set yet for peer : {:?}", peer_index); + continue; + } + data.push(PeerStateEntry { + peer_index: peer.index, + public_key: peer.public_key.unwrap_or([0; 33]), + msg_limit_exceeded: peer.has_message_limit_exceeded(current_time), + invalid_blocks_received: peer.has_invalid_block_limit_exceeded(current_time), + same_depth_blocks_received: false, + too_far_blocks_received: false, + limited_till: None, + }); + } + peers + .peer_state_writer + .write_state(data, &mut self.network.io_interface) + .await + .unwrap(); + self.peer_file_write_timer = 0; + *work_done = true; + } + } } #[async_trait] @@ -656,38 +689,8 @@ impl ProcessEvent for RoutingThread { work_done = true; } - self.peer_file_write_timer += duration_value; - if self.peer_file_write_timer >= PEER_STATE_WRITE_PERIOD { - let mut peers = self.network.peer_lock.write().await; - let mut data: HashMap = Default::default(); - - let current_time = self.timer.get_timestamp_in_ms(); - - for (peer_index, peer) in peers.index_to_peers.iter_mut() { - if peer.public_key.is_none() { - info!("public key not set yet for peer : {:?}", peer_index); - continue; - } - data.insert( - peer.public_key.unwrap(), - PeerStateEntry { - msg_limit_exceeded: peer.has_message_limit_exceeded(current_time), - invalid_blocks_received: peer - .has_invalid_block_limit_exceeded(current_time), - same_depth_blocks_received: false, - too_far_blocks_received: false, - limited_till: None, - }, - ); - } - peers - .peer_state_writer - .write_state(data, &mut self.network.io_interface) - .await - .unwrap(); - self.peer_file_write_timer = 0; - work_done = true; - } + self.write_peer_state_data(duration_value, &mut work_done) + .await; if work_done { return Some(()); From d1412ad1509193bec8e30ad58e67d65f63e9255b Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Tue, 8 Oct 2024 10:54:46 +0530 Subject: [PATCH 5/6] Write peer state and stats to a human readable file for analytical purposes #828 --- saito-rust/data/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/saito-rust/data/.gitignore b/saito-rust/data/.gitignore index 12a72c8e3..77d50b794 100644 --- a/saito-rust/data/.gitignore +++ b/saito-rust/data/.gitignore @@ -5,3 +5,4 @@ /issuance/test/issuance ./wallet wallet +*.txt \ No newline at end of file From e4586e4f8a2e26eee1cb0dd11948cbf782ac0076 Mon Sep 17 00:00:00 2001 From: sankadarshana Date: Tue, 8 Oct 2024 10:57:21 +0530 Subject: [PATCH 6/6] Write peer state and stats to a human readable file for analytical purposes #828 --- .../src/core/consensus/peers/peer_state_writer.rs | 10 ++++++++-- saito-core/src/core/routing_thread.rs | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/saito-core/src/core/consensus/peers/peer_state_writer.rs b/saito-core/src/core/consensus/peers/peer_state_writer.rs index 8d26947a1..7cc877f36 100644 --- a/saito-core/src/core/consensus/peers/peer_state_writer.rs +++ b/saito-core/src/core/consensus/peers/peer_state_writer.rs @@ -18,6 +18,8 @@ pub(crate) struct PeerStateEntry { pub invalid_blocks_received: bool, pub same_depth_blocks_received: bool, pub too_far_blocks_received: bool, + pub handshake_limit_exceeded: bool, + pub keylist_limit_exceeded: bool, pub limited_till: Option, } @@ -30,6 +32,8 @@ impl Default for PeerStateEntry { invalid_blocks_received: false, same_depth_blocks_received: false, too_far_blocks_received: false, + handshake_limit_exceeded: false, + keylist_limit_exceeded: false, limited_till: None, } } @@ -58,7 +62,7 @@ impl PeerStateWriter { io_handler: &mut Box, ) -> Result<(), Error> { let line = - "peer_index,public_key,limited_till,msg_limit,invalid_blocks_limit,same_depth_limit,too_far_block_limit\r\n" + "peer_index,public_key,limited_till,msg_limit,invalid_blocks_limit,same_depth_limit,too_far_block_limit,handshake_limit,keylist_limit\r\n" .to_string(); io_handler .write_value(PEER_STATE_FILENAME, line.as_bytes()) @@ -66,7 +70,7 @@ impl PeerStateWriter { for data in data.iter() { let line = format!( - "{:?},{:?},{:?},{:?},{:?},{:?},{:?}\r\n", + "{:?},{:?},{:?},{:?},{:?},{:?},{:?},{:?},{:?}\r\n", data.peer_index, data.public_key.to_base58(), data.limited_till.unwrap_or(0), @@ -74,6 +78,8 @@ impl PeerStateWriter { data.invalid_blocks_received, data.same_depth_blocks_received, data.too_far_blocks_received, + data.handshake_limit_exceeded, + data.keylist_limit_exceeded, ); io_handler .append_value(PEER_STATE_FILENAME, line.as_bytes()) diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index d7116cf11..c95e18443 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -548,6 +548,8 @@ impl RoutingThread { invalid_blocks_received: peer.has_invalid_block_limit_exceeded(current_time), same_depth_blocks_received: false, too_far_blocks_received: false, + handshake_limit_exceeded: peer.has_handshake_limit_exceeded(current_time), + keylist_limit_exceeded: peer.has_key_list_limit_exceeded(current_time), limited_till: None, }); }