graph.rs (8686B)
1 use super::{ 2 Selection, Slot, 3 codec::{self, Manifest}, 4 context, routes, 5 }; 6 use crate::workspace::{State, address, crypto, installation::Records, retained}; 7 use crate::{Mutation, ReadSnapshot, RecordKey}; 8 use radroots_domain::OperationId; 9 use radroots_storage::command::{ 10 DeliveryAttempt, DeliveryAttemptSummary, DeliveryObservation, OperationError, 11 }; 12 use sha2::{Digest, Sha256}; 13 use std::collections::{BTreeMap, BTreeSet}; 14 use zeroize::Zeroizing; 15 16 pub(in crate::workspace) struct Graph { 17 pub summaries: Vec<DeliveryAttemptSummary>, 18 pub selected: Option<DeliveryAttempt>, 19 pub(super) selected_slot: Option<Slot>, 20 pub observations: Vec<DeliveryObservation>, 21 pub slots: [u16; 3], 22 } 23 impl Graph { 24 pub(in crate::workspace) async fn read( 25 state: &State, 26 view: &ReadSnapshot, 27 original: &retained::Graph, 28 installed: &Records, 29 operation: OperationId, 30 selected: Option<Selection<'_>>, 31 ) -> Result<Self, OperationError> { 32 let mut positions = BTreeMap::<(u8, u16), BTreeSet<(u8, u8)>>::new(); 33 let mut observations = BTreeMap::<(u8, u16), BTreeMap<(u8, u8), RecordKey>>::new(); 34 let mut observation_count = 0; 35 for key in view.keys() { 36 let (kind, _, _) = address::fields(key)?; 37 if kind == address::OBSERVATION { 38 let (recipient, ordinal, target, kind) = address::observation_position(key)?; 39 observation_count += 1; 40 if observation_count > 2304 { 41 return Err(OperationError::RecoveryRequired); 42 } 43 observations 44 .entry((recipient, ordinal)) 45 .or_default() 46 .insert((target, kind), key); 47 continue; 48 } 49 if !matches!(kind, address::DELIVERY | address::REQUEST_INDEX) { 50 continue; 51 } 52 let (kind, recipient, ordinal, part) = address::delivery_position(key)?; 53 positions 54 .entry((recipient, ordinal)) 55 .or_default() 56 .insert((kind, part)); 57 } 58 if positions.len() > 384 { 59 return Err(OperationError::RecoveryRequired); 60 } 61 let mut result = Self { 62 summaries: Vec::with_capacity(positions.len()), 63 selected: None, 64 selected_slot: None, 65 observations: Vec::with_capacity(observation_count), 66 slots: [0; 3], 67 }; 68 let mut representations = BTreeMap::new(); 69 for ((recipient, ordinal), positions) in positions { 70 if ordinal != result.slots[recipient as usize] + 1 { 71 return Err(OperationError::RecoveryRequired); 72 } 73 result.slots[recipient as usize] = ordinal; 74 let bound = original.bound().ok_or(OperationError::RecoveryRequired)?; 75 let participant = super::super::issuance::target(bound, recipient)?; 76 let work = installed.artifact(participant)?; 77 let commitment = installed.commitment(work)?; 78 let slot = Slot { 79 operation, 80 recipient, 81 ordinal, 82 commitment, 83 }; 84 let manifest = read(state, view, slot, 0, false).await?; 85 let parsed = Manifest::decode(&manifest, commitment)?; 86 let count = parsed.bytes.div_ceil(codec::PART_BYTES) as u8; 87 if positions.len() != count as usize + 2 88 || !positions.contains(&(address::REQUEST_INDEX, 0)) 89 || (0..=count).any(|part| !positions.contains(&(address::DELIVERY, part))) 90 { 91 return Err(OperationError::RecoveryRequired); 92 } 93 let index = read(state, view, slot, 0, true).await?; 94 if index.as_slice() != codec::index(&manifest, &parsed.request).as_slice() { 95 return Err(OperationError::RecoveryRequired); 96 } 97 let mut frame = Zeroizing::new(Vec::with_capacity(parsed.bytes)); 98 for part in 1..=count { 99 let clear = read(state, view, slot, part, false).await?; 100 if clear.len() != (parsed.bytes - frame.len()).min(codec::PART_BYTES) { 101 return Err(OperationError::RecoveryRequired); 102 } 103 frame.extend_from_slice(&clear); 104 } 105 if Sha256::digest(frame.as_slice()).as_slice() != parsed.digest { 106 return Err(OperationError::RecoveryRequired); 107 } 108 let report = codec::decode(&frame, work, commitment)?; 109 if !parsed.matches(&report) { 110 return Err(OperationError::RecoveryRequired); 111 } 112 result.observations.extend( 113 super::observation::read( 114 state, 115 view, 116 slot, 117 &report, 118 observations 119 .remove(&(recipient, ordinal)) 120 .unwrap_or_default(), 121 ) 122 .await?, 123 ); 124 routes(bound, participant, report.intent()) 125 .map_err(|_| OperationError::RecoveryRequired)?; 126 if representations 127 .insert(report.intent().event_id(), *report.envelope_digest()) 128 .is_some_and(|digest| digest != *report.envelope_digest()) 129 { 130 return Err(OperationError::RecoveryRequired); 131 } 132 result 133 .summaries 134 .push(DeliveryAttemptSummary::from_attempt(&report)); 135 if let Some(selected) = selected.filter(|selected| selected.recipient == participant) { 136 let exact = selected.request == report.intent().request_id(); 137 if exact 138 || (result.selected.is_none() 139 && selected.event == Some(report.intent().event_id())) 140 { 141 result.selected = Some(report); 142 result.selected_slot = Some(slot); 143 } 144 } 145 } 146 if !observations.is_empty() { 147 return Err(OperationError::RecoveryRequired); 148 } 149 Ok(result) 150 } 151 } 152 async fn read( 153 state: &State, 154 view: &ReadSnapshot, 155 slot: Slot, 156 part: u8, 157 index: bool, 158 ) -> Result<Zeroizing<Vec<u8>>, OperationError> { 159 let kind = if index { 160 address::REQUEST_INDEX 161 } else { 162 address::DELIVERY 163 }; 164 let raw = state 165 .read_view_key( 166 view, 167 address::delivery_key(slot.operation, kind, slot.recipient, slot.ordinal, part), 168 ) 169 .await? 170 .ok_or(OperationError::RecoveryRequired)?; 171 let clear = crypto::open_bound( 172 state.provider.as_ref(), 173 &state.reference, 174 context(state, slot, part, index)?, 175 &raw, 176 ) 177 .await?; 178 Ok(clear.expose_secret(|raw| Zeroizing::new(raw.to_vec()))) 179 } 180 pub(super) async fn protect( 181 state: &State, 182 operation: OperationId, 183 recipient: u8, 184 ordinal: u16, 185 commitment: [u8; 32], 186 report: &DeliveryAttempt, 187 ) -> Result<Vec<Mutation>, OperationError> { 188 let frame = codec::encode(report, commitment)?; 189 let manifest = Manifest::encode(report, commitment, &frame); 190 let slot = Slot { 191 operation, 192 recipient, 193 ordinal, 194 commitment, 195 }; 196 let mut changes = Vec::with_capacity(frame.len().div_ceil(codec::PART_BYTES) + 2); 197 changes.push(seal(state, slot, 0, false, &manifest).await?); 198 changes.push( 199 seal( 200 state, 201 slot, 202 0, 203 true, 204 &codec::index(&manifest, report.intent().request_id().as_str()), 205 ) 206 .await?, 207 ); 208 for (part, raw) in frame.chunks(codec::PART_BYTES).enumerate() { 209 changes.push(seal(state, slot, part as u8 + 1, false, raw).await?); 210 } 211 Ok(changes) 212 } 213 async fn seal( 214 state: &State, 215 slot: Slot, 216 part: u8, 217 index: bool, 218 raw: &[u8], 219 ) -> Result<Mutation, OperationError> { 220 let protected = crypto::seal_bound( 221 state.provider.as_ref(), 222 &state.reference, 223 context(state, slot, part, index)?, 224 raw, 225 ) 226 .await?; 227 Mutation::new( 228 address::delivery_key( 229 slot.operation, 230 if index { 231 address::REQUEST_INDEX 232 } else { 233 address::DELIVERY 234 }, 235 slot.recipient, 236 slot.ordinal, 237 part, 238 ), 239 None, 240 Some(protected), 241 ) 242 .map_err(super::super::journal::storage_error) 243 }