Skip to content

Commit 98f4de6

Browse files
committed
sdk: refactor logic for checking outdated gossip public keys
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 9366eaf commit 98f4de6

File tree

1 file changed

+30
-20
lines changed
  • crates/nostr-sdk/src/client

1 file changed

+30
-20
lines changed

crates/nostr-sdk/src/client/mod.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,30 @@ impl Client {
13321332

13331333
// Gossip
13341334
impl Client {
1335+
async fn check_outdated_public_keys<'a, I>(
1336+
&self,
1337+
gossip: &Arc<dyn NostrGossip>,
1338+
public_keys: I,
1339+
gossip_kind: GossipListKind,
1340+
) -> Result<HashSet<PublicKey>, Error>
1341+
where
1342+
I: IntoIterator<Item = &'a PublicKey>,
1343+
{
1344+
// First check: check if there are outdated public keys.
1345+
let mut outdated_public_keys: HashSet<PublicKey> = HashSet::new();
1346+
1347+
for public_key in public_keys.into_iter() {
1348+
// Get the public key status
1349+
let status = gossip.status(public_key, gossip_kind).await?;
1350+
1351+
if let GossipPublicKeyStatus::Outdated { .. } = status {
1352+
outdated_public_keys.insert(*public_key);
1353+
}
1354+
}
1355+
1356+
Ok(outdated_public_keys)
1357+
}
1358+
13351359
/// Check for and update outdated public key data
13361360
///
13371361
/// Steps:
@@ -1349,16 +1373,9 @@ impl Client {
13491373
let public_keys: HashSet<PublicKey> = public_keys.into_iter().collect();
13501374

13511375
// First check: check if there are outdated public keys.
1352-
let mut outdated_public_keys_first_check: HashSet<PublicKey> = HashSet::new();
1353-
1354-
for public_key in public_keys.iter() {
1355-
// Get the public key status
1356-
let status = gossip.status(public_key, gossip_kind).await?;
1357-
1358-
if let GossipPublicKeyStatus::Outdated { .. } = status {
1359-
outdated_public_keys_first_check.insert(*public_key);
1360-
}
1361-
}
1376+
let outdated_public_keys_first_check: HashSet<PublicKey> = self
1377+
.check_outdated_public_keys(gossip, public_keys.iter(), gossip_kind)
1378+
.await?;
13621379

13631380
// No outdated public keys, immediately return.
13641381
if outdated_public_keys_first_check.is_empty() {
@@ -1374,16 +1391,9 @@ impl Client {
13741391

13751392
// Second check: check data is still outdated after acquiring permit
13761393
// (another process might have updated it while we were waiting)
1377-
let mut outdated_public_keys: HashSet<PublicKey> = HashSet::new();
1378-
1379-
for public_key in public_keys.into_iter() {
1380-
// Get the public key status
1381-
let status = gossip.status(&public_key, gossip_kind).await?;
1382-
1383-
if let GossipPublicKeyStatus::Outdated { .. } = status {
1384-
outdated_public_keys.insert(public_key);
1385-
}
1386-
}
1394+
let outdated_public_keys: HashSet<PublicKey> = self
1395+
.check_outdated_public_keys(gossip, public_keys.iter(), gossip_kind)
1396+
.await?;
13871397

13881398
// Double-check: data might have been updated while waiting for permit
13891399
if outdated_public_keys.is_empty() {

0 commit comments

Comments
 (0)