holochain_data 0.7.0-dev.17

Database abstraction layer for Holochain using sqlx
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Free-standing operations against the `LimboChainOp` table.

use crate::models::dht::LimboChainOpRow;
use holo_hash::{ActionHash, AnyLinkableHash, DhtOpHash};
use holochain_integrity_types::dht_v2::OpValidity;
use holochain_timestamp::Timestamp;
use sqlx::{Executor, Sqlite, SqliteConnection};

/// A row joining `LimboChainOp` with its associated `Action` and optional
/// `Entry` blob. Enables reconstructing a `DhtOpHashed` without an N+1
/// round-trip per row.
#[derive(Debug, sqlx::FromRow)]
pub struct LimboChainOpJoinedRow {
    // --- LimboChainOp columns ---
    pub hash: Vec<u8>,
    pub op_type: i64,
    pub action_hash: Vec<u8>,
    pub basis_hash: Vec<u8>,
    pub storage_center_loc: i64,
    pub sys_validation_status: Option<i64>,
    pub app_validation_status: Option<i64>,
    pub abandoned_at: Option<i64>,
    pub require_receipt: i64,
    pub when_received: i64,
    pub sys_validation_attempts: i64,
    pub app_validation_attempts: i64,
    pub last_validation_attempt: Option<i64>,
    pub serialized_size: i64,
    // --- Action columns (aliased to avoid collision) ---
    pub action_author: Vec<u8>,
    pub action_seq: i64,
    pub action_prev_hash: Option<Vec<u8>>,
    pub action_timestamp: i64,
    pub action_type: i64,
    pub action_data: Vec<u8>,
    pub action_signature: Vec<u8>,
    pub action_entry_hash: Option<Vec<u8>>,
    pub action_private_entry: Option<i64>,
    pub action_record_validity: Option<i64>,
    // --- Entry columns (LEFT JOIN — may be NULL) ---
    pub entry_blob: Option<Vec<u8>>,
}

/// Parameters for inserting a row into `LimboChainOp`.
pub struct InsertLimboChainOp<'a> {
    /// DHT op hash (primary key).
    pub op_hash: &'a DhtOpHash,
    /// Hash of the action carried by this op.
    pub action_hash: &'a ActionHash,
    /// `ChainOpType` discriminant; see
    /// [`From<ChainOpType> for i64`](holochain_zome_types::dht_v2).
    pub op_type: i64,
    /// DHT basis hash (`OpBasis`) where the op is stored.
    /// `AnyLinkableHash`, not `AnyDhtHash`: link-op bases may be `External`
    /// hashes, which `AnyDhtHash` cannot hold.
    pub basis_hash: &'a AnyLinkableHash,
    /// Numeric storage center derived from `basis_hash`.
    pub storage_center_loc: u32,
    /// Whether the receiving authority should issue a validation receipt.
    pub require_receipt: bool,
    /// Microsecond timestamp at which the op was received.
    pub when_received: Timestamp,
    /// Wire-size of the op in bytes.
    pub serialized_size: u32,
}

pub(crate) async fn insert_limbo_chain_op<'a, 'e, E>(
    executor: E,
    op: InsertLimboChainOp<'a>,
) -> sqlx::Result<()>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query(
        "INSERT INTO LimboChainOp
            (hash, op_type, action_hash, basis_hash, storage_center_loc,
             require_receipt, when_received, serialized_size)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(op.op_hash.get_raw_36())
    .bind(op.op_type)
    .bind(op.action_hash.get_raw_36())
    .bind(op.basis_hash.get_raw_36())
    .bind(op.storage_center_loc as i64)
    .bind(op.require_receipt as i64)
    .bind(op.when_received.as_micros())
    .bind(op.serialized_size as i64)
    .execute(executor)
    .await?;
    Ok(())
}

pub(crate) async fn get_limbo_chain_op<'e, E>(
    executor: E,
    hash: DhtOpHash,
) -> sqlx::Result<Option<LimboChainOpRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT hash, op_type, action_hash, basis_hash, storage_center_loc,
                sys_validation_status, app_validation_status, abandoned_at,
                require_receipt, when_received, sys_validation_attempts,
                app_validation_attempts, last_validation_attempt, serialized_size
         FROM LimboChainOp WHERE hash = ?",
    )
    .bind(hash.get_raw_36())
    .fetch_optional(executor)
    .await
}

pub(crate) async fn limbo_chain_ops_pending_sys_validation<'e, E>(
    executor: E,
    limit: u32,
) -> sqlx::Result<Vec<LimboChainOpRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT * FROM LimboChainOp
         WHERE sys_validation_status IS NULL
         ORDER BY sys_validation_attempts, when_received
         LIMIT ?",
    )
    .bind(limit as i64)
    .fetch_all(executor)
    .await
}

