Skip to content

Commit 1e86b59

Browse files
TheSentinel454Elrondclaude
committed
buzz-db: check the replacement floor after the replay exit
Replaying a stored but soft-deleted parameterized or addressable id older than the floor returned CreatedAtFloorRejected, because the guard ran before the INSERT ... ON CONFLICT DO NOTHING and the early exits only see live heads. The trigger never fired on a conflict, so the old result was a duplicate. Both writers now check only when the INSERT stored a row; the savepoint or the caller's rollback restores the superseded head. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 9512e8c commit 1e86b59

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎crates/buzz-db/src/runtime/replica_fence.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ pub const CREATED_AT_FLOOR_SECS: i64 = 960;
8383

8484
/// Enforce the channel-event floor on the admitted INSERT connection.
8585
///
86-
/// One SELECT keeps the five existing INSERT shapes intact. Ordinary/thread
87-
/// writers check after INSERT only when it inserted a row, before follow-ups;
88-
/// replacements check after replay exits, before superseding the old head.
86+
/// One SELECT keeps the five existing INSERT shapes intact. Every writer
87+
/// checks after its INSERT, only when it inserted a row, before follow-ups, so
88+
/// replaying a stored id (even a soft-deleted one) stays a duplicate.
89+
/// Replacements roll back the supersede with it (savepoint or caller).
8990
/// The caller must propagate failure and roll back the write transaction. Its
9091
/// database clock is sampled after BEGIN: transactions open at the probe scan
9192
/// are bounded by xact_start; transactions starting later sample after S. No

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,51 @@ async fn addressable_event_floor_rejects_old_and_accepts_fresh() {
268268
assert_eq!(count(&db, community, &fresh).await, 1);
269269
}
270270

271+
/// Seed an aged, stored and then soft-deleted event, as NIP-09 leaves it.
272+
async fn soft_deleted_aged(db: &Db, community: CommunityId, channel: Uuid, keys: &Keys, kind: u16) -> Event {
273+
let now = database_clock(db).await;
274+
let aged = signed(
275+
keys,
276+
kind,
277+
channel,
278+
now - chrono::Duration::seconds(CREATED_AT_FLOOR_SECS + 60),
279+
);
280+
crate::test_support::insert_historical_event(db.pool(), community, &aged, channel).await;
281+
sqlx::query("UPDATE events SET deleted_at = now() WHERE community_id = $1 AND id = $2")
282+
.bind(community.as_uuid())
283+
.bind(aged.id.as_bytes().as_slice())
284+
.execute(db.pool())
285+
.await
286+
.expect("soft-delete");
287+
aged
288+
}
289+
290+
#[tokio::test]
291+
#[ignore = "requires Postgres"]
292+
async fn parameterized_replay_of_soft_deleted_aged_event_is_a_duplicate() {
293+
let (db, community, channel, keys) = fixture().await;
294+
let aged = soft_deleted_aged(&db, community, channel, &keys, 30078).await;
295+
let (_, stored) = db
296+
.replace_parameterized_event(community, &aged, &channel.to_string(), Some(channel))
297+
.await
298+
.expect("replay of a stored id is a duplicate, not a floor rejection");
299+
assert!(!stored);
300+
assert_eq!(count(&db, community, &aged).await, 1);
301+
}
302+
303+
#[tokio::test]
304+
#[ignore = "requires Postgres"]
305+
async fn addressable_replay_of_soft_deleted_aged_event_is_a_duplicate() {
306+
let (db, community, channel, keys) = fixture().await;
307+
let aged = soft_deleted_aged(&db, community, channel, &keys, 39000).await;
308+
let (_, stored) = db
309+
.replace_addressable_event(community, &aged, Some(channel))
310+
.await
311+
.expect("replay of a stored id is a duplicate, not a floor rejection");
312+
assert!(!stored);
313+
assert_eq!(count(&db, community, &aged).await, 1);
314+
}
315+
271316
#[tokio::test]
272317
#[ignore = "requires Postgres"]
273318
async fn member_snapshot_event_floor_rejects_old_and_accepts_fresh() {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ async fn replace_parameterized_event_in_transaction_impl(
251251
));
252252
}
253253

254-
crate::replica_fence::guard_event_created_at_floor(tx, created_at, channel_id).await?;
255-
256254
// Everything from the supersede through the mention index runs under one
257255
// savepoint on the admitted transaction. A duplicate insert or any failure
258256
// (including mention indexing) rolls the whole replacement back and leaves
@@ -331,6 +329,10 @@ async fn replace_parameterized_event_in_transaction_impl(
331329
if insert_result.rows_affected() == 0 {
332330
return Ok(false);
333331
}
332+
// After the replay exit: a stored id (even soft-deleted) is a
333+
// duplicate, not a floor rejection. Failure rolls back to the
334+
// savepoint, restoring the previous live head.
335+
crate::replica_fence::guard_event_created_at_floor(tx, created_at, channel_id).await?;
334336

335337
if is_nip_rs {
336338
sqlx::query(
@@ -480,8 +482,6 @@ pub(crate) async fn replace_addressable_event_under_lock(
480482
crate::channel_members::guard_channel_roster_event(tx, kind_i32, channel_id, &tags_json)
481483
.await?;
482484

483-
crate::replica_fence::guard_event_created_at_floor(tx, created_at, channel_id).await?;
484-
485485
// Soft-delete the old event (if any). IS NOT DISTINCT FROM for NULL safety.
486486
sqlx::query(superseded_with_mentions_sql!(
487487
"UPDATE events SET deleted_at = NOW() \
@@ -528,6 +528,9 @@ pub(crate) async fn replace_addressable_event_under_lock(
528528
false,
529529
));
530530
}
531+
// After the replay exit, as in the parameterized path; the caller's
532+
// rollback restores the previous head on failure.
533+
crate::replica_fence::guard_event_created_at_floor(tx, created_at, channel_id).await?;
531534

532535
crate::store::event_follow_up::after_admitted_insert(
533536
tx,

0 commit comments

Comments
 (0)