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

chore: update ceramic http client and fix recon sync test #189

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ async-trait.workspace = true
ceramic-core.workspace = true
iroh-car.workspace = true
ceramic-http-client = { git = "https://github.com/3box/ceramic-http-client-rs.git", branch = "main", default-features = false }
#ceramic-http-client = { path = "../../ceramic-http-client-rs", default-features = false }
clap.workspace = true
did-method-key = "0.2"
goose = { version = "0.16", features = ["gaggle"] }
Expand All @@ -25,7 +24,7 @@ opentelemetry.workspace = true
rand = "0.8.5"
redis = { version = "0.24", features = ["tokio-comp"] }
reqwest.workspace = true
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_ipld_dagcbor = "0.6"
serde_ipld_dagjson = "0.2"
schemars.workspace = true
Expand Down
18 changes: 12 additions & 6 deletions runner/src/scenario/ceramic/anchor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use anyhow::Result;
use ceramic_core::{Cid, DagCborEncoded};
use ceramic_http_client::ceramic_event::{DidDocument, JwkSigner, Jws, StreamId};
use ceramic_http_client::{
ceramic_event::{unvalidated, DidDocument, StreamId},
JwsBuilder,
};
use chrono::Utc;
use goose::prelude::*;
use ipld_core::ipld;
Expand Down Expand Up @@ -32,10 +35,13 @@ async fn auth_header(url: String, controller: String, digest: Cid) -> Result<Str
let node_private_key = std::env::var("NODE_PRIVATE_KEY").unwrap_or_else(|_| {
"b650ae31651a33cc1a40402e5b38266b8b6eb9213013b3fa3a91b3f065ab643a".to_string()
});
let signer = JwkSigner::new(DidDocument::new(controller.as_str()), &node_private_key)
.await
.unwrap();
let auth_jws = Jws::builder(&signer).build_for_data(&auth_payload).await?;
let signer = unvalidated::signed::JwkSigner::new(
DidDocument::new(controller.as_str()),
&node_private_key,
)
.await
.unwrap();
let auth_jws = JwsBuilder::new(&signer).build_for_data(&auth_payload)?;

let (sig, protected) = auth_jws
.signatures
Expand All @@ -54,7 +60,7 @@ pub async fn stream_tip_car(
) -> Result<(Cid, Vec<u8>)> {
let root_block = ipld!({
"timestamp": Utc::now().to_rfc3339(),
"streamId": stream_id.to_vec()?,
"streamId": stream_id.to_vec(),
"tip": genesis_cid,
});

Expand Down
2 changes: 1 addition & 1 deletion runner/src/scenario/ceramic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod write_only;

use ceramic_core::ssi::did::{DIDMethod, Document, DocumentBuilder, Source};
use ceramic_core::ssi::jwk::{self, Base64urlUInt, Params, JWK};
use ceramic_http_client::ceramic_event::JwkSigner;
use ceramic_http_client::ceramic_event::unvalidated::signed::JwkSigner;
use ceramic_http_client::CeramicHttpClient;

use models::RandomModelInstance;
Expand Down
1 change: 0 additions & 1 deletion runner/src/scenario/ceramic/model_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,6 @@ impl ModelInstanceRequests {
let name = format!("create_index_model_{}", tx_name);
let req = cli
.create_index_model_request(model_id, &resp.code)
.await
samika98 marked this conversation as resolved.
Show resolved Hide resolved
.unwrap();
let mut goose = user
.request(
Expand Down
11 changes: 5 additions & 6 deletions runner/src/scenario/recon_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ async fn create_new_event(user: &mut GooseUser) -> TransactionResult {
let user_data: &ReconCeramicModelInstanceTestUser = user
.get_session_data()
.expect("we are missing sync_event_id user data");
let data = random_init_event_car(
SORT_KEY,
user_data.model_id.to_vec().unwrap(),
let event = random_init_event_car(
user_data.model_id.to_vec(),
Some(TEST_CONTROLLER.to_string()),
)
.await
.unwrap();
let event_key_body = serde_json::json!({"data": data});
// eventId needs to be a multibase encoded string for the API to accept it

let cnt = NEW_EVENT_CNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let event_key_body = serde_json::json!({
"data": event,
});

if cnt == 0 || cnt % 1000 == 0 {
tracing::trace!("new sync_event_id body: {:?}", event_key_body);
Expand All @@ -150,7 +150,6 @@ async fn create_new_event(user: &mut GooseUser) -> TransactionResult {
}
}

const SORT_KEY: &str = "model";
// hard code test controller in case we want to find/prune later
const TEST_CONTROLLER: &str = "did:key:z6MkoFUppcKEVYTS8oVidrja94UoJTatNhnhxJRKF7NYPScS";

Expand Down
36 changes: 12 additions & 24 deletions runner/src/scenario/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::Write;

use ceramic_core::{Cid, DagCborEncoded};
use ceramic_core::{Cid, DagCborEncoded, MultiBase32String};
use ceramic_http_client::ceramic_event::unvalidated;
use goose::GooseError;
use ipld_core::ipld;
use multihash_codetable::{Code, MultihashDigest};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use unsigned_varint::encode;
Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn create_stream() -> anyhow::Result<(
.map(char::from)
.collect();

let genesis_commit = ipld!({
let genesis_commit = ipld_core::ipld!({
"header": {
"unique": gen_rand_bytes::<12>().as_slice(),
"controllers": [controller]
Expand Down Expand Up @@ -73,10 +73,9 @@ pub fn write_stream_bytes(cid: &Cid) -> anyhow::Result<Vec<u8>> {
}

pub(crate) async fn random_init_event_car(
sort_key: &str,
model: Vec<u8>,
controller: Option<String>,
) -> anyhow::Result<String> {
) -> Result<MultiBase32String, anyhow::Error> {
let controller = if let Some(owner) = controller {
owner
} else {
Expand All @@ -88,25 +87,14 @@ pub(crate) async fn random_init_event_car(
};

let unique = gen_rand_bytes::<12>();
let init = ipld!({
"header": {
"controllers": [controller],
"model": model,
"sep": sort_key,
"unique": unique.as_slice(),
}
});

let block = DagCborEncoded::new(&init)?;
let cid = Cid::new_v1(DAG_CBOR_CODEC, Code::Sha2_256.digest(block.as_ref()));

let mut buf = Vec::new();
let roots = vec![cid];
let mut writer = iroh_car::CarWriter::new(iroh_car::CarHeader::V1(roots.into()), &mut buf);
writer.write(cid, block).await?;
writer.finish().await.unwrap();

Ok(multibase::encode(multibase::Base::Base36Lower, buf))
let res = unvalidated::Builder::init()
.with_controller(controller)
.with_sep("model".to_string(), model)
.with_unique(unique.to_vec())
.with_data(ipld_core::ipld!({"a": 1, "b": 2}))
.build();
let car = res.encode_car().await?;
Ok(MultiBase32String::from(car))
}

fn gen_rand_bytes<const SIZE: usize>() -> [u8; SIZE] {
Expand Down
Loading