refactor: move Stats into Ledger
Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
parent
4093e64119
commit
29d22dfd2e
@ -6,7 +6,7 @@
|
||||
//! The `Bitswap` struct implements the `NetworkBehaviour` trait. When used, it
|
||||
//! will allow providing and reciving IPFS blocks.
|
||||
use crate::block::Block;
|
||||
use crate::ledger::{Ledger, Message, Priority};
|
||||
use crate::ledger::{Ledger, Message, Priority, Stats};
|
||||
use crate::protocol::BitswapConfig;
|
||||
use crate::strategy::{AltruisticStrategy, StrategyEvent};
|
||||
use cid::Cid;
|
||||
@ -34,16 +34,6 @@ pub struct Bitswap {
|
||||
strategy: AltruisticStrategy,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Stats {
|
||||
pub sent_blocks: u64,
|
||||
pub sent_data: u64,
|
||||
pub received_blocks: u64,
|
||||
pub received_data: u64,
|
||||
pub duplicate_blocks: u64,
|
||||
pub duplicate_data: u64,
|
||||
}
|
||||
|
||||
impl Bitswap {
|
||||
/// Creates a `Bitswap`.
|
||||
pub fn new(strategy: AltruisticStrategy) -> Self {
|
||||
@ -75,12 +65,12 @@ impl Bitswap {
|
||||
self.connected_peers
|
||||
.values()
|
||||
.fold(Stats::default(), |mut acc, ledger| {
|
||||
acc.sent_blocks += ledger.sent_blocks;
|
||||
acc.sent_data += ledger.sent_data;
|
||||
acc.received_blocks += ledger.received_blocks;
|
||||
acc.received_data += ledger.received_data;
|
||||
acc.duplicate_blocks += ledger.duplicate_blocks;
|
||||
acc.duplicate_data += ledger.duplicate_data;
|
||||
acc.sent_blocks += ledger.stats.sent_blocks;
|
||||
acc.sent_data += ledger.stats.sent_data;
|
||||
acc.received_blocks += ledger.stats.received_blocks;
|
||||
acc.received_data += ledger.stats.received_data;
|
||||
acc.duplicate_blocks += ledger.stats.duplicate_blocks;
|
||||
acc.duplicate_data += ledger.stats.duplicate_data;
|
||||
acc
|
||||
})
|
||||
}
|
||||
|
@ -9,24 +9,25 @@ use std::collections::HashMap;
|
||||
|
||||
pub type Priority = i32;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Stats {
|
||||
pub sent_blocks: u64,
|
||||
pub sent_data: u64,
|
||||
pub received_blocks: u64,
|
||||
pub received_data: u64,
|
||||
pub duplicate_blocks: u64,
|
||||
pub duplicate_data: u64,
|
||||
}
|
||||
|
||||
/// The Ledger contains the history of transactions with a peer.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Ledger {
|
||||
/// The number of blocks sent to the peer.
|
||||
pub(crate) sent_blocks: u64,
|
||||
pub(crate) sent_data: u64,
|
||||
|
||||
/// The number of blocks received from the peer.
|
||||
pub(crate) received_blocks: u64,
|
||||
pub(crate) received_data: u64,
|
||||
|
||||
pub(crate) duplicate_blocks: u64,
|
||||
pub(crate) duplicate_data: u64,
|
||||
|
||||
/// The list of wanted blocks sent to the peer.
|
||||
sent_want_list: HashMap<Cid, Priority>,
|
||||
/// The list of wanted blocks received from the peer.
|
||||
received_want_list: HashMap<Cid, Priority>,
|
||||
/// Statistics related to a given peer.
|
||||
pub stats: Stats,
|
||||
}
|
||||
|
||||
impl Ledger {
|
||||
@ -58,7 +59,7 @@ impl Ledger {
|
||||
}
|
||||
|
||||
pub fn update_outgoing_stats(&mut self, message: &Message) {
|
||||
self.sent_blocks += message.blocks.len() as u64;
|
||||
self.stats.sent_blocks += message.blocks.len() as u64;
|
||||
for cid in message.cancel() {
|
||||
self.sent_want_list.remove(cid);
|
||||
}
|
||||
@ -77,13 +78,13 @@ impl Ledger {
|
||||
}
|
||||
|
||||
pub(crate) fn update_incoming_stored(&mut self, bytes: u64) {
|
||||
self.received_blocks += 1;
|
||||
self.received_data += bytes;
|
||||
self.stats.received_blocks += 1;
|
||||
self.stats.received_data += bytes;
|
||||
}
|
||||
|
||||
pub(crate) fn update_incoming_duplicate(&mut self, bytes: u64) {
|
||||
self.duplicate_blocks += 1;
|
||||
self.duplicate_data += bytes;
|
||||
self.stats.duplicate_blocks += 1;
|
||||
self.stats.duplicate_data += bytes;
|
||||
}
|
||||
|
||||
/// Returns the blocks wanted by the peer in unspecified order
|
||||
|
@ -10,10 +10,10 @@ mod prefix;
|
||||
mod protocol;
|
||||
mod strategy;
|
||||
|
||||
pub use self::behaviour::{Bitswap, Stats};
|
||||
pub use self::behaviour::Bitswap;
|
||||
pub use self::block::Block;
|
||||
pub use self::error::BitswapError;
|
||||
pub use self::ledger::Priority;
|
||||
pub use self::ledger::{Priority, Stats};
|
||||
pub use self::strategy::{AltruisticStrategy, BitswapStore, BlockPut, Strategy};
|
||||
|
||||
mod bitswap_pb {
|
||||
|
Loading…
x
Reference in New Issue
Block a user