MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / check_record_fence

Function check_record_fence

libatomic/src/daemon/services.rs:1173–1227  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

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

Callers 1

recordMethod · 0.85

Calls 5

domain_statusFunction · 0.85
hash_from_protoFunction · 0.85
get_view_infoMethod · 0.80
as_refMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected