@@ -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+
559747async 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