Skip to content

Commit

Permalink
Merge pull request #255 from rustaceanrob/gossip-1-9
Browse files Browse the repository at this point in the history
Rename `PeerStatus::New` to `Gossiped`
  • Loading branch information
rustaceanrob authored Jan 9, 2025
2 parents 8ff81ce + 232f21d commit 4523bfe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<P: PeerStore> PeerMap<P> {
peer.addr.clone(),
peer.port,
peer.services,
PeerStatus::New,
PeerStatus::Gossiped,
))
.await
{
Expand Down Expand Up @@ -412,7 +412,7 @@ impl<P: PeerStore> PeerMap<P> {
peer,
default_port_from_network(&self.network),
ServiceFlags::NONE,
PeerStatus::New,
PeerStatus::Gossiped,
))
.await
.map_err(PeerManagerError::Database)?;
Expand Down
18 changes: 14 additions & 4 deletions src/db/memory/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl StatelessPeerStore {

async fn update(&mut self, peer: PersistedPeer) -> Result<(), StatelessPeerStoreError> {
match peer.status {
PeerStatus::New => {
PeerStatus::Gossiped => {
self.list
.entry(peer.clone().addr)
.and_modify(|stored| {
Expand Down Expand Up @@ -108,10 +108,20 @@ mod tests {
let mut peer_store = StatelessPeerStore::new();
let ip_1 = Ipv4Addr::new(1, 1, 1, 1);
let ip_2 = Ipv4Addr::new(2, 2, 2, 2);
let peer_1 = PersistedPeer::new(AddrV2::Ipv4(ip_1), 0, ServiceFlags::NONE, PeerStatus::New);
let peer_2 = PersistedPeer::new(AddrV2::Ipv4(ip_2), 0, ServiceFlags::NONE, PeerStatus::New);
let peer_1 = PersistedPeer::new(
AddrV2::Ipv4(ip_1),
0,
ServiceFlags::NONE,
PeerStatus::Gossiped,
);
let peer_2 = PersistedPeer::new(
AddrV2::Ipv4(ip_2),
0,
ServiceFlags::NONE,
PeerStatus::Gossiped,
);
let tor = AddrV2::TorV2([0; 10]);
let peer_3 = PersistedPeer::new(tor, 0, ServiceFlags::NONE, PeerStatus::New);
let peer_3 = PersistedPeer::new(tor, 0, ServiceFlags::NONE, PeerStatus::Gossiped);
let try_peer_2 =
PersistedPeer::new(AddrV2::Ipv4(ip_2), 0, ServiceFlags::NONE, PeerStatus::Tried);
let ban_peer_1 =
Expand Down
6 changes: 3 additions & 3 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl From<PersistedPeer> for (AddrV2, u16) {
/// The status of a peer in the database.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum PeerStatus {
/// A newly found peer from DNS or the peer-to-peer network.
New,
/// A peer was gossiped via DNS or the peer-to-peer network.
Gossiped,
/// The node successfully connected to this peer.
Tried,
/// A connected peer responded with faulty or malicious behavior.
Expand All @@ -65,7 +65,7 @@ pub enum PeerStatus {
impl Distribution<PeerStatus> for Standard {
fn sample<R: bitcoin::key::rand::Rng + ?Sized>(&self, rng: &mut R) -> PeerStatus {
match rng.gen_range(0..=1) {
0 => PeerStatus::New,
0 => PeerStatus::Gossiped,
_ => PeerStatus::Tried,
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/db/sqlite/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ impl SqlitePeerDb {
async fn update(&mut self, peer: PersistedPeer) -> Result<(), SqlPeerStoreError> {
let lock = self.conn.lock().await;
let stmt = match peer.status {
PeerStatus::New => "INSERT OR IGNORE INTO peers (ip_addr, port, service_flags, tried, banned) VALUES (?1, ?2, ?3, ?4, ?5)",
PeerStatus::Gossiped => "INSERT OR IGNORE INTO peers (ip_addr, port, service_flags, tried, banned) VALUES (?1, ?2, ?3, ?4, ?5)",
_ => "INSERT OR REPLACE INTO peers (ip_addr, port, service_flags, tried, banned) VALUES (?1, ?2, ?3, ?4, ?5)",
};
let (tried, banned) = match peer.status {
PeerStatus::New => (false, false),
PeerStatus::Gossiped => (false, false),
PeerStatus::Tried => (true, false),
PeerStatus::Ban => (true, true),
};
Expand All @@ -111,7 +111,7 @@ impl SqlitePeerDb {
let status = if tried {
PeerStatus::Tried
} else {
PeerStatus::New
PeerStatus::Gossiped
};
let ip = deserialize(&ip_addr)?;
let services: ServiceFlags = ServiceFlags::from(service_flags);
Expand Down Expand Up @@ -161,9 +161,19 @@ mod tests {
let ip_1 = Ipv4Addr::new(1, 1, 1, 1);
let ip_2 = Ipv4Addr::new(2, 2, 2, 2);
let tor = AddrV2::TorV2([8; 10]);
let peer_1 = PersistedPeer::new(AddrV2::Ipv4(ip_1), 0, ServiceFlags::NONE, PeerStatus::New);
let peer_2 = PersistedPeer::new(AddrV2::Ipv4(ip_2), 0, ServiceFlags::NONE, PeerStatus::New);
let peer_3 = PersistedPeer::new(tor, 0, ServiceFlags::NONE, PeerStatus::New);
let peer_1 = PersistedPeer::new(
AddrV2::Ipv4(ip_1),
0,
ServiceFlags::NONE,
PeerStatus::Gossiped,
);
let peer_2 = PersistedPeer::new(
AddrV2::Ipv4(ip_2),
0,
ServiceFlags::NONE,
PeerStatus::Gossiped,
);
let peer_3 = PersistedPeer::new(tor, 0, ServiceFlags::NONE, PeerStatus::Gossiped);
let try_peer_2 =
PersistedPeer::new(AddrV2::Ipv4(ip_2), 0, ServiceFlags::NONE, PeerStatus::Tried);
let ban_peer_1 =
Expand Down

0 comments on commit 4523bfe

Please sign in to comment.