holochain_types 0.7.0-rc.0

Holochain common types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! DHT state-model op types (see `docs/design/state_model.md`).

use holo_hash::{
    hash_type, ActionHash, AgentPubKey, AnyLinkableHash, DhtOpHash, EntryHash, HasHash,
    HashableContent, HashableContentBytes, HoloHashed,
};
use holochain_serialized_bytes::prelude::*;
use holochain_zome_types::op::ChainOpType;
use holochain_zome_types::prelude::*;
use holochain_zome_types::Entry;

/// How an entry is represented inside a `ChainOp`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
pub enum OpEntry {
    /// The entry is included with the op.
    Present(Entry),
    /// The action references a private entry, which is not included.
    Hidden,
    /// The action type doesn't have an associated entry.
    ActionOnly,
}

/// Chain-level DHT ops. Each variant targets a specific authority.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
pub enum ChainOp {
    /// Store an action record at the action's hash authority.
    CreateRecord(SignedAction, OpEntry),
    /// Store an entry (+ its create action) at the entry's hash authority.
    CreateEntry(SignedAction, OpEntry),
    /// Register activity on an agent's source chain at the agent authority.
    AgentActivity(SignedAction),
    /// Register an updated entry at the entry authority of the new entry.
    UpdateEntry(SignedAction, OpEntry),
    /// Register an updated record at the original action's authority.
    UpdateRecord(SignedAction, OpEntry),
    /// Register a delete-entry at the entry authority.
    DeleteEntry(SignedAction),
    /// Register a delete-record at the action authority.
    DeleteRecord(SignedAction),
    /// Register a link creation at the link base authority.
    CreateLink(SignedAction),
    /// Register a link deletion at the link base authority.
    DeleteLink(SignedAction),
}

/// Top-level DHT op.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
pub enum DhtOp {
    /// A chain-level op (record/entry/activity/link).
    ChainOp(Box<ChainOp>),
    /// A warrant op.
    WarrantOp(Box<crate::warrant::WarrantOp>),
}

impl From<ChainOp> for DhtOp {
    fn from(op: ChainOp) -> Self {
        DhtOp::ChainOp(Box::new(op))
    }
}

impl From<crate::warrant::WarrantOp> for DhtOp {
    fn from(op: crate::warrant::WarrantOp) -> Self {
        DhtOp::WarrantOp(Box::new(op))
    }
}

impl From<SignedWarrant> for DhtOp {
    fn from(op: SignedWarrant) -> Self {
        DhtOp::WarrantOp(Box::new(crate::warrant::WarrantOp::from(op)))
    }
}

