34个版本 (破坏性更新)
0.25.1 | 2023年5月19日 |
---|---|
0.25.0 | 2022年11月30日 |
0.24.0 | 2022年10月18日 |
0.22.0 | 2022年3月26日 |
0.5.1 | 2020年7月17日 |
#603 in 网络编程
每月 49 次下载
用于 11 个crate(4个直接使用)
88KB
2K SLoC
libp2p-bitswap
bitswap协议的实现。
高效同步块的有向无环图(dag)
Bitswap是一个非常简单的协议。它被修改并简化为ipfs-embed。消息格式可以用以下枚举表示。
pub enum BitswapRequest {
Have(Cid),
Block(Cid),
}
pub enum BitswapResponse {
Have(bool),
Block(Vec<u8>),
}
定位提供者的机制可以被抽象化。可以插入一个dht或集中式db查询。Bitswap api看起来如下
#[derive(Debug)]
pub enum BitswapEvent {
/// Received a block from a peer. Includes the number of known missing blocks for a
/// sync query. When a block is received and missing blocks is not empty the counter
/// is increased. If missing blocks is empty the counter is decremented.
Progress(QueryId, usize),
/// A get or sync query completed.
Complete(QueryId, Result<()>),
}
pub trait BitswapStore: Send + Sync + 'static {
/// The store params.
type Params: StoreParams;
/// A have query needs to know if the block store contains the block.
fn contains(&mut self, cid: &Cid) -> Result<bool>;
/// A block query needs to retrieve the block from the store.
fn get(&mut self, cid: &Cid) -> Result<Option<Vec<u8>>>;
/// A block response needs to insert the block into the store.
fn insert(&mut self, block: &Block<Self::Params>) -> Result<()>;
/// A sync query needs a list of missing blocks to make progress.
fn missing_blocks(&mut self, cid: &Cid) -> Result<Vec<Cid>>;
}
pub struct BitswapConfig {
/// Timeout of a request.
pub request_timeout: Duration,
/// Time a connection is kept alive.
pub connection_keep_alive: Duration,
}
impl<P: StoreParams> Bitswap<P> {
/// Creates a new `Bitswap` behaviour.
pub fn new(config: BitswapConfig) -> Self;
/// Adds an address for a peer.
pub fn add_address(&mut self, peer_id: &PeerId, addr: Multiaddr);
/// Removes an address for a peer.
pub fn remove_address(&mut self, peer_id: &PeerId, addr: &Multiaddr);
/// Starts a get query with an initial guess of providers.
pub fn get(&mut self, cid: Cid, peers: impl Iterator<Item = PeerId>) -> QueryId;
/// Starts a sync query with an the initial set of missing blocks.
pub fn sync(&mut self, cid: Cid, peers: Vec<PeerId>, missing: impl Iterator<Item = Cid>) -> QueryId;
/// Cancels an in progress query. Returns true if a query was cancelled.
pub fn cancel(&mut self, id: QueryId) -> bool;
/// Register bitswap stats in a prometheus registry.
pub fn register_metrics(&self, registry: &Registry) -> Result<()>;
}
当你创建一个get请求时会发生什么?首先,使用have请求查询初始集中的所有提供者。作为一个优化,在每次查询批中发送一个块请求。如果get查询找到一个块,则返回查询完成。如果在初始集中没有找到该块,则发出一个 Providers
事件。这就是bitswap消费者尝试通过例如执行dht查找来定位提供者的地方。在定位提供者完成后,通过调用 inject_providers
来发出信号。然后,查询管理器使用新提供者集执行bitswap请求,这导致找到块或返回 BlockNotFound
错误。
我们通常想要同步整个块的有向无环图(dag)。我们可以通过添加一个同步查询来高效地同步块的有向无环图(dag),该同步查询并行地对一个块的引用的所有引用执行get查询。使用具有块的提供者集作为引用查询的初始集。
许可证
MIT OR Apache-2.0
依赖项
~11–20MB
~301K SLoC