import hashlib from gravityweave.pcp import create_entry, build_chain, inclusion_proof, head_hash, verify_inclusion def test_create_and_chain_basic(): ops = [("add-c1", "nodeA"), ("add-c2", "nodeB"), ("mutate-c1", "nodeA")] chain = build_chain(ops) assert len(chain) == 3 # each entry hash should be a valid sha256 hex for e in chain: h = e.get("hash") assert isinstance(h, str) and len(h) == 64 def test_inclusion_proof_and_head(): ops = [("op1", "A"), ("op2", "B"), ("op3", "C")] chain = build_chain(ops) head = head_hash(chain) # pick middle entry target = chain[1] proof = inclusion_proof(chain, target_entry_id=target["entry_id"]) assert proof[0] == target["hash"] assert proof[-1] == head # verify inclusion using head-only verification (best-effort) assert verify_inclusion(head, proof) def test_verify_inclusion_with_chain_map(): ops = [("opA", "X"), ("opB", "Y"), ("opC", "Z")] chain = build_chain(ops) head = head_hash(chain) target = chain[0] proof = inclusion_proof(chain, target_entry_id=target["entry_id"]) # build chain_map: hash -> entry chain_map = {e["hash"]: e for e in chain} # verification should succeed when supplying chain_map assert verify_inclusion(head, proof, chain_map=chain_map) # build a clean chain_map and verify inclusion with the map provided chain_map = {e["hash"]: e for e in chain} assert verify_inclusion(head, proof, chain_map=chain_map)