Skip to content

Commit b7dacd5

Browse files
baxenmbotclaude
committed
feat(relay): publish verified names on open relays without membership
When BUZZ_REQUIRE_RELAY_MEMBERSHIP is false, an enforced NIP-FI admission is the eligibility proof: a key no longer needs a relay_members row to publish, refresh, or keep its verified profile. Bans, issuer unbind, policy disable and disconnect cleanup still retract, and ban, binding and policy generations still fence stale work. Relays that require membership keep today's rule. Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Bradley Axen <[email protected]>
1 parent bac8e4d commit b7dacd5

3 files changed

Lines changed: 237 additions & 18 deletions

File tree

‎crates/buzz-db/src/store/verified_profiles.rs‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ pub struct ProfileAdmission<'a> {
5151
pub epoch: ProfileEpoch,
5252
/// Database community bindings, as opposed to operator environment bindings.
5353
pub database_binding: bool,
54+
/// Whether the relay requires an explicit `relay_members` row to connect.
55+
/// Open relays treat enforced NIP-FI admission as eligibility instead.
56+
pub membership_required: bool,
5457
}
5558

5659
/// Access-owned incarnations survive removal/restoration without projection locks.
@@ -92,13 +95,24 @@ async fn lock_profile(tx: &mut AdmittedTx, pubkey: &str) -> Result<()> {
9295
Ok(())
9396
}
9497

95-
fn same_access(mut left: AccessEpoch, mut right: AccessEpoch, database_binding: bool) -> bool {
98+
fn same_access(
99+
mut left: AccessEpoch,
100+
mut right: AccessEpoch,
101+
database_binding: bool,
102+
membership_required: bool,
103+
) -> bool {
96104
if !database_binding {
97105
left.binding = false;
98106
left.issuer = None;
99107
right.binding = false;
100108
right.issuer = None;
101109
}
110+
if !membership_required {
111+
left.member = false;
112+
left.membership = None;
113+
right.member = false;
114+
right.membership = None;
115+
}
102116
left == right
103117
}
104118

@@ -296,8 +310,9 @@ impl Db {
296310
}))
297311
}
298312

