caio.co/de/foca

use addr equality instead of has_same_prefix

finally something broke proper. soon i'll know if this will actually
go somewhere
Id
c8cfb1f62c5bc8af0c3090462c68a352b1cc39b4
Author
Caio
Commit time
2024-03-13T12:31:18+01:00

Modified examples/foca_insecure_udp_agent.rs

@@ -142,14 +142,6
impl Identity for ID {
type Addr = SocketAddr;

- // Since a client outside the cluster will not be aware of our
- // `bump` field, we implement the optional trait method
- // `has_same_prefix` to allow anyone that knows our `addr`
- // to join our cluster.
- fn has_same_prefix(&self, other: &Self) -> bool {
- self.addr.eq(&other.addr)
- }
-
// And by implementing `renew` we enable automatic rejoining:
// when another member declares us as down, Foca immediatelly
// switches to this new identity and rejoins the cluster for us

Modified examples/identity_golf.rs

@@ -52,12 +52,6
})
}

- // And we ensure that members can Announce to us without
- // knowing our (randomized) extra field
- fn has_same_prefix(&self, other: &Self) -> bool {
- self.addr.eq(&other.addr)
- }
-
fn addr(&self) -> SocketAddrV4 {
self.addr
}
@@ -115,12 +109,6
addr: self.addr,
extra: self.extra.wrapping_add(1),
})
- }
-
- // And we ensure that members can Announce to us without
- // knowing our (randomized) extra field
- fn has_same_prefix(&self, other: &Self) -> bool {
- self.addr.eq(&other.addr)
}

fn addr(&self) -> (u8, u8) {

Modified src/identity.rs

@@ -40,21 +40,6

/// FIXME missing docs
fn addr(&self) -> Self::Addr;
-
- /// Optionally accept Announce messages addressed to an identity
- /// that isn't exactly the same as ours.
- ///
- /// Foca discards messages that aren't addressed to its exact
- /// identity. This means that if your identity has an unpredictable
- /// field (a UUID or a random number, for example), nobody will
- /// be able to join with us directly.
- ///
- /// The [`Self::has_same_prefix`] method is how we teach Foca to
- /// relax this restriction: Upon receiving an Announce message it
- /// will call `current_id.has_same_prefix(dst)` and if it yields
- /// `true` the message will be accepted and the new member will
- /// be allowed to join the cluster.
- fn has_same_prefix(&self, other: &Self) -> bool;
}

#[cfg(feature = "std")]
@@ -69,10 +54,6

fn addr(&self) -> $type {
*self
- }
-
- fn has_same_prefix(&self, _other: &Self) -> bool {
- false
}
}
};

Modified src/lib.rs

@@ -393,7 +393,7
for update in updates {
if update.id() == &self.identity {
self.handle_self_update(update.incarnation(), update.state(), &mut runtime)?;
- } else if self.identity.has_same_prefix(update.id()) {
+ } else if self.identity.addr() == update.id().addr() {
// We received an update that's about an identity that *could*
// have been ours but definitely isn't (the branch right above,
// where we check equality)
@@ -618,7 +618,7
self.config.num_indirect_probes.get(),
&mut self.member_buf,
&mut self.rng,
- |candidate| candidate != &probed_id && !candidate.has_same_prefix(&probed_id),
+ |candidate| candidate != &probed_id,
);

#[cfg(feature = "tracing")]
@@ -1436,7 +1436,7
|| (header.message == Message::Announce
// Then we accept it if DST is one of our _possible_
// identities
- && self.identity.has_same_prefix(&header.dst))
+ && self.identity.addr() == header.dst.addr())
}

fn handle_self_update(
@@ -2429,7 +2429,7
// passing the "has same prefix" check to verify the join
// doesn't happen
let wrong_dst = ID::new(3);
- assert!(!target_id.has_same_prefix(&wrong_dst));
+ assert_ne!(target_id.addr(), wrong_dst.addr());
let data = (
Header {
src: src_id,
@@ -2449,7 +2449,7
// prefix check
let dst = ID::new_with_bump(1, 42);
assert_ne!(target_id, dst);
- assert!(target_id.has_same_prefix(&dst));
+ assert_eq!(target_id.addr(), dst.addr());
let data = (
Header {
src: src_id,
@@ -3751,7 +3751,7
// So that they are not the same
assert_ne!(id, renewed);
// But have the same prefix
- assert!(id.has_same_prefix(&renewed));
+ assert_eq!(id.addr(), renewed.addr());

// If we have an instance running with the renewed
// id as its identity

Modified src/member.rs

@@ -360,10 +360,6
fn addr(&self) -> Self::Addr {
self.0
}
-
- fn has_same_prefix(&self, other: &Self) -> bool {
- self.0 == other.0
- }
}

use State::*;

Modified src/testing.rs

@@ -76,10 +76,6
impl Identity for ID {
type Addr = ID;

- fn has_same_prefix(&self, other: &Self) -> bool {
- self.id == other.id
- }
-
fn renew(&self) -> Option<Self> {
if self.rejoinable {
Some(ID::new_with_bump(self.id, self.bump.wrapping_add(1)).rejoinable())