-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #836 from SaitoTech/feature/828/peer_file
Feature/828/peer file
- Loading branch information
Showing
32 changed files
with
206 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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; | ||
|
||
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(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, | ||
pub too_far_blocks_received: bool, | ||
pub handshake_limit_exceeded: bool, | ||
pub keylist_limit_exceeded: bool, | ||
pub limited_till: Option<Timestamp>, | ||
} | ||
|
||
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, | ||
handshake_limit_exceeded: false, | ||
keylist_limit_exceeded: false, | ||
limited_till: None, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub(crate) struct PeerStateWriter {} | ||
|
||
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( | ||
&self, | ||
data: Vec<PeerStateEntry>, | ||
io_handler: &mut Box<dyn InterfaceIO + Send + Sync>, | ||
) -> Result<(), Error> { | ||
let line = | ||
"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()) | ||
.await?; | ||
|
||
for data in data.iter() { | ||
let line = format!( | ||
"{:?},{:?},{:?},{:?},{:?},{:?},{:?},{:?},{:?}\r\n", | ||
data.peer_index, | ||
data.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, | ||
data.handshake_limit_exceeded, | ||
data.keylist_limit_exceeded, | ||
); | ||
io_handler | ||
.append_value(PEER_STATE_FILENAME, line.as_bytes()) | ||
.await? | ||
} | ||
|
||
if !data.is_empty() { | ||
io_handler.flush_data(PEER_STATE_FILENAME).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.