@@ -834,180 +834,6 @@ mod postgres_tests {
834834 drop_scratch_db ( & admin, pool, & scratch_name) . await ;
835835 }
836836
837- #[ tokio:: test]
838- #[ ignore = "requires Postgres" ]
839- async fn stale_legacy_roster_cannot_replace_new_locked_snapshot ( ) {
840- use nostr:: { EventBuilder , Keys , Kind , Tag , Timestamp } ;
841-
842- let admin = PgPool :: connect ( & admin_url ( ) . await )
843- . await
844- . expect ( "connect admin" ) ;
845- let ( setup_pool, scratch_name) = create_scratch_db ( & admin, "mixed_roster_writer" ) . await ;
846- let base_url = admin_url ( ) . await ;
847- let slash = base_url. rfind ( '/' ) . expect ( "database URL has path segment" ) ;
848- let scratch_url = format ! ( "{}/{}" , & base_url[ ..slash] , scratch_name) ;
849- let pool = PgPoolOptions :: new ( )
850- . max_connections ( 1 )
851- . acquire_timeout ( Duration :: from_secs ( 1 ) )
852- . connect ( & scratch_url)
853- . await
854- . expect ( "connect one-connection scratch pool" ) ;
855- setup_pool. close ( ) . await ;
856- let db = Db :: from_pool ( pool. clone ( ) ) ;
857- let community_uuid = Uuid :: new_v4 ( ) ;
858- let community = CommunityId :: from_uuid ( community_uuid) ;
859- let channel = Uuid :: new_v4 ( ) ;
860- let relay_keys = Keys :: generate ( ) ;
861- let owner_keys = Keys :: generate ( ) ;
862- let owner = owner_keys. public_key ( ) . to_bytes ( ) ;
863- seed_community_channel ( & pool, community_uuid, channel, & owner_keys) . await ;
864-
865- // This is the old pod's unlocked capture A. It remains in process memory
866- // while a role-only canonical mutation advances and the new pod publishes B.
867- let base = Timestamp :: now ( ) . as_secs ( ) ;
868- let roster = |members : & [ ( & [ u8 ] , & str ) ] , timestamp| {
869- let tags =
870- std:: iter:: once ( Tag :: parse ( [ "d" , channel. to_string ( ) . as_str ( ) ] ) . expect ( "d tag" ) )
871- . chain ( members. iter ( ) . map ( |( member, role) | {
872- Tag :: parse ( [ "p" , hex:: encode ( member) . as_str ( ) , "" , * role] ) . expect ( "p tag" )
873- } ) )
874- . collect :: < Vec < _ > > ( ) ;
875- EventBuilder :: new ( Kind :: Custom ( 39002 ) , "" )
876- . tags ( tags)
877- . custom_created_at ( Timestamp :: from ( timestamp) )
878- . sign_with_keys ( & relay_keys)
879- . expect ( "sign roster" )
880- } ;
881-
882- let newcomer = Keys :: generate ( ) . public_key ( ) . to_bytes ( ) ;
883- sqlx:: query (
884- "INSERT INTO channel_members (community_id, channel_id, pubkey, role, invited_by) \
885- VALUES ($1, $2, $3, 'member', $4)",
886- )
887- . bind ( community_uuid)
888- . bind ( channel)
889- . bind ( newcomer. as_slice ( ) )
890- . bind ( owner. as_slice ( ) )
891- . execute ( & pool)
892- . await
893- . expect ( "seed member before legacy capture" ) ;
894- let stale_a = roster (
895- & [ ( owner. as_slice ( ) , "owner" ) , ( newcomer. as_slice ( ) , "member" ) ] ,
896- base + 2 ,
897- ) ;
898-
899- sqlx:: query (
900- "UPDATE channel_members SET role = 'admin' \
901- WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3",
902- )
903- . bind ( community_uuid)
904- . bind ( channel)
905- . bind ( newcomer. as_slice ( ) )
906- . execute ( & pool)
907- . await
908- . expect ( "commit newer canonical role" ) ;
909-
910- let relay_pubkey = relay_keys. public_key ( ) . to_bytes ( ) ;
911- let mut snapshot = db
912- . lock_member_snapshot ( community, channel, & relay_pubkey)
913- . await
914- . expect ( "new writer captures locked roster B" ) ;
915- let fresh_b = roster (
916- & [ ( owner. as_slice ( ) , "owner" ) , ( newcomer. as_slice ( ) , "admin" ) ] ,
917- base + 1 ,
918- ) ;
919- assert ! (
920- snapshot
921- . replace_member_event( & fresh_b)
922- . await
923- . expect( "new writer publishes B" )
924- . 1
925- ) ;
926- snapshot
927- . release ( )
928- . await
929- . expect ( "commit B and release locks" ) ;
930-
931- // Reproduce the old binary on one connection: replacement key ->
932- // old-head UPDATE -> raw INSERT. Do not use the app API, whose guard
933- // rejects before the retained trigger can exercise this legacy fence.
934- // A size-one pool and bounded attempt also catch self-deadlock.
935- let error = tokio:: time:: timeout ( Duration :: from_secs ( 3 ) , async {
936- let mut tx = pool. begin ( ) . await . expect ( "begin legacy replacement" ) ;
937- let relay_pubkey = relay_keys. public_key ( ) . to_bytes ( ) ;
938- sqlx:: query ( "SELECT pg_advisory_xact_lock($1)" )
939- . bind ( crate :: store:: replaceable:: event_replacement_lock_key (
940- community,
941- 39002 ,
942- & relay_pubkey,
943- Some ( channel. as_bytes ( ) . as_slice ( ) ) ,
944- ) )
945- . execute ( & mut * tx)
946- . await
947- . expect ( "legacy replacement key" ) ;
948- let deleted = sqlx:: query (
949- "UPDATE events SET deleted_at = NOW() \
950- WHERE community_id=$1 AND kind=39002 AND pubkey=$2 \
951- AND channel_id=$3 AND deleted_at IS NULL",
952- )
953- . bind ( community_uuid)
954- . bind ( relay_pubkey. as_slice ( ) )
955- . bind ( channel)
956- . execute ( & mut * tx)
957- . await
958- . expect ( "legacy soft-delete of current head" ) ;
959- assert_eq ! ( deleted. rows_affected( ) , 1 , "legacy path must delete the live head" ) ;
960- let result = sqlx:: query (
961- "INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag) \
962- VALUES ($1, $2, $3, $4, 39002, $5, $6, $7, NOW(), $8, $9)",
963- )
964- . bind ( community_uuid)
965- . bind ( stale_a. id . as_bytes ( ) . as_slice ( ) )
966- . bind ( relay_pubkey. as_slice ( ) )
967- . bind ( chrono:: DateTime :: from_timestamp ( stale_a. created_at . as_secs ( ) as i64 , 0 ) . expect ( "timestamp" ) )
968- . bind ( serde_json:: to_value ( & stale_a. tags ) . expect ( "tags" ) )
969- . bind ( & stale_a. content )
970- . bind ( stale_a. sig . serialize ( ) . as_slice ( ) )
971- . bind ( channel)
972- . bind ( channel. to_string ( ) )
973- . execute ( & mut * tx)
974- . await ;
975- tx. rollback ( ) . await . expect ( "restore live head after legacy attempt" ) ;
976- result
977- } )
978- . await
979- . expect ( "legacy replacement must not deadlock" )
980- . expect_err ( "stale captured roster A must be rejected" ) ;
981- assert ! (
982- matches!( error, sqlx:: Error :: Database ( ref db_error)
983- if db_error. code( ) . as_deref( ) == Some ( "23514" )
984- && db_error. message( ) == "kind 39002 roster does not match canonical channel membership" ) ,
985- "expected trigger roster fence check violation, got {error:?}"
986- ) ;
987-
988- let live_ids: Vec < Vec < u8 > > = sqlx:: query_scalar (
989- "SELECT id FROM events WHERE community_id=$1 AND channel_id=$2 \
990- AND kind=39002 AND pubkey=$3 AND deleted_at IS NULL",
991- )
992- . bind ( community_uuid)
993- . bind ( channel)
994- . bind ( relay_pubkey. as_slice ( ) )
995- . fetch_all ( & pool)
996- . await
997- . expect ( "load live roster heads" ) ;
998- assert_eq ! ( live_ids, vec![ fresh_b. id. as_bytes( ) . to_vec( ) ] ) ;
999- let stale_rows: i64 =
1000- sqlx:: query_scalar ( "SELECT count(*) FROM events WHERE community_id=$1 AND id=$2" )
1001- . bind ( community_uuid)
1002- . bind ( stale_a. id . as_bytes ( ) . as_slice ( ) )
1003- . fetch_one ( & pool)
1004- . await
1005- . expect ( "count rejected stale roster" ) ;
1006- assert_eq ! ( stale_rows, 0 , "stale roster insert must roll back" ) ;
1007-
1008- drop_scratch_db ( & admin, pool, & scratch_name) . await ;
1009- }
1010-
1011837 #[ tokio:: test]
1012838 #[ ignore = "requires Postgres" ]
1013839 async fn nip_rs_replacement_hard_deletes_payload_and_watermark_rejects_replay ( ) {
0 commit comments