aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-pds
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2022-11-06 23:20:09 -0800
committerBryan Newbold <bnewbold@robocracy.org>2022-11-06 23:20:09 -0800
commitbd225c6532dcc0d4fa7cb4d21444105d6d23771d (patch)
tree266630a20b469a3277e596eac6f5afad6430ac0a /adenosine-pds
parent388d4c004146865161e2661fb63c705e7f097f3a (diff)
downloadadenosine-bd225c6532dcc0d4fa7cb4d21444105d6d23771d.tar.gz
adenosine-bd225c6532dcc0d4fa7cb4d21444105d6d23771d.zip
pds: refactor to use higher-level repo mutation code path
Diffstat (limited to 'adenosine-pds')
-rw-r--r--adenosine-pds/src/lib.rs57
-rw-r--r--adenosine-pds/src/repo.rs25
2 files changed, 34 insertions, 48 deletions
diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs
index d0ef02a..dc34d09 100644
--- a/adenosine-pds/src/lib.rs
+++ b/adenosine-pds/src/lib.rs
@@ -1,5 +1,4 @@
use adenosine_cli::identifiers::{Did, Nsid, Tid, TidLord};
-use anyhow::Context;
use anyhow::{anyhow, Result};
use libipld::Cid;
use libipld::Ipld;
@@ -371,8 +370,6 @@ fn xrpc_post_handler(
let did = Did::from_str(&xrpc_required_param(request, "did")?)?;
let mut srv = srv.lock().unwrap();
let _auth_did = &xrpc_check_auth_header(&mut srv, request, Some(&did))?;
- let commit_cid = &srv.repo.lookup_commit(&did)?.unwrap();
- let last_commit = srv.repo.get_commit(commit_cid)?;
let mut mutations: Vec<Mutation> = Default::default();
for w in batch.writes.iter() {
let m = match w.op_type.as_str() {
@@ -398,13 +395,8 @@ fn xrpc_post_handler(
};
mutations.push(m);
}
- let new_mst_cid = srv.repo.update_mst(&last_commit.mst_cid, &mutations)?;
- let new_root_cid = srv.repo.write_root(
- last_commit.meta_cid,
- Some(last_commit.commit_cid),
- new_mst_cid,
- )?;
- srv.repo.write_commit(&did, new_root_cid, "dummy-sig")?;
+ let keypair = srv.pds_keypair.clone();
+ srv.repo.mutate_repo(&did, &mutations, &keypair)?;
// TODO: next handle updates to database
Ok(json!({}))
}
@@ -415,27 +407,13 @@ fn xrpc_post_handler(
let record: Value = rouille::input::json_input(request)?;
let mut srv = srv.lock().unwrap();
let _auth_did = &xrpc_check_auth_header(&mut srv, request, Some(&did))?;
- debug!("reading commit");
- let commit_cid = &srv.repo.lookup_commit(&did)?.unwrap();
- let last_commit = srv.repo.get_commit(commit_cid)?;
let mutations: Vec<Mutation> = vec![Mutation::Create(
collection,
srv.tid_gen.next_tid(),
json_value_into_ipld(record),
)];
- debug!("mutating tree");
- let new_mst_cid = srv
- .repo
- .update_mst(&last_commit.mst_cid, &mutations)
- .context("updating MST in repo")?;
- debug!("writing new root");
- let new_root_cid = srv.repo.write_root(
- last_commit.meta_cid,
- Some(last_commit.commit_cid),
- new_mst_cid,
- )?;
- debug!("writing new commit");
- srv.repo.write_commit(&did, new_root_cid, "dummy-sig")?;
+ let keypair = srv.pds_keypair.clone();
+ srv.repo.mutate_repo(&did, &mutations, &keypair)?;
// TODO: next handle updates to database
Ok(json!({}))
}
@@ -447,23 +425,14 @@ fn xrpc_post_handler(
let record: Value = rouille::input::json_input(request)?;
let mut srv = srv.lock().unwrap();
let _auth_did = &xrpc_check_auth_header(&mut srv, request, Some(&did))?;
- let commit_cid = &srv.repo.lookup_commit(&did)?.unwrap();
- let last_commit = srv.repo.get_commit(commit_cid)?;
+
let mutations: Vec<Mutation> = vec![Mutation::Update(
collection,
tid,
json_value_into_ipld(record),
)];
- let new_mst_cid = srv
- .repo
- .update_mst(&last_commit.mst_cid, &mutations)
- .context("updating MST in repo")?;
- let new_root_cid = srv.repo.write_root(
- last_commit.meta_cid,
- Some(last_commit.commit_cid),
- new_mst_cid,
- )?;
- srv.repo.write_commit(&did, new_root_cid, "dummy-sig")?;
+ let keypair = srv.pds_keypair.clone();
+ srv.repo.mutate_repo(&did, &mutations, &keypair)?;
// TODO: next handle updates to database
Ok(json!({}))
}
@@ -473,16 +442,10 @@ fn xrpc_post_handler(
let tid = Tid::from_str(&xrpc_required_param(request, "rkey")?)?;
let mut srv = srv.lock().unwrap();
let _auth_did = &xrpc_check_auth_header(&mut srv, request, Some(&did))?;
- let commit_cid = &srv.repo.lookup_commit(&did)?.unwrap();
- let last_commit = srv.repo.get_commit(commit_cid)?;
+
let mutations: Vec<Mutation> = vec![Mutation::Delete(collection, tid)];
- let new_mst_cid = srv.repo.update_mst(&last_commit.mst_cid, &mutations)?;
- let new_root_cid = srv.repo.write_root(
- last_commit.meta_cid,
- Some(last_commit.commit_cid),
- new_mst_cid,
- )?;
- srv.repo.write_commit(&did, new_root_cid, "dummy-sig")?;
+ let keypair = srv.pds_keypair.clone();
+ srv.repo.mutate_repo(&did, &mutations, &keypair)?;
// TODO: next handle updates to database
Ok(json!({}))
}
diff --git a/adenosine-pds/src/repo.rs b/adenosine-pds/src/repo.rs
index ad9aade..1bfc581 100644
--- a/adenosine-pds/src/repo.rs
+++ b/adenosine-pds/src/repo.rs
@@ -1,5 +1,5 @@
-use crate::load_car_to_blockstore;
use crate::mst::{collect_mst_keys, generate_mst, CommitNode, MetadataNode, RootNode};
+use crate::{load_car_to_blockstore, KeyPair};
use adenosine_cli::identifiers::{Did, Nsid, Tid};
use anyhow::{anyhow, ensure, Context, Result};
use ipfs_sqlite_block_store::BlockStore;
@@ -235,6 +235,29 @@ impl RepoStore {
Ok(mst_cid)
}
+ /// High-level helper to write a batch of mutations to the repo corresponding to the DID, and
+ /// signing the resulting new root CID with the given keypair.
+ pub fn mutate_repo(
+ &mut self,
+ did: &Did,
+ mutations: &[Mutation],
+ signing_key: &KeyPair,
+ ) -> Result<Cid> {
+ let commit_cid = self.lookup_commit(did)?.unwrap();
+ let last_commit = self.get_commit(&commit_cid)?;
+ let new_mst_cid = self
+ .update_mst(&last_commit.mst_cid, &mutations)
+ .context("updating MST in repo")?;
+ let new_root_cid = self.write_root(
+ last_commit.meta_cid,
+ Some(last_commit.commit_cid),
+ new_mst_cid,
+ )?;
+ // TODO: is this how signatures are supposed to work?
+ let sig = signing_key.sign_bytes(new_root_cid.to_string().as_bytes());
+ self.write_commit(&did, new_root_cid, &sig)
+ }
+
/// returns the root commit from CAR file
pub fn load_car(&mut self, car_path: &PathBuf, alias: &str) -> Result<Cid> {
let cid = load_car_to_blockstore(&mut self.db, car_path, alias)?;