selection.rs (5371B)
1 //! Bind every returned selector and absence fact through final SQL observation. 2 use super::*; 3 #[derive(Clone, Copy, Eq, PartialEq)] 4 pub(in crate::workspace::stock) struct Selected { 5 pub digest: [u8; 32], 6 pub entry: OperationId, 7 pub revision: Sequence, 8 } 9 pub(in crate::workspace::stock) async fn page( 10 connection: &mut SqliteConnection, 11 actor: PublicKey, 12 operation: OperationId, 13 recipient: PublicKey, 14 after: Option<Sequence>, 15 ) -> Result<Vec<Selected>, OperationError> { 16 let rows=sqlx::query("SELECT CASE WHEN length(e.evidence_digest)=32 THEN e.evidence_digest END,CASE WHEN length(e.entry_id)=32 THEN e.entry_id END,o.revision FROM stock_signing_evidence e JOIN stock_operations o ON o.actor=e.actor AND o.operation_id=e.entry_id WHERE e.actor=? AND e.operation_id=? AND e.recipient=? AND (? IS NULL OR o.revision>?) ORDER BY o.revision LIMIT 33") 17 .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()).bind(after.map(|v|v.get() as i64)).bind(after.map(|v|v.get() as i64)).fetch_all(connection).await.map_err(|_|OperationError::RecoveryRequired)?; 18 let mut selected = Vec::with_capacity(rows.len()); 19 let mut previous = after.map(Sequence::get).unwrap_or(0); 20 for row in rows { 21 let field = |index| { 22 row.try_get::<Option<Vec<u8>>, _>(index) 23 .map_err(|_| OperationError::RecoveryRequired)? 24 .ok_or(OperationError::RecoveryRequired)? 25 .try_into() 26 .map_err(|_| OperationError::RecoveryRequired) 27 }; 28 let revision: i64 = row 29 .try_get(2) 30 .map_err(|_| OperationError::RecoveryRequired)?; 31 if revision <= 0 || revision as u64 <= previous { 32 return Err(OperationError::RecoveryRequired); 33 } 34 let revision = 35 Sequence::new(revision as u64).map_err(|_| OperationError::RecoveryRequired)?; 36 previous = revision.get(); 37 selected.push(Selected { 38 digest: field(0)?, 39 entry: OperationId::from_bytes(field(1)?), 40 revision, 41 }); 42 } 43 Ok(selected) 44 } 45 pub(super) async fn entry( 46 connection: &mut SqliteConnection, 47 actor: PublicKey, 48 operation: OperationId, 49 recipient: PublicKey, 50 digest: [u8; 32], 51 ) -> Result<Option<OperationId>, OperationError> { 52 let value:Option<Vec<u8>>=sqlx::query_scalar("SELECT CASE WHEN length(entry_id)=32 THEN entry_id END FROM stock_signing_evidence WHERE actor=? AND operation_id=? AND recipient=? AND evidence_digest=?").bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()).bind(digest.as_slice()).fetch_optional(connection).await.map_err(|_|OperationError::RecoveryRequired)?; 53 value 54 .map(|id| { 55 Ok(OperationId::from_bytes( 56 id.try_into() 57 .map_err(|_| OperationError::RecoveryRequired)?, 58 )) 59 }) 60 .transpose() 61 } 62 pub(in crate::workspace::stock) async fn open( 63 connection: &mut SqliteConnection, 64 input: &AttachmentInput, 65 crypto: &WorkspaceCrypto, 66 operation: OperationId, 67 recipient: PublicKey, 68 selected: Selected, 69 ) -> Result<codec::Retained, OperationError> { 70 let retained = read_entry(connection, input, crypto, selected.entry) 71 .await? 72 .ok_or(OperationError::RecoveryRequired)?; 73 if retained.frame.header.revision != selected.revision 74 || retained.value.receipt.attempt().operation_id() != operation 75 || retained.value.receipt.recipient() != recipient 76 || retained.value.receipt.digest() != &selected.digest 77 { 78 return Err(OperationError::RecoveryRequired); 79 } 80 Ok(retained) 81 } 82 #[derive(Clone, Copy, Eq, PartialEq)] 83 pub(in crate::workspace::stock) struct Summary { 84 pub count: Sequence, 85 pub first: Option<Selected>, 86 } 87 impl Summary { 88 pub(in crate::workspace::stock) async fn read( 89 connection: &mut SqliteConnection, 90 actor: PublicKey, 91 operation: OperationId, 92 recipient: PublicKey, 93 ) -> Result<Self, OperationError> { 94 let count:i64=sqlx::query_scalar("SELECT count(*) FROM stock_signing_evidence WHERE actor=? AND operation_id=? AND recipient=?").bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()).fetch_one(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?; 95 if count < 0 { 96 return Err(OperationError::RecoveryRequired); 97 } 98 let count = Sequence::new(count as u64).map_err(|_| OperationError::RecoveryRequired)?; 99 let first = page(connection, actor, operation, recipient, None) 100 .await? 101 .first() 102 .copied(); 103 if (count.get() == 0) != first.is_none() { 104 return Err(OperationError::RecoveryRequired); 105 } 106 Ok(Self { count, first }) 107 } 108 pub(in crate::workspace::stock) async fn confirm( 109 &self, 110 connection: &mut SqliteConnection, 111 actor: PublicKey, 112 operation: OperationId, 113 recipient: PublicKey, 114 ) -> Result<(), OperationError> { 115 if Self::read(connection, actor, operation, recipient).await? != *self { 116 return Err(OperationError::RecoveryRequired); 117 } 118 Ok(()) 119 } 120 }