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
use crate::prelude::StateMutationResult;
use holochain_sqlite::rusqlite::OptionalExtension;
use holochain_sqlite::rusqlite::{named_params, Transaction};
use holochain_zome_types::Timestamp;

/// True if the chain is currently locked for the given lock id.
/// The chain is never locked for the id that created it.
/// The chain is always locked for all other ids until the lock end time is in the past.
pub fn is_chain_locked(txn: &Transaction, lock: &[u8]) -> StateMutationResult<bool> {
    match txn
        .query_row(
            "
            SELECT 1
            FROM ChainLock
            WHERE expires_at_timestamp >= :now
            AND lock != :lock
            LIMIT 1
            ",
            named_params! {
                ":lock": lock,
                ":now": holochain_zome_types::Timestamp::now()
            },
            |row| row.get::<_, u32>(0),
        )
        .optional()?
    {
        Some(_) => Ok(true),
        None => Ok(false),
    }
}

/// Check if a lock is expired.
pub fn is_lock_expired(txn: &Transaction, lock: &[u8]) -> StateMutationResult<bool> {
    let r = txn
        .query_row(
            "
            SELECT expires_at_timestamp
            FROM ChainLock
            WHERE
            lock = :lock
            ",
            named_params! {
                ":lock": lock,
            },
            |row| {
                Ok(row.get::<_, Timestamp>("expires_at_timestamp")?
                    < holochain_zome_types::Timestamp::now())
            },
        )
        .optional()?;
    // If there's no lock then it's expired.
    Ok(r.unwrap_or(true))
}