MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / accept

Method accept

nodedb-cluster/src/rpc_codec/peer_seq.rs:94–137  ·  view source on GitHub ↗

Accept `seq` from `peer_id`, rejecting replays and out-of-window stale frames. Returns `Err(ClusterError::Codec)` on rejection. Sequence `0` is always rejected — a well-formed sender starts at 1, so `0` means "nothing sent", which is not a valid inbound frame.

(&self, peer_id: u64, seq: u64)

Source from the content-addressed store, hash-verified

92 /// Sequence `0` is always rejected — a well-formed sender starts at
93 /// 1, so `0` means "nothing sent", which is not a valid inbound frame.
94 pub fn accept(&self, peer_id: u64, seq: u64) -> Result<()> {
95 if seq == 0 {
96 return Err(ClusterError::Codec {
97 detail: format!("peer {peer_id} sent reserved sequence 0"),
98 });
99 }
100
101 let mut guard = self.windows.write().unwrap_or_else(|p| p.into_inner());
102 let state = guard.entry(peer_id).or_default();
103
104 if seq > state.high {
105 // Frame advances the window. Shift by the delta and set bit 0.
106 let delta = seq - state.high;
107 state.mask = if delta >= REPLAY_WINDOW {
108 1
109 } else {
110 (state.mask << delta) | 1
111 };
112 state.high = seq;
113 return Ok(());
114 }
115
116 // Frame is `state.high - seq` positions back in the window.
117 let offset = state.high - seq;
118 if offset >= REPLAY_WINDOW {
119 return Err(ClusterError::Codec {
120 detail: format!(
121 "peer {peer_id} sent stale sequence {seq}, window high is {}",
122 state.high
123 ),
124 });
125 }
126 let bit = 1u64 << offset;
127 if state.mask & bit != 0 {
128 return Err(ClusterError::Codec {
129 detail: format!(
130 "peer {peer_id} replayed sequence {seq} (window high {})",
131 state.high
132 ),
133 });
134 }
135 state.mask |= bit;
136 Ok(())
137 }
138
139 #[cfg(test)]
140 pub fn highest(&self, peer_id: u64) -> u64 {

Calls 2

entryMethod · 0.80
writeMethod · 0.45