chio-store-sqlite 0.1.2

SQLite-backed persistence, query, and report implementations for Chio
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
use super::*;
use chio_core::capability::governance::{
    GovernedApprovalDecision, GovernedApprovalToken, GovernedApprovalTokenBody,
    ThresholdApprovalProposal, ThresholdApprovalProposalBody, VerifiedApprovalSetBody,
    THRESHOLD_APPROVAL_PROPOSAL_SCHEMA,
};
use chio_kernel::ThresholdApprovalReplayReservationV1;

fn replay_reservation(
    proposal_id: &str,
    request_id: &str,
    token_ids: [&str; 2],
    created_at: u64,
) -> ThresholdApprovalReplayReservationV1 {
    let authority = Keypair::generate();
    let subject = Keypair::generate();
    let approvers = [Keypair::generate(), Keypair::generate()];
    let deadline = created_at + 300;
    let proposal = ThresholdApprovalProposal::sign(
        ThresholdApprovalProposalBody {
            schema: THRESHOLD_APPROVAL_PROPOSAL_SCHEMA.to_string(),
            proposal_id: proposal_id.to_owned(),
            request_id: request_id.to_owned(),
            governed_intent_hash: sha256_hex(b"threshold-intent"),
            subject: subject.public_key(),
            authorizing_capability_digest: sha256_hex(b"threshold-capability"),
            policy_hash: sha256_hex(b"threshold-policy"),
            threshold: 2,
            eligible_set_digest: sha256_hex(b"threshold-eligible-set"),
            proposal_created_at: created_at,
            proposal_deadline: deadline,
            policy_authority: authority.public_key(),
        },
        &authority,
    )
    .expect("proposal");
    let proposal_hash = proposal.artifact_digest().expect("proposal hash");
    let tokens = approvers
        .iter()
        .zip(token_ids)
        .map(|(approver, token_id)| {
            GovernedApprovalToken::sign(
                GovernedApprovalTokenBody {
                    id: token_id.to_owned(),
                    approver: approver.public_key(),
                    subject: subject.public_key(),
                    governed_intent_hash: sha256_hex(b"threshold-intent"),
                    request_id: request_id.to_owned(),
                    threshold_proposal_hash: Some(proposal_hash.clone()),
                    issued_at: created_at,
                    expires_at: deadline,
                    decision: GovernedApprovalDecision::Approved,
                },
                approver,
            )
            .expect("token")
        })
        .collect::<Vec<_>>();
    let token_digests = tokens
        .iter()
        .map(|token| token.artifact_digest().expect("token digest"))
        .collect();
    let verified = VerifiedApprovalSetBody::new(token_digests, &proposal).expect("verified set");
    ThresholdApprovalReplayReservationV1::new(proposal, tokens, verified)
        .expect("replay reservation")
}

fn reserve(
    fixture: &Fixture,
    operation: &AdmissionOperationV1,
    reservation: &ThresholdApprovalReplayReservationV1,
    now: u64,
) -> Result<AdmissionOperationV1, AdmissionOperationStoreError> {
    let proposal_hash = reservation
        .proposal()
        .artifact_digest()
        .expect("proposal hash");
    let set_hash = reservation
        .verified_set()
        .approval_set_hash()
        .expect("set hash");
    let lease = claim(fixture, operation, "threshold-worker", now);
    let command = AdmissionOperationCommand::new(
        operation.binding().operation_id().clone(),
        operation.version(),
        lease,
        vec![
            AdmissionAttachment::ThresholdProposalHash(
                AdmissionDigest::try_new("threshold_proposal_hash", proposal_hash)
                    .expect("proposal digest"),
            ),
            AdmissionAttachment::ApprovalSetHash(
                AdmissionDigest::try_new("approval_set_hash", set_hash).expect("set digest"),
            ),
        ],
        Some(AdmissionOperationState::ApprovalReserved),
        None,
        None,
    )
    .expect("reservation command");
    fixture
        .store
        .reserve_threshold_approval_and_commit_admission(&command, reservation, now)
        .map(|result| result.into_operation())
}

fn transition(
    fixture: &Fixture,
    operation: &AdmissionOperationV1,
    state: AdmissionOperationState,
    now: u64,
) -> AdmissionOperationV1 {
    let lease = claim(fixture, operation, "threshold-worker", now);
    let command = AdmissionOperationCommand::new(
        operation.binding().operation_id().clone(),
        operation.version(),
        lease,
        Vec::new(),
        Some(state),
        None,
        None,
    )
    .expect("transition command");
    fixture
        .store
        .compare_and_swap(&command, now)
        .expect("transition")
        .into_operation()
}

