bitswap: Insert received block into repo.
This commit is contained in:
parent
b21d9c39ca
commit
ac56b74acd
@ -5,7 +5,7 @@
|
||||
//!
|
||||
//! The `Bitswap` struct implements the `NetworkBehaviour` trait. When used, it
|
||||
//! will allow providing and reciving IPFS blocks.
|
||||
use crate::bitswap::ledger::{BitswapEvent, Ledger, Message, Priority, I, O};
|
||||
use crate::bitswap::ledger::{Ledger, Message, Priority, I, O};
|
||||
use crate::bitswap::protocol::BitswapConfig;
|
||||
use crate::bitswap::strategy::{Strategy, StrategyEvent};
|
||||
use crate::block::{Block, Cid};
|
||||
@ -24,7 +24,7 @@ pub struct Bitswap<TSubstream, TStrategy: Strategy> {
|
||||
/// Marker to pin the generics.
|
||||
marker: PhantomData<TSubstream>,
|
||||
/// Queue of events to report to the user.
|
||||
events: VecDeque<NetworkBehaviourAction<Message<O>, BitswapEvent>>,
|
||||
events: VecDeque<NetworkBehaviourAction<Message<O>, ()>>,
|
||||
/// Ledger
|
||||
peers: HashMap<PeerId, Ledger>,
|
||||
/// Wanted blocks
|
||||
@ -102,7 +102,7 @@ where
|
||||
TSubstream: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type ProtocolsHandler = OneShotHandler<TSubstream, BitswapConfig, Message<O>, InnerMessage>;
|
||||
type OutEvent = BitswapEvent;
|
||||
type OutEvent = ();
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
Default::default()
|
||||
@ -153,11 +153,7 @@ where
|
||||
for block in message.blocks() {
|
||||
// Cancel the block.
|
||||
self.cancel_block(&block.cid());
|
||||
// Add block to received blocks
|
||||
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
|
||||
BitswapEvent::Block {
|
||||
block: block.to_owned(),
|
||||
}));
|
||||
self.strategy.process_block(source.clone(), block.to_owned());
|
||||
}
|
||||
for (cid, priority) in message.want() {
|
||||
self.strategy.process_want(source.clone(), cid.to_owned(), *priority);
|
||||
|
@ -217,14 +217,6 @@ impl<T> std::fmt::Debug for Message<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Event generated by the `Bitswap` behaviour.
|
||||
pub enum BitswapEvent {
|
||||
/// A block was received.
|
||||
Block {
|
||||
block: Block,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/*
|
||||
|
@ -6,5 +6,5 @@ pub mod strategy;
|
||||
mod protocol;
|
||||
|
||||
pub use self::behaviour::Bitswap;
|
||||
pub use self::ledger::{BitswapEvent, Priority};
|
||||
pub use self::ledger::Priority;
|
||||
pub use self::strategy::{AltruisticStrategy, Strategy};
|
||||
|
@ -7,6 +7,7 @@ use std::collections::VecDeque;
|
||||
pub trait Strategy {
|
||||
fn new(repo: Repo) -> Self;
|
||||
fn process_want(&mut self, source: PeerId, cid: Cid, priority: Priority);
|
||||
fn process_block(&mut self, source: PeerId, block: Block);
|
||||
fn poll(&mut self) -> Option<StrategyEvent>;
|
||||
}
|
||||
|
||||
@ -47,6 +48,13 @@ impl Strategy for AltruisticStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
fn process_block(&mut self, source: PeerId, block: Block) {
|
||||
println!("Received block {} from peer {}",
|
||||
block.cid().to_string(),
|
||||
source.to_base58());
|
||||
self.repo.put(block);
|
||||
}
|
||||
|
||||
fn poll(&mut self) -> Option<StrategyEvent> {
|
||||
self.events.pop_front()
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::bitswap::{Bitswap, BitswapEvent, Strategy};
|
||||
use crate::bitswap::{Bitswap, Strategy};
|
||||
use crate::block::Cid;
|
||||
use crate::config::NetworkConfig;
|
||||
use libp2p::{NetworkBehaviour, PeerId};
|
||||
@ -56,17 +56,10 @@ impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy>
|
||||
}
|
||||
|
||||
impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy>
|
||||
NetworkBehaviourEventProcess<BitswapEvent> for
|
||||
NetworkBehaviourEventProcess<()> for
|
||||
Behaviour<TSubstream, TStrategy>
|
||||
{
|
||||
fn inject_event(&mut self, event: BitswapEvent) {
|
||||
match event {
|
||||
BitswapEvent::Block { block } => {
|
||||
println!("Received block with contents: '{:?}'",
|
||||
String::from_utf8_lossy(&block.data()));
|
||||
}
|
||||
}
|
||||
}
|
||||
fn inject_event(&mut self, _event: ()) {}
|
||||
}
|
||||
|
||||
impl<TSubstream: AsyncRead + AsyncWrite, TStrategy: Strategy> Behaviour<TSubstream, TStrategy>
|
||||
|
Loading…
x
Reference in New Issue
Block a user