/// The canonical, content-derived form a [`ChainOp`] is hashed over to produce
/// its [`DhtOpHash`].
///
/// Signatures and entries are deliberately excluded: the signature is redundant
/// with the action (and would make the hash unstable across re-signings), and
/// the entry is already addressed by the action's `entry_hash`. The enum
/// discriminant distinguishes ops that share an action — a `Create` produces
/// both a `CreateRecord` and a `CreateEntry` op — so one action yields distinct
/// op hashes per op type.
///
/// Every variant borrows `&Action` directly, because `Action` is a single flat
/// struct.
#[allow(missing_docs)]
#[derive(serde::Serialize, Debug)]
pub enum ChainOpUniqueForm<'a> {
    CreateRecord(&'a Action),
    CreateEntry(&'a Action),
    AgentActivity(&'a Action),
    UpdateEntry(&'a Action),
    UpdateRecord(&'a Action),
    DeleteEntry(&'a Action),
    DeleteRecord(&'a Action),
    CreateLink(&'a Action),
    DeleteLink(&'a Action),
}

impl HashableContent for ChainOpUniqueForm<'_> {
    type HashType = hash_type::DhtOp;

    fn hash_type(&self) -> Self::HashType {
        hash_type::DhtOp
    }

    fn hashable_content(&self) -> HashableContentBytes {
        HashableContentBytes::Content(
            UnsafeBytes::from(
                holochain_serialized_bytes::encode(self)
                    .expect("Could not serialize HashableContent"),
            )
            .into(),
        )
    }
}

impl ChainOp {
    /// Build a [`ChainOp`] from its op type, signed action, and the [`OpEntry`]
    /// to carry for the entry-bearing variants (the link, delete, and
    /// agent-activity variants ignore `op_entry`).
    pub fn from_type(
        op_type: holochain_zome_types::op::ChainOpType,
        signed_action: SignedAction,
        op_entry: OpEntry,
    ) -> Self {
        use holochain_zome_types::op::ChainOpType;
        match op_type {
            ChainOpType::CreateRecord => ChainOp::CreateRecord(signed_action, op_entry),
            ChainOpType::CreateEntry => ChainOp::CreateEntry(signed_action, op_entry),
            ChainOpType::AgentActivity => ChainOp::AgentActivity(signed_action),
            ChainOpType::UpdateEntry => ChainOp::UpdateEntry(signed_action, op_entry),
            ChainOpType::UpdateRecord => ChainOp::UpdateRecord(signed_action, op_entry),
            ChainOpType::DeleteEntry => ChainOp::DeleteEntry(signed_action),
            ChainOpType::DeleteRecord => ChainOp::DeleteRecord(signed_action),
            ChainOpType::CreateLink => ChainOp::CreateLink(signed_action),
            ChainOpType::DeleteLink => ChainOp::DeleteLink(signed_action),
        }
    }

    /// The content-derived [`DhtOpHash`] for this op.
    ///
    /// Excludes the signature and entry (see [`ChainOpUniqueForm`]), so it is
    /// reproducible from the op alone.
    pub fn to_hash(&self) -> DhtOpHash {
        DhtOpHash::with_data_sync(&self.to_unique_form())
    }

    fn to_unique_form(&self) -> ChainOpUniqueForm<'_> {
        match self {
            ChainOp::CreateRecord(sa, _) => ChainOpUniqueForm::CreateRecord(sa.data()),
            ChainOp::CreateEntry(sa, _) => ChainOpUniqueForm::CreateEntry(sa.data()),
            ChainOp::AgentActivity(sa) => ChainOpUniqueForm::AgentActivity(sa.data()),
            ChainOp::UpdateEntry(sa, _) => ChainOpUniqueForm::UpdateEntry(sa.data()),
            ChainOp::UpdateRecord(sa, _) => ChainOpUniqueForm::UpdateRecord(sa.data()),
            ChainOp::DeleteEntry(sa) => ChainOpUniqueForm::DeleteEntry(sa.data()),
            ChainOp::DeleteRecord(sa) => ChainOpUniqueForm::DeleteRecord(sa.data()),
            ChainOp::CreateLink(sa) => ChainOpUniqueForm::CreateLink(sa.data()),
            ChainOp::DeleteLink(sa) => ChainOpUniqueForm::DeleteLink(sa.data()),
        }
    }

    /// The [`ChainOpType`] discriminant of this op.
    pub fn op_type(&self) -> ChainOpType {
        match self {
            ChainOp::CreateRecord(..) => ChainOpType::CreateRecord,
            ChainOp::CreateEntry(..) => ChainOpType::CreateEntry,
            ChainOp::AgentActivity(..) => ChainOpType::AgentActivity,
            ChainOp::UpdateEntry(..) => ChainOpType::UpdateEntry,
            ChainOp::UpdateRecord(..) => ChainOpType::UpdateRecord,
            ChainOp::DeleteEntry(..) => ChainOpType::DeleteEntry,
            ChainOp::DeleteRecord(..) => ChainOpType::DeleteRecord,
            ChainOp::CreateLink(..) => ChainOpType::CreateLink,
            ChainOp::DeleteLink(..) => ChainOpType::DeleteLink,
        }
    }

    /// The signed action carried by this op.
    pub fn signed_action(&self) -> &SignedAction {
        match self {
            ChainOp::CreateRecord(sa, _)
            | ChainOp::CreateEntry(sa, _)
            | ChainOp::UpdateEntry(sa, _)
            | ChainOp::UpdateRecord(sa, _) => sa,
            ChainOp::AgentActivity(sa)
            | ChainOp::DeleteEntry(sa)
            | ChainOp::DeleteRecord(sa)
            | ChainOp::CreateLink(sa)
            | ChainOp::DeleteLink(sa) => sa,
        }
    }

    /// The op's entry payload, for the variants that carry one.
    pub fn op_entry(&self) -> Option<&OpEntry> {
        match self {
            ChainOp::CreateRecord(_, e)
            | ChainOp::CreateEntry(_, e)
            | ChainOp::UpdateEntry(_, e)
            | ChainOp::UpdateRecord(_, e) => Some(e),
            ChainOp::AgentActivity(_)
            | ChainOp::DeleteEntry(_)
            | ChainOp::DeleteRecord(_)
            | ChainOp::CreateLink(_)
            | ChainOp::DeleteLink(_) => None,
        }
    }

    /// The DHT basis where this op is stored.
    pub fn dht_basis(&self) -> AnyLinkableHash {
        let action = self.signed_action().data();
        let action_hash = ActionHash::with_data_sync(action);
        op_basis(self.op_type(), &action_hash, action)
            .expect("op_basis is total over an op paired with its own action")
    }

    /// Enzymatic countersigning session ops need special handling so that
    /// they arrive at the enzyme and not elsewhere. `None` when this isn't
    /// an enzymatic countersigning session, so it doubles as a boolean via
    /// `is_some()`.
    pub fn enzymatic_countersigning_enzyme(&self) -> Option<&AgentPubKey> {
        let OpEntry::Present(entry) = self.op_entry()? else {
            return None;
        };
        let Entry::CounterSign(session_data, _) = entry else {
            return None;
        };
        if session_data.preflight_request().enzymatic {
            session_data
                .preflight_request()
                .signing_agents
                .first()
                .map(|(pubkey, _)| pubkey)
        } else {
            None
        }
    }
}

impl DhtOp {
    /// The content-derived [`DhtOpHash`] for this op.
    pub fn to_hash(&self) -> DhtOpHash {
        match self {
            DhtOp::ChainOp(op) => op.to_hash(),
            // `WarrantOp` implements `HashableContent` directly.
            DhtOp::WarrantOp(op) => DhtOpHash::with_data_sync(op.as_ref()),
        }
    }

    /// The DHT basis where this op is stored.
    pub fn dht_basis(&self) -> AnyLinkableHash {
        match self {
            DhtOp::ChainOp(op) => op.dht_basis(),
            DhtOp::WarrantOp(op) => op.data().warrantee.clone().into(),
        }
    }
}

/// A [`ChainOp`] paired with its [`DhtOpHash`].
pub type ChainOpHashed = HoloHashed<ChainOp>;

/// A [`DhtOp`] paired with its [`DhtOpHash`].
pub type DhtOpHashed = HoloHashed<DhtOp>;

impl HashableContent for ChainOp {
    type HashType = hash_type::DhtOp;

    fn hash_type(&self) -> Self::HashType {
        hash_type::DhtOp
    }

    fn hashable_content(&self) -> HashableContentBytes {
        // `to_hash` already routes through `ChainOpUniqueForm`; reuse it
        // rather than re-serializing, mirroring `HoloHashed`'s own
        // prehashed-content pattern.
        HashableContentBytes::Prehashed39(self.to_hash().get_raw_39().to_vec())
    }
}

impl HashableContent for DhtOp {
    type HashType = hash_type::DhtOp;

    fn hash_type(&self) -> Self::HashType {
        hash_type::DhtOp
    }

    fn hashable_content(&self) -> HashableContentBytes {
        HashableContentBytes::Prehashed39(self.to_hash().get_raw_39().to_vec())
    }
}

impl ChainOpUniqueForm<'_> {
    /// The content-derived [`DhtOpHash`] for an op of `op_type` carrying
    /// `action`, without needing a full [`ChainOp`] in hand.
    ///
    /// Agrees with [`ChainOp::to_hash`] for the corresponding op; used when
    /// producing the several ops a single record generates.
    pub fn op_hash(op_type: ChainOpType, action: &Action) -> DhtOpHash {
        let form = match op_type {
            ChainOpType::CreateRecord => ChainOpUniqueForm::CreateRecord(action),
            ChainOpType::CreateEntry => ChainOpUniqueForm::CreateEntry(action),
            ChainOpType::AgentActivity => ChainOpUniqueForm::AgentActivity(action),
            ChainOpType::UpdateEntry => ChainOpUniqueForm::UpdateEntry(action),
            ChainOpType::UpdateRecord => ChainOpUniqueForm::UpdateRecord(action),
            ChainOpType::DeleteRecord => ChainOpUniqueForm::DeleteRecord(action),
            ChainOpType::DeleteEntry => ChainOpUniqueForm::DeleteEntry(action),
            ChainOpType::CreateLink => ChainOpUniqueForm::CreateLink(action),
            ChainOpType::DeleteLink => ChainOpUniqueForm::DeleteLink(action),
        };
        DhtOpHash::with_data_sync(&form)
    }
}

/// The set of op types a record with this action produces.
///
/// Matched over the action's [`ActionData`].
pub fn action_to_op_types(action: &Action) -> Vec<ChainOpType> {
    use ChainOpType::*;
    match &action.data {
        ActionData::Dna(_)
        | ActionData::OpenChain(_)
        | ActionData::CloseChain(_)
        | ActionData::AgentValidationPkg(_)
        | ActionData::InitZomesComplete(_) => vec![CreateRecord, AgentActivity],
        ActionData::CreateLink(_) => vec![CreateRecord, AgentActivity, CreateLink],
        ActionData::DeleteLink(_) => vec![CreateRecord, AgentActivity, DeleteLink],
        ActionData::Create(_) => vec![CreateRecord, AgentActivity, CreateEntry],
        ActionData::Update(_) => vec![
            CreateRecord,
            AgentActivity,
            CreateEntry,
            UpdateEntry,
            UpdateRecord,
        ],
        ActionData::Delete(_) => vec![CreateRecord, AgentActivity, DeleteRecord, DeleteEntry],
    }
}

/// The DHT basis where an op of `op_type` for `action` (hashed as
/// `action_hash`) is stored.
///
/// Returns `None` only for an `op_type` that does not match the action's data,
/// which [`action_to_op_types`] never emits.
fn op_basis(
    op_type: ChainOpType,
    action_hash: &ActionHash,
    action: &Action,
) -> Option<AnyLinkableHash> {
    use ChainOpType::*;
    Some(match (op_type, &action.data) {
        (CreateRecord, _) => action_hash.clone().into(),
        (AgentActivity, _) => action.header.author.clone().into(),
        (CreateEntry, ActionData::Create(d)) => d.entry_hash.clone().into(),
        (CreateEntry, ActionData::Update(d)) => d.entry_hash.clone().into(),
        (UpdateEntry, ActionData::Update(d)) => d.original_entry_address.clone().into(),
        (UpdateRecord, ActionData::Update(d)) => d.original_action_address.clone().into(),
        (DeleteRecord, ActionData::Delete(d)) => d.deletes_address.clone().into(),
        (DeleteEntry, ActionData::Delete(d)) => d.deletes_entry_address.clone().into(),
        (CreateLink, ActionData::CreateLink(d)) => d.base_address.clone(),
        (DeleteLink, ActionData::DeleteLink(d)) => d.base_address.clone(),
        _ => return None,
    })
}

/// Produce the [`HashedChainOp`]s a [`Record`] generates, with all hashes,
/// bases, and storage locations pre-computed.
///
/// The `CreateEntry` op is omitted whenever the entry is not present — whether
/// because it is private (`Hidden`), inapplicable (`NA`), or held without its
/// body (`NotStored`). Separately, the entry-bearing ops (`op_carries_entry`)
/// carry the entry payload only when it is present.
pub fn produce_ops_from_record(record: &Record) -> Vec<HashedChainOp> {
    let action = record.action();
    let action_hash = record.action_address();
    let entry = record.entry.as_option();

    let mut ops = Vec::new();
    for op_type in action_to_op_types(action) {
        // The store-entry op carries nothing without the entry.
        if op_type == ChainOpType::CreateEntry && entry.is_none() {
            continue;
        }
        let Some(basis_hash) = op_basis(op_type, action_hash, action) else {
            continue;
        };
        let op_entry = if op_carries_entry(op_type) {
            // Re-hashed from content; for a valid record this equals the
            // action's `entry_hash`.
            entry.map(|e| HoloHashed::from_content_sync(e.clone()))
        } else {
            None
        };
        ops.push(HashedChainOp {
            op_hash: ChainOpUniqueForm::op_hash(op_type, action),
            action: record.signed_action.clone(),
            entry: op_entry,
            op_type,
            storage_center_loc: basis_hash.get_loc(),
            basis_hash,
        });
    }
    ops
}

/// Whether an op of this type carries the record's entry payload.
fn op_carries_entry(op_type: ChainOpType) -> bool {
    matches!(
        op_type,
        ChainOpType::CreateRecord
            | ChainOpType::CreateEntry
            | ChainOpType::UpdateEntry
            | ChainOpType::UpdateRecord
    )
}

/// Internal representation of a `ChainOp` with all hashes pre-computed.
/// Used during the incoming-ops workflow so hashes aren't recomputed for
/// each database write.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HashedChainOp {
    /// The hash of this op.
    pub op_hash: DhtOpHash,
    /// The signed action with its pre-computed hash.
    pub action: SignedActionHashed,
    /// The entry (if any) with its pre-computed hash.
    pub entry: Option<HoloHashed<Entry>>,
    /// The type discriminant of the op.
    pub op_type: ChainOpType,
    /// The DHT basis hash (where the op is stored).
    ///
    /// `AnyLinkableHash`, not `AnyDhtHash`: link-op bases may be `External`
    /// hashes, which `AnyDhtHash` cannot hold (matches `InsertChainOp`).
    pub basis_hash: AnyLinkableHash,
    /// The numeric storage center derived from `basis_hash`.
    pub storage_center_loc: u32,
}

impl HashedChainOp {
    /// Build a [`HashedChainOp`] for a single op type from a signed action and
    /// optional entry, computing the op hash and DHT basis.
    ///
    /// Returns `None` when `op_type` does not correspond to the action's data
    /// (a mismatched op that [`action_to_op_types`] would never emit).
    pub fn from_signed_action(
        action: SignedActionHashed,
        op_type: ChainOpType,
        entry: Option<HoloHashed<Entry>>,
    ) -> Option<Self> {
        let op_hash = ChainOpUniqueForm::op_hash(op_type, action.action());
        let basis_hash = op_basis(op_type, action.as_hash(), action.action())?;
        Some(Self {
            op_hash,
            action,
            entry,
            op_type,
            storage_center_loc: basis_hash.get_loc(),
            basis_hash,
        })
    }

    /// Return the action hash of the wrapped signed action.
    pub fn action_hash(&self) -> &ActionHash {
        self.action.as_hash()
    }

    /// Return the entry hash if this op carries an entry.
    pub fn entry_hash(&self) -> Option<&EntryHash> {
        self.entry.as_ref().map(|e| e.as_hash())
    }
}

#[cfg(test)]
mod op_hash_tests {
    use super::*;
    use holo_hash::{ActionHash, AgentPubKey, EntryHash};
    use holochain_timestamp::Timestamp;
    use holochain_zome_types::prelude::{AppEntryDef, EntryType, EntryVisibility};
    use holochain_zome_types::signature::Signature;
    use holochain_zome_types::Entry;

    fn create_action() -> Action {
        Action {
            header: ActionHeader {
                author: AgentPubKey::from_raw_36(vec![1u8; 36]),
                timestamp: Timestamp::from_micros(1_000),
                action_seq: 4,
                prev_action: Some(ActionHash::from_raw_36(vec![2u8; 36])),
            },
            data: ActionData::Create(CreateData {
                entry_type: EntryType::App(AppEntryDef::new(
                    0.into(),
                    0.into(),
                    EntryVisibility::Public,
                )),
                entry_hash: EntryHash::from_raw_36(vec![3u8; 36]),
            }),
        }
    }

    fn signed(action: Action, sig: u8) -> SignedAction {
        SignedAction::new(action, Signature::from([sig; 64]))
    }

    #[test]
    fn op_hash_is_deterministic() {
        // Two independently-built ops with identical content hash equally.
        let a = ChainOp::CreateRecord(signed(create_action(), 7), OpEntry::ActionOnly);
        let b = ChainOp::CreateRecord(signed(create_action(), 7), OpEntry::ActionOnly);
        assert_eq!(a.to_hash(), b.to_hash());
    }

    #[test]
    fn op_hash_differs_by_op_type() {
        let sa = signed(create_action(), 7);
        let record = ChainOp::CreateRecord(sa.clone(), OpEntry::ActionOnly);
        let entry = ChainOp::CreateEntry(sa, OpEntry::ActionOnly);
        assert_ne!(record.to_hash(), entry.to_hash());
    }

    #[test]
    fn op_hash_ignores_signature_and_entry() {
        let action = create_action();
        let with_sig_7 = ChainOp::CreateRecord(signed(action.clone(), 7), OpEntry::ActionOnly);
        let with_sig_9_and_entry = ChainOp::CreateRecord(
            signed(action, 9),
            OpEntry::Present(Entry::Agent(AgentPubKey::from_raw_36(vec![5u8; 36]))),
        );
        assert_eq!(with_sig_7.to_hash(), with_sig_9_and_entry.to_hash());
    }

    #[test]
    fn op_hash_is_content_derived() {
        let base = ChainOp::CreateRecord(signed(create_action(), 7), OpEntry::ActionOnly);
        let mut changed_action = create_action();
        changed_action.header.action_seq = 99;
        let changed = ChainOp::CreateRecord(signed(changed_action, 7), OpEntry::ActionOnly);
        assert_ne!(base.to_hash(), changed.to_hash());
    }

    #[test]
    fn op_hash_entry_point_matches_chain_op_to_hash() {
        let sa = signed(create_action(), 7);
        // Cover every variant: a `ChainOp` accepts any `SignedAction`
        // regardless of the action's content, so the same `sa` exercises the
        // full `to_unique_form` / `op_hash` mapping.
        let cases = [
            (
                ChainOp::CreateRecord(sa.clone(), OpEntry::ActionOnly),
                ChainOpType::CreateRecord,
            ),
            (
                ChainOp::CreateEntry(sa.clone(), OpEntry::ActionOnly),
                ChainOpType::CreateEntry,
            ),
            (
                ChainOp::AgentActivity(sa.clone()),
                ChainOpType::AgentActivity,
            ),
            (
                ChainOp::UpdateEntry(sa.clone(), OpEntry::ActionOnly),
                ChainOpType::UpdateEntry,
            ),
            (
                ChainOp::UpdateRecord(sa.clone(), OpEntry::ActionOnly),
                ChainOpType::UpdateRecord,
            ),
            (ChainOp::DeleteRecord(sa.clone()), ChainOpType::DeleteRecord),
            (ChainOp::DeleteEntry(sa.clone()), ChainOpType::DeleteEntry),
            (ChainOp::CreateLink(sa.clone()), ChainOpType::CreateLink),
            (ChainOp::DeleteLink(sa.clone()), ChainOpType::DeleteLink),
        ];
        for (op, op_type) in cases {
            assert_eq!(op.to_hash(), ChainOpUniqueForm::op_hash(op_type, sa.data()));
        }
    }

    #[test]
    fn dht_op_hash_and_basis_delegate_to_chain_op() {
        let sa = signed(create_action(), 7);
        let chain_op = ChainOp::CreateRecord(sa.clone(), OpEntry::ActionOnly);
        let dht_op = DhtOp::ChainOp(Box::new(chain_op.clone()));

        // DhtOp::to_hash agrees with ChainOp::to_hash.
        assert_eq!(dht_op.to_hash(), chain_op.to_hash());

        // CreateRecord basis is the action hash.
        let action_hash = ActionHash::with_data_sync(sa.data());
        assert_eq!(dht_op.dht_basis(), AnyLinkableHash::from(action_hash));
        assert_eq!(dht_op.dht_basis(), chain_op.dht_basis());
    }

    #[test]
    fn action_to_op_types_create_produces_record_activity_entry() {
        let action = create_action();
        assert_eq!(
            action_to_op_types(&action),
            vec![
                ChainOpType::CreateRecord,
                ChainOpType::AgentActivity,
                ChainOpType::CreateEntry,
            ]
        );
    }

    #[test]
    fn op_basis_uses_action_hash_for_store_record_and_entry_hash_for_store_entry() {
        use holo_hash::AnyLinkableHash;
        let action = create_action();
        let action_hash = ActionHash::from_raw_36(vec![8u8; 36]);

        let record_basis = op_basis(ChainOpType::CreateRecord, &action_hash, &action).unwrap();
        assert_eq!(record_basis, AnyLinkableHash::from(action_hash.clone()));

        let entry_basis = op_basis(ChainOpType::CreateEntry, &action_hash, &action).unwrap();
        assert_eq!(
            entry_basis,
            AnyLinkableHash::from(EntryHash::from_raw_36(vec![3u8; 36]))
        );
    }
}

#[cfg(test)]
mod produce_ops_tests {
    use super::*;
    use holo_hash::{ActionHash, AgentPubKey, EntryHash, HoloHashed};
    use holochain_timestamp::Timestamp;
    use holochain_zome_types::prelude::{AppEntryDef, EntryType, EntryVisibility};
    use holochain_zome_types::record::{Record, RecordEntry, SignedHashed};
    use holochain_zome_types::signature::Signature;
    use holochain_zome_types::Entry;

    fn create_action() -> Action {
        Action {
            header: ActionHeader {
                author: AgentPubKey::from_raw_36(vec![1u8; 36]),
                timestamp: Timestamp::from_micros(1_000),
                action_seq: 4,
                prev_action: Some(ActionHash::from_raw_36(vec![2u8; 36])),
            },
            data: ActionData::Create(CreateData {
                entry_type: EntryType::App(AppEntryDef::new(
                    0.into(),
                    0.into(),
                    EntryVisibility::Public,
                )),
                entry_hash: EntryHash::from_raw_36(vec![3u8; 36]),
            }),
        }
    }

    fn update_action() -> Action {
        Action {
            header: ActionHeader {
                author: AgentPubKey::from_raw_36(vec![1u8; 36]),
                timestamp: Timestamp::from_micros(2_000),
                action_seq: 5,
                prev_action: Some(ActionHash::from_raw_36(vec![2u8; 36])),
            },
            data: ActionData::Update(UpdateData {
                original_action_address: ActionHash::from_raw_36(vec![6u8; 36]),
                original_entry_address: EntryHash::from_raw_36(vec![7u8; 36]),
                entry_type: EntryType::App(AppEntryDef::new(
                    0.into(),
                    0.into(),
                    EntryVisibility::Public,
                )),
                entry_hash: EntryHash::from_raw_36(vec![3u8; 36]),
            }),
        }
    }

    fn record(action: Action, entry: RecordEntry<Entry>) -> Record {
        let hashed = HoloHashed::with_pre_hashed(action, ActionHash::from_raw_36(vec![9u8; 36]));
        Record::new(
            SignedHashed::with_presigned(hashed, Signature([7u8; 64])),
            entry,
        )
    }

    fn op_types(ops: &[HashedChainOp]) -> Vec<ChainOpType> {
        ops.iter().map(|o| o.op_type).collect()
    }

    #[test]
    fn create_with_public_entry_produces_record_activity_entry() {
        let entry = Entry::Agent(AgentPubKey::from_raw_36(vec![5u8; 36]));
        let ops = produce_ops_from_record(&record(create_action(), RecordEntry::Present(entry)));
        assert_eq!(
            op_types(&ops),
            vec![
                ChainOpType::CreateRecord,
                ChainOpType::AgentActivity,
                ChainOpType::CreateEntry,
            ]
        );
        let by_type = |t| ops.iter().find(|o| o.op_type == t).unwrap();
        assert!(by_type(ChainOpType::CreateRecord).entry.is_some());
        assert!(by_type(ChainOpType::CreateEntry).entry.is_some());
        assert!(by_type(ChainOpType::AgentActivity).entry.is_none());
    }

    #[test]
    fn create_with_hidden_entry_skips_store_entry_and_omits_payload() {
        let ops = produce_ops_from_record(&record(create_action(), RecordEntry::Hidden));
        assert_eq!(
            op_types(&ops),
            vec![ChainOpType::CreateRecord, ChainOpType::AgentActivity]
        );
        let store_record = ops
            .iter()
            .find(|o| o.op_type == ChainOpType::CreateRecord)
            .unwrap();
        assert!(store_record.entry.is_none());
    }

    #[test]
    fn store_record_basis_is_the_action_hash() {
        use holo_hash::AnyLinkableHash;
        let r = record(create_action(), RecordEntry::Hidden);
        let action_hash = r.action_address().clone();
        let ops = produce_ops_from_record(&r);
        let store_record = ops
            .iter()
            .find(|o| o.op_type == ChainOpType::CreateRecord)
            .unwrap();
        assert_eq!(store_record.basis_hash, AnyLinkableHash::from(action_hash));
    }

    #[test]
    fn update_with_public_entry_produces_full_op_set_with_update_payloads() {
        use holo_hash::AnyLinkableHash;
        let entry = Entry::Agent(AgentPubKey::from_raw_36(vec![5u8; 36]));
        let ops = produce_ops_from_record(&record(update_action(), RecordEntry::Present(entry)));
        assert_eq!(
            op_types(&ops),
            vec![
                ChainOpType::CreateRecord,
                ChainOpType::AgentActivity,
                ChainOpType::CreateEntry,
                ChainOpType::UpdateEntry,
                ChainOpType::UpdateRecord,
            ]
        );
        let by_type = |t| ops.iter().find(|o| o.op_type == t).unwrap();
        // The update ops are entry-bearing and carry the new entry.
        assert!(by_type(ChainOpType::UpdateEntry).entry.is_some());
        assert!(by_type(ChainOpType::UpdateRecord).entry.is_some());
        assert!(by_type(ChainOpType::AgentActivity).entry.is_none());
        // Update bases: content → original entry, record → original action.
        assert_eq!(
            by_type(ChainOpType::UpdateEntry).basis_hash,
            AnyLinkableHash::from(EntryHash::from_raw_36(vec![7u8; 36]))
        );
        assert_eq!(
            by_type(ChainOpType::UpdateRecord).basis_hash,
            AnyLinkableHash::from(ActionHash::from_raw_36(vec![6u8; 36]))
        );
    }
}