Refuse unless the workspace's current view is at the state the caller last saw: `expected` names the view's merkle root (or the view snapshot carrying it), and the record response's `view_merkle` is the next call's expected state. The other fence forms (generation, sequence) are refused as unsupported-for-record — an unreadable fence must never pass as a checked one.
(
repo: &atomic_repository::Repository,
view: &str,
expected: &ExpectedState,
)
| 1170 | /// refused as unsupported-for-record — an unreadable fence must never |
| 1171 | /// pass as a checked one. |
| 1172 | pub(crate) fn check_record_fence( |
| 1173 | repo: &atomic_repository::Repository, |
| 1174 | view: &str, |
| 1175 | expected: &ExpectedState, |
| 1176 | ) -> Result<(), Status> { |
| 1177 | use expected_state::Kind; |
| 1178 | let current = repo.get_view_info(view).map_err(repository_error)?.state; |
| 1179 | let (fenced_view, expected_root) = match &expected.kind { |
| 1180 | Some(Kind::MerkleRoot(hash)) => (view.to_string(), hash.clone()), |
| 1181 | Some(Kind::ViewSnapshot(snapshot)) => { |
| 1182 | let name = snapshot |
| 1183 | .view |
| 1184 | .as_ref() |
| 1185 | .and_then(|view| view.name.clone()) |
| 1186 | .unwrap_or_else(|| view.to_string()); |
| 1187 | let root = snapshot |
| 1188 | .own_merkle |
| 1189 | .clone() |
| 1190 | .or_else(|| snapshot.effective_state.clone()) |
| 1191 | .ok_or_else(|| { |
| 1192 | domain_status(ErrorCode::InvalidArgument, "the fence carries no state") |
| 1193 | })?; |
| 1194 | (name, root) |
| 1195 | } |
| 1196 | _ => { |
| 1197 | return Err(domain_status( |
| 1198 | ErrorCode::InvalidArgument, |
| 1199 | "Record fences on the view's merkle root (merkle_root or view_snapshot)", |
| 1200 | )) |
| 1201 | } |
| 1202 | }; |
| 1203 | if fenced_view != view { |
| 1204 | return Err(domain_status( |
| 1205 | ErrorCode::ViewStale, |
| 1206 | format!("the fence names view '{fenced_view}' but the workspace is on '{view}'"), |
| 1207 | )); |
| 1208 | } |
| 1209 | let expected_root = hash_from_proto(Some(&expected_root)).map_err(|error| { |
| 1210 | domain_status( |
| 1211 | ErrorCode::InvalidArgument, |
| 1212 | format!("the fence's state hash: {error}"), |
| 1213 | ) |
| 1214 | })?; |
| 1215 | if expected_root != current { |
| 1216 | return Err(domain_status( |
| 1217 | ErrorCode::ViewStale, |
| 1218 | format!( |
| 1219 | "view '{view}' has moved on (expected {}, at {}) — re-read and try again", |
| 1220 | expected_root.to_base32(), |
| 1221 | current.to_base32() |
| 1222 | ), |
| 1223 | )); |
| 1224 | } |
| 1225 | Ok(()) |
| 1226 | } |
| 1227 | |
| 1228 | /// Refuse unless `view` is at the state `expected` names — the write-path |
| 1229 | /// fence shared by insert, promote and the vault writes, checked inside the |
no test coverage detected