pub(crate) async fn limbo_chain_ops_pending_app_validation<'e, E>(
    executor: E,
    limit: u32,
) -> sqlx::Result<Vec<LimboChainOpRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT * FROM LimboChainOp
         WHERE sys_validation_status = 1 AND app_validation_status IS NULL
         ORDER BY app_validation_attempts, when_received
         LIMIT ?",
    )
    .bind(limit as i64)
    .fetch_all(executor)
    .await
}

/// Fetch limbo chain ops pending sys-validation, joined with their `Action`
/// and (optionally) their `Entry`, in a single query.
pub(crate) async fn limbo_chain_ops_pending_sys_validation_with_action<'e, E>(
    executor: E,
    limit: u32,
) -> sqlx::Result<Vec<LimboChainOpJoinedRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT
            l.hash              AS hash,
            l.op_type           AS op_type,
            l.action_hash       AS action_hash,
            l.basis_hash        AS basis_hash,
            l.storage_center_loc AS storage_center_loc,
            l.sys_validation_status AS sys_validation_status,
            l.app_validation_status AS app_validation_status,
            l.abandoned_at      AS abandoned_at,
            l.require_receipt   AS require_receipt,
            l.when_received     AS when_received,
            l.sys_validation_attempts AS sys_validation_attempts,
            l.app_validation_attempts AS app_validation_attempts,
            l.last_validation_attempt AS last_validation_attempt,
            l.serialized_size   AS serialized_size,
            a.author            AS action_author,
            a.seq               AS action_seq,
            a.prev_hash         AS action_prev_hash,
            a.timestamp         AS action_timestamp,
            a.action_type       AS action_type,
            a.action_data       AS action_data,
            a.signature         AS action_signature,
            a.entry_hash        AS action_entry_hash,
            a.private_entry     AS action_private_entry,
            a.record_validity   AS action_record_validity,
            e.blob              AS entry_blob
         FROM LimboChainOp l
         JOIN Action a ON l.action_hash = a.hash
         LEFT JOIN Entry e ON a.entry_hash = e.hash
         WHERE l.sys_validation_status IS NULL
         ORDER BY l.sys_validation_attempts, l.when_received
         LIMIT ?",
    )
    .bind(limit as i64)
    .fetch_all(executor)
    .await
}

/// Fetch limbo chain ops pending app-validation, joined with their `Action`
/// and (optionally) their `Entry`, in a single query.
pub(crate) async fn limbo_chain_ops_pending_app_validation_with_action<'e, E>(
    executor: E,
    limit: u32,
) -> sqlx::Result<Vec<LimboChainOpJoinedRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT
            l.hash              AS hash,
            l.op_type           AS op_type,
            l.action_hash       AS action_hash,
            l.basis_hash        AS basis_hash,
            l.storage_center_loc AS storage_center_loc,
            l.sys_validation_status AS sys_validation_status,
            l.app_validation_status AS app_validation_status,
            l.abandoned_at      AS abandoned_at,
            l.require_receipt   AS require_receipt,
            l.when_received     AS when_received,
            l.sys_validation_attempts AS sys_validation_attempts,
            l.app_validation_attempts AS app_validation_attempts,
            l.last_validation_attempt AS last_validation_attempt,
            l.serialized_size   AS serialized_size,
            a.author            AS action_author,
            a.seq               AS action_seq,
            a.prev_hash         AS action_prev_hash,
            a.timestamp         AS action_timestamp,
            a.action_type       AS action_type,
            a.action_data       AS action_data,
            a.signature         AS action_signature,
            a.entry_hash        AS action_entry_hash,
            a.private_entry     AS action_private_entry,
            a.record_validity   AS action_record_validity,
            e.blob              AS entry_blob
         FROM LimboChainOp l
         JOIN Action a ON l.action_hash = a.hash
         LEFT JOIN Entry e ON a.entry_hash = e.hash
         WHERE l.sys_validation_status = 1 AND l.app_validation_status IS NULL
         ORDER BY l.app_validation_attempts, l.when_received
         LIMIT ?",
    )
    .bind(limit as i64)
    .fetch_all(executor)
    .await
}

pub(crate) async fn limbo_chain_ops_ready_for_integration<'e, E>(
    executor: E,
    limit: u32,
) -> sqlx::Result<Vec<LimboChainOpRow>>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query_as(
        "SELECT * FROM LimboChainOp
         WHERE sys_validation_status = 2
            OR (sys_validation_status = 1 AND app_validation_status IN (1, 2))
         ORDER BY when_received
         LIMIT ?",
    )
    .bind(limit as i64)
    .fetch_all(executor)
    .await
}

pub(crate) async fn set_sys_validation_status<'e, E>(
    executor: E,
    op_hash: &DhtOpHash,
    status: Option<i64>,
) -> sqlx::Result<u64>
where
    E: Executor<'e, Database = Sqlite>,
{
    let result = sqlx::query(
        "UPDATE LimboChainOp SET sys_validation_status = ?
         WHERE hash = ? AND sys_validation_status IS NULL",
    )
    .bind(status)
    .bind(op_hash.get_raw_36())
    .execute(executor)
    .await?;
    Ok(result.rows_affected())
}

