Skip to content

Commit

Permalink
Merge pull request #10 from perspect3vism/simple-link-revision
Browse files Browse the repository at this point in the history
Simple latest link revision fetching
  • Loading branch information
jdeepee authored Jan 30, 2023
2 parents 3022373 + a193910 commit 7a6ad4b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 98 deletions.
39 changes: 0 additions & 39 deletions hc-dna/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions hc-dna/zomes/perspective_diff_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ derive_more = "0"
serde = "1"
lazy_static = "*"
chrono = { version = "0.4.22", default-features = false, features = ["clock", "std", "oldtime", "serde"] }
hc_time_index = { git = "https://github.com/holochain-open-dev/holochain-time-index" }
#hc_time_index = { path = "../../../../holochain-time-index" }
thiserror = "1.0.20"
petgraph = "0.6.2"
maplit = "1.0.2"
Expand Down
8 changes: 0 additions & 8 deletions hc-dna/zomes/perspective_diff_sync/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use hdk::prelude::*;
use holo_hash::error::HoloHashError;
use std::convert::Infallible;

use hc_time_index::errors::IndexError;

#[derive(thiserror::Error, Debug)]
pub enum SocialContextError {
#[error(transparent)]
Expand All @@ -12,18 +10,12 @@ pub enum SocialContextError {
Infallible(#[from] Infallible),
#[error(transparent)]
EntryError(#[from] EntryError),
// #[error("Failed to convert an agent link tag to an agent pub key")]
// AgentTag,
#[error(transparent)]
Wasm(#[from] WasmError),
#[error(transparent)]
HoloHashError(#[from] HoloHashError),
#[error("Internal Error. Error: {0}")]
InternalError(&'static str),
// #[error("Invalid Request Data. Error: {0}")]
// RequestError(&'static str),
#[error(transparent)]
IndexError(#[from] IndexError),
#[error("No common ancestor found")]
NoCommonAncestorFound,
}
Expand Down
24 changes: 12 additions & 12 deletions hc-dna/zomes/perspective_diff_sync/src/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down Expand Up @@ -399,7 +399,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down Expand Up @@ -457,7 +457,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down Expand Up @@ -514,7 +514,7 @@ mod tests {
let node_1 = &node_id_hash(&dot_structures::Id::Plain(String::from("1"))).to_string();
let expected_additions = vec![create_link_expression(node_1, node_1)];

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
assert!(pull_res
.unwrap()
Expand Down Expand Up @@ -577,7 +577,7 @@ mod tests {
create_link_expression(node_2, node_2),
];

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
assert!(pull_res
.unwrap()
Expand Down Expand Up @@ -624,7 +624,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
println!("{:#?}", pull_res);
let pull_res = pull_res.unwrap();
Expand Down Expand Up @@ -685,7 +685,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
println!("{:#?}", pull_res);
let pull_res = pull_res.unwrap();
Expand Down Expand Up @@ -734,7 +734,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
//println!("{:#?}", pull_res);
let pull_res = pull_res.unwrap();
Expand Down Expand Up @@ -776,7 +776,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
//println!("{:#?}", pull_res);
let pull_res = pull_res.unwrap();
Expand Down Expand Up @@ -813,7 +813,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down Expand Up @@ -853,7 +853,7 @@ mod tests {
MockPerspectiveGraph::update_current_revision(current_node_hash, chrono::Utc::now());
assert!(update_current.is_ok());

let pull_res = pull::<MockPerspectiveGraph>();
let pull_res = pull::<MockPerspectiveGraph>(false);
assert!(pull_res.is_ok());
let pull_res = pull_res.unwrap();

Expand Down
72 changes: 51 additions & 21 deletions hc-dna/zomes/perspective_diff_sync/src/retriever/holochain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::str::FromStr;

use chrono::{DateTime, NaiveDateTime, Utc};
use hdk::prelude::*;
use perspective_diff_sync_integrity::{EntryTypes, HashReference, LinkTypes, LocalHashReference};
use perspective_diff_sync_integrity::{
Anchor, EntryTypes, HashReference, LinkTypes, LocalHashReference,
};

use super::PerspectiveDiffRetreiver;
use crate::errors::{SocialContextError, SocialContextResult};
use crate::utils::get_now;
use crate::Hash;

pub struct HolochainRetreiver;
Expand Down Expand Up @@ -94,18 +97,39 @@ impl PerspectiveDiffRetreiver for HolochainRetreiver {
}

fn latest_revision() -> SocialContextResult<Option<HashReference>> {
let mut latest =
hc_time_index::get_links_and_load_for_time_span::<HashReference, LinkTypes, LinkTypes>(
String::from("current_rev"),
get_now()?,
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc),
None,
hc_time_index::SearchStrategy::Dfs,
Some(1),
LinkTypes::Index,
LinkTypes::TimePath,
)?;
Ok(latest.pop())
let latest_root_entry = get_latest_revision_anchor();
let latest_root_entry_hash = hash_entry(latest_root_entry.clone())?;

let mut latest_revision_links = get_links(latest_root_entry_hash, LinkTypes::Index, None)?;

latest_revision_links.sort_by(|link_a, link_b| {
let link_a_str = std::str::from_utf8(&link_a.tag.0).unwrap();
let link_b_str = std::str::from_utf8(&link_b.tag.0).unwrap();
let link_a = DateTime::<Utc>::from_str(link_a_str).unwrap();
let link_b = DateTime::<Utc>::from_str(link_b_str).unwrap();
link_a.cmp(&link_b)
});

let mut latest_hash_revisions = latest_revision_links
.into_iter()
.map(|link| {
let hash =
link.target
.into_action_hash()
.ok_or(SocialContextError::InternalError(
"Could not convert link target to hash",
))?;
let timestamp = std::str::from_utf8(&link.tag.0)
.map_err(|_| SocialContextError::InternalError("Could not tag to string"))?;

let timestamp = DateTime::<Utc>::from_str(timestamp).map_err(|_| {
SocialContextError::InternalError("Could not convert string to timestamp")
})?;
Ok(HashReference { hash, timestamp })
})
.collect::<SocialContextResult<Vec<HashReference>>>()?;

Ok(latest_hash_revisions.pop())
}

fn update_current_revision(hash: Hash, timestamp: DateTime<Utc>) -> SocialContextResult<()> {
Expand All @@ -115,15 +139,21 @@ impl PerspectiveDiffRetreiver for HolochainRetreiver {
}

fn update_latest_revision(hash: Hash, timestamp: DateTime<Utc>) -> SocialContextResult<()> {
let hash_ref = HashReference { hash, timestamp };
create_entry(EntryTypes::HashReference(hash_ref.clone()))?;
hc_time_index::index_entry(
String::from("current_rev"),
hash_ref,
LinkTag::new(""),
let latest_root_entry = get_latest_revision_anchor();
let _latest_root_entry_action =
self::create_entry(EntryTypes::Anchor(latest_root_entry.clone()))?;

create_link(
hash_entry(latest_root_entry)?,
hash,
LinkTypes::Index,
LinkTypes::TimePath,
LinkTag::new(timestamp.to_string()),
)?;

Ok(())
}
}

fn get_latest_revision_anchor() -> Anchor {
Anchor("latest_revision".to_string())
}
5 changes: 1 addition & 4 deletions hc-dna/zomes/perspective_diff_sync_integrity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ chrono = { version = "0.4.22", default-features = false, features = ["clock", "s

holo_hash = "0.0.35"
hdi = "0.1.10"
hdk = "0.0.163"

hc_time_index = { git = "https://github.com/holochain-open-dev/holochain-time-index" }
#hc_time_index = { path = "../../../../holochain-time-index" }
hdk = "0.0.163"
13 changes: 1 addition & 12 deletions hc-dna/zomes/perspective_diff_sync_integrity/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
use hc_time_index::IndexableEntry;
use hdk::prelude::*;

use crate::{HashReference, PerspectiveDiff, PerspectiveDiffReference};

impl IndexableEntry for HashReference {
fn entry_time(&self) -> chrono::DateTime<chrono::Utc> {
self.timestamp
}

fn hash(&self) -> ExternResult<hdk::prelude::HoloHash<holo_hash::hash_type::Entry>> {
hash_entry(self)
}
}
use crate::{PerspectiveDiff, PerspectiveDiffReference};

impl PerspectiveDiff {
pub fn get_sb(self) -> ExternResult<SerializedBytes> {
Expand Down

0 comments on commit 7a6ad4b

Please sign in to comment.