use ::fixt::prelude::*;
use holo_hash::ActionHash;
use holo_hash::AgentPubKey;
use holo_hash::EntryHash;
use holochain_types::activity::ChainItems;
use holochain_types::dht_op::ChainOp;
use holochain_types::dht_op::ChainOpHashed;
use holochain_zome_types::prelude::*;
#[derive(Debug)]
pub struct ActivityTestData {
pub hash_ops: Vec<ChainOpHashed>,
pub noise_ops: Vec<ChainOpHashed>,
pub store_ops: Vec<ChainOpHashed>,
pub agent: AgentPubKey,
pub valid_hashes: ChainItems<ActionHash>,
pub valid_records: ChainItems<Record>,
pub chain_head: ChainHead,
pub highest_observed: HighestObserved,
}
impl ActivityTestData {
pub fn valid_chain_scenario() -> Self {
let agent = fixt!(AgentPubKey);
let entry = Entry::App(fixt!(AppEntryBytes));
let entry_hash = EntryHash::with_data_sync(&entry);
let to_op = |h| {
ChainOpHashed::from_content_sync(ChainOp::RegisterAgentActivity(fixt!(Signature), h))
};
let to_record_and_op = |h: Action| {
let sig = fixt!(Signature);
let op = ChainOpHashed::from_content_sync(ChainOp::StoreRecord(
sig.clone(),
h.clone(),
entry.clone().into(),
));
let shh = SignedActionHashed::with_presigned(ActionHashed::from_content_sync(h), sig);
(Record::new(shh, Some(entry.clone())), op)
};
let to_record_dna_op = |a: Action| {
let sig = fixt!(Signature);
let op = ChainOpHashed::from_content_sync(ChainOp::StoreRecord(
sig.clone(),
a.clone(),
RecordEntry::NA,
));
let shh = SignedActionHashed::with_presigned(ActionHashed::from_content_sync(a), sig);
(Record::new(shh, None), op)
};
let mut valid_hashes = Vec::new();
let mut valid_records = Vec::new();
let mut store_ops = Vec::new();
let mut hash_ops = Vec::new();
let mut dna = fixt!(Dna);
dna.author = agent.clone();
let dna = Action::Dna(dna);
let (el, op) = to_record_dna_op(dna.clone());
valid_records.push(el);
store_ops.push(op);
hash_ops.push(to_op(dna.clone()));
let creates: Vec<_> = CreateFixturator::new(Unpredictable)
.enumerate()
.take(50)
.collect();
let mut prev_hash = ActionHash::with_data_sync(&dna);
valid_hashes.push((0, prev_hash.clone()));
for (seq, mut create) in creates {
let action_seq = (seq + 1) as u32;
create.author = agent.clone();
create.action_seq = action_seq;
create.prev_action = prev_hash.clone();
create.entry_hash = entry_hash.clone();
let action = Action::Create(create);
prev_hash = ActionHash::with_data_sync(&action);
hash_ops.push(to_op(action.clone()));
valid_hashes.push((action_seq, prev_hash.clone()));
let (el, op) = to_record_and_op(action);
valid_records.push(el);
store_ops.push(op);
}
let last = valid_hashes.last().unwrap();
let chain_head = ChainHead {
action_seq: last.0,
hash: last.1.clone(),
};
let highest_observed = HighestObserved {
action_seq: last.0,
hash: vec![last.1.clone()],
};
let noise_ops = ActionFixturator::new(Unpredictable)
.take(50)
.map(to_op)
.collect();
Self {
hash_ops,
agent,
valid_hashes: ChainItems::Hashes(valid_hashes),
highest_observed,
chain_head,
noise_ops,
store_ops,
valid_records: ChainItems::Full(valid_records),
}
}
}