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_types::prelude::NewEntryAction;
use holochain_zome_types::prelude::*;
#[derive(Debug, Clone)]
pub struct ActivityTestData {
pub agent_activity_ops: Vec<ChainOpHashed>,
pub noise_agent_activity_ops: Vec<ChainOpHashed>,
pub store_entry_ops: Vec<ChainOpHashed>,
pub agent: AgentPubKey,
pub valid_hashes: ChainItems,
pub valid_records: ChainItems,
pub chain_head: ChainHead,
pub highest_observed: HighestObserved,
}
impl ActivityTestData {
pub fn valid_chain_scenario(private_entries: bool) -> Self {
let agent = fixt!(AgentPubKey);
let entry = Entry::App(fixt!(AppEntryBytes));
let entry_hash = EntryHash::with_data_sync(&entry);
let to_agent_activity_op =
|h, sig| ChainOpHashed::from_content_sync(ChainOp::RegisterAgentActivity(sig, h));
let to_record_and_op = |a: Action, sig: Signature| {
let op = ChainOpHashed::from_content_sync(ChainOp::StoreEntry(
sig.clone(),
match a {
Action::Create(ref c) => NewEntryAction::Create(c.clone()),
_ => unreachable!(),
},
entry.clone(),
));
let shh = SignedActionHashed::with_presigned(ActionHashed::from_content_sync(a), sig);
(Record::new(shh, Some(entry.clone())), op)
};
let to_record_dna_op = |a: Action, sig: 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_entry_ops = Vec::new();
let mut agent_activity_ops = Vec::new();
let mut dna = fixt!(Dna);
dna.author = agent.clone();
let dna = Action::Dna(dna);
let dna_sig = fixt!(Signature);
let (el, op) = to_record_dna_op(dna.clone(), dna_sig.clone());
valid_records.push(el);
store_entry_ops.push(op);
agent_activity_ops.push(to_agent_activity_op(dna.clone(), dna_sig));
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();
create.entry_type = EntryType::App(AppEntryDef::new(
1.into(),
1.into(),
if private_entries {
EntryVisibility::Private
} else {
EntryVisibility::Public
},
));
let action = Action::Create(create);
let sig = fixt!(Signature);
prev_hash = ActionHash::with_data_sync(&action);
agent_activity_ops.push(to_agent_activity_op(action.clone(), sig.clone()));
valid_hashes.push((action_seq, prev_hash.clone()));
let (el, op) = to_record_and_op(action, sig);
valid_records.push(el);
store_entry_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_agent_activity_ops = ActionFixturator::new(Unpredictable)
.take(50)
.map(|a| to_agent_activity_op(a, fixt!(Signature)))
.collect();
Self {
agent_activity_ops,
noise_agent_activity_ops,
store_entry_ops,
agent,
valid_hashes: ChainItems::Hashes(valid_hashes),
valid_records: ChainItems::Full(valid_records),
highest_observed,
chain_head,
}
}
}