#[test]
fn threshold_replay_reservation_scopes_token_ids_to_each_proposal() {
    let fixture = fixture();
    let now = now_ms();
    let created_at = now / 1_000;
    let first = prepared_operation(
        &fixture.fence,
        AdmissionOperationKind::GovernedActiveResponse,
        "threshold-request-a",
        "threshold-capability-a",
    );
    fixture
        .store
        .begin(&first, &fixture.fence, now)
        .expect("begin first");
    let reservation = replay_reservation(
        "threshold-proposal-a",
        "threshold-request-a",
        ["threshold-token-a", "threshold-token-b"],
        created_at,
    );
    let reserved = reserve(&fixture, &first, &reservation, now + 1).expect("reserve first");
    assert_eq!(reserved.state(), AdmissionOperationState::ApprovalReserved);

    let connection = fixture.store.connection().expect("connection");
    let stored: (String, i64, String) = connection
        .query_row(
            "SELECT state, proposal_deadline, approval_set_hash FROM threshold_approval_proposals WHERE operation_id = ?1",
            [first.binding().operation_id().as_str()],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
        )
        .expect("stored proposal");
    assert_eq!(stored.0, "reserved");
    assert_eq!(stored.1, i64::try_from(created_at + 300).expect("deadline"));
    assert_eq!(
        stored.2,
        reservation
            .verified_set()
            .approval_set_hash()
            .expect("set hash")
    );
    let token_count: i64 = connection
        .query_row(
            "SELECT COUNT(*) FROM threshold_approval_tokens WHERE proposal_id = ?1",
            [reservation.proposal().body.proposal_id.as_str()],
            |row| row.get(0),
        )
        .expect("token count");
    assert_eq!(token_count, 2);
    drop(connection);

    let second = prepared_operation(
        &fixture.fence,
        AdmissionOperationKind::GovernedActiveResponse,
        "threshold-request-b",
        "threshold-capability-b",
    );
    fixture
        .store
        .begin(&second, &fixture.fence, now + 2)
        .expect("begin second");
    let second_reservation = replay_reservation(
        "threshold-proposal-b",
        "threshold-request-b",
        ["threshold-token-a", "threshold-token-b"],
        created_at,
    );
    let second_reserved =
        reserve(&fixture, &second, &second_reservation, now + 3).expect("reserve second");
    assert_eq!(
        second_reserved.state(),
        AdmissionOperationState::ApprovalReserved
    );
    let connection = fixture.store.connection().expect("connection");
    let scoped_token_count: i64 = connection
        .query_row(
            r#"
            SELECT COUNT(*)
            FROM threshold_approval_tokens
            WHERE token_id IN ('threshold-token-a', 'threshold-token-b')
            "#,
            [],
            |row| row.get(0),
        )
        .expect("scoped token count");
    assert_eq!(scoped_token_count, 4);
}

#[test]
fn threshold_replay_state_commits_when_dispatch_commits() {
    let fixture = fixture();
    let now = now_ms();
    let operation = prepared_operation(
        &fixture.fence,
        AdmissionOperationKind::GovernedActiveResponse,
        "threshold-request-commit",
        "threshold-capability-commit",
    );
    fixture
        .store
        .begin(&operation, &fixture.fence, now)
        .expect("begin");
    let reservation = replay_reservation(
        "threshold-proposal-commit",
        "threshold-request-commit",
        ["threshold-token-commit-a", "threshold-token-commit-b"],
        now / 1_000,
    );
    let reserved = reserve(&fixture, &operation, &reservation, now + 1).expect("reserve");
    let ready = transition(
        &fixture,
        &reserved,
        AdmissionOperationState::ReadyToDispatch,
        now + 2,
    );
    let committed = transition(
        &fixture,
        &ready,
        AdmissionOperationState::DispatchCommitted,
        now + 3,
    );
    assert_eq!(
        committed.state(),
        AdmissionOperationState::DispatchCommitted
    );
    let connection = fixture.store.connection().expect("connection");
    let state: String = connection
        .query_row(
            "SELECT state FROM threshold_approval_proposals WHERE operation_id = ?1",
            [operation.binding().operation_id().as_str()],
            |row| row.get(0),
        )
        .expect("replay state");
    assert_eq!(state, "committed");
}

fn prepared_approval_and_budget_operation(
    fence: &StoreMutationFence,
    request_id: &str,
    capability_id: &str,
) -> AdmissionOperationV1 {
    let namespace = AuthenticatedRequestNamespace::for_local_system(identifier(
        "coordinator_authority_id",
        "local-test-authority",
    ))
    .expect("namespace");
    let requirements = AdmissionParticipantRequirements {
        broker_attempt: true,
        budget_capture: true,
        approval: true,
        ..AdmissionParticipantRequirements::NONE
    };
    let binding = AdmissionOperationBindingV1::new(AdmissionOperationBindingInputV1 {
        kind: AdmissionOperationKind::ToolDispatch,
        namespace,
        request_id: identifier("request_id", request_id),
        capability_id: identifier("capability_id", capability_id),
        authorization_capability_hash: digest("authorization_capability_hash", 'a'),
        request_binding: AdmissionRequestBindingV1::new(
            digest("immutable_request_hash", 'b'),
            requirements,
        )
        .expect("request binding"),
        policy_hash: digest("policy_hash", 'c'),
        effect_class: SideEffectClass::SideEffecting,
    })
    .expect("binding");
    AdmissionOperationV1::prepare(binding, fence.owner_epoch).expect("prepared operation")
}

#[test]
fn approval_required_operations_are_persisted_but_excluded_from_recovery_pages() {
    let fixture = fixture();
    let now = now_ms();
    let created_at = now / 1_000;
    let operation = prepared_approval_and_budget_operation(
        &fixture.fence,
        "approval-required-request",
        "approval-required-capability",
    );
    fixture
        .store
        .begin(&operation, &fixture.fence, now)
        .expect("begin operation");

    let lease = claim(&fixture, &operation, "approval-required-worker", now + 1);
    let registered = fixture
        .store
        .compare_and_swap(
            &AdmissionOperationCommand::new(
                operation.binding().operation_id().clone(),
                operation.version(),
                lease,
                vec![AdmissionAttachment::BrokerAttempt(provider_attempt(
                    &operation,
                    "approval-required-attempt",
                ))],
                Some(AdmissionOperationState::BrokerAttemptRegistered),
                None,
                None,
            )
            .expect("broker attempt command"),
            now + 2,
        )
        .expect("register broker attempt")
        .into_operation();

    let reservation = replay_reservation(
        "approval-required-proposal",
        "approval-required-request",
        ["approval-required-token-a", "approval-required-token-b"],
        created_at,
    );
    let proposal_hash = reservation
        .proposal()
        .artifact_digest()
        .expect("proposal hash");
    let lease = claim(&fixture, &registered, "approval-required-worker", now + 3);
    let command = AdmissionOperationCommand::new(
        registered.binding().operation_id().clone(),
        registered.version(),
        lease,
        vec![
            AdmissionAttachment::ThresholdProposalHash(
                AdmissionDigest::try_new("threshold_proposal_hash", proposal_hash)
                    .expect("proposal digest"),
            ),
            AdmissionAttachment::ThresholdProposal(Box::new(reservation.proposal().clone())),
            AdmissionAttachment::BudgetHoldId(identifier(
                "budget_hold_id",
                "approval-required-hold",
            )),
        ],
        Some(AdmissionOperationState::ApprovalRequired),
        None,
        None,
    )
    .expect("approval-required command");

    // The generic compare-and-swap is the path that writes this state, so the SQL
    // state CHECK has to admit it or the transition can never be persisted.
    let advanced = fixture
        .store
        .compare_and_swap(&command, now + 4)
        .expect("advance to approval_required")
        .into_operation();
    assert_eq!(advanced.state(), AdmissionOperationState::ApprovalRequired);

    let connection = fixture.store.connection().expect("connection");
    let state: String = connection
        .query_row(
            "SELECT state FROM admission_operations WHERE operation_id = ?1",
            [operation.binding().operation_id().as_str()],
            |row| row.get(0),
        )
        .expect("persisted state");
    assert_eq!(state, "approval_required");
    drop(connection);

    let later = prepared_operation(
        &fixture.fence,
        AdmissionOperationKind::ToolDispatch,
        "recoverable-after-approval-required",
        "recoverable-after-approval-required-capability",
    );
    fixture
        .store
        .begin(&later, &fixture.fence, now + 5)
        .expect("begin later recoverable operation");
    assert_eq!(
        fixture
            .store
            .list_recoverable(now + 6, 1)
            .expect("recovery page must pass pending approval"),
        vec![later]
    );
}