lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

replacement_commitment.rs (9674B)


      1 #![cfg(feature = "commerce")]
      2 use radroots_domain::{
      3     CommerceContent, CommerceHeader, CommercePayload, PublicKey, RecordId, Sequence, SourceId,
      4     TermsAuthorization, TermsProposal, UnixSeconds,
      5 };
      6 use radroots_protocol::{
      7     command::CapturedTermsInstallation,
      8     commerce::{self, DecodedRecord, EncodedRecord},
      9     terms::EncodedTerms,
     10 };
     11 use serde_json::{Value, json};
     12 
     13 fn time(value: u64) -> UnixSeconds {
     14     UnixSeconds::new(value).unwrap()
     15 }
     16 fn decoded(content: &CommerceContent) -> DecodedRecord {
     17     commerce::decode(EncodedRecord::capture(content).unwrap().protected_wire()).unwrap()
     18 }
     19 struct Fixture {
     20     command: CapturedTermsInstallation,
     21     base: DecodedRecord,
     22     proposal: DecodedRecord,
     23     authorization: DecodedRecord,
     24     head: (RecordId, Sequence),
     25 }
     26 fn fixture(buyer_proposes: bool) -> Fixture {
     27     let original: Value =
     28         serde_json::from_str(include_str!("fixtures/commerce_signing.v1.json")).unwrap();
     29     let request = commerce::decode(original["request_wire"].as_str().unwrap().as_bytes()).unwrap();
     30     let base = commerce::decode(original["record_wire"].as_str().unwrap().as_bytes()).unwrap();
     31     let CommercePayload::Request(requested) = request.content().payload() else {
     32         panic!()
     33     };
     34     let terms = requested.terms();
     35     let header = base.content().header();
     36     let make = |author: PublicKey,
     37                 previous: &DecodedRecord,
     38                 sequence: u64,
     39                 payload: CommercePayload,
     40                 extra: &[RecordId]| {
     41         let mut parents: Vec<_> = terms
     42             .lines()
     43             .iter()
     44             .map(|line| SourceId::from_bytes(line.source().event_id().into_bytes()))
     45             .collect();
     46         parents.extend(
     47             [previous.record_id(), base.record_id()]
     48                 .into_iter()
     49                 .chain(extra.iter().copied())
     50                 .map(|id| SourceId::from_bytes(id.into_bytes())),
     51         );
     52         parents.sort();
     53         parents.dedup();
     54         decoded(
     55             &CommerceContent::new(
     56                 CommerceHeader::new(
     57                     header.thread_id(),
     58                     header.order_id(),
     59                     author,
     60                     header.buyer(),
     61                     header.seller(),
     62                     time(452),
     63                     Sequence::new(sequence).unwrap(),
     64                     Some(previous.record_id()),
     65                     parents,
     66                 )
     67                 .unwrap(),
     68                 payload,
     69             )
     70             .unwrap(),
     71         )
     72     };
     73     let proposer = if buyer_proposes {
     74         header.buyer()
     75     } else {
     76         header.seller()
     77     };
     78     let authorizer = if buyer_proposes {
     79         header.seller()
     80     } else {
     81         header.buyer()
     82     };
     83     let proposal = make(
     84         proposer,
     85         if buyer_proposes { &request } else { &base },
     86         2,
     87         CommercePayload::TermsProposed(
     88             TermsProposal::new(
     89                 Some(base.record_id()),
     90                 terms.clone(),
     91                 time(700),
     92                 "complete replacement",
     93             )
     94             .unwrap(),
     95         ),
     96         &[],
     97     );
     98     let authorization = make(
     99         authorizer,
    100         if buyer_proposes { &base } else { &request },
    101         2,
    102         CommercePayload::TermsAuthorized(TermsAuthorization::new(
    103             proposal.record_id(),
    104             EncodedTerms::capture(terms).unwrap().digest(),
    105             time(650),
    106         )),
    107         &[proposal.record_id()],
    108     );
    109     let seller = if buyer_proposes {
    110         &authorization
    111     } else {
    112         &proposal
    113     };
    114     let buyer = if buyer_proposes {
    115         &proposal
    116     } else {
    117         &authorization
    118     };
    119     let head = (
    120         seller.record_id(),
    121         seller.content().header().author_sequence(),
    122     );
    123     let mut wire: Value = serde_json::from_str(original["command_wire"].as_str().unwrap()).unwrap();
    124     wire["type"] = json!("orders.install_terms");
    125     wire["input"] = json!({"proposal":proposal.record_id(),"authorization":authorization.record_id(),"terms_digest":EncodedTerms::capture(terms).unwrap().digest(),"allocation":{"type":"automatic"}});
    126     wire["context"]["approval"] = Value::Null;
    127     wire["context"]["not_after"] = json!("600");
    128     wire["context"]["expected"]["agreement"] = json!(base.record_id());
    129     wire["context"]["expected"]["seller_sequence"] = json!("1");
    130     wire["context"]["expected"]["buyer_head"] = json!(buyer.record_id());
    131     let command = CapturedTermsInstallation::parse(&serde_json::to_vec(&wire).unwrap()).unwrap();
    132     Fixture {
    133         command,
    134         base,
    135         proposal,
    136         authorization,
    137         head,
    138     }
    139 }
    140 
    141 #[test]
    142 fn either_participant_proposes_and_the_next_seller_record_binds_all_originals() {
    143     for buyer_proposes in [false, true] {
    144         let f = fixture(buyer_proposes);
    145         let rumor = commerce::replacement_commitment_rumor(
    146             &f.command,
    147             &f.base,
    148             &f.proposal,
    149             &f.authorization,
    150             f.head,
    151             time(453),
    152         )
    153         .unwrap();
    154         let wire: Value = serde_json::from_str(rumor.protected_wire()).unwrap();
    155         let record = commerce::decode(wire["content"].as_str().unwrap().as_bytes()).unwrap();
    156         let header = record.content().header();
    157         assert_eq!(header.author_sequence().get(), 3);
    158         assert_eq!(header.author_previous(), Some(f.head.0));
    159         assert_eq!(header.author(), f.base.content().header().seller());
    160         assert_eq!(header.created_at(), time(453));
    161         for id in [
    162             f.base.record_id(),
    163             f.proposal.record_id(),
    164             f.authorization.record_id(),
    165             f.head.0,
    166             f.command
    167                 .context()
    168                 .expected()
    169                 .unwrap()
    170                 .buyer_head()
    171                 .unwrap(),
    172         ] {
    173             assert!(
    174                 header
    175                     .parents()
    176                     .contains(&SourceId::from_bytes(id.into_bytes()))
    177             );
    178         }
    179         let CommercePayload::AgreementCommitted(commitment) = record.content().payload() else {
    180             panic!()
    181         };
    182         assert_eq!(commitment.base_agreement(), Some(f.base.record_id()));
    183         assert_eq!(commitment.authorization(), f.authorization.record_id());
    184         assert_eq!(commitment.terms_digest(), f.command.input().terms_digest());
    185         assert_eq!(commitment.seller_sequence().get(), 2);
    186         assert_eq!(commitment.authority(), f.command.context().authority());
    187         assert_eq!(record.record_id(), rumor.record_id());
    188         assert_eq!(
    189             commerce::replacement_commitment_rumor(
    190                 &f.command,
    191                 &f.base,
    192                 &f.proposal,
    193                 &f.authorization,
    194                 f.head,
    195                 time(453)
    196             )
    197             .unwrap()
    198             .protected_wire(),
    199             rumor.protected_wire()
    200         );
    201     }
    202 }
    203 
    204 #[test]
    205 fn changed_base_sources_digest_sequence_authority_and_known_author_heads_refuse() {
    206     let f = fixture(false);
    207     let original: Value = serde_json::from_slice(f.command.protected_wire()).unwrap();
    208     for (path, value) in [
    209         ("/context/expected/agreement", json!("71".repeat(32))),
    210         ("/context/expected/agreement", Value::Null),
    211         ("/context/expected/seller_sequence", json!("2")),
    212         ("/context/expected/buyer_head", Value::Null),
    213         (
    214             "/context/expected/buyer_head",
    215             json!(f.proposal.record_id()),
    216         ),
    217         ("/context/authority/generation", json!("2")),
    218         ("/input/proposal", json!("72".repeat(32))),
    219         ("/input/authorization", json!("73".repeat(32))),
    220         ("/input/terms_digest", json!("74".repeat(32))),
    221     ] {
    222         let mut changed = original.clone();
    223         *changed.pointer_mut(path).unwrap() = value;
    224         let command =
    225             CapturedTermsInstallation::parse(&serde_json::to_vec(&changed).unwrap()).unwrap();
    226         assert!(
    227             commerce::replacement_commitment_rumor(
    228                 &command,
    229                 &f.base,
    230                 &f.proposal,
    231                 &f.authorization,
    232                 f.head,
    233                 time(453)
    234             )
    235             .is_err(),
    236             "{path}"
    237         );
    238     }
    239     for head in [
    240         (f.base.record_id(), Sequence::new(2).unwrap()),
    241         (f.authorization.record_id(), Sequence::new(2).unwrap()),
    242         (f.head.0, Sequence::new(1).unwrap()),
    243     ] {
    244         assert!(
    245             commerce::replacement_commitment_rumor(
    246                 &f.command,
    247                 &f.base,
    248                 &f.proposal,
    249                 &f.authorization,
    250                 head,
    251                 time(453)
    252             )
    253             .is_err()
    254         );
    255     }
    256     assert!(
    257         commerce::replacement_commitment_rumor(
    258             &f.command,
    259             &f.base,
    260             &f.authorization,
    261             &f.proposal,
    262             f.head,
    263             time(453)
    264         )
    265         .is_err()
    266     );
    267 }
    268 
    269 #[test]
    270 fn no_deadline_or_future_author_claim_is_replaced_with_ambient_time() {
    271     let f = fixture(true);
    272     for at in [0, 451, 600, 650, 700] {
    273         assert!(
    274             commerce::replacement_commitment_rumor(
    275                 &f.command,
    276                 &f.base,
    277                 &f.proposal,
    278                 &f.authorization,
    279                 f.head,
    280                 time(at)
    281             )
    282             .is_err(),
    283             "{at}"
    284         );
    285     }
    286     assert!(
    287         commerce::replacement_commitment_rumor(
    288             &f.command,
    289             &f.base,
    290             &f.proposal,
    291             &f.authorization,
    292             f.head,
    293             time(599)
    294         )
    295         .is_ok()
    296     );
    297 }