use crate::models::dht::LimboWarrantRow;
use holo_hash::{AgentPubKey, DhtOpHash};
use holochain_timestamp::Timestamp;
use sqlx::{Executor, Sqlite, SqliteConnection};
pub struct InsertLimboWarrant<'a> {
pub hash: &'a DhtOpHash,
pub author: &'a AgentPubKey,
pub timestamp: Timestamp,
pub warrantee: &'a AgentPubKey,
pub proof: &'a [u8],
pub signature: &'a [u8],
pub reason: Option<&'a str>,
pub storage_center_loc: u32,
pub when_received: Timestamp,
pub serialized_size: u32,
}
pub(crate) async fn insert_limbo_warrant<'a>(
conn: &mut SqliteConnection,
w: InsertLimboWarrant<'a>,
) -> sqlx::Result<()> {
sqlx::query(
"INSERT INTO Warrant (hash, author, timestamp, warrantee, proof, signature, reason)
VALUES (?, ?, ?, ?, ?, ?, ?)",
)
.bind(w.hash.get_raw_36())
.bind(w.author.get_raw_36())
.bind(w.timestamp.as_micros())
.bind(w.warrantee.get_raw_36())
.bind(w.proof)
.bind(w.signature)
.bind(w.reason)
.execute(&mut *conn)
.await?;
sqlx::query(
"INSERT INTO LimboWarrantOp
(hash, storage_center_loc, when_received, serialized_size)
VALUES (?, ?, ?, ?)",
)
.bind(w.hash.get_raw_36())
.bind(w.storage_center_loc as i64)
.bind(w.when_received.as_micros())
.bind(w.serialized_size as i64)
.execute(&mut *conn)
.await?;
Ok(())
}
pub(crate) async fn get_limbo_warrant<'e, E>(
executor: E,
hash: DhtOpHash,
) -> sqlx::Result<Option<LimboWarrantRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as(
"SELECT w.hash, w.author, w.timestamp, w.warrantee, w.proof, w.signature, w.reason,
op.storage_center_loc, op.sys_validation_status, op.abandoned_at,
op.when_received, op.sys_validation_attempts, op.last_validation_attempt,
op.serialized_size
FROM Warrant w
JOIN LimboWarrantOp op ON op.hash = w.hash
WHERE w.hash = ?",
)
.bind(hash.get_raw_36())
.fetch_optional(executor)
.await
}
pub(crate) async fn limbo_warrants_pending_sys_validation<'e, E>(
executor: E,
limit: u32,
) -> sqlx::Result<Vec<LimboWarrantRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as(
"SELECT w.hash, w.author, w.timestamp, w.warrantee, w.proof, w.signature, w.reason,
op.storage_center_loc, op.sys_validation_status, op.abandoned_at,
op.when_received, op.sys_validation_attempts, op.last_validation_attempt,
op.serialized_size
FROM Warrant w
JOIN LimboWarrantOp op ON op.hash = w.hash
WHERE op.sys_validation_status IS NULL
ORDER BY op.sys_validation_attempts, op.when_received
LIMIT ?",
)
.bind(limit as i64)
.fetch_all(executor)
.await
}
pub(crate) async fn limbo_warrants_ready_for_integration<'e, E>(
executor: E,
limit: u32,
) -> sqlx::Result<Vec<LimboWarrantRow>>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query_as(
"SELECT w.hash, w.author, w.timestamp, w.warrantee, w.proof, w.signature, w.reason,
op.storage_center_loc, op.sys_validation_status, op.abandoned_at,
op.when_received, op.sys_validation_attempts, op.last_validation_attempt,
op.serialized_size
FROM Warrant w
JOIN LimboWarrantOp op ON op.hash = w.hash
WHERE op.sys_validation_status IN (1, 2)
ORDER BY op.when_received
LIMIT ?",
)
.bind(limit as i64)
.fetch_all(executor)
.await
}
pub(crate) async fn set_sys_validation_status<'e, E>(
executor: E,
hash: &DhtOpHash,
status: Option<i64>,
) -> sqlx::Result<u64>
where
E: Executor<'e, Database = Sqlite>,
{
let result = sqlx::query(
"UPDATE LimboWarrantOp SET sys_validation_status = ?
WHERE hash = ? AND sys_validation_status IS NULL",
)
.bind(status)
.bind(hash.get_raw_36())
.execute(executor)
.await?;
Ok(result.rows_affected())
}
pub(crate) async fn delete_limbo_warrant(
conn: &mut SqliteConnection,
hash: DhtOpHash,
) -> sqlx::Result<()> {
sqlx::query("DELETE FROM LimboWarrantOp WHERE hash = ?")
.bind(hash.get_raw_36())
.execute(&mut *conn)
.await?;
sqlx::query("DELETE FROM Warrant WHERE hash = ?")
.bind(hash.get_raw_36())
.execute(&mut *conn)
.await?;
Ok(())
}
pub(crate) async fn promote_to_warrant(
conn: &mut SqliteConnection,
hash: &DhtOpHash,
when_integrated: Timestamp,
) -> sqlx::Result<bool> {
let result = sqlx::query(
"INSERT INTO WarrantOp (hash, storage_center_loc, when_received,
when_integrated, serialized_size)
SELECT hash, storage_center_loc, when_received, ?, serialized_size
FROM LimboWarrantOp WHERE hash = ?",
)
.bind(when_integrated.as_micros())
.bind(hash.get_raw_36())
.execute(&mut *conn)
.await?;
if result.rows_affected() == 0 {
return Ok(false);
}
sqlx::query("DELETE FROM LimboWarrantOp WHERE hash = ?")
.bind(hash.get_raw_36())
.execute(&mut *conn)
.await?;
Ok(true)
}