OXYGENHUB

Coverage / crates/hub/src/db/tlog.rs 93.5%

87 of 93 lines run, 18 of 36 functions · run #474 on main · run missed

1
//! The transparency log's index (migration 0076): which entry logged which
2
//! object. The log itself is files (`crate::tlog`).
3
4
use super::Pool;
5
use sqlx::FromRow;
6
7
#[derive(Clone, Debug, FromRow)]
8
pub struct Logged {
9
    pub idx: i64,
10
    pub object: String,
11
    pub kind: String,
12
    pub repo_id: Option<i64>,
13
    pub received_ms: i64,
14
}
15
16
/// The entry that logged `object` as `kind`, if one did.
17385×
pub async fn entry_of(pool: &Pool, object: &str, kind: &str) -> sqlx::Result<Option<i64>> {
18385×
    sqlx::query_scalar("SELECT idx FROM tlog_entries WHERE object = $1 AND kind = $2").bind(object).bind(kind).fetch_optional(pool).await
19385×
}
20
21195×
pub async fn record(pool: &Pool, idx: i64, object: &str, kind: &str, repo_id: Option<i64>, received_ms: i64) -> sqlx::Result<()> {
22195×
    sqlx::query("INSERT INTO tlog_entries (idx, object, kind, repo_id, received_ms) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING")
23195×
        .bind(idx)
24195×
        .bind(object)
25195×
        .bind(kind)
26195×
        .bind(repo_id)
27195×
        .bind(received_ms)
28195×
        .execute(pool)
29195×
        .await?;
30195×
    Ok(())
31195×
}
32
33
/// The newest entries, newest first; of one repository when given.
341×
pub async fn recent(pool: &Pool, repo_id: Option<i64>, limit: i64) -> sqlx::Result<Vec<Logged>> {
351×
    match repo_id {
360×
        Some(r) => {
370×
            sqlx::query_as("SELECT * FROM tlog_entries WHERE repo_id = $1 ORDER BY idx DESC LIMIT $2")
380×
                .bind(r)
390×
                .bind(limit)
400×
                .fetch_all(pool)
410×
                .await
42
        }
431×
        None => sqlx::query_as("SELECT * FROM tlog_entries ORDER BY idx DESC LIMIT $1").bind(limit).fetch_all(pool).await,
44
    }
451×
}
46
47
/// The last checkpoint this hub cosigned for log `origin`: its size and
48
/// root (hex).
494×
pub async fn witnessed(pool: &Pool, origin: &str) -> sqlx::Result<Option<(i64, String)>> {
504×
    sqlx::query_as("SELECT size, root FROM tlog_witnessed WHERE origin = $1").bind(origin).fetch_optional(pool).await
514×
}
52
53
/// Move what this hub cosigned for `origin` from `old` entries (none when
54
/// zero) to `size` with `root`; false when another request moved it first.
552×
pub async fn witness_advance(pool: &Pool, origin: &str, old: i64, size: i64, root: &str) -> sqlx::Result<bool> {
562×
    let now = super::now_ms();
572×
    let r = if old == 0 {
581×
        sqlx::query(
591×
            "INSERT INTO tlog_witnessed (origin, size, root, updated_ms) VALUES ($1, $2, $3, $4) \
601×
             ON CONFLICT (origin) DO UPDATE SET size = $2, root = $3, updated_ms = $4 WHERE tlog_witnessed.size = 0",
611×
        )
621×
        .bind(origin)
631×
        .bind(size)
641×
        .bind(root)
651×
        .bind(now)
661×
        .execute(pool)
671×
        .await?
68
    } else {
691×
        sqlx::query("UPDATE tlog_witnessed SET size = $1, root = $2, updated_ms = $3 WHERE origin = $4 AND size = $5")
701×
            .bind(size)
711×
            .bind(root)
721×
            .bind(now)
731×
            .bind(origin)
741×
            .bind(old)
751×
            .execute(pool)
761×
            .await?
77
    };
782×
    Ok(r.rows_affected() == 1)
792×
}
80
81
/// How far witness `name` has cosigned this hub's log.
822×
pub async fn cosigned_size(pool: &Pool, name: &str) -> sqlx::Result<i64> {
832×
    Ok(sqlx::query_scalar("SELECT size FROM tlog_cosigned WHERE witness = $1").bind(name).fetch_optional(pool).await?.unwrap_or(0))
842×
}
85
862×
pub async fn set_cosigned(pool: &Pool, name: &str, size: i64) -> sqlx::Result<()> {
872×
    sqlx::query(
882×
        "INSERT INTO tlog_cosigned (witness, size, updated_ms) VALUES ($1, $2, $3) \
892×
         ON CONFLICT (witness) DO UPDATE SET size = $2, updated_ms = $3",
902×
    )
912×
    .bind(name)
922×
    .bind(size)
932×
    .bind(super::now_ms())
942×
    .execute(pool)
952×
    .await?;
962×
    Ok(())
972×
}
98
99
/// Keep the cosigned checkpoint of `size` entries, with every cosignature
100
/// it has gathered so far (#240).
1012×
pub async fn keep_cosigned_note(pool: &Pool, size: i64, note: &str) -> sqlx::Result<()> {
1022×
    sqlx::query(
1032×
        "INSERT INTO tlog_cosigned_notes (size, note, updated_ms) VALUES ($1, $2, $3) \
1042×
         ON CONFLICT (size) DO UPDATE SET note = $2, updated_ms = $3",
1052×
    )
1062×
    .bind(size)
1072×
    .bind(note)
1082×
    .bind(super::now_ms())
1092×
    .execute(pool)
1102×
    .await?;
1112×
    Ok(())
1122×
}
113
114
/// The first `n` cosigned checkpoints that hold entry `index`: (size, note).
11525×
pub async fn cosigned_notes_after(pool: &Pool, index: i64, n: i64) -> sqlx::Result<Vec<(i64, String)>> {
11625×
    sqlx::query_as("SELECT size, note FROM tlog_cosigned_notes WHERE size > $1 ORDER BY size LIMIT $2")
11725×
        .bind(index)
11825×
        .bind(n)
11925×
        .fetch_all(pool)
12025×
        .await
12125×
}