Skip to content

Commit

Permalink
Merge pull request #42 from mattyg/build/upgrade-holochain-0.2
Browse files Browse the repository at this point in the history
Build/upgrade holochain 0.2
  • Loading branch information
mattyg authored Aug 1, 2023
2 parents 2262898 + 2e06f5a commit 3c3982b
Show file tree
Hide file tree
Showing 31 changed files with 1,758 additions and 1,174 deletions.
559 changes: 530 additions & 29 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ opt-level = "z"
members = ["dnas/*/zomes/coordinator/*", "dnas/*/zomes/integrity/*"]

[workspace.dependencies]
hdi = "=0.2.0"
hdk = "=0.1.0"
hdi = "=0.3.1"
hdk = "=0.2.1"
serde = "1"

[workspace.dependencies.directory]
Expand Down
17 changes: 10 additions & 7 deletions dnas/directory/zomes/coordinator/directory/src/all_listings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hdk::prelude::{*, holo_hash::DnaHash};
use directory_integrity::*;
use hdk::prelude::{holo_hash::DnaHash, *};

#[derive(Serialize, Deserialize, SerializedBytes, Debug, Clone)]
pub struct GetListingsInput {
Expand All @@ -9,7 +9,7 @@ pub struct GetListingsInput {

#[hdk_extern]
pub fn get_listings(input: GetListingsInput) -> ExternResult<Vec<(DnaHash, ActionHash)>> {
let mut records: Vec<Record> = vec!();
let mut records: Vec<Record> = vec![];

if input.include_public {
let mut public_listing_records = get_public_listings()?;
Expand All @@ -23,10 +23,11 @@ pub fn get_listings(input: GetListingsInput) -> ExternResult<Vec<(DnaHash, Actio

records.sort_by_key(|r| r.action_hashed().timestamp());

let listing_dna_hashes: Vec<DnaHash> = records.clone()
let listing_dna_hashes: Vec<DnaHash> = records
.clone()
.into_iter()
.filter_map(|r| r.entry.to_app_option().ok())
.filter_map(|r| r)
.flatten()
.map(|listing: Listing| listing.dna)
.collect();

Expand All @@ -50,10 +51,12 @@ fn get_public_listings() -> ExternResult<Vec<Record>> {

let get_input: Vec<GetInput> = links
.into_iter()
.map(|link| GetInput::new(ActionHash::from(link.target).into(), GetOptions::default()))
.filter_map(|link| ActionHash::try_from(link.target).ok())
.map(|target| GetInput::new(target.into(), GetOptions::default()))
.collect();

let records: Vec<Record> = HDK.with(|hdk| hdk.borrow().get(get_input))?
let records: Vec<Record> = HDK
.with(|hdk| hdk.borrow().get(get_input))?
.into_iter()
.flatten()
.collect();
Expand All @@ -70,4 +73,4 @@ pub fn query_private_listings() -> ExternResult<Vec<Record>> {
let records = query(filter)?;

Ok(records)
}
}
82 changes: 48 additions & 34 deletions dnas/directory/zomes/coordinator/directory/src/listing.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
use hdk::prelude::*;
use directory_integrity::*;
use crate::all_listings::*;
use directory_integrity::*;
use hdk::prelude::*;

#[hdk_extern]
pub fn create_private_listing_idempotent(listing: PrivateListing) -> ExternResult<ActionHash> {
let private_listings = query_private_listings()?;

let identical_listings: Vec<Record> = private_listings
.into_iter()
.filter(|r| r.entry().to_app_option().ok().unwrap().eq(&Some(listing.clone())))
.filter(|r| {
r.entry()
.to_app_option()
.ok()
.unwrap()
.eq(&Some(listing.clone()))
})
.collect();

match identical_listings.len() > 0 {
true => Ok(identical_listings.first().unwrap().action_hashed().hash.clone()),
false => create_entry(EntryTypes::PrivateListing(listing.clone())),
match identical_listings.is_empty() {
true => create_entry(EntryTypes::PrivateListing(listing)),
false => Ok(identical_listings
.first()
.unwrap()
.action_hashed()
.hash
.clone()),
}
}

#[hdk_extern]
pub fn create_listing(listing: Listing) -> ExternResult<ActionHash> {
let listing_hash = create_entry(&EntryTypes::Listing(listing.clone()))?;
let listing_hash = create_entry(&EntryTypes::Listing(listing))?;

let path = Path::from("all_listings");
create_link(path.path_entry_hash()?, listing_hash.clone(), LinkTypes::AllListings, ())?;
create_link(
path.path_entry_hash()?,
listing_hash.clone(),
LinkTypes::AllListings,
(),
)?;

Ok(listing_hash)
}

#[hdk_extern]
pub fn get_listing(original_listing_hash: ActionHash) -> ExternResult<Option<Record>> {
let links = get_links(original_listing_hash.clone(), LinkTypes::ListingUpdates, None)?;
let links = get_links(
original_listing_hash.clone(),
LinkTypes::ListingUpdates,
None,
)?;
let latest_link = links
.into_iter()
.max_by(|link_a, link_b| link_b.timestamp.cmp(&link_a.timestamp));
let latest_listing_hash = match latest_link {
Some(link) => ActionHash::from(link.target.clone()),
None => original_listing_hash.clone(),
Some(link) => ActionHash::try_from(link.target).map_err(|e| wasm_error!(e))?,
None => original_listing_hash,
};
get(latest_listing_hash, GetOptions::default())
}
Expand All @@ -48,19 +68,17 @@ pub struct UpdateListingInput {
}
#[hdk_extern]
pub fn update_listing(input: UpdateListingInput) -> ExternResult<Record> {
let updated_listing_hash = update_entry(
input.previous_listing_hash.clone(),
&input.updated_listing
)?;
let updated_listing_hash =
update_entry(input.previous_listing_hash.clone(), &input.updated_listing)?;
create_link(
input.original_listing_hash.clone(),
input.original_listing_hash,
updated_listing_hash.clone(),
LinkTypes::ListingUpdates,
()
)?;
let record = get(updated_listing_hash.clone(), GetOptions::default())?.ok_or(
wasm_error!(WasmErrorInner::Guest(String::from("Could not find the newly updated Listing")))
(),
)?;
let record = get(updated_listing_hash, GetOptions::default())?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from("Could not find the newly updated Listing"))
))?;
Ok(record)
}
#[hdk_extern]
Expand All @@ -70,27 +88,23 @@ pub fn delete_listing(original_listing_hash: ActionHash) -> ExternResult<ActionH

#[hdk_extern]
pub fn listing_to_bubble_babble(listing: Listing) -> ExternResult<String> {
let bytes = SerializedBytes::try_from(listing).map_err(|e|
wasm_error!(WasmErrorInner::Guest(e.into()))
)?;
let bytes = SerializedBytes::try_from(listing)
.map_err(|e| wasm_error!(WasmErrorInner::Guest(e.into())))?;
let babble = boba::encode(bytes.bytes());

Ok(babble)
}

#[hdk_extern]
pub fn bubble_babble_to_listing(babble: String) -> ExternResult<Listing> {
let bytes = boba
::decode(babble)
.map_err(|_|
wasm_error!(
WasmErrorInner::Guest("Failed to decode bytes from provided bubble babble".into())
)
)?;
let bytes = boba::decode(babble).map_err(|_| {
wasm_error!(WasmErrorInner::Guest(
"Failed to decode bytes from provided bubble babble".into()
))
})?;
let serialized_bytes = SerializedBytes::try_from(UnsafeBytes::from(bytes))?;
let listing = Listing::try_from(serialized_bytes).map_err(|e|
wasm_error!(WasmErrorInner::Guest(e.into()))
)?;
let listing = Listing::try_from(serialized_bytes)
.map_err(|e| wasm_error!(WasmErrorInner::Guest(e.into())))?;

Ok(listing)
}
}
2 changes: 1 addition & 1 deletion dnas/directory/zomes/coordinator/profiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ hdk = { workspace = true }
serde = { workspace = true }

profiles_integrity = { workspace = true }
hc_zome_profiles_coordinator = "0.1.2"
hc_zome_profiles_coordinator = "0.2.0"
2 changes: 1 addition & 1 deletion dnas/directory/zomes/coordinator/profiles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
extern crate hc_zome_profiles_coordinator;
extern crate hc_zome_profiles_coordinator;
43 changes: 24 additions & 19 deletions dnas/directory/zomes/integrity/directory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod listing;
pub use listing::*;
use hdi::prelude::*;
pub use listing::*;

#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
Expand All @@ -19,9 +19,7 @@ pub enum LinkTypes {
// Validation you perform during the genesis process. Nobody else on the network performs it, only you.
// There *is no* access to network calls in this callback
#[hdk_extern]
pub fn genesis_self_check(
_data: GenesisSelfCheckData,
) -> ExternResult<ValidateCallbackResult> {
pub fn genesis_self_check(_data: GenesisSelfCheckData) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
}
// Validation the network performs when you try to join, you can't perform this validation yourself as you are not a member yet.
Expand Down Expand Up @@ -56,8 +54,8 @@ pub fn validate_agent_joining(
#[allow(dead_code)]
pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
/*
match op.to_type::<EntryTypes, LinkTypes>()? {
OpType::StoreEntry(store_entry) => {
match op.flattened()::<EntryTypes, LinkTypes>()? {
FlatOp::StoreEntry(store_entry) => {
match store_entry {
OpEntry::CreateEntry { app_entry, action } => {
match app_entry {
Expand Down Expand Up @@ -86,7 +84,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
_ => Ok(ValidateCallbackResult::Valid),
}
}
OpType::RegisterUpdate(update_entry) => {
FlatOp::RegisterUpdate(update_entry) => {
match update_entry {
OpUpdate::Entry {
original_action,
Expand All @@ -112,7 +110,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
_ => Ok(ValidateCallbackResult::Valid),
}
}
OpType::RegisterDelete(delete_entry) => {
FlatOp::RegisterDelete(delete_entry) => {
match delete_entry {
OpDelete::Entry { original_action, original_app_entry, action } => {
match original_app_entry {
Expand All @@ -125,7 +123,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
_ => Ok(ValidateCallbackResult::Valid),
}
}
OpType::RegisterCreateLink {
FlatOp::RegisterCreateLink {
link_type,
base_address,
target_address,
Expand All @@ -151,7 +149,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
}
}
}
OpType::RegisterDeleteLink {
FlatOp::RegisterDeleteLink {
link_type,
base_address,
target_address,
Expand Down Expand Up @@ -180,7 +178,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
}
}
}
OpType::StoreRecord(store_record) => {
FlatOp::StoreRecord(store_record) => {
match store_record {
// Complementary validation to the `StoreEntry` Op, in which the record itself is validated
// If you want to optimize performance, you can remove the validation for an entry type here and keep it in `StoreEntry`
Expand Down Expand Up @@ -378,7 +376,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
}
}
}
OpRecord::CreatePrivateEntry { app_entry_type: _, action: _ } =>
OpRecord::CreatePrivateEntry { app_entry_type: _, action: _ } =>
Ok(ValidateCallbackResult::Valid),
OpRecord::UpdatePrivateEntry {
original_action_hash: _,
Expand Down Expand Up @@ -411,7 +409,7 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
_ => Ok(ValidateCallbackResult::Valid),
}
}
OpType::RegisterAgentActivity(agent_activity) => {
FlatOp::RegisterAgentActivity(agent_activity) => {
match agent_activity {
OpActivity::CreateAgent { agent, action } => {
let previous_action = must_get_action(action.prev_action)?;
Expand All @@ -437,14 +435,21 @@ pub fn validate(_op: Op) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
}
fn _record_to_app_entry(record: &Record) -> ExternResult<Option<EntryTypes>> {
if let Record { signed_action, entry: RecordEntry::Present(entry) } = record {
if let Some(EntryType::App(AppEntryDef { entry_index, zome_index, .. }))
= signed_action.action().entry_type()
if let Record {
signed_action,
entry: RecordEntry::Present(entry),
} = record
{
if let Some(EntryType::App(AppEntryDef {
entry_index,
zome_index,
..
})) = signed_action.action().entry_type()
{
return EntryTypes::deserialize_from_type(
zome_index.clone(),
entry_index.clone(),
&entry,
*zome_index,
*entry_index,
entry,
);
}
}
Expand Down
Loading

0 comments on commit 3c3982b

Please sign in to comment.