Skip to content

Commit c507a4d

Browse files
fix(db): allow naturally expired push lease replacement (#7653)
## Summary Allow a naturally expired push lease to be replaced by a newer lease for the same author and installation. ## Failure mode Expiry cleanup marked the old lease inactive but left active-only fields populated. The push_leases_check constraint requires those fields to be NULL when a lease is inactive. When the client later registered a replacement lease, the cleanup ran before the replacement and violated the constraint with SQLSTATE 23514. The transaction rolled back, so the replacement failed instead of taking over the expired lease. ## Fix Clear all active-only fields as part of natural-expiry cleanup, preserving the existing constraint and replacement flow. No schema change is required. ## Testing - Passed the focused Postgres regression test: cargo test -p buzz-db replacing_naturally_expired_lease_replaces_old_state -- --ignored - Passed scripts/test-postgres-test-discovery.sh - Ran just ci: formatting, lint, security, file-size, desktop, web, mobile, and 12 unit-test lanes passed. The overall run was red only because existing timing-sensitive buzz-acp tests failed. - The pre-push test-unit hook reproduced the same unrelated buzz-acp failures, so the already-run verification was documented and the branch was published with that hook bypassed. ## Related issue No matching open issue or pull request was found. ## Scope One file changed: crates/buzz-db/src/store/push.rs. The test uses the production accept_lease_event path and identifies the replacement by its cryptographic source event ID and generation. Signed-off-by: Jarrod Sibbison <[email protected]>
1 parent b8aa023 commit c507a4d

1 file changed

Lines changed: 78 additions & 1 deletion

File tree

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ pub async fn accept_lease_event(
328328
// uniqueness forever. The author lock makes this cleanup atomic with the
329329
// subsequent author-wide checks and replacement.
330330
sqlx::query(
331-
"UPDATE push_leases SET active=false, endpoint_enabled=false, updated_at=now() \
331+
"UPDATE push_leases SET active=false, endpoint_enabled=false, app_profile=NULL, \
332+
endpoint_hash=NULL, endpoint_grant=NULL, max_class=NULL, subscriptions=NULL, updated_at=now() \
332333
WHERE community_id=$1 AND author=$2 AND active \
333334
AND expires_at <= EXTRACT(EPOCH FROM now())::bigint",
334335
)
@@ -1665,6 +1666,82 @@ mod postgres_tests {
16651666
assert_eq!((event_count, lease_count), (0, 0));
16661667
}
16671668

1669+
#[tokio::test]
1670+
#[ignore = "requires Postgres"]
1671+
async fn replacing_naturally_expired_lease_replaces_old_state() {
1672+
let pool = setup_pool().await;
1673+
let community = make_community(&pool).await;
1674+
let keys = nostr::Keys::generate();
1675+
let endpoint = [42; 32];
1676+
let subscriptions = serde_json::json!([]);
1677+
1678+
let expired_event = lease_event(&keys, "install", 100);
1679+
assert_eq!(
1680+
accept_lease_event(
1681+
&pool,
1682+
community,
1683+
&expired_event,
1684+
"install",
1685+
LeaseVersion {
1686+
source_event_id: expired_event.id.as_bytes(),
1687+
source_created_at: 100,
1688+
generation: 1,
1689+
expires_at: 1,
1690+
},
1691+
Some(ActiveLease {
1692+
app_profile: "ios-production",
1693+
endpoint_hash: &endpoint,
1694+
endpoint_grant: "opaque-grant",
1695+
max_class: "default",
1696+
subscriptions: &subscriptions,
1697+
}),
1698+
16,
1699+
)
1700+
.await
1701+
.expect("insert expired lease"),
1702+
AcceptLeaseOutcome::Accepted
1703+
);
1704+
1705+
let replacement_event = lease_event(&keys, "install", 200);
1706+
let outcome = accept_lease_event(
1707+
&pool,
1708+
community,
1709+
&replacement_event,
1710+
"install",
1711+
LeaseVersion {
1712+
source_event_id: replacement_event.id.as_bytes(),
1713+
source_created_at: 200,
1714+
generation: 2,
1715+
expires_at: i64::MAX / 2,
1716+
},
1717+
Some(ActiveLease {
1718+
app_profile: "ios-production",
1719+
endpoint_hash: &[43; 32],
1720+
endpoint_grant: "replacement-grant",
1721+
max_class: "default",
1722+
subscriptions: &subscriptions,
1723+
}),
1724+
16,
1725+
)
1726+
.await
1727+
.expect("replace expired lease");
1728+
assert_eq!(outcome, AcceptLeaseOutcome::Accepted);
1729+
1730+
let persisted: (bool, Vec<u8>, i64) = sqlx::query_as(
1731+
"SELECT active, source_event_id, generation FROM push_leases \
1732+
WHERE community_id=$1 AND author=$2 AND installation_id=$3",
1733+
)
1734+
.bind(community.as_uuid())
1735+
.bind(expired_event.pubkey.as_bytes())
1736+
.bind("install")
1737+
.fetch_one(&pool)
1738+
.await
1739+
.expect("read original lease");
1740+
assert!(persisted.0, "replacement lease must be active");
1741+
assert_eq!(persisted.1, replacement_event.id.as_bytes());
1742+
assert_eq!(persisted.2, 2);
1743+
}
1744+
16681745
#[tokio::test]
16691746
#[ignore = "requires Postgres"]
16701747
async fn source_event_collision_is_protocol_outcome_without_event_insert() {

0 commit comments

Comments
 (0)