Skip to content

Commit 78b5dae

Browse files
TheSentinel454Elrondclaude
authored
fix(relay): store a kind:9000 and its membership writes in one transaction (#8606)
Follow-up to #8575. ## Problem Today a kind:9000 is stored first and the membership write runs afterwards, on its own. If that write fails, the relay logs it and still returns an accepted OK. A resend of the same event then returns `duplicate:`, so the person is never added. This is the catch-log-and-accept failure that Review-Proven Rules #1 and #5 describe. ## Change kind:9000 now uses the same atomic path as kind:9002 (`persisted_atomically` in ingest). - **`buzz-db`:** new `put_members_with_event`, which runs one transaction: 1. Take the channel's membership lock, before the event insert, the TTL lock and the channel row. This matches the order the other membership writers use, so it adds no new lock cycle. 2. Store the event. If it's already stored, roll back and return `Duplicate`. 3. Run the existing `put_members` decisions and upsert. These now live in a shared `put_members_on` step on the transaction. 4. Commit. - **Failure:** nothing is stored and the client gets an `error:` rejection, so it can resend the same event and have it applied. - **Everyone refused:** if the decisions under the lock refuse every person, nothing is stored and the event is rejected with the same text validation gives. Before, a single-person add refused at that point was accepted with an empty message. - **After commit:** the live effects stay as they were. They're best-effort and read the committed state: cache invalidation, `member_joined` notices, member-added notifications and 39002 discovery. - **`NOSTR.md`:** now says a failed kind:9000 can be resent as is. ## What got simpler - kind:9000 no longer has its own store-then-apply path in ingest. It shares the atomic branch with 9002 and workflow deletion. - `apply_put_user`'s "write failed, report it as a per-person rejection inside an accepted OK" branch is gone. - The `Db::put_members` wrapper is deleted. Its only caller was the kind:9000 path. ## Identified debt (not in this PR) kind:9001 (remove), 9021 (join) and 9022 (leave) have the same gap. Each stores the event, then runs the membership write in `handle_side_effects`, and a failure is only logged. kind:9008 and 9005 follow the same store-then-apply pattern. These can move onto the same atomic shape in a follow-up. ## Tests - `failed_put_user_stores_nothing` (relay, Postgres): a test trigger fails the second person's row in a two-person add, after the event row and the first person were already written in the same transaction. It checks: - Both the send and a resend are rejected with `error:`. - No kind:9000 is stored, nobody is added, and no `member_joined` notice is posted. - With the trigger dropped, the same signed event adds both people. - One more resend returns `duplicate:` and posts no second notice. - `put_members_with_event_stores_the_event_with_its_writes` (buzz-db): checks the three outcomes: - Refused: nothing is stored. - Applied: the event is stored and the person is written. - Duplicate: the roster is left alone. - `put_user_refused_under_the_lock_stores_nothing` (relay, Postgres): a test trigger removes the sender from a private channel inside the write transaction, so validation passes and the decision under the lock refuses. For a single add and a batch, it checks the `invalid:` rejection, that nothing is stored and no notice is posted, and that the same event applies once the trigger is gone. - Mutation checks, run in Blox gates: - At `d18e88fd`: committing the refused event fails the buzz-db test; applying a duplicate fails the first two tests; storing the event in its own transaction fails the first two tests. - At `3b106623`: turning the refusal under the lock into `error:` fails the new refusal test, and so does committing the refused event. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Luke Tornquist <[email protected]> Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3ed07e5 commit 78b5dae

5 files changed

Lines changed: 502 additions & 55 deletions

File tree

‎NOSTR.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ PGPASSWORD=buzz_dev psql -h localhost -U buzz -d buzz -c \
5151
| **Deletions (kind:5)** | ✅ | Standard NIP-09; self-authored only. `#h` optional, `#e` required |
5252
| **User profiles (kind:0)** | ✅ | NIP-01 metadata; synced to users table (display_name, avatar, about, NIP-05). NIP-05 handles must canonicalize to this relay's domain — off-domain or invalid handles are silently cleared. If a NIP-05 handle collides with another user's (UNIQUE constraint), the handle is skipped but other profile fields (display_name, avatar, about) are still synced. |
5353
| **Group creation (kind:9007)** | ✅ | NIP-29; include `name` tag, optional `visibility`, `channel_type`, label `t` and `unlisted` tags |
54-
| **Add user (kind:9000)** | ✅ | Open: any user, subject to target's `channel_add_policy` (`owner_only`/`nobody` can block). Private: owner/admin only. Self-add bypasses agent policy but not private-channel auth. A guest cannot add anyone. A new member with no role tag gets the channel's `join_role`; a non-member adding themselves may ask for `join_role` or a lower role. One event may name up to 100 people: one `p` tag each, with an optional role as the `p` tag's third element (NIP-29), falling back to the event's `role` tag. Each person is checked and added on their own. When the event names several people, the OK message is `members: ` followed by JSON listing the non-empty `added`, `updated` (role changed), `already_member` and `rejected` (`[{"reason", "pubkeys"}]`) results. The event is refused only when it is malformed or nobody it names may be added; then one person keeps the single-add error, and several get `invalid: members: {…}`. A resent event returns `duplicate:` without results; to retry an `error:` rejection, send a new event. Re-adding someone at their current role posts no second `member_joined` notice. |
54+
| **Add user (kind:9000)** | ✅ | Open: any user, subject to target's `channel_add_policy` (`owner_only`/`nobody` can block). Private: owner/admin only. Self-add bypasses agent policy but not private-channel auth. A guest cannot add anyone. A new member with no role tag gets the channel's `join_role`; a non-member adding themselves may ask for `join_role` or a lower role. One event may name up to 100 people: one `p` tag each, with an optional role as the `p` tag's third element (NIP-29), falling back to the event's `role` tag. Each person is checked and added on their own. When the event names several people, the OK message is `members: ` followed by JSON listing the non-empty `added`, `updated` (role changed), `already_member` and `rejected` (`[{"reason", "pubkeys"}]`) results. The event is refused only when it is malformed or nobody it names may be added; then one person keeps the single-add error, and several get `invalid: members: {…}`. The event and its membership changes are stored together, so an `error:` rejection stores nothing and the same event can be resent; a resent stored event returns `duplicate:` without results. Re-adding someone at their current role posts no second `member_joined` notice. |
5555
| **Remove user (kind:9001)** | ✅ | Self-remove allowed (with last-owner guard). Removing others: owner/admin only. |
5656
| **Edit group metadata (kind:9002)** | ✅ | `name`/`about`/label `t`/`visibility`/`ttl`/`archived`/`join_role`/`unlisted` tags: owner/admin. `topic`/`purpose` tags: any member except a guest. Buzz applies 9002 as a partial update: an absent tag leaves that setting unchanged. |
5757
| **Admin delete event (kind:9005)** | ✅ | Event author can always delete own. Otherwise owner/admin required. Target must be in same channel. |

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

Lines changed: 218 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -791,14 +791,7 @@ pub async fn put_members(
791791
targets: &[MemberPut<'_>],
792792
invited_by: Option<&[u8]>,
793793
) -> Result<Vec<Result<PutMemberResult>>> {
794-
let mut seen = std::collections::HashSet::with_capacity(targets.len());
795-
if let Some(dup) = targets.iter().find(|t| !seen.insert(t.pubkey)) {
796-
return Err(DbError::InvalidData(format!(
797-
"pubkey {} appears more than once",
798-
hex::encode(dup.pubkey)
799-
)));
800-
}
801-
794+
check_distinct_targets(targets)?;
802795
let mut tx = crate::begin_community_write_transaction(
803796
pool,
804797
community_id,
@@ -809,7 +802,100 @@ pub async fn put_members(
809802
// First statement: serialize the whole decision / owner-count / upsert
810803
// sequence against concurrent membership writes on this channel.
811804
acquire_channel_membership_lock(tx.conn(), community_id, channel_id).await?;
805+
let results = put_members_on(&mut tx, channel_id, targets, invited_by).await?;
806+
tx.commit().await?;
807+
Ok(results)
808+
}
809+
810+
/// The result of [`Db::put_members_with_event`].
811+
pub enum MemberPutEventWrite {
812+
/// The event was stored and at least one person was written. One result
813+
/// per target, in order, as from [`put_members`].
814+
Applied {
815+
/// The stored kind:9000 event.
816+
stored: buzz_core::StoredEvent,
817+
/// One result per target.
818+
results: Vec<Result<PutMemberResult>>,
819+
},
820+
/// The event was already stored. Nothing changed: its membership changes
821+
/// committed with it the first time.
822+
Duplicate(buzz_core::StoredEvent),
823+
/// Every person was refused, so nothing was stored. One error per target.
824+
Refused(Vec<DbError>),
825+
}
826+
827+
/// Store a kind:9000 `event` and apply its [`put_members`] writes in one
828+
/// transaction. If any write fails, nothing is stored, so the client can
829+
/// resend the same event and have it applied.
830+
///
831+
/// The membership lock comes first, as in [`put_members`] and the channel
832+
/// settings writers, so a second copy of the event waits for the first and
833+
/// then finds it stored.
834+
pub(crate) async fn put_members_with_event(
835+
pool: &PgPool,
836+
push_enqueue: Option<crate::runtime::push_enqueue::PushEnqueue>,
837+
community_id: CommunityId,
838+
channel_id: Uuid,
839+
event: &nostr::Event,
840+
targets: &[MemberPut<'_>],
841+
thread_meta: Option<crate::event::ThreadMetadataParams<'_>>,
842+
) -> Result<MemberPutEventWrite> {
843+
check_distinct_targets(targets)?;
844+
let mut tx = crate::begin_community_event_write_transaction(
845+
pool,
846+
community_id,
847+
crate::observability::WriterOperation::EventWrite,
848+
)
849+
.await?;
850+
tx.set_push_enqueue(push_enqueue);
851+
acquire_channel_membership_lock(tx.conn(), community_id, channel_id).await?;
812852

853+
// The same writes as `Db::insert_event_with_thread_metadata`.
854+
let (stored, inserted) = crate::event::insert_event_with_thread_metadata_tx(
855+
&mut tx,
856+
event,
857+
Some(channel_id),
858+
thread_meta,
859+
)
860+
.await?;
861+
if !inserted {
862+
tx.rollback().await?;
863+
return Ok(MemberPutEventWrite::Duplicate(stored));
864+
}
865+
crate::insert_mentions_in_transaction(&mut tx, event, Some(channel_id)).await?;
866+
867+
let actor = event.pubkey.to_bytes();
868+
let results = put_members_on(&mut tx, channel_id, targets, Some(&actor)).await?;
869+
if results.iter().all(Result::is_err) {
870+
tx.rollback().await?;
871+
return Ok(MemberPutEventWrite::Refused(
872+
results.into_iter().filter_map(Result::err).collect(),
873+
));
874+
}
875+
tx.commit().await?;
876+
Ok(MemberPutEventWrite::Applied { stored, results })
877+
}
878+
879+
fn check_distinct_targets(targets: &[MemberPut<'_>]) -> Result<()> {
880+
let mut seen = std::collections::HashSet::with_capacity(targets.len());
881+
match targets.iter().find(|t| !seen.insert(t.pubkey)) {
882+
Some(dup) => Err(DbError::InvalidData(format!(
883+
"pubkey {} appears more than once",
884+
hex::encode(dup.pubkey)
885+
))),
886+
None => Ok(()),
887+
}
888+
}
889+
890+
/// The decisions and upsert of [`put_members`], on a transaction that holds
891+
/// `channel_id`'s membership lock. The caller owns the commit.
892+
async fn put_members_on(
893+
tx: &mut AdmittedTx,
894+
channel_id: Uuid,
895+
targets: &[MemberPut<'_>],
896+
invited_by: Option<&[u8]>,
897+
) -> Result<Vec<Result<PutMemberResult>>> {
898+
let community_id = tx.community();
813899
let channel = get_channel_tx(tx.conn(), community_id, channel_id).await?;
814900
let private = channel.visibility == "private";
815901
let join_role = buzz_core::channel::ChannelJoinRole::parse_fail_closed(&channel.join_role);
@@ -945,7 +1031,6 @@ pub async fn put_members(
9451031
written.insert(member.pubkey.clone(), member);
9461032
}
9471033
}
948-
tx.commit().await?;
9491034

9501035
Ok(targets
9511036
.iter()
@@ -2115,17 +2200,28 @@ impl Db {
21152200
.await
21162201
}
21172202

2118-
/// [`Db::put_member`] for several people in one transaction (see
2119-
/// [`put_members`]).
2120-
#[datastore_span(name = "put_members", system = "postgresql")]
2121-
pub async fn put_members(
2203+
/// Stores a kind:9000 `event` and applies its [`put_members`] writes,
2204+
/// with the event's author as the actor, in one transaction (see
2205+
/// [`put_members_with_event`]).
2206+
#[datastore_span(name = "put_members_with_event", system = "postgresql")]
2207+
pub async fn put_members_with_event(
21222208
&self,
21232209
community_id: CommunityId,
21242210
channel_id: Uuid,
2211+
event: &nostr::Event,
21252212
targets: &[MemberPut<'_>],
2126-
invited_by: Option<&[u8]>,
2127-
) -> Result<Vec<Result<PutMemberResult>>> {
2128-
put_members(&self.pool, community_id, channel_id, targets, invited_by).await
2213+
thread_meta: Option<crate::event::ThreadMetadataParams<'_>>,
2214+
) -> Result<MemberPutEventWrite> {
2215+
put_members_with_event(
2216+
&self.pool,
2217+
self.push_enqueue.clone(),
2218+
community_id,
2219+
channel_id,
2220+
event,
2221+
targets,
2222+
thread_meta,
2223+
)
2224+
.await
21292225
}
21302226

21312227
/// Removes a member from a channel.
@@ -3476,6 +3572,112 @@ mod postgres_tests {
34763572
assert!(matches!(err, DbError::InvalidData(_)), "{err:?}");
34773573
}
34783574

3575+
/// `put_members_with_event` stores the event only with the writes it
3576+
/// carries: a stored event applies its people, a resend changes nothing,
3577+
/// and an event whose every person is refused under the lock is not
3578+
/// stored. Mutations: commit when everyone is refused → the refused event
3579+
/// is stored → RED; write people on a duplicate → the removed person is
3580+
/// back → RED.
3581+
#[tokio::test]
3582+
#[ignore = "requires Postgres"]
3583+
async fn put_members_with_event_stores_the_event_with_its_writes() {
3584+
let pool = setup_pool().await;
3585+
let community_id = make_test_community(&pool).await;
3586+
let community = CommunityId::from_uuid(community_id);
3587+
let (owner, outsider) = (Keys::generate(), Keys::generate());
3588+
let (owner_pk, outsider_pk) = (
3589+
owner.public_key().to_bytes().to_vec(),
3590+
outsider.public_key().to_bytes().to_vec(),
3591+
);
3592+
let person = random_pubkey();
3593+
for pk in [&owner_pk, &outsider_pk, &person] {
3594+
ensure_user(&pool, community, pk)
3595+
.await
3596+
.expect("ensure user");
3597+
}
3598+
let channel = create_test_channel(
3599+
&pool,
3600+
community_id,
3601+
"put-members-event",
3602+
ChannelType::Stream,
3603+
ChannelVisibility::Private,
3604+
None,
3605+
&owner_pk,
3606+
None,
3607+
)
3608+
.await
3609+
.expect("create channel");
3610+
let event = |actor: &Keys| {
3611+
nostr::EventBuilder::new(nostr::Kind::Custom(9000), "")
3612+
.tags([
3613+
nostr::Tag::parse(["h", &channel.id.to_string()]).unwrap(),
3614+
nostr::Tag::parse(["p", &hex::encode(&person)]).unwrap(),
3615+
])
3616+
.sign_with_keys(actor)
3617+
.expect("sign 9000")
3618+
};
3619+
let targets = [MemberPut {
3620+
pubkey: &person,
3621+
requested: None,
3622+
}];
3623+
let stored = |id: nostr::EventId| {
3624+
let pool = pool.clone();
3625+
async move {
3626+
crate::event::get_event_by_id(&pool, community, id.as_bytes())
3627+
.await
3628+
.expect("read event")
3629+
.is_some()
3630+
}
3631+
};
3632+
3633+
// A non-member may not add anyone to a private channel.
3634+
let refused = event(&outsider);
3635+
let write =
3636+
put_members_with_event(&pool, None, community, channel.id, &refused, &targets, None)
3637+
.await
3638+
.expect("refused write");
3639+
assert!(
3640+
matches!(&write, MemberPutEventWrite::Refused(errors)
3641+
if matches!(errors.as_slice(), [DbError::AccessDenied(_)])),
3642+
"expected a refusal"
3643+
);
3644+
assert!(!stored(refused.id).await, "a refused event is not stored");
3645+
assert_eq!(
3646+
get_member_role(&pool, community, channel.id, &person)
3647+
.await
3648+
.expect("read role"),
3649+
None
3650+
);
3651+
3652+
let added = event(&owner);
3653+
let write =
3654+
put_members_with_event(&pool, None, community, channel.id, &added, &targets, None)
3655+
.await
3656+
.expect("applied write");
3657+
let MemberPutEventWrite::Applied { results, .. } = write else {
3658+
panic!("expected the event to apply");
3659+
};
3660+
let put = results[0].as_ref().expect("person added");
3661+
assert_eq!(put.prior_role, None);
3662+
assert!(stored(added.id).await);
3663+
3664+
// A resend finds the event stored and leaves the roster alone.
3665+
remove_member(&pool, community, channel.id, &person, &owner_pk)
3666+
.await
3667+
.expect("remove person");
3668+
let write =
3669+
put_members_with_event(&pool, None, community, channel.id, &added, &targets, None)
3670+
.await
3671+
.expect("duplicate write");
3672+
assert!(matches!(write, MemberPutEventWrite::Duplicate(_)));
3673+
assert_eq!(
3674+
get_member_role(&pool, community, channel.id, &person)
3675+
.await
3676+
.expect("read role"),
3677+
None
3678+
);
3679+
}
3680+
34793681
/// `put_member` enforces admission itself, so a caller that skips the
34803682
/// relay validator still cannot join an announce channel above
34813683
/// `join_role`, and a guest still cannot add anyone. No role tag keeps an

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,7 @@ async fn ingest_event_inner(
30533053
validate_huddle_lifecycle_event(tenant, state, &event, kind_u32).await?;
30543054

30553055
// kind:9000 is checked person by person; the plan carries each result
3056-
// through storage to `apply_put_user`.
3056+
// to `persist_put_user`, which stores the event and applies it.
30573057
let put_user = if kind_u32 == KIND_NIP29_PUT_USER {
30583058
Some(
30593059
crate::handlers::side_effects::validate_put_user(tenant, &event, state)
@@ -3540,6 +3540,7 @@ async fn ingest_event_inner(
35403540
};
35413541

35423542
let workflow_deletion = crate::handlers::side_effects::is_workflow_deletion(&event);
3543+
let mut message = String::new();
35433544
let (stored_event, was_inserted) = if let Some(create) = channel_create.as_ref() {
35443545
let creator = event.pubkey.to_bytes();
35453546
let channel = buzz_db::channel::NewChannel {
@@ -3590,6 +3591,28 @@ async fn ingest_event_inner(
35903591
crate::handlers::side_effects::persist_workflow_deletion(tenant, &event, state)
35913592
.await
35923593
.map_err(|e| IngestError::Internal(format!("error: workflow deletion failed: {e}")))?
3594+
} else if let Some(plan) = put_user {
3595+
// One commit stores the event and applies its membership changes. A
3596+
// failure stores nothing, so the client is told and can resend it.
3597+
// Refusing everyone under the membership lock is the client's error;
3598+
// anything else is ours.
3599+
let thread_params = thread_meta.as_ref().map(|m| m.as_params());
3600+
let (stored, inserted, ok_message) = crate::handlers::side_effects::persist_put_user(
3601+
tenant,
3602+
&event,
3603+
state,
3604+
plan,
3605+
thread_params,
3606+
)
3607+
.await
3608+
.map_err(|e| match e.downcast_ref::<buzz_db::DbError>() {
3609+
Some(buzz_db::DbError::AccessDenied(reason)) => {
3610+
IngestError::Rejected(format!("invalid: {reason}"))
3611+
}
3612+
_ => IngestError::Internal(format!("error: membership write failed: {e}")),
3613+
})?;
3614+
message = ok_message;
3615+
(stored, inserted)
35933616
} else if kind_u32 == KIND_NIP29_EDIT_METADATA {
35943617
// One commit stores the event and applies its channel changes. A
35953618
// failure stores nothing, so the client is told and can resend it.
@@ -3694,14 +3717,12 @@ async fn ingest_event_inner(
36943717
});
36953718
}
36963719

3697-
// Workflow deletion and kind:9002 applied their changes in the insert's
3698-
// own transaction above.
3699-
let persisted_atomically = workflow_deletion || kind_u32 == KIND_NIP29_EDIT_METADATA;
3700-
let mut message = String::new();
3701-
if let Some(plan) = put_user {
3702-
message = crate::handlers::side_effects::apply_put_user(tenant, &event, state, plan).await;
3703-
} else if !persisted_atomically && crate::handlers::side_effects::is_side_effect_kind(kind_u32)
3704-
{
3720+
// Workflow deletion, kind:9000 and kind:9002 applied their changes in the
3721+
// insert's own transaction above.
3722+
let persisted_atomically = workflow_deletion
3723+
|| kind_u32 == KIND_NIP29_PUT_USER
3724+
|| kind_u32 == KIND_NIP29_EDIT_METADATA;
3725+
if !persisted_atomically && crate::handlers::side_effects::is_side_effect_kind(kind_u32) {
37053726
if let Err(e) =
37063727
crate::handlers::side_effects::handle_side_effects(tenant, kind_u32, &event, state)
37073728
.await

0 commit comments

Comments
 (0)