299-
/// Publish/refresh/retract under the current policy, membership, and
300-
/// recorded assertion ordering in one admitted transaction.
313+
/// Publish/refresh/retract under the current policy, access (membership
314+
/// only where the relay requires it), and recorded assertion ordering in
315+
/// one admitted transaction.
301316
pub async fn apply_profile_admission(
302317
&self,
303318
input: ProfileAdmission<'_>,
@@ -327,9 +342,14 @@ impl Db {
327342
lock_profile(&mut tx, input.pubkey).await?;
328343
let (access, banned) =
329344
access_epoch(tx.conn(), input.community, input.pubkey, input.issuer).await?;
330-
if !same_access(access, input.epoch.access, input.database_binding)
331-
|| (input.database_binding && !access.binding)
332-
|| (input.profile.is_some() && (!access.member || banned))
345+
if !same_access(
346+
access,
347+
input.epoch.access,
348+
input.database_binding,
349+
input.membership_required,
350+
) || (input.database_binding && !access.binding)
351+
|| (input.profile.is_some()
352+
&& ((input.membership_required && !access.member) || banned))
333353
{
334354
return Ok(None);
335355
}
@@ -434,6 +454,7 @@ impl Db {
434454
.0,
435455
access,
436456
input.database_binding,
457+
input.membership_required,
437458
) {
438459
return Ok(None);
439460
}
@@ -489,12 +510,14 @@ impl Db {
489510
}
490511

491512
/// Materialize a pending retraction, retaining ordering records, then return
492-
/// the durable event for retryable local/cross-process broadcast.
513+
/// the durable event for retryable local/cross-process broadcast. Only
514+
/// relays that require membership retract a key that has no member row.
493515
pub async fn process_profile_work(
494516
&self,
495517
community: CommunityId,
496518
pubkey: &str,
497519
keys: &Keys,
520+
membership_required: bool,
498521
) -> Result<Option<Event>> {
499522
let mut tx = crate::begin_community_write_transaction(
500523
&self.pool,
@@ -524,7 +547,7 @@ impl Db {
524547
let retract = row.get::<bool, _>("active")
525548
&& (pending
526549
|| row.try_get::<bool, _>("retract")?
527-
|| (!current.member
550+
|| ((membership_required && !current.member)
528551
|| banned
529552
|| (row.get::<bool, _>("database_binding") && !current.binding)));
530553
if retract {

‎crates/buzz-db/src/store/verified_profiles/postgres_tests.rs‎

Lines changed: 199 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async fn admit(
6666
profile,
6767
epoch,
6868
database_binding: false,
69+
membership_required: true,
6970
},
7071
keys,
7172
)
@@ -173,7 +174,7 @@ async fn lifecycle() {
173174
assert!(!db.pending_profile_work(None).await.unwrap().is_empty()); // initial broadcast still queued
174175
assert_eq!(
175176
tag(
176-
&db.process_profile_work(community, &key, &keys)
177+
&db.process_profile_work(community, &key, &keys, true)
177178
.await
178179
.unwrap()
179180
.unwrap(),
@@ -184,7 +185,7 @@ async fn lifecycle() {
184185
);
185186
admit(&db, community, &keys, &key, &policy, epoch, now, None).await;
186187
let retracted = db
187-
.process_profile_work(community, &key, &keys)
188+
.process_profile_work(community, &key, &keys, true)
188189
.await
189190
.unwrap()
190191
.unwrap();
@@ -271,7 +272,7 @@ async fn affected_scope_and_configuration_epochs_fence_restored_state() {
271272
)
272273
.await
273274
.unwrap();
274-
db.process_profile_work(community, &key, &keys)
275+
db.process_profile_work(community, &key, &keys, true)
275276
.await
276277
.unwrap();
277278
admit(
@@ -339,6 +340,7 @@ async fn ordering_and_access_races() {
339340
profile: Some(("Alice", "alice", None)),
340341
epoch,
341342
database_binding: false,
343+
membership_required: true,
342344
};
343345
db.apply_profile_admission(input(now, epoch), &keys)
344346
.await
@@ -358,7 +360,7 @@ async fn ordering_and_access_races() {
358360
first.id,
359361
"unchanged values must not refresh early or erase pending cleanup"
360362
);
361-
db.process_profile_work(community, &key, &keys)
363+
db.process_profile_work(community, &key, &keys, true)
362364
.await
363365
.unwrap();
364366
assert_eq!(
@@ -389,7 +391,7 @@ async fn ordering_and_access_races() {
389391
db.retract_profile_admission(community, "another-issuer", &key, trigger + 99, None)
390392
.await
391393
.unwrap();
392-
db.process_profile_work(community, &key, &keys)
394+
db.process_profile_work(community, &key, &keys, true)
393395
.await
394396
.unwrap();
395397
assert_eq!(
@@ -448,7 +450,7 @@ async fn ordering_and_access_races() {
448450
.await
449451
.unwrap()
450452
.unwrap();
451-
db.process_profile_work(community, &key, &keys)
453+
db.process_profile_work(community, &key, &keys, true)
452454
.await
453455
.unwrap();
454456
assert_eq!(
@@ -508,7 +510,7 @@ async fn ordering_and_access_races() {
508510
.await
509511
.unwrap()
510512
.unwrap();
511-
db.process_profile_work(community, &key, &keys)
513+
db.process_profile_work(community, &key, &keys, true)
512514
.await
513515
.unwrap();
514516
assert_eq!(
@@ -530,7 +532,7 @@ async fn ordering_and_access_races() {
530532
.await
531533
.unwrap();
532534
tx.commit().await.unwrap();
533-
db.process_profile_work(community, &key, &keys)
535+
db.process_profile_work(community, &key, &keys, true)
534536
.await
535537
.unwrap();
536538
assert_eq!(
@@ -556,6 +558,192 @@ async fn migration_schema_cleanup_versions_preserve_later_admissions_and_access_
556558
ordering_and_access_races().await;
557559
}
558560

561+
async fn set_member(db: &Db, community: CommunityId, key: &str, member: bool) {
562+
let mut tx = crate::begin_community_write_transaction(
563+
&db.pool,
564+
community,
565+
crate::observability::WriterOperation::Authorization,
566+
)
567+
.await
568+
.unwrap();
569+
let sql = if member {
570+
"INSERT INTO relay_members (community_id, pubkey, role) VALUES ($1, $2, 'member')"
571+
} else {
572+
"DELETE FROM relay_members WHERE community_id = $1 AND pubkey = $2"
573+
};
574+
sqlx::query(sql)
575+
.bind(community.as_uuid())
576+
.bind(key)
577+
.execute(tx.conn())
578+
.await
579+
.unwrap();
580+
tx.commit().await.unwrap();
581+
}
582+
583+
async fn active(db: &Db, community: CommunityId, key: &str) -> Option<String> {
584+
current(db, community, key)
585+
.await
586+
.and_then(|event| tag(&event, "active").map(str::to_owned))
587+
}
588+
589+
async fn open_relay_eligibility() {
590+
let (db, community, keys, key, policy) = fixture().await;
591+
set_member(&db, community, &key, false).await;
592+
let epoch = db
593+
.profile_epoch(community, &key, &policy)
594+
.await
595+
.unwrap()
596+
.unwrap();
597+
let now = Utc::now() - Duration::seconds(60);
598+
let input = |admitted_at, epoch, membership_required| ProfileAdmission {
599+
community,
600+
issuer: &policy.issuer,
601+
pubkey: &key,
602+
issued_at: now,
603+
admitted_at,
604+
profile: Some(("Alice", "alice", None)),
605+
epoch,
606+
database_binding: false,
607+
membership_required,
608+
};
609+
assert!(
610+
db.apply_profile_admission(input(now + Duration::seconds(1), epoch, true), &keys)
611+
.await
612+
.unwrap()
613+
.is_none(),
614+
"members-required relays need an explicit member row"
615+
);
616+
assert!(
617+
db.apply_profile_admission(input(now + Duration::seconds(2), epoch, false), &keys)
618+
.await
619+
.unwrap()
620+
.is_some(),
621+
"open relays publish for enforced admissions without a member row"
622+
);
623+
db.process_profile_work(community, &key, &keys, false)
624+
.await
625+
.unwrap();
626+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("true"));
627+
628+
// Membership incarnations do not fence open-relay work, but still fence
629+
// work on relays that require membership.
630+
set_member(&db, community, &key, true).await;
631+
assert!(
632+
db.apply_profile_admission(input(now + Duration::seconds(3), epoch, true), &keys)
633+
.await
634+
.unwrap()
635+
.is_none(),
636+
"a new membership incarnation fences members-required work"
637+
);
638+
assert!(
639+
db.apply_profile_admission(input(now + Duration::seconds(3), epoch, false), &keys)
640+
.await
641+
.unwrap()
642+
.is_some(),
643+
"membership incarnations do not fence open-relay work"
644+
);
645+
set_member(&db, community, &key, false).await;
646+
647+
// A ban retracts, and ban/unban still fences the earlier admission. Its
648+
// cleanup is stamped with wall time, so later admissions must follow it.
649+
let bytes = hex::decode(&key).unwrap();
650+
crate::moderation::ban_member(&db.pool, community, &bytes, &bytes, None, None)
651+
.await
652+
.unwrap();
653+
db.process_profile_work(community, &key, &keys, false)
654+
.await
655+
.unwrap();
656+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("false"));
657+
crate::moderation::unban_member(&db.pool, community, &bytes, &bytes)
658+
.await
659+
.unwrap();
660+
let later = Utc::now() + Duration::seconds(1);
661+
assert!(
662+
db.apply_profile_admission(input(later, epoch, false), &keys)
663+
.await
664+
.unwrap()
665+
.is_none(),
666+
"ban/unban fences open-relay work from the previous incarnation"
667+
);
668+
let epoch = db
669+
.profile_epoch(community, &key, &policy)
670+
.await
671+
.unwrap()
672+
.unwrap();
673+
db.apply_profile_admission(input(later + Duration::seconds(1), epoch, false), &keys)
674+
.await
675+
.unwrap()
676+
.unwrap();
677+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("true"));
678+
679+
// A disconnect cleanup retracts; a later admission republishes.
680+
db.retract_profile_admission(
681+
community,
682+
&policy.issuer,
683+
&key,
684+
(later + Duration::seconds(2)).timestamp(),
685+
None,
686+
)
687+
.await
688+
.unwrap();
689+
db.process_profile_work(community, &key, &keys, false)
690+
.await
691+
.unwrap();
692+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("false"));
693+
db.apply_profile_admission(input(later + Duration::seconds(3), epoch, false), &keys)
694+
.await
695+
.unwrap()
696+
.unwrap();
697+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("true"));
698+
699+
// Requiring membership later retracts keys without a member row.
700+
db.process_profile_work(community, &key, &keys, true)
701+
.await
702+
.unwrap();
703+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("false"));
704+
db.apply_profile_admission(input(later + Duration::seconds(4), epoch, false), &keys)
705+
.await
706+
.unwrap()
707+
.unwrap();
708+
709+
// Disabling the policy retracts and fences the earlier admission.
710+
db.configure_profile_policies(
711+
&[ProfilePolicy {
712+
label: None,
713+
..policy.clone()
714+
}],
715+
1,
716+
)
717+
.await
718+
.unwrap();
719+
db.process_profile_work(community, &key, &keys, false)
720+
.await
721+
.unwrap();
722+
assert_eq!(active(&db, community, &key).await.as_deref(), Some("false"));
723+
db.configure_profile_policies(std::slice::from_ref(&policy), 2)
724+
.await
725+
.unwrap();
726+
assert!(
727+
db.apply_profile_admission(input(later + Duration::seconds(5), epoch, false), &keys)
728+
.await
729+
.unwrap()
730+
.is_none(),
731+
"policy disable/restore fences open-relay work"
732+
);
733+
}
734+
735+
#[tokio::test]
736+
#[ignore = "requires Postgres"]
737+
async fn open_relays_publish_without_membership_and_keep_other_fences() {
738+
open_relay_eligibility().await;
739+
}
740+
741+
#[tokio::test]
742+
#[ignore = "requires Postgres"]
743+
async fn migration_schema_open_relays_publish_without_membership_and_keep_other_fences() {
744+
open_relay_eligibility().await;
745+
}
746+
559747
async fn environment_removal_cleanup() {
560748
let (db, community, keys, key, mut policy) = fixture().await;
561749
let other = CommunityId::from_uuid(uuid::Uuid::new_v4());
@@ -619,7 +807,9 @@ async fn environment_removal_cleanup() {
619807
.await
620808
.unwrap();
621809
for id in [community, other] {
622-
db.process_profile_work(id, &key, &keys).await.unwrap();
810+
db.process_profile_work(id, &key, &keys, true)
811+
.await
812+
.unwrap();
623813
}
624814
assert_eq!(
625815
tag(&current(&db, community, &key).await.unwrap(), "active"),

0 commit comments

Comments
 (0)