Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access child (to!) links when parent is updated #2106

Closed
marcusnewton opened this issue Feb 12, 2020 · 10 comments
Closed

Unable to access child (to!) links when parent is updated #2106

marcusnewton opened this issue Feb 12, 2020 · 10 comments

Comments

@marcusnewton
Copy link

@marcusnewton marcusnewton commented Feb 12, 2020

Version: 0.0.42-alpha5

I've created a minimum repro of this issue: https://github.com/marcusnewton/happ-sandbox/blob/b60dd952e5dc468a928904ec0728db544b63c80c/test/scenario/sandbox.js

Whether it's a bug or conceptual misunderstanding, I cannot use the updated entry address of the parent to get linked child entries.

@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

This part below may be the real bug.

One would think we should intercept the address being used for get_links, so that we're calling get_links on the first version of the parent entry.

It seems to make no difference, because you can't actually retrieve the first version of the parent entry: https://github.com/marcusnewton/happ-sandbox/blob/2fb1adc2b76d22e6f5d0b2a8ee4b18d982e559e7/test/scenario/sandbox.js

Previously:

    #[zome_fn("hc_public")]
    fn get_children(address: Address) -> ZomeApiResult<GetLinksResult> {
        hdk::get_links(
            &address,
            LinkMatch::Exactly("parent_to_child"),
            LinkMatch::Any,
        )
    }

Intercepted:

    #[zome_fn("hc_public")]
    fn get_children(address: Address) -> ZomeApiResult<GetLinksResult> {
        let get_entry_result = hdk::get_entry_result(
            &address,
            GetEntryOptions {
                status_request: StatusRequestKind::Initial,
                entry: false,
                headers: true,
                timeout: Default::default(),
            },
        )?;

        let headers = match get_entry_result.result {
            GetEntryResultType::Single(item) => Ok(item.headers),
            _ => Err(ZomeApiError::Internal(
                "Error getting headers of parent entry".into(),
            )),
        }?;

        let canonical_address = headers[0].entry_address();

        hdk::get_links(
            &canonical_address,
            LinkMatch::Exactly("parent_to_child"),
            LinkMatch::Any,
        )
    }
@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

I also tried to see if I was getting the original entry address wrong by using StatusRequestKind:All instead of StatusRequestKind::Initial.

But this also turns up with the exact same problem:https://github.com/marcusnewton/happ-sandbox/blob/65e6d5da764ed1ee70fc25a64ca16b3d68496d39/test/scenario/sandbox.js

    #[zome_fn("hc_public")]
    fn get_children(address: Address) -> ZomeApiResult<GetLinksResult> {
        let get_entry_result = hdk::get_entry_result(
            &address,
            GetEntryOptions {
                status_request: StatusRequestKind::All,
                entry: false,
                headers: true,
                timeout: Default::default(),
            },
        )?;

        let items = match get_entry_result.result {
            GetEntryResultType::All(entry_history) => Ok(entry_history.items),
            _ => Err(ZomeApiError::Internal("Error getting entry history".into())),
        }?;

        let canonical_address = items[0].headers[0].entry_address();

        hdk::get_links(
            &canonical_address,
            LinkMatch::Exactly("parent_to_child"),
            LinkMatch::Any,
        )
    }
@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

I have also tried using get_entry_history to no avail: https://github.com/marcusnewton/happ-sandbox/blob/79b51766209b8920863962d2dc4531b35cb99ef3/test/scenario/sandbox.js

    #[zome_fn("hc_public")]
    fn get_children(address: Address) -> ZomeApiResult<GetLinksResult> {
        let get_entry_history = hdk::get_entry_history(&address)?;

        let canonical_address = match get_entry_history {
            Some(entry_history) => match entry_history.items[0].meta.clone() {
                Some(meta) => Ok(meta.address),
                None => Err(ZomeApiError::Internal("Error getting entry meta".into())),
            },
            None => Err(ZomeApiError::Internal("Error getting entry history".into())),
        }?;

        hdk::get_links(
            &canonical_address,
            LinkMatch::Exactly("parent_to_child"),
            LinkMatch::Any,
        )
    }
@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

hdk::get_entry_history(updated_parent_entry_address) is not returning the full entry history

{
  Ok: {
    items: [
      {
        meta: {
          address: "QmWA4VCED5C6o4o5bMH5bHGYjxLQZo5MWMQjpGp7bBPYWS",
          entry_type: { App: "my_parent_entry" },
          crud_status: "live"
        },
        entry: { App: ["my_parent_entry", '{"content":"barParentContent"}'] },
        headers: []
      }
    ],
    crud_links: {}
  }
}
@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

Of course, the entry history is available only on the first parent entry address, i.e. hdk::get_entry_history(original_parent_entry_address).

{
  Ok: {
    items: [
      {
        meta: {
          address: "QmNVMcQnFVsVojUuocpayiAUdfTGc7tW8gbRRQd62ctKVp",
          entry_type: { App: "my_parent_entry" },
          crud_status: "modified"
        },
        entry: { App: ["my_parent_entry", '{"content":"fooParentContent"}'] },
        headers: []
      },
      {
        meta: {
          address: "QmWA4VCED5C6o4o5bMH5bHGYjxLQZo5MWMQjpGp7bBPYWS",
          entry_type: { App: "my_parent_entry" },
          crud_status: "live"
        },
        entry: { App: ["my_parent_entry", '{"content":"barParentContent"}'] },
        headers: []
      }
    ],
    crud_links: {
      QmNVMcQnFVsVojUuocpayiAUdfTGc7tW8gbRRQd62ctKVp:
        "QmWA4VCED5C6o4o5bMH5bHGYjxLQZo5MWMQjpGp7bBPYWS"
    }
  }
}

So if you only have access to an updated entry address, there's no way to look back in time at earlier versions of the entry.

@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

So since we do not have access to entry history going back in time, I guess I need to manually copy child links to the new parent whenever the parent is updated.

@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

So if I do this, it works

#[zome_fn("hc_public")]
fn update_my_parent_entry(address: Address, entry: MyParentEntry) -> ZomeApiResult<Address> {
    let updated_parent_address = hdk::update_entry(entry.into(), &address)?;

    // Now, copy links to new parent address
    let children_links_result = hdk::get_links(
        &address,
        LinkMatch::Exactly("parent_to_child"),
        LinkMatch::Any,
    )?;

    let child_links = children_links_result.links();

    for child_link in child_links {
        hdk::link_entries(
            &updated_parent_address,
            &child_link.address,
            "parent_to_child",
            "",
        )?;
    }

    Ok(updated_parent_address)
}
@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 12, 2020

And I'm also ensuring I'm getting the children using the latest parent address: https://github.com/marcusnewton/happ-sandbox/blob/parent-update-links-solution/zomes/sandbox/code/src/lib.rs

    #[zome_fn("hc_public")]
    fn get_children(address: Address) -> ZomeApiResult<GetLinksResult> {
        // Get up to date parent
        // Get latest entry
        let latest_entry = match hdk::get_entry(&address)? {
            Some(entry) => Ok(entry),
            None => Err(ZomeApiError::Internal("Failed to get latest entry".into())),
        }?;
        // Get latest address
        let latest_address = hdk::entry_address(&latest_entry)?;

        hdk::get_links(
            &latest_address,
            LinkMatch::Exactly("parent_to_child"),
            LinkMatch::Any,
        )
    }
@marcusnewton marcusnewton changed the title Losing links to child entries when parent is updated Losing ability to access child links when parent is updated Feb 12, 2020
@marcusnewton marcusnewton changed the title Losing ability to access child links when parent is updated Unable to access child links when parent is updated Feb 12, 2020
@marcusnewton marcusnewton changed the title Unable to access child links when parent is updated Unable to access child (to!) links when parent is updated Feb 12, 2020
@zippy

This comment has been minimized.

Copy link
Member

@zippy zippy commented Feb 12, 2020

@marcusnewton thanks for the excellent reporting and sample repos, really helpfull. Looking into this.

@marcusnewton

This comment has been minimized.

Copy link
Author

@marcusnewton marcusnewton commented Feb 13, 2020

Closing this as it confounds the problem. The real bug is with getting older versions of an entry.

See #2111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.