Skip to content

Commit

Permalink
feat: expose more data for convenience of explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
8e8b2c committed Jun 28, 2024
1 parent f8da78b commit 8c51c6c
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 26 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions crates/holoom_dna_tests/src/tests/username_registry/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use hdk::prelude::*;

use holochain::conductor::api::error::ConductorApiResult;
use holoom_types::{
JqExecution, OracleDocument, OracleDocumentListSnapshot,
RefreshJqExecutionForNamedRelationPayload, RelateOracleDocumentPayload, SnapshotInput,
DocumentRelationTag, JqExecution, OracleDocument, OracleDocumentListSnapshot,
RefreshJqExecutionForNamedRelationPayload, SnapshotInput,
};
use username_registry_utils::deserialize_record_entry;

Expand Down Expand Up @@ -41,7 +41,7 @@ async fn can_fetch_documents_by_relation() {
.authority_call(
"username_registry",
"relate_oracle_document",
RelateOracleDocumentPayload {
DocumentRelationTag {
name: "foo/1".into(),
relation: "foo".into(),
},
Expand All @@ -53,7 +53,7 @@ async fn can_fetch_documents_by_relation() {
.authority_call(
"username_registry",
"relate_oracle_document",
RelateOracleDocumentPayload {
DocumentRelationTag {
name: "foo/2".into(),
relation: "foo".into(),
},
Expand All @@ -64,7 +64,7 @@ async fn can_fetch_documents_by_relation() {
let identifiers: Vec<String> = setup
.authority_call(
"username_registry",
"get_related_oracle_document",
"get_related_oracle_document_names",
String::from("foo"),
)
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/holoom_types/src/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct RefreshJqExecutionForNamedRelationPayload {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RelateOracleDocumentPayload {
pub struct DocumentRelationTag {
pub name: String,
pub relation: String,
}
12 changes: 12 additions & 0 deletions crates/records_coordinator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "records_coordinator"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
name = "records_coordinator"

[dependencies]
hdk = { workspace = true }
serde = { workspace = true }
31 changes: 31 additions & 0 deletions crates/records_coordinator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use hdk::prelude::*;

#[hdk_extern]
pub fn get_record(action_hash: ActionHash) -> ExternResult<Option<Record>> {
get(action_hash, GetOptions::default())
}

#[hdk_extern]
pub fn get_chain_status(agent: AgentPubKey) -> ExternResult<AgentActivity> {
get_agent_activity(agent, ChainQueryFilter::default(), ActivityRequest::Status)
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetChainSegmentPayload {
pub agent: AgentPubKey,
pub start: u32,
pub end: u32,
}

#[hdk_extern]
pub fn get_chain_segment(payload: GetChainSegmentPayload) -> ExternResult<AgentActivity> {
let query = ChainQueryFilter::default()
.descending()
.include_entries(true)
.sequence_range(ChainQueryFilterRange::ActionSeqRange(
payload.start,
payload.end,
));

get_agent_activity(payload.agent, query, ActivityRequest::Full)
}
5 changes: 5 additions & 0 deletions crates/username_registry_coordinator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ fn recv_remote_signal(signal_io: ExternIO) -> ExternResult<()> {

Ok(())
}

#[hdk_extern]
fn get_authority(_: ()) -> ExternResult<AgentPubKey> {
get_authority_agent()
}
52 changes: 42 additions & 10 deletions crates/username_registry_coordinator/src/oracle_document.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use hdk::prelude::*;
use holoom_types::{OracleDocument, RelateOracleDocumentPayload};
use holoom_types::{DocumentRelationTag, OracleDocument};
use username_registry_integrity::{EntryTypes, LinkTypes};
use username_registry_utils::hash_identifier;

#[hdk_extern]
pub fn create_oracle_document(oracle_document: OracleDocument) -> ExternResult<Record> {
let base_address = hash_identifier(oracle_document.name.clone())?;
let link_tag = oracle_document.name.as_bytes().to_vec();
let oracle_document_ah = create_entry(EntryTypes::OracleDocument(oracle_document))?;
create_link(
base_address,
oracle_document_ah.clone(),
LinkTypes::NameToOracleDocument,
(),
link_tag,
)?;
let record = get(oracle_document_ah, GetOptions::default())?.ok_or(wasm_error!(
WasmErrorInner::Guest(String::from(
Expand All @@ -21,7 +22,6 @@ pub fn create_oracle_document(oracle_document: OracleDocument) -> ExternResult<R

Ok(record)
}

#[hdk_extern]
pub fn get_latest_oracle_document_ah_for_name(name: String) -> ExternResult<Option<ActionHash>> {
let base_address = hash_identifier(name)?;
Expand All @@ -38,6 +38,18 @@ pub fn get_latest_oracle_document_ah_for_name(name: String) -> ExternResult<Opti
Ok(Some(action_hash))
}

#[hdk_extern]
pub fn get_oracle_document_link_ahs_for_name(name: String) -> ExternResult<Vec<ActionHash>> {
let base_address = hash_identifier(name)?;
let mut links = get_links(base_address, LinkTypes::NameToOracleDocument, None)?;
links.sort_by_key(|link| link.timestamp);
let action_hashes = links
.into_iter()
.filter_map(|link| ActionHash::try_from(link.create_link_hash).ok())
.collect();
Ok(action_hashes)
}

#[hdk_extern]
pub fn get_latest_oracle_document_for_name(name: String) -> ExternResult<Option<Record>> {
let Some(action_hash) = get_latest_oracle_document_ah_for_name(name)? else {
Expand All @@ -47,21 +59,24 @@ pub fn get_latest_oracle_document_for_name(name: String) -> ExternResult<Option<
}

#[hdk_extern]
pub fn relate_oracle_document(payload: RelateOracleDocumentPayload) -> ExternResult<()> {
let base_address = hash_identifier(payload.relation)?;
let target_address = hash_identifier(payload.name.clone())?;
pub fn relate_oracle_document(relation_tag: DocumentRelationTag) -> ExternResult<()> {
let tag_bytes =
ExternIO::encode(&relation_tag).expect("Couldn't serialize DocumentRelationTag");

let base_address = hash_identifier(relation_tag.relation)?;
let target_address = hash_identifier(relation_tag.name.clone())?;
create_link(
base_address,
target_address,
LinkTypes::RelateOracleDocumentName,
payload.name.as_bytes().to_vec(),
tag_bytes.0,
)?;

Ok(())
}

#[hdk_extern]
pub fn get_related_oracle_document(relation_name: String) -> ExternResult<Vec<String>> {
pub fn get_related_oracle_document_names(relation_name: String) -> ExternResult<Vec<String>> {
// BTreeSet ensures order an no repeats
let identifiers: BTreeSet<String> = get_links(
hash_identifier(relation_name)?,
Expand All @@ -70,9 +85,26 @@ pub fn get_related_oracle_document(relation_name: String) -> ExternResult<Vec<St
)?
.into_iter()
.map(|link| {
String::from_utf8(link.tag.into_inner())
.map_err(|_| wasm_error!(WasmErrorInner::Guest("LinkTag isn't utf8".into())))
let document_relation: DocumentRelationTag = ExternIO(link.tag.into_inner())
.decode()
.map_err(|_| wasm_error!(WasmErrorInner::Guest("LinkTag isn't utf8".into())))?;
Ok(document_relation.name)
})
.collect::<ExternResult<_>>()?;
Ok(identifiers.into_iter().collect())
}

#[hdk_extern]
pub fn get_relation_link_ahs(relation_name: String) -> ExternResult<Vec<ActionHash>> {
let mut links = get_links(
hash_identifier(relation_name)?,
LinkTypes::RelateOracleDocumentName,
None,
)?;
links.sort_by_key(|link| link.timestamp);
let action_hashes = links
.into_iter()
.filter_map(|link| ActionHash::try_from(link.create_link_hash).ok())
.collect();
Ok(action_hashes)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use serde_json;
use username_registry_integrity::EntryTypes;
use username_registry_utils::deserialize_record_entry;

use crate::oracle_document::{get_latest_oracle_document_ah_for_name, get_related_oracle_document};
use crate::oracle_document::{
get_latest_oracle_document_ah_for_name, get_related_oracle_document_names,
};

pub fn resolve_snapshot_input(input: SnapshotInput) -> ExternResult<Vec<String>> {
let json_list = match input {
Expand Down Expand Up @@ -74,7 +76,7 @@ pub fn refresh_oracle_document_snapshot_for_named_input_list_document(
pub fn refresh_oracle_document_snapshot_for_relation(
relation_name: String,
) -> ExternResult<Record> {
let identifiers = get_related_oracle_document(relation_name)?;
let identifiers = get_related_oracle_document_names(relation_name)?;
let snapshot_input = SnapshotInput::RelationSnapshot(identifiers);
let snapshot = build_latest_oracle_document_list_snapshot_for_frozen_input(snapshot_input)?;
let snapshot_ah = create_entry(EntryTypes::OracleDocumentListSnapshot(snapshot))?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
use hdi::prelude::*;
use holoom_types::DocumentRelationTag;
use username_registry_utils::hash_identifier;

pub fn validate_create_link_relate_oracle_document_name(
_action: CreateLink,
_base_address: AnyLinkableHash,
_target_address: AnyLinkableHash,
_tag: LinkTag,
base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// TODO: maybe check tag looks like a valid address?
// TODO: only authority?
let Ok(document_relation) = ExternIO(tag.into_inner()).decode::<DocumentRelationTag>() else {
return Ok(ValidateCallbackResult::Invalid(
"Tag must be a DocumentRelationTag".into(),
));
};
if base_address != hash_identifier(document_relation.relation)?.into() {
return Ok(ValidateCallbackResult::Invalid(
"Base address must be the hash of the document relation".into(),
));
}
if target_address != hash_identifier(document_relation.name)?.into() {
return Ok(ValidateCallbackResult::Invalid(
"Target address must be the hash of the target document name".into(),
));
}

Ok(ValidateCallbackResult::Valid)
}
Expand All @@ -18,7 +33,7 @@ pub fn validate_delete_link_relate_oracle_document_name(
_target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
// TODO: only authority?

Ok(ValidateCallbackResult::Valid)
Ok(ValidateCallbackResult::Invalid(
"Cannot delete oracle document relation links".into(),
))
}
3 changes: 3 additions & 0 deletions workdir/dna.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ coordinator:
bundled: "../target/wasm32-unknown-unknown/release/username_registry_coordinator.wasm"
dependencies:
- name: username_registry_integrity
- name: records
hash: ~
bundled: "../target/wasm32-unknown-unknown/release/records_coordinator.wasm"
- name: signer
hash: ~
bundled: "../target/wasm32-unknown-unknown/release/signer_coordinator.wasm"
Expand Down

0 comments on commit 8c51c6c

Please sign in to comment.