diff --git a/packages/cultmesh-py/tests/test_stampless_replication.py b/packages/cultmesh-py/tests/test_stampless_replication.py index 0b2cf9e4..6acb647a 100644 --- a/packages/cultmesh-py/tests/test_stampless_replication.py +++ b/packages/cultmesh-py/tests/test_stampless_replication.py @@ -1,8 +1,8 @@ """A record a peer replicates to this node is stored under the stamp it carries, so one with no stamp is refused where it becomes an envelope. The stamp shape is tests/vectors/stored-at-stamp/stamps.msgpack, held to the same answers as the raw put of the server -(variants:ruling:raw-put-without-stamp-refused). A snapshot response or shard log that carries a record with no stamp is refused whole -with ValueError before anything is stored (as a record of an unregistered schema is refused whole); the refusal of a put is +(variants:ruling:raw-put-without-stamp-refused). A snapshot response is refused whole with ValueError before anything is stored (as a record of an unregistered schema is refused whole). A shard log is +refused whole too: every put it would apply is judged for its stamp before the first entry is stored (a put of an unregistered schema is skipped, as ever, and is not judged). The refusal of a put is message_invalid (field `document`), tested in test_remote_write_authority.py. """ @@ -160,6 +160,24 @@ class StamplessShardLogTests(unittest.TestCase): self.assertEqual(again.cache.get(NOTE, "note:good")["text"], "good") + def test_a_shard_log_with_a_bad_entry_in_the_middle_stores_nothing_and_tells_no_watcher(self) -> None: + for label, stamp in REFUSED: + with self.subTest(label), tempfile.TemporaryDirectory() as directory: + node = create_node(Path(directory) / "node.cc", runtime_id="stampless-multi") + node.database.register_document(NOTE) + seen: list[Any] = [] + node.database.watch(seen.append) + log = shard_log_of(record_of("note:a", "a"), record_of("note:b", "b", stamp), record_of("note:c", "c")) + with self.assertRaises(ValueError): + node.database.apply_shard_log_response(log) + for key in ("note:a", "note:b", "note:c"): + self.assertIsNone(node.cache.get(NOTE, key), f"{key} was not stored") + self.assertEqual(seen, [], "no watcher heard of a refused log") + node.database.apply_shard_log_response(shard_log_of(record_of("note:a", "a"), record_of("note:c", "c"))) + self.assertEqual([node.cache.get(NOTE, k)["text"] for k in ("note:a", "note:c")], ["a", "c"]) + self.assertEqual(len(seen), 2, "a later good log applies and is heard") + + class _StubSubscription: def __init__(self, change: CultNetDatabaseChange) -> None: self._change = change diff --git a/packages/cultnet-py/src/cultnet_py/replication.py b/packages/cultnet-py/src/cultnet_py/replication.py index fb4cec91..41d63295 100644 --- a/packages/cultnet-py/src/cultnet_py/replication.py +++ b/packages/cultnet-py/src/cultnet_py/replication.py @@ -51,6 +51,14 @@ def apply_raw_document_record( ) +def require_stored_at_stamp(record: dict[str, Any]) -> str: + """The storedAt a replicated raw record carries; ValueError when it is missing or no stamp. The one place the rule is judged.""" + stored_at = record.get("storedAt") + if parse_stored_at(stored_at) is None: + raise ValueError("A replicated raw record must carry a storedAt stamp.") + return stored_at + + def raw_record_to_envelope( document: DocumentDefinition[Any], schema_id: str, @@ -59,9 +67,7 @@ def raw_record_to_envelope( """The envelope a replicated raw record is stored as; the one place a record becomes an envelope (a direct apply, a snapshot and a shard log all come through here). A peer's record is stored under the stamp it carries, so one whose storedAt is missing, empty or no stamp is refused with ValueError and nothing is minted or stored (variants:ruling:raw-put-without-stamp-refused).""" - stored_at = record.get("storedAt") - if parse_stored_at(stored_at) is None: - raise ValueError("A replicated raw record must carry a storedAt stamp.") + stored_at = require_stored_at_stamp(record) return CultCacheEnvelope( key=str(record["recordKey"]), type=document.type, @@ -124,6 +130,12 @@ def apply_shard_log_response( reason = response.get("reason") or "unspecified" raise ValueError(f"Shard log response requires resync: {reason}") documents_by_schema_id = schema_document_map(documents) + # The whole log is refused before anything is stored: every put that would be applied is judged for its stamp first. + for entry in response.get("entries", []): + if isinstance(entry, dict) and entry.get("changeKind") in ("added", "updated") and isinstance(entry.get("put"), dict): + record = entry["put"]["document"] + if can_resolve_document_for_raw_record(documents_by_schema_id, str(record["schemaId"]), record): + require_stored_at_stamp(record) applied: list[CultNetAppliedRecord] = [] for entry in response.get("entries", []): if not isinstance(entry, dict):