harbor run -t openthoughts/tasktrove-exp-rpt-stack-rust-v2-stack-rust-0904harbor download openthoughts/tasktrove-exp-rpt-stack-rust-v2-stack-rust-0904Implement the functionality of the Swarm struct within the module /app/. The Swarm is responsible for managing network connections and the state of active peers in a libp2p network.
Initialization:
new method to create a new Swarm given a Transport, a NetworkBehaviour, and a local PeerId. This method should initialize the internal structures of the Swarm.Peer Management:
dial(&mut self, peer_id: &PeerId) -> Result<(), DialError>: Initiate a connection to a peer. If the peer is banned, return an error.dial_addr(&mut self, addr: Multiaddr) -> Result<(), DialError>: Initiate a connection using a specified address and handle the connection process.disconnect_peer_id(&mut self, peer_id: PeerId) -> Result<(), ()>: Disconnect from the specified peer.Listener Management:
listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<io::Error>>: Start listening on a given address and report new listener events.remove_listener(&mut self, id: ListenerId) -> Result<(), ()>: Remove a listener by its assigned ID.Event Handling:
poll_next_event(&mut self, cx: &mut Context<'_>) -> Poll<SwarmEvent<TBehaviour::OutEvent, THandleErr>>: This method should collect and return events related to peer connections and listener status.External Address Management:
add_external_address(&mut self, addr: Multiaddr, score: AddressScore) -> AddAddressResult: Add an external address record while handling any needed reporting to the behaviour.remove_external_address(&mut self, addr: &Multiaddr) -> bool: Remove a specified external address.Banning Mechanism:
ban_peer_id(&mut self, peer_id: PeerId): Add a peer to the ban list, rejecting any incoming connections.unban_peer_id(&mut self, peer_id: PeerId): Remove a peer from the ban list.Connections Management:
is_connected(&self, peer_id: &PeerId) -> bool: Check if there’s an active connection to a given peer.Traits Implementation:
Swarm struct implements necessary traits such as Stream for event handling.Once you've implemented these functionalities, the associated tests in tests module should pass, confirming the correctness of the implementation.