Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
chore!: Apply breaking domain concept in anticipation of beta (#298)
Browse files Browse the repository at this point in the history
* chore!: Rename a bunch of things
* feat!: Sphere fields are no longer optional
* chore!: Revise HAMT layout, `VersionedMap` signatures
  • Loading branch information
cdata authored Apr 3, 2023
1 parent 9520826 commit bd34ab4
Show file tree
Hide file tree
Showing 45 changed files with 1,038 additions and 803 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

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

36 changes: 18 additions & 18 deletions rust/noosphere-cli/src/native/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{anyhow, Result};
use cid::Cid;
use noosphere_core::{
authority::{SphereAction, SphereReference},
data::{CidKey, DelegationIpld, RevocationIpld},
data::{DelegationIpld, Link, RevocationIpld},
view::{Sphere, SphereMutation},
};
use serde_json::{json, Value};
Expand All @@ -29,10 +29,10 @@ pub async fn auth_add(did: &str, name: Option<String>, workspace: &Workspace) ->
let sphere = Sphere::at(&latest_sphere_cid, &db);

let authority = sphere.get_authority().await?;
let allowed_ucans = authority.try_get_allowed_ucans().await?;
let mut allowed_stream = allowed_ucans.stream().await?;
let delegations = authority.get_delegations().await?;
let mut delegations_stream = delegations.stream().await?;

while let Some((CidKey(cid), delegation)) = allowed_stream.try_next().await? {
while let Some((Link { cid, .. }, delegation)) = delegations_stream.try_next().await? {
let ucan = delegation.resolve_ucan(&db).await?;
let authorized_did = ucan.audience();

Expand Down Expand Up @@ -105,18 +105,18 @@ You will be able to add a new one after the old one is revoked"#,

let jwt = signable.sign().await?.encode()?;

let delegation = DelegationIpld::try_register(&name, &jwt, &mut db).await?;
let delegation = DelegationIpld::register(&name, &jwt, &mut db).await?;

let sphere = Sphere::at(&latest_sphere_cid, &db);

let mut mutation = SphereMutation::new(&my_did);

mutation
.allowed_ucans_mut()
.set(&CidKey(delegation.jwt), &delegation);
.delegations_mut()
.set(&Link::new(delegation.jwt), &delegation);

let mut revision = sphere.apply_mutation(&mutation).await?;
let version_cid = revision.try_sign(&my_key, Some(&authorization)).await?;
let version_cid = revision.sign(&my_key, Some(&authorization)).await?;

db.set_version(&sphere_did, &version_cid).await?;

Expand Down Expand Up @@ -151,7 +151,7 @@ pub async fn auth_list(as_json: bool, workspace: &Workspace) -> Result<()> {

let authorization = sphere.get_authority().await?;

let allowed_ucans = authorization.try_get_allowed_ucans().await?;
let allowed_ucans = authorization.get_delegations().await?;

let mut authorizations: Vec<(String, String, Cid)> = Vec::new();
let mut delegation_stream = allowed_ucans.stream().await?;
Expand Down Expand Up @@ -207,27 +207,27 @@ pub async fn auth_revoke(name: &str, workspace: &Workspace) -> Result<()> {

let sphere = Sphere::at(&latest_sphere_cid, &db);

let authorization = sphere.get_authority().await?;
let authority = sphere.get_authority().await?;

let allowed_ucans = authorization.try_get_allowed_ucans().await?;
let delegations = authority.get_delegations().await?;

let mut delegation_stream = allowed_ucans.stream().await?;
let mut delegation_stream = delegations.stream().await?;

while let Some(Ok((CidKey(cid), delegation))) = delegation_stream.next().await {
while let Some(Ok((Link { cid, .. }, delegation))) = delegation_stream.next().await {
if delegation.name == name {
let revocation = RevocationIpld::try_revoke(cid, &my_key).await?;
let revocation = RevocationIpld::revoke(cid, &my_key).await?;

let mut mutation = SphereMutation::new(&my_did);

let key = CidKey(*cid);
let key = Link::new(*cid);

mutation.allowed_ucans_mut().remove(&key);
mutation.revoked_ucans_mut().set(&key, &revocation);
mutation.delegations_mut().remove(&key);
mutation.revocations_mut().set(&key, &revocation);

let mut revision = sphere.apply_mutation(&mutation).await?;
let ucan = workspace.authorization().await?;

let sphere_cid = revision.try_sign(&my_key, Some(&ucan)).await?;
let sphere_cid = revision.sign(&my_key, Some(&ucan)).await?;

db.set_version(&sphere_did, &sphere_cid).await?;

Expand Down
19 changes: 10 additions & 9 deletions rust/noosphere-cli/src/native/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ impl Workspace {
let sphere_context = self.sphere_context().await?;
let sphere_cid = sphere_context.version().await?;

let content = self.read_file_content(new_blocks).await?;
let file_content = self.read_file_content(new_blocks).await?;

let sphere = Sphere::at(&sphere_cid, &db);
let links = sphere.get_links().await?;
let content = sphere.get_content().await?;

let mut stream = links.stream().await?;
let mut stream = content.stream().await?;

let mut changes = ContentChanges::default();

while let Some(Ok((slug, memo))) = stream.next().await {
if content.ignored.contains(slug) {
if file_content.ignored.contains(slug) {
continue;
}

match content.matched.get(slug) {
match file_content.matched.get(slug) {
Some(FileReference {
cid: body_cid,
content_type,
Expand All @@ -239,12 +239,13 @@ impl Workspace {
.insert(slug.clone(), Some(content_type.clone()));
}
None => {
let memo = memo.load_from(&db).await?;
changes.removed.insert(slug.clone(), memo.content_type());
}
}
}

for (slug, FileReference { content_type, .. }) in &content.matched {
for (slug, FileReference { content_type, .. }) in &file_content.matched {
if changes.updated.contains_key(slug)
|| changes.removed.contains_key(slug)
|| changes.unchanged.contains(slug)
Expand All @@ -255,7 +256,7 @@ impl Workspace {
changes.new.insert(slug.clone(), Some(content_type.clone()));
}

Ok(Some((content, changes)))
Ok(Some((file_content, changes)))
}

/// Read the local content of the workspace in its entirety.
Expand Down Expand Up @@ -346,9 +347,9 @@ impl Workspace {
let context = self.sphere_context().await?;
let sphere = context.to_sphere().await?;

let links = sphere.get_links().await?;
let content = sphere.get_content().await?;

let mut stream = links.stream().await?;
let mut stream = content.stream().await?;

// TODO(#106): We render the whole sphere every time, but we should probably
// have a fast path where we only render the changes within a CID range
Expand Down
27 changes: 15 additions & 12 deletions rust/noosphere-cli/tests/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![cfg(not(target_arch = "wasm32"))]

#[macro_use]
extern crate tracing;

use anyhow::anyhow;
use libipld_cbor::DagCborCodec;
use noosphere::key::KeyStorage;
use noosphere_sphere::{
HasMutableSphereContext, SphereContentRead, SphereContentWrite, SphereCursor, SphereSync,
Expand Down Expand Up @@ -414,14 +412,19 @@ async fn gateway_updates_an_existing_sphere_with_changes_from_the_client() {
let memo = MemoIpld::for_body(client_sphere_context.db_mut(), vec![value])
.await
.unwrap();
let memo_cid = client_sphere_context
.db_mut()
.save::<DagCborCodec, _>(memo)
.await
.unwrap();

let mut mutation =
SphereMutation::new(&client_sphere_context.author().identity().await.unwrap());
mutation.links_mut().set(&value.into(), &memo);
mutation.content_mut().set(&value.into(), &memo_cid.into());

let mut revision = sphere.apply_mutation(&mutation).await.unwrap();
final_cid = revision
.try_sign(
.sign(
&client_sphere_context.author().key,
client_sphere_context.author().authorization.as_ref(),
)
Expand Down Expand Up @@ -560,14 +563,19 @@ async fn gateway_serves_sphere_revisions_to_a_client() {
let memo = MemoIpld::for_body(client_sphere_context.db_mut(), vec![value])
.await
.unwrap();
let memo_cid = client_sphere_context
.db_mut()
.save::<DagCborCodec, _>(memo)
.await
.unwrap();
let mut mutation =
SphereMutation::new(&client_sphere_context.author().identity().await.unwrap());
mutation.links_mut().set(&value.into(), &memo);
mutation.content_mut().set(&value.into(), &memo_cid.into());

let mut revision = sphere.apply_mutation(&mutation).await.unwrap();

final_cid = revision
.try_sign(
.sign(
&client_sphere_context.author().key,
client_sphere_context.author().authorization.as_ref(),
)
Expand Down Expand Up @@ -689,9 +697,6 @@ async fn gateway_can_sync_an_authorized_sphere_across_multiple_replicas() {
.unwrap(),
);

debug!("EXPECTED AUTHORIZATION: {}", client_replica_authorization);
debug!("SPHERE_JOIN");

sphere_join(
client_replica_key_name,
Some(client_replica_authorization.to_string()),
Expand All @@ -701,8 +706,6 @@ async fn gateway_can_sync_an_authorized_sphere_across_multiple_replicas() {
.await
.unwrap();

debug!("STARTING CLIENT TASK");

let mut client_sphere_context = client_workspace.sphere_context().await.unwrap();
let mut client_replica_sphere_context =
client_replica_workspace.sphere_context().await.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions rust/noosphere-collections/src/hamt/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::Result;
use libipld_cbor::DagCborCodec;

use noosphere_storage::BlockStore;
use std::borrow::{Borrow, BorrowMut};
use std::borrow::Borrow;
use std::marker::PhantomData;
use std::pin::Pin;
use tokio_stream::{Stream, StreamExt};
Expand Down Expand Up @@ -170,7 +170,7 @@ where
/// ```
pub async fn set(&mut self, key: K, value: V) -> Result<Option<V>> {
self.root
.set(key, value, self.store.borrow_mut(), self.bit_width, true)
.set(key, value, self.store.borrow(), self.bit_width, true)
.await
.map(|(r, _)| r)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ where
V: PartialEq,
{
self.root
.set(key, value, self.store.borrow_mut(), self.bit_width, false)
.set(key, value, self.store.borrow(), self.bit_width, false)
.await
.map(|(_, set)| set)
}
Expand Down
85 changes: 13 additions & 72 deletions rust/noosphere-collections/src/hamt/key_value_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,27 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use anyhow::{anyhow, Result};
use cid::Cid;
use libipld_cbor::DagCborCodec;
use noosphere_storage::BlockStore;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tokio::sync::OnceCell;
use serde::{Deserialize, Serialize};

use super::TargetConditionalSendSync;

#[derive(Debug, Serialize, Deserialize, Eq, Clone)]
pub struct KeyValuePair<K, V> {
key: K,
link: Cid,
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct KeyValuePair<K, V>(K, V);

#[serde(skip)]
#[serde(default = "OnceCell::new")]
value: OnceCell<V>,
}

impl<K, V> PartialEq for KeyValuePair<K, V>
where
K: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.link == other.link
}
}

impl<K: TargetConditionalSendSync, V: TargetConditionalSendSync> KeyValuePair<K, V>
where
V: Serialize + DeserializeOwned,
{
impl<K: TargetConditionalSendSync, V: TargetConditionalSendSync> KeyValuePair<K, V> {
pub fn key(&self) -> &K {
&self.key
&self.0
}

pub async fn get_value<S>(&self, store: &S) -> Result<&V>
where
S: BlockStore,
{
self.value
.get_or_try_init(|| async { store.load::<DagCborCodec, V>(&self.link).await })
.await
pub fn value(&self) -> &V {
&self.1
}

pub async fn overwrite_value<S>(&mut self, value: V, store: &mut S) -> Result<V>
where
S: BlockStore,
{
self.get_value(store).await?;
self.link = store.save::<DagCborCodec, V>(value).await?;
self.value
.take()
.ok_or_else(|| anyhow!("Expected previous value not found"))
pub fn value_mut(&mut self) -> &mut V {
&mut self.1
}

pub async fn take<S>(mut self, store: &S) -> Result<(K, V)>
where
S: BlockStore,
{
self.get_value(store).await?;

Ok((
self.key,
self.value
.take()
.ok_or_else(|| anyhow!("Failed to load value"))?,
))
pub fn take(self) -> (K, V) {
(self.0, self.1)
}

pub async fn new<S>(key: K, value: V, store: &mut S) -> Result<Self>
where
S: BlockStore,
{
let link = store.save::<DagCborCodec, _>(&value).await?;

Ok(KeyValuePair {
key,
link,
value: OnceCell::new_with(Some(value)),
})
pub fn new(key: K, value: V) -> Self {
KeyValuePair(key, value)
}
}
Loading

0 comments on commit bd34ab4

Please sign in to comment.