pub(crate) async fn set_app_validation_status<'e, E>(
    executor: E,
    op_hash: &DhtOpHash,
    status: Option<i64>,
) -> sqlx::Result<u64>
where
    E: Executor<'e, Database = Sqlite>,
{
    let result = sqlx::query(
        "UPDATE LimboChainOp SET app_validation_status = ?
         WHERE hash = ? AND sys_validation_status IS NOT NULL AND app_validation_status IS NULL",
    )
    .bind(status)
    .bind(op_hash.get_raw_36())
    .execute(executor)
    .await?;
    Ok(result.rows_affected())
}

/// Force a limbo op into a rejected terminal state, bypassing the normal
/// validation ordering.  Used by `reject_chain_ops` for ops that fail outside
/// the normal sys/app validation workflows.
///
/// The rejection is recorded at whichever stage has not yet completed:
///
/// - `sys_validation_status IS NULL` → set `sys_validation_status = Rejected`,
///   leave `app_validation_status` as NULL.
/// - `sys_validation_status = Accepted, app_validation_status IS NULL` →
///   set `app_validation_status = Rejected`.
///
/// Returns the number of rows updated (0 or 1).  Rows already at a terminal
/// state (`sys = Rejected`, or `app IN (Accepted, Rejected)`) are not modified.
pub(crate) async fn force_reject<'e, E>(executor: E, op_hash: &DhtOpHash) -> sqlx::Result<u64>
where
    E: Executor<'e, Database = Sqlite>,
{
    let result = sqlx::query(
        "UPDATE LimboChainOp SET
             sys_validation_status = CASE
                 WHEN sys_validation_status IS NULL THEN 2
                 ELSE sys_validation_status
             END,
             app_validation_status = CASE
                 WHEN sys_validation_status = 1 AND app_validation_status IS NULL THEN 2
                 ELSE app_validation_status
             END
         WHERE hash = ?
           AND (sys_validation_status IS NULL
                OR (sys_validation_status = 1 AND app_validation_status IS NULL))",
    )
    .bind(op_hash.get_raw_36())
    .execute(executor)
    .await?;
    Ok(result.rows_affected())
}

pub(crate) async fn delete_limbo_chain_op<'e, E>(executor: E, hash: DhtOpHash) -> sqlx::Result<()>
where
    E: Executor<'e, Database = Sqlite>,
{
    sqlx::query("DELETE FROM LimboChainOp WHERE hash = ?")
        .bind(hash.get_raw_36())
        .execute(executor)
        .await?;
    Ok(())
}

/// Promote a `LimboChainOp` row to the `ChainOp` table.
///
/// Reads the limbo row, inserts into `ChainOp` with the supplied
/// `validation_status` and `when_integrated`, then deletes the limbo row.
/// All three statements run on the given connection.
/// **The caller must wrap this call in a transaction** to ensure atomicity.
///
/// Returns `true` if the limbo row existed and was promoted, `false` if it
/// did not exist.
pub(crate) async fn promote_to_chain_op(
    conn: &mut SqliteConnection,
    op_hash: &DhtOpHash,
    validation_status: OpValidity,
    when_integrated: Timestamp,
) -> sqlx::Result<bool> {
    // SELECT the limbo row's payload.
    let row: Option<LimboChainOpRow> = sqlx::query_as(
        "SELECT hash, op_type, action_hash, basis_hash, storage_center_loc,
                sys_validation_status, app_validation_status, abandoned_at,
                require_receipt, when_received, sys_validation_attempts,
                app_validation_attempts, last_validation_attempt, serialized_size
         FROM LimboChainOp WHERE hash = ?",
    )
    .bind(op_hash.get_raw_36())
    .fetch_optional(&mut *conn)
    .await?;

    let limbo = match row {
        Some(r) => r,
        None => return Ok(false),
    };

    // INSERT into ChainOp. Carry `require_receipt` over so the receipt
    // workflow can find the op on `ChainOp` and clear the flag once the
    // receipt has been sent.
    sqlx::query(
        "INSERT INTO ChainOp
            (hash, op_type, action_hash, basis_hash, storage_center_loc,
             validation_status, locally_validated, require_receipt,
             when_received, when_integrated, serialized_size)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(&limbo.hash)
    .bind(limbo.op_type)
    .bind(&limbo.action_hash)
    .bind(&limbo.basis_hash)
    .bind(limbo.storage_center_loc)
    .bind(i64::from(validation_status))
    .bind(1_i64) // locally_validated = true: op has been through limbo and validated locally
    .bind(limbo.require_receipt)
    .bind(limbo.when_received)
    .bind(when_integrated.as_micros())
    .bind(limbo.serialized_size)
    .execute(&mut *conn)
    .await?;

    // DELETE the limbo row.
    sqlx::query("DELETE FROM LimboChainOp WHERE hash = ?")
        .bind(op_hash.get_raw_36())
        .execute(&mut *conn)
        .await?;

    Ok(true)
}