Skip to content

Commit

Permalink
Merge pull request #209 from holo-rea/fix-update-tree
Browse files Browse the repository at this point in the history
fixes #208 - update multiple times
  • Loading branch information
pospi authored Mar 2, 2022
2 parents a1edfd3 + 33fc388 commit 1e87c3f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/hdk_records/src/record_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,25 @@ fn get_header_hash(shh: element::SignedHeaderHashed) -> HeaderHash {
///
/// Useful in coordinating updates between different entry types.
///
/// NOTE: this is a very naive recursive algorithm that basically assumes full network
/// connectivity between everyone at all times, and Updates form a Linked List, rather
/// than a multi-branching tree. This should be updated during other 'conflict resolution' related
/// changes outlined in issue https://github.com/holo-rea/holo-rea/issues/196
pub fn get_latest_header_hash(entry_hash: EntryHash) -> RecordAPIResult<HeaderHash> {
match get_details(entry_hash, GetOptions { strategy: GetStrategy::Latest })? {
Some(Details::Entry(details)) => match details.entry_dht_status {
metadata::EntryDhtStatus::Live => match details.updates.len() {
0 => {
// no updates yet, latest header hash is the first one
// https://docs.rs/hdk/latest/hdk/prelude/struct.EntryDetails.html#structfield.headers
Ok(get_header_hash(details.headers.first().unwrap().to_owned()))
},
_ => {
// updates exist, find most recent header
let mut sortlist = details.updates.to_vec();
sortlist.sort_by_key(|update| update.header().timestamp().as_micros());
let last = sortlist.last().unwrap().to_owned();
Ok(get_header_hash(last))
// recurse (unwrap should be safe because these are Update headers)
get_latest_header_hash(last.header().entry_hash().unwrap().clone())
},
},
_ => Err(DataIntegrityError::EntryNotFound),
Expand Down

0 comments on commit 1e87c3f

Please sign in to comment.