open-standards/jcp/jcp-rs/crates/jcp-cite/tests/cite_example.rs
Transaction Science 68cb3c1b13 jcp-cite: the cite example's illustrative retrieval is sealed as an estimate
The `cite` example records a retrieval of 4_200 µJ that no fetch spent
(no document is read) and decided and sealed it as Energy::measured, so
its jcp receipt claimed a hardware reading of work no meter saw.

The cost now comes from `retrieval_cost()`, the citation's figure graded
Estimator with no measured figure; the docs and the first output line say
the retrieval is illustrative. The figure, and so the receipt's cost_uj
and the citation commitment, are unchanged.

tests/cite_example.rs compiles the example in as a module: its cost must
be Estimator with no measured figure, and the receipt sealed over it must
carry energy_provenance "Estimator", decoded from the signed core.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-10 17:47:12 -04:00

57 lines
2.7 KiB
Rust

//! The `cite` example's retrieval is illustrative — no document is fetched —
//! so the cost it is decided and sealed at must be an estimate, never a
//! hardware reading. Checked on the cost the example builds (the example
//! file is compiled in here as a module) and on the `jcp` receipt sealed
//! over it, decoded from the signed core.
#[allow(dead_code)]
#[path = "../examples/cite.rs"]
mod cite;
use ed25519_dalek::SigningKey;
use jcp_cite::{Citation, Source};
use jcp_grant::issue;
use jcp_runtime::Runtime;
use jcp_schema::{Capability, ContentId, Did, Grant, Provenance};
#[test]
fn the_cite_examples_retrieval_is_sealed_as_an_estimate() {
let issuer = SigningKey::from_bytes(&[1u8; 32]);
let agent = Did::from_verifying_key(&SigningKey::from_bytes(&[2u8; 32]).verifying_key());
let corpus = ContentId::of_bytes(b"acme-workspace");
let citation = Citation::new(
agent,
ContentId::of_bytes(b"answer"),
vec![Source::new(ContentId::of_bytes(b"doc"), ContentId::of_bytes(b"grant:finance"))],
4_200,
[7; 16],
);
let cost = cite::retrieval_cost(&citation);
eprintln!("retrieval cost: {} µJ [{}] measured {:?}", cost.uj(), cost.provenance.jcr1_label(), cost.measured_uj);
assert_eq!(cost.provenance, Provenance::Estimator, "an illustrative retrieval graded {:?}", cost.provenance);
assert_eq!(cost.measured_uj, None, "a measured figure nothing read");
assert_eq!(cost.uj(), citation.energy_uj, "the receipt's cost is the citation's figure");
let grant = Grant {
issuer: Did::from_verifying_key(&issuer.verifying_key()),
subject: agent,
capability: Capability::new(corpus, ["retrieve"]),
budget_uj: 1_000_000,
budget_funds: None,
caveats: Vec::new(),
expiry: 0,
parent: None,
delegate_depth: None,
nonce: [0; 16],
};
let sg = issue(&issuer, grant.clone()).expect("issue");
let frame = citation.frame(grant.id(), corpus).expect("frame");
let decision = Runtime::new().decide(&frame, &sg, agent, cost, 1);
assert!(decision.is_allow(), "{:?}", decision.verdict);
let steward = SigningKey::from_bytes(&[9u8; 32]);
let env = jcp_receipt::seal(&frame, &grant, &decision, &steward, "did:example:steward").expect("seal");
let core = jcp_receipt::jcr1::verify_cose_sign1(&env, "did:example:steward", &steward.verifying_key().to_bytes()).expect("verifies");
let sealed: serde_json::Value = jcp_schema::from_canonical_cbor(&core).expect("decodes");
eprintln!("sealed: cost_uj {} [{}]", sealed["cost_uj"], sealed["energy_provenance"]);
assert_eq!(sealed["energy_provenance"].as_str(), Some("Estimator"), "{sealed}");
}