Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,7 @@ BUZZ_NIP_FI_CHANGES=false
BUZZ_MEMBERSHIP_SNAPSHOT_ENABLED=false
# Independent listed-open public membership lookup; also deploy disabled.
BUZZ_MEMBERSHIP_PUBLIC_ENABLED=false

# Enable community NIP-AR only after the separate operator contract.
# See docs/community-artifact-rollout.md.
BUZZ_COMMUNITY_ARTIFACTS_ENABLED=false
22 changes: 17 additions & 5 deletions crates/buzz-audit/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,24 @@ impl AuditService {
pub async fn log(&self, entry: NewAuditEntry) -> Result<AuditEntry, AuditError> {
let mut tx = self.pool.begin().await?;
buzz_db::deletion::guard_community_write(&mut tx, entry.community_id).await?;
let result = Self::log_in_transaction(&mut tx, entry).await?;
tx.commit().await?;
Ok(result)
}

/// Append on a caller's transaction so an administrative mutation and its
/// hash-chain audit entry commit together. Admission is rechecked here.
/// The caller owns commit/rollback, and must not hold an audit lock before
/// acquiring the mutation's policy/identity locks.
pub async fn log_in_transaction(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
entry: NewAuditEntry,
) -> Result<AuditEntry, AuditError> {
buzz_db::deletion::guard_community_write(tx, entry.community_id).await?;
let lock_key = format!("{AUDIT_LOCK_NAMESPACE}{}", entry.community_id);
sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
.bind(&lock_key)
.execute(&mut *tx)
.execute(&mut **tx)
.await?;

// The stored row keys on the raw UUID; the typed `CommunityId` on the
Expand All @@ -71,7 +85,7 @@ impl AuditService {
ORDER BY seq DESC LIMIT 1",
)
.bind(community_id)
.fetch_optional(&mut *tx)
.fetch_optional(&mut **tx)
.await?;

let (prev_seq, prev_hash): (i64, Option<Vec<u8>>) = match head {
Expand Down Expand Up @@ -119,11 +133,9 @@ impl AuditService {
.bind(&audit_entry.detail)
.bind(audit_entry.created_at)
.bind(audit_entry.hash_version)
.execute(&mut *tx)
.execute(&mut **tx)
.await?;

tx.commit().await?;

Ok(audit_entry)
}

Expand Down
Loading
Loading