Skip to main content

rings_node/
seed.rs

1//! Seed and SeedLoader use for getting peers from endpoint.
2
3use rings_rpc::protos::rings_node::ConnectWithSeedRequest;
4use serde::Deserialize;
5use serde::Serialize;
6
7use crate::error::Error;
8
9/// A list contains SeedPeer.
10#[derive(Deserialize, Serialize, Debug)]
11pub struct Seed {
12    /// Peers loaded from seed configuration.
13    pub peers: Vec<SeedPeer>,
14}
15
16/// SeedPeer contain `Did` and `endpoint`.
17#[derive(Deserialize, Serialize, Debug)]
18pub struct SeedPeer {
19    /// an unique identify.
20    pub did: String,
21    /// remote client endpoint
22    pub url: String,
23}
24
25impl TryFrom<ConnectWithSeedRequest> for Seed {
26    type Error = Error;
27
28    fn try_from(req: ConnectWithSeedRequest) -> Result<Self, Error> {
29        let mut peers = Vec::new();
30
31        for peer in req.peers {
32            peers.push(SeedPeer {
33                did: peer.did,
34                url: peer.url,
35            });
36        }
37
38        Ok(Seed { peers })
39    }
40}
41
42impl Seed {
43    /// Converts this seed list into the RPC request used by `connectWithSeed`.
44    pub fn into_connect_with_seed_request(self) -> ConnectWithSeedRequest {
45        let mut peers = Vec::new();
46
47        for peer in self.peers {
48            peers.push(rings_rpc::protos::rings_node::SeedPeer {
49                did: peer.did,
50                url: peer.url,
51            });
52        }
53
54        ConnectWithSeedRequest { peers }
55    }
56}