Skip to content

Commit 2e936d4

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwellnpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
authored
relay: skip TTL deadline bump for known-permanent channels (T1a write-amp) (#2125)
Signed-off-by: Tyler Longwell <[email protected]> Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
1 parent d04a4c9 commit 2e936d4

8 files changed

Lines changed: 403 additions & 42 deletions

File tree

‎bench/t1a_evidence.sh‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
# T1a correctness evidence (runnable from this lane tip).
3+
#
4+
# Proves, against a live relay built from this tree:
5+
# 1. permanent channel: kind:9 ingest emits no separate top-level TTL UPDATE
6+
# transaction (the deferred trigger's conditional statement stays inside the
7+
# event transaction and affects zero rows);
8+
# 2. ephemeral channel: the TTL bump is still observed (ttl_deadline strictly advances
9+
# across a message);
10+
# 3. TTL-set-during-ingest race: messages committed after concurrent TTL activation
11+
# extend the deadline beyond the activation update's own deadline.
12+
#
13+
# Usage: bench/t1a_evidence.sh <pg_container> <db_url> <relay_ws_url> <community_uuid>
14+
# Requires: relay running FROM THIS TREE against <db_url>; psql via docker exec;
15+
# BENCH_PRIVATE_KEY env (member secret key hex); wamp_bench built --release.
16+
set -euo pipefail
17+
18+
PG="$1"; DBURL="$2"; RELAY="$3"; COMMUNITY="$4"
19+
PSQL=(docker exec "$PG" psql "$DBURL" -tA)
20+
sql() { "${PSQL[@]}" -c "$1"; }
21+
BIN="./target/release/wamp_bench"
22+
# BENCH_PUB = x-only pubkey hex for BENCH_PRIVATE_KEY (both required).
23+
PUB="${BENCH_PUB:?set BENCH_PUB (x-only pubkey hex matching BENCH_PRIVATE_KEY)}"
24+
25+
mkchan() { # $1 name, $2 ttl_seconds or NULL -> echoes uuid
26+
local id; id=$(python3 -c "import uuid;print(uuid.uuid4())")
27+
sql "insert into channels (id, community_id, name, channel_type, visibility, created_by, ttl_seconds, ttl_deadline)
28+
values ('$id','$COMMUNITY','$1','stream','private',decode('$PUB','hex'),$2,
29+
case when $2::int is null then null else now() + ($2::int || ' seconds')::interval end)" >/dev/null
30+
sql "insert into channel_members (community_id, channel_id, pubkey, role)
31+
values ('$COMMUNITY','$id',decode('$PUB','hex'),'owner')" >/dev/null
32+
echo "$id"
33+
}
34+
35+
FAIL=0
36+
37+
echo "== 1. permanent channel: zero separate top-level TTL UPDATE statements =="
38+
PERM=$(mkchan t1a-perm NULL)
39+
sql "select pg_stat_statements_reset()" >/dev/null
40+
env -u BUZZ_AUTH_TAG BUZZ_RELAY_URL="$RELAY" "$BIN" "$PERM" 20 10 2 /tmp/t1a-perm.lat >/tmp/t1a-perm.json
41+
ACC=$(python3 -c "import json;print(json.load(open('/tmp/t1a-perm.json'))['accepted'])")
42+
TTL_CALLS=$(sql "select coalesce(sum(calls),0) from pg_stat_statements where query ilike '%UPDATE channels SET ttl_deadline%'")
43+
echo "accepted=$ACC ttl_update_calls=$TTL_CALLS"
44+
[[ "$ACC" -gt 0 && "$TTL_CALLS" == "0" ]] || { echo "FAIL: expected >0 accepted and 0 TTL updates"; FAIL=1; }
45+
46+
echo "== 2. ephemeral channel: bump still observed =="
47+
EPH=$(mkchan t1a-eph 3600)
48+
D0=$(sql "select extract(epoch from ttl_deadline) from channels where id='$EPH'")
49+
sleep 2
50+
env -u BUZZ_AUTH_TAG BUZZ_RELAY_URL="$RELAY" "$BIN" "$EPH" 5 3 1 /tmp/t1a-eph.lat >/tmp/t1a-eph.json
51+
D1=$(sql "select extract(epoch from ttl_deadline) from channels where id='$EPH'")
52+
echo "deadline before=$D0 after=$D1"
53+
python3 -c "import sys; sys.exit(0 if float('$D1') > float('$D0') else 1)" \
54+
|| { echo "FAIL: ephemeral ttl_deadline did not advance"; FAIL=1; }
55+
56+
echo "== 3. TTL-set-during-ingest race =="
57+
RACE=$(mkchan t1a-race NULL)
58+
env -u BUZZ_AUTH_TAG BUZZ_RELAY_URL="$RELAY" "$BIN" "$RACE" 50 6 4 /tmp/t1a-race.lat >/tmp/t1a-race.json &
59+
BPID=$!
60+
sleep 2
61+
# update_channel-equivalent: set TTL and reset deadline in one statement, mid-burst.
62+
ACTIVATION_DEADLINE=$(sql "update channels set ttl_seconds=600, ttl_deadline=clock_timestamp() + interval '600 seconds', updated_at=now() where id='$RACE' and deleted_at is null returning extract(epoch from ttl_deadline)")
63+
wait "$BPID"
64+
ROW=$(sql "select ttl_seconds, extract(epoch from ttl_deadline) from channels where id='$RACE'")
65+
echo "activation_deadline=$ACTIVATION_DEADLINE post-race=$ROW"
66+
FINAL_DEADLINE="${ROW#*|}"
67+
python3 -c "import sys; sys.exit(0 if float('$FINAL_DEADLINE') > float('$ACTIVATION_DEADLINE') else 1)" \
68+
|| { echo "FAIL: later message did not extend TTL beyond activation deadline"; FAIL=1; }
69+
# and subsequent messages now bump it (channel is ephemeral now)
70+
D0=$(sql "select extract(epoch from ttl_deadline) from channels where id='$RACE'")
71+
sleep 2
72+
env -u BUZZ_AUTH_TAG BUZZ_RELAY_URL="$RELAY" "$BIN" "$RACE" 5 3 1 /tmp/t1a-race2.lat >/tmp/t1a-race2.json
73+
D1=$(sql "select extract(epoch from ttl_deadline) from channels where id='$RACE'")
74+
python3 -c "import sys; sys.exit(0 if float('$D1') > float('$D0') else 1)" \
75+
|| { echo "FAIL: post-race ephemeral bump not observed"; FAIL=1; }
76+
77+
[[ "$FAIL" == 0 ]] && echo "T1A EVIDENCE: ALL PASS" || { echo "T1A EVIDENCE: FAILURES"; exit 1; }

‎crates/buzz-db/src/channel.rs‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,25 +1312,6 @@ pub async fn get_member_role(
13121312
Ok(row.map(|r| r.try_get("role")).transpose()?)
13131313
}
13141314

1315-
/// Bump the TTL deadline for an ephemeral channel after a new message.
1316-
///
1317-
/// No-op for permanent channels or channels that are already archived/deleted.
1318-
pub async fn bump_ttl_deadline(
1319-
pool: &PgPool,
1320-
community_id: CommunityId,
1321-
channel_id: Uuid,
1322-
) -> Result<()> {
1323-
sqlx::query(
1324-
"UPDATE channels SET ttl_deadline = NOW() + (ttl_seconds || ' seconds')::interval \
1325-
WHERE community_id = $1 AND id = $2 AND ttl_seconds IS NOT NULL AND archived_at IS NULL AND deleted_at IS NULL",
1326-
)
1327-
.bind(community_id.as_uuid())
1328-
.bind(channel_id)
1329-
.execute(pool)
1330-
.await?;
1331-
Ok(())
1332-
}
1333-
13341315
/// Archive ephemeral channels whose TTL deadline has passed.
13351316
///
13361317
/// Returns the `(community_id, host, channel_id)` list that was archived. Idempotent — the

‎crates/buzz-db/src/event.rs‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,157 @@ mod tests {
14541454
id
14551455
}
14561456

1457+
async fn make_test_channel(
1458+
pool: &PgPool,
1459+
community_id: Uuid,
1460+
ttl_seconds: Option<i32>,
1461+
) -> Uuid {
1462+
let id = Uuid::new_v4();
1463+
sqlx::query(
1464+
"INSERT INTO channels \
1465+
(id, community_id, name, created_by, ttl_seconds, ttl_deadline) \
1466+
VALUES ($1, $2, $3, $4, $5, \
1467+
CASE WHEN $5 IS NULL THEN NULL \
1468+
ELSE clock_timestamp() + make_interval(secs => $5) END)",
1469+
)
1470+
.bind(id)
1471+
.bind(community_id)
1472+
.bind(format!("event-ttl-test-{}", id.simple()))
1473+
.bind(vec![7_u8; 32])
1474+
.bind(ttl_seconds)
1475+
.execute(pool)
1476+
.await
1477+
.expect("insert test channel");
1478+
id
1479+
}
1480+
1481+
#[tokio::test]
1482+
#[ignore = "requires Postgres"]
1483+
async fn event_insert_ttl_trigger_handles_permanent_ephemeral_duplicate_and_activation_race() {
1484+
let pool = setup_pool().await;
1485+
let community_uuid = make_test_community(&pool).await;
1486+
let community = CommunityId::from_uuid(community_uuid);
1487+
1488+
let permanent = make_test_channel(&pool, community_uuid, None).await;
1489+
let permanent_event = make_text_event("permanent channel event");
1490+
assert!(
1491+
insert_event(&pool, community, &permanent_event, Some(permanent))
1492+
.await
1493+
.expect("insert permanent event")
1494+
.1
1495+
);
1496+
let permanent_deadline: Option<DateTime<Utc>> = sqlx::query_scalar(
1497+
"SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2",
1498+
)
1499+
.bind(community_uuid)
1500+
.bind(permanent)
1501+
.fetch_one(&pool)
1502+
.await
1503+
.expect("read permanent deadline");
1504+
assert_eq!(permanent_deadline, None);
1505+
1506+
let ephemeral = make_test_channel(&pool, community_uuid, Some(60)).await;
1507+
let initial_deadline: DateTime<Utc> = sqlx::query_scalar(
1508+
"SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2",
1509+
)
1510+
.bind(community_uuid)
1511+
.bind(ephemeral)
1512+
.fetch_one(&pool)
1513+
.await
1514+
.expect("read initial ephemeral deadline");
1515+
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
1516+
let ephemeral_event = make_text_event("ephemeral channel event");
1517+
assert!(
1518+
insert_event(&pool, community, &ephemeral_event, Some(ephemeral))
1519+
.await
1520+
.expect("insert ephemeral event")
1521+
.1
1522+
);
1523+
let bumped_deadline: DateTime<Utc> = sqlx::query_scalar(
1524+
"SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2",
1525+
)
1526+
.bind(community_uuid)
1527+
.bind(ephemeral)
1528+
.fetch_one(&pool)
1529+
.await
1530+
.expect("read bumped ephemeral deadline");
1531+
assert!(bumped_deadline > initial_deadline);
1532+
assert!(
1533+
!insert_event(&pool, community, &ephemeral_event, Some(ephemeral))
1534+
.await
1535+
.expect("insert duplicate event")
1536+
.1
1537+
);
1538+
let duplicate_deadline: DateTime<Utc> = sqlx::query_scalar(
1539+
"SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2",
1540+
)
1541+
.bind(community_uuid)
1542+
.bind(ephemeral)
1543+
.fetch_one(&pool)
1544+
.await
1545+
.expect("read deadline after duplicate");
1546+
assert_eq!(duplicate_deadline, bumped_deadline);
1547+
1548+
// Reproduce the blocked stale-prefetch ordering: ingest has already
1549+
// observed a permanent channel, then TTL activation locks/updates the
1550+
// row before the event INSERT reaches its trigger. The trigger must
1551+
// wait and refresh from the later event after activation commits.
1552+
let racing = make_test_channel(&pool, community_uuid, None).await;
1553+
let stale_ttl: Option<i32> = sqlx::query_scalar(
1554+
"SELECT ttl_seconds FROM channels WHERE community_id = $1 AND id = $2",
1555+
)
1556+
.bind(community_uuid)
1557+
.bind(racing)
1558+
.fetch_one(&pool)
1559+
.await
1560+
.expect("prefetch permanent channel");
1561+
assert_eq!(stale_ttl, None);
1562+
1563+
let mut activation = pool.begin().await.expect("begin TTL activation");
1564+
let activation_deadline: DateTime<Utc> = sqlx::query_scalar(
1565+
"UPDATE channels \
1566+
SET ttl_seconds = 60, ttl_deadline = clock_timestamp() + interval '60 seconds' \
1567+
WHERE community_id = $1 AND id = $2 RETURNING ttl_deadline",
1568+
)
1569+
.bind(community_uuid)
1570+
.bind(racing)
1571+
.fetch_one(&mut *activation)
1572+
.await
1573+
.expect("activate TTL while holding channel row lock");
1574+
1575+
let race_pool = pool.clone();
1576+
let racing_event = make_text_event("event after stale permanent prefetch");
1577+
let insert = tokio::spawn(async move {
1578+
insert_event(&race_pool, community, &racing_event, Some(racing)).await
1579+
});
1580+
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
1581+
assert!(
1582+
!insert.is_finished(),
1583+
"event trigger must wait on TTL activation"
1584+
);
1585+
activation.commit().await.expect("commit TTL activation");
1586+
assert!(
1587+
insert
1588+
.await
1589+
.expect("join racing insert")
1590+
.expect("racing insert")
1591+
.1
1592+
);
1593+
1594+
let final_deadline: DateTime<Utc> = sqlx::query_scalar(
1595+
"SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2",
1596+
)
1597+
.bind(community_uuid)
1598+
.bind(racing)
1599+
.fetch_one(&pool)
1600+
.await
1601+
.expect("read deadline after racing event");
1602+
assert!(
1603+
final_deadline > activation_deadline + chrono::Duration::milliseconds(50),
1604+
"later event must extend TTL beyond activation deadline: activation={activation_deadline}, final={final_deadline}"
1605+
);
1606+
}
1607+
14571608
#[tokio::test]
14581609
#[ignore = "requires Postgres"]
14591610
async fn get_event_by_id_is_scoped_when_event_id_collides_across_communities() {

‎crates/buzz-db/src/lib.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,15 +1688,6 @@ impl Db {
16881688
channel::get_member_role(&self.pool, community_id, channel_id, pubkey).await
16891689
}
16901690

1691-
/// Bump the TTL deadline for an ephemeral channel after a new message.
1692-
pub async fn bump_ttl_deadline(
1693-
&self,
1694-
community_id: CommunityId,
1695-
channel_id: Uuid,
1696-
) -> Result<()> {
1697-
channel::bump_ttl_deadline(&self.pool, community_id, channel_id).await
1698-
}
1699-
17001691
/// Archive ephemeral channels whose TTL deadline has passed.
17011692
pub async fn reap_expired_ephemeral_channels(
17021693
&self,

‎crates/buzz-db/src/migration.rs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,12 @@ mod tests {
532532
let normalized = normalize_sql(statement);
533533
let updates_channels =
534534
identifier_after_keyword(statement, "update").as_deref() == Some("channels");
535+
let update_assignments = normalized
536+
.split_once(" set ")
537+
.map(|(_, tail)| tail.split_once(" where ").map_or(tail, |(set, _)| set));
535538
let mutates_with_update = updates_channels
536-
&& normalized.contains(" set ")
537-
&& normalized.contains("community_id");
539+
&& update_assignments
540+
.is_some_and(|assignments| assignments.contains("community_id"));
538541
let alters_channels = identifier_after_keyword(statement, "alter table").as_deref()
539542
== Some("channels");
540543
let drops_channels = identifier_after_keyword(statement, "drop table").as_deref()
@@ -557,7 +560,7 @@ mod tests {
557560
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
558561
migrations.sort_by_key(|migration| migration.version);
559562

560-
assert_eq!(migrations.len(), 21);
563+
assert_eq!(migrations.len(), 22);
561564
assert_eq!(migrations[0].version, 1);
562565
assert_eq!(&*migrations[0].description, "initial schema");
563566
assert!(migrations[0]
@@ -839,6 +842,16 @@ mod tests {
839842
.sql
840843
.as_str()
841844
.contains("join_policy_acceptances"));
845+
846+
// Channel TTL refresh belongs to the event insertion transaction so a
847+
// concurrent permanent -> ephemeral transition cannot be missed.
848+
assert_eq!(migrations[21].version, 22);
849+
let ttl_refresh = migrations[21].sql.as_str();
850+
assert!(ttl_refresh.contains("CREATE CONSTRAINT TRIGGER events_refresh_channel_ttl"));
851+
assert!(ttl_refresh.contains("AFTER INSERT ON events"));
852+
assert!(ttl_refresh.contains("DEFERRABLE INITIALLY DEFERRED"));
853+
assert!(ttl_refresh.contains("clock_timestamp()"));
854+
assert!(ttl_refresh.contains("NEW.kind <> 9007"));
842855
}
843856

844857
#[test]
@@ -1081,7 +1094,7 @@ mod tests {
10811094
run_migrations(&pool)
10821095
.await
10831096
.expect("retry succeeds after operator repair");
1084-
assert_eq!(applied_versions(&pool).await.last().copied(), Some(21));
1097+
assert_eq!(applied_versions(&pool).await.last().copied(), Some(22));
10851098
}
10861099

10871100
#[tokio::test]

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,16 +2351,6 @@ async fn ingest_event_inner(
23512351
});
23522352
}
23532353

2354-
// Any successfully stored channel-scoped event keeps the channel alive.
2355-
// Skip kind:9007 (create) — the deadline was just set during creation.
2356-
if let Some(ch_id) = channel_id {
2357-
if kind_u32 != KIND_NIP29_CREATE_GROUP {
2358-
if let Err(e) = state.db.bump_ttl_deadline(tenant.community(), ch_id).await {
2359-
warn!(channel = %ch_id, "TTL deadline bump failed: {e}");
2360-
}
2361-
}
2362-
}
2363-
23642354
if crate::handlers::side_effects::is_side_effect_kind(kind_u32) {
23652355
if let Err(e) =
23662356
crate::handlers::side_effects::handle_side_effects(tenant, kind_u32, &event, state)

0 commit comments

Comments
 (0)