Skip to main content

coven_database/
database_provider_access.rs

1//! Atomic provider intentions and request confirmations.
2use crate::provider_access::{adopt, finish, read, retake};
3use crate::{
4    DbError, NewOperation, OperationId, PendingValue, ProviderAccess, ProviderAccessChange,
5    ProviderAccessRequest,
6};
7use coven_crypto::SecretBytes;
8
9impl crate::StoreDatabase {
10    /// Read confirmed sharing and its unfinished request from one committed row.
11    pub async fn provider_access(
12        &self,
13        account: String,
14    ) -> Result<Option<ProviderAccess>, DbError> {
15        self.call(move |inner| inner.access.with_writer(|db, _| read(db, &account))).await
16    }
17
18    /// Record a new invitation and its account intention atomically.
19    pub async fn start_operation_with_access<P: PendingValue>(
20        &self,
21        operation: NewOperation,
22        changes: Vec<ProviderAccessChange>,
23    ) -> Result<OperationId, DbError> {
24        self.call(move |inner| {
25            inner.access.with_writer(|db, _| {
26                db.transaction(|db| {
27                    let id = crate::operation::insert(db, &operation)?;
28                    adopt::<P>(db, &changes)?;
29                    Ok(id)
30                })
31            })
32        })
33        .await
34    }
35
36    /// Advance an invitation and its account intention atomically, ending
37    /// the operation it started for the step it leaves, `child`, in the same
38    /// transaction (§13.2). `coven_operations` lists no such operation, so
39    /// its outcome is nobody's to read.
40    pub async fn advance_operation_with_access<P: PendingValue>(
41        &self,
42        update: crate::OperationUpdate,
43        changes: Vec<ProviderAccessChange>,
44        child: Option<OperationId>,
45    ) -> Result<(), DbError> {
46        self.call(move |inner| {
47            inner.access.with_writer(|db, _| {
48                db.transaction(|db| {
49                    crate::operation::advance(db, &update)?;
50                    if let Some(child) = child {
51                        crate::operation::finish_with_outcome(db, child, None)?;
52                    }
53                    adopt::<P>(db, &changes)
54                })
55            })
56        })
57        .await
58    }
59
60    /// Forget each account's confirmed grant and queue a request for its
61    /// intention, atomically (`provider_access::retake`).
62    pub async fn retake_provider_access<P: PendingValue>(
63        &self,
64        changes: Vec<ProviderAccessChange>,
65    ) -> Result<(), DbError> {
66        self.call(move |inner| {
67            inner.access.with_writer(|db, _| db.transaction(|db| retake::<P>(db, &changes)))
68        })
69        .await
70    }
71
72    /// Fix the request's target before calling storage. A failed or interrupted
73    /// request has unknown provider state, so retry must confirm the target again.
74    pub async fn start_provider_request<P: PendingValue>(
75        &self,
76        account: String,
77        id: OperationId,
78    ) -> Result<bool, DbError> {
79        self.call(move |inner| {
80            inner.access.with_writer(|db, _| {
81                db.transaction(|db| {
82                    let state = read(db, &account)?.ok_or(DbError::OperationChanged(id))?;
83                    if state.request.as_ref().map(|r| r.operation) != Some(id) {
84                        return Err(DbError::OperationChanged(id));
85                    }
86                    let record =
87                        crate::operation::find(db, id)?.ok_or(DbError::OperationChanged(id))?;
88                    if record.last_step == crate::provider_access::REQUEST_FIXED_STEP {
89                        return Err(DbError::OperationChanged(id));
90                    }
91                    crate::provider_access::fix_target(db, &account)?;
92                    crate::operation::set_step(db, id, crate::provider_access::REQUEST_FIXED_STEP)?;
93                    crate::retry::clear(db, crate::RetrySubject::Operation(id))?;
94                    crate::pending::save(db, &P::provider_access(id, account, state.intended))?;
95                    Ok(state.intended)
96                })
97            })
98        })
99        .await
100    }
101
102    /// Record a revocation's reply that listed grants left in place: the
103    /// operation `id` moves from its fixed request to a reported failure,
104    /// recording `data`, with `pending` as its record. An entry that made
105    /// the account intended again while the request ran makes the reply
106    /// obsolete: the opposite request is queued on the same pending subject
107    /// instead, as a confirmation's is, and this returns false (§20.9).
108    pub async fn fail_provider_request<P: PendingValue>(
109        &self,
110        account: String,
111        id: OperationId,
112        data: Vec<u8>,
113        pending: P,
114    ) -> Result<bool, DbError> {
115        let update = crate::OperationUpdate {
116            id,
117            previous: crate::provider_access::REQUEST_FIXED_STEP,
118            last_step: crate::provider_access::REQUEST_FAILED_STEP,
119            data,
120        };
121        self.call(move |inner| {
122            inner.access.with_writer(|db, _| {
123                db.transaction(|db| {
124                    let state = read(db, &account)?.ok_or(DbError::OperationChanged(update.id))?;
125                    let request = state.request.ok_or(DbError::OperationChanged(update.id))?;
126                    if request.operation != update.id || pending.operation() != Some(update.id) {
127                        return Err(DbError::Invariant {
128                            what: "a provider request's condition names its own operation",
129                        });
130                    }
131                    let target = request.target.ok_or(DbError::OperationChanged(update.id))?;
132                    if state.intended != target {
133                        crate::provider_access::requeue(db, &account, update.id)?;
134                        crate::pending::save(
135                            db,
136                            &P::provider_access(update.id, account, state.intended),
137                        )?;
138                        return Ok(false);
139                    }
140                    crate::operation::advance(db, &update)?;
141                    crate::pending::save(db, &pending)?;
142                    Ok(true)
143                })
144            })
145        })
146        .await
147    }
148
149    /// Advance an invite whose grant the provider refused for good, and end
150    /// the account's grant request `grant` as not granted, in one commit: the
151    /// refusal leaves nothing shared, and retrying the grant would be refused
152    /// the same way. The account's intention is the invite's end's to change
153    /// (§13.2). A request that is not `grant`, fixed on granting, fails with
154    /// `OperationChanged`.
155    pub async fn advance_operation_refusing_grant(
156        &self,
157        update: crate::OperationUpdate,
158        account: String,
159        grant: OperationId,
160    ) -> Result<(), DbError> {
161        self.call(move |inner| {
162            inner.access.with_writer(|db, _| {
163                db.transaction(|db| {
164                    crate::operation::advance(db, &update)?;
165                    let state = read(db, &account)?.ok_or(DbError::OperationChanged(grant))?;
166                    if state.request
167                        != Some(ProviderAccessRequest { operation: grant, target: Some(true) })
168                    {
169                        return Err(DbError::OperationChanged(grant));
170                    }
171                    crate::provider_access::confirm(db, &account, false, None)?;
172                    finish(db, &account, grant)
173                })
174            })
175        })
176        .await
177    }
178
179    /// Confirm a fixed request and either finish it or queue the opposite target
180    /// on the same pending subject, in one commit.
181    pub async fn complete_provider_request<P: PendingValue>(
182        &self,
183        account: String,
184        id: OperationId,
185        target: bool,
186        invitation: Option<SecretBytes>,
187    ) -> Result<bool, DbError> {
188        self.call(move |inner| {
189            inner.access.with_writer(|db, _| {
190                db.transaction(|db| {
191                    let state = read(db, &account)?.ok_or(DbError::OperationChanged(id))?;
192                    if state.request
193                        != Some(ProviderAccessRequest { operation: id, target: Some(target) })
194                    {
195                        return Err(DbError::OperationChanged(id));
196                    }
197                    crate::provider_access::confirm(db, &account, target, invitation.as_ref())?;
198                    if state.intended == target {
199                        finish(db, &account, id)?;
200                        Ok(true)
201                    } else {
202                        crate::provider_access::requeue(db, &account, id)?;
203                        crate::pending::save(db, &P::provider_access(id, account, state.intended))?;
204                        Ok(false)
205                    }
206                })
207            })
208        })
209        .await
210    }
211}
212
213#[cfg(test)]
214#[path = "database_provider_access_tests.rs"]
215mod tests;