< 返回版块

huang12zheng 发表于 2022-07-21 12:27

Tags:swarm,libp2p

  • Swarm 用户视角

transport 用于与其他节点建立连接,从而进行协商

NetworkBehaviour,在devrive下,将你写的NetworkBehaviourEventProcess.inject流入到 poll函数的最前面

Swarm可以作为inner,而外部的函数api是 async的.

比如 handle_evnent (SwarmEvent::Behaviour(..(message))=>match message => ...send().await

  • 缺了什么? 目前,能写libp2p代码建个网络,但发现对于全局无法把握.不知每个Event有什么上下文.

也许,可能需要看一下,proposal里的一些例子里是怎么写的:

  • 又回到Swarm
  1. 猜想 impl Stream for Swarm, 确实是, 下游是poll_next_event
  2. Swarm.poll_next_event
pending_event
behaviour.poll => handle_xx_event
pool.poll      => handle_xx_event
transport.poll => handle_xx_event

左边是这四个子属性的网络行为,输出out或说委托 到swarm里. 注意,这里的介质outevent类型是各子属性内部定义的OutEvent. |handle_behaviour_event | NetworkBehaviourAction.| |handle_pool_event | PoolEvent | |handle_transport_event | TransportEvent|

以用户会涉及的NetworkBehaviour来说,

#[derive(NetworkBehaviour)]
#[behaviour(event_process = true)]
struct MyBehaviour {
    floodsub: Floodsub,
    mdns: Mdns,
}

behaviour.poll来调用floodsub.poll 和 mdns.poll.结果如果是GenerateEvent,调用inject_event,

否则应该是NetworkBehaviourAction,这些会转发给 handle_behaviour_event,来实现处理。

即处理{Dial,NotifyHandler,ReportObservedAddr,CloseConnectio}

注意, poll的结果可能不是NetworkBehaviourAction,虽然 derive 生成了对NetworkBehaviourAction的转发,

即,比如floodsub.poll里的OutEvent,没有out ReportObservedAddr,你也不用奇怪。

floodsub.poll又是什么东西?

self.events.pop_front()

它的来源就比较多了

{
    add_node_to_partial_view|{
        NetworkBehaviourAction::NotifyHandler(FloodsubRpc(FloodsubSubscription))|
        NetworkBehaviourAction::Dial
        }
}|
{
    subscribe|{
        NetworkBehaviourAction::NotifyHandler
    }
}|{
    unsubscribe|NetworkBehaviourAction::NotifyHandler

}|{
    publish_many_inner|{
        NetworkBehaviourAction::GenerateEvent(FloodsubEvent::Message|NetworkBehaviourAction::NotifyHandler
    }
}|{
    inject_connection_established|NetworkBehaviourAction::NotifyHandler
}|{
    inject_connection_closed|NetworkBehaviourAction::Dial
}|{
    inject_event|{
        NetworkBehaviourAction::GenerateEvent(FloodsubEvent::Subscribed|
        NetworkBehaviourAction::GenerateEvent(FloodsubEvent::Unsubscribed|
        NetworkBehaviourAction::GenerateEvent(FloodsubEvent::Message|NetworkBehaviourAction::NotifyHandler
    }
}

这里我们知道了, 我们调用 具体的behaviour的api,会做一些事,然后发一个事件, 这些事件,要么在MyBehaviour的poll里被处理,要么流到NetworkBehaviourEventProcess::inject_event处理, 要么流出于handle_behaviour_event

另个, handle_behaviour_event 反回的SwarmEvent 会被record 然后到metrics里

注意NetworkBehaviourEventProcess::inject_event 与NetworkBehaviour::inject_event的区别

评论区

写评论

还没有评论

1 共 0 条评论, 1 页