rings_core/dht/chord/
action.rs1use serde::Deserialize;
2use serde::Serialize;
3
4use super::PeerRing;
5use crate::dht::entry::EntryLookupEvidence;
6use crate::dht::entry::EntryLookupKey;
7use crate::dht::entry::PlacedEntry;
8use crate::dht::entry::PlacedEntryOperation;
9use crate::dht::entry::PlacementMiss;
10use crate::dht::storage::StorageSyncPurpose;
11use crate::dht::storage::StorageSyncRoute;
12use crate::dht::Did;
13use crate::error::Error;
14use crate::error::Result;
15
16#[derive(Clone, Debug, PartialEq)]
18pub enum PeerRingAction {
19 None,
21 SomeEntry(EntryLookupEvidence),
23 EntryMisses(Vec<PlacementMiss>),
25 Some(Did),
27 RemoteAction(Did, RemoteAction),
29 MultiActions(Vec<PeerRingAction>),
31}
32
33#[derive(Clone, Debug, PartialEq, Eq)]
38pub enum RemoteAction {
39 FindSuccessor(Did),
41 FindEntry(EntryLookupKey),
43 FindEntryForOperate(PlacedEntryOperation),
45 Notify(Did),
47 SyncEntriesWithSuccessor {
49 purpose: StorageSyncPurpose,
51 route: StorageSyncRoute,
53 data: Vec<PlacedEntry>,
55 },
56 FindSuccessorForConnect(Did),
58 FindSuccessorForFix {
60 did: Did,
62 index: usize,
64 },
65 QueryForSuccessorList,
67 QueryForSuccessorListAndPred,
69 TryConnect,
71}
72
73#[derive(Debug, PartialEq, Eq, Deserialize, Serialize, Clone)]
75pub struct TopoInfo {
76 pub successors: Vec<Did>,
78 pub predecessor: Option<Did>,
80}
81
82impl TopoInfo {
83 pub(crate) fn confirmed_by(&self, mut is_routable: impl FnMut(Did) -> bool) -> Self {
85 Self {
86 successors: self
87 .successors
88 .iter()
89 .copied()
90 .filter(|peer| is_routable(*peer))
91 .collect(),
92 predecessor: self.predecessor.filter(|peer| is_routable(*peer)),
93 }
94 }
95
96 pub(crate) fn has_confirmed_peer(&self) -> bool {
98 self.predecessor.is_some() || !self.successors.is_empty()
99 }
100}
101
102impl TryFrom<&PeerRing> for TopoInfo {
103 type Error = Error;
104
105 fn try_from(dht: &PeerRing) -> Result<Self> {
106 let state = dht.topology_state()?;
107 Ok(Self {
108 successors: state.successors,
109 predecessor: state.predecessor,
110 })
111 }
112}
113
114impl PeerRingAction {
115 pub fn is_none(&self) -> bool {
117 matches!(self, Self::None)
118 }
119
120 pub fn is_some(&self) -> bool {
122 matches!(self, Self::Some(_))
123 }
124
125 pub fn is_some_entry(&self) -> bool {
127 matches!(self, Self::SomeEntry(_))
128 }
129
130 pub fn is_remote(&self) -> bool {
132 matches!(self, Self::RemoteAction(..))
133 }
134
135 pub fn is_multi(&self) -> bool {
137 matches!(self, Self::MultiActions(..))
138 }
139}
140
141impl From<Vec<PeerRingAction>> for PeerRingAction {
142 fn from(actions: Vec<PeerRingAction>) -> Self {
143 if actions.is_empty() {
144 Self::None
145 } else {
146 Self::MultiActions(actions)
147 }
148 }
149}