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

refactor: move to 0.2.0 #67

Merged
merged 2 commits into from
Mar 7, 2023
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
25 changes: 20 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ unknown-git = "deny"
allow-registry = ["https://github.com/rust-lang/crates.io-index"]
# List of URLs for allowed Git repositories
allow-git = [
"https://github.com/ucan-wg/rs-ucan",
"https://github.com/bytecodealliance/preview2-prototyping",
"https://github.com/bytecodealliance/wasmtime",
"https://github.com/bytecodealliance/wit-bindgen",
Expand Down
25 changes: 12 additions & 13 deletions homestar-wasm/src/wasmtime/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use crate::wasmtime::ipld::{InterfaceType, RuntimeVal};
use anyhow::{anyhow, Result};
use heck::{ToKebabCase, ToSnakeCase};
use itertools::Itertools;
use libipld::{serde::from_ipld, Ipld};
use std::iter;
use wasmtime::{
Expand Down Expand Up @@ -94,12 +93,14 @@ impl<T> Env<T> {
let result_typs = self.bindings.func().results(&self.store);
let args = from_ipld::<Vec<Ipld>>(args)?;

let params: Vec<component::Val> = iter::zip(param_typs.iter(), args.into_iter())
.map(|(typ, arg)| RuntimeVal::try_from(arg, &InterfaceType::from(typ)))
.fold_ok(vec![], |mut acc, v| {
let params: Vec<component::Val> = iter::zip(param_typs.iter(), args.into_iter()).try_fold(
vec![],
|mut acc, (typ, arg)| {
let v = RuntimeVal::try_from(arg, &InterfaceType::from(typ))?;
acc.push(v.value());
acc
})?;
Ok::<_, anyhow::Error>(acc)
},
)?;

let mut results_alloc: Vec<component::Val> = result_typs
.iter()
Expand All @@ -115,13 +116,11 @@ impl<T> Env<T> {
.post_return_async(&mut self.store)
.await?;

let results: Vec<Ipld> = results_alloc
.into_iter()
.map(|v| Ipld::try_from(RuntimeVal::new(v)))
.fold_ok(vec![], |mut acc, v| {
acc.push(v);
acc
})?;
let results: Vec<Ipld> = results_alloc.into_iter().try_fold(vec![], |mut acc, res| {
let v = Ipld::try_from(RuntimeVal::new(res))?;
acc.push(v);
Ok::<_, anyhow::Error>(acc)
})?;

Ok(Ipld::from(results))
}
Expand Down
9 changes: 7 additions & 2 deletions homestar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ derive_more = "0.99"
diesel = { version = "2.0", features = ["sqlite"] }
diesel_migrations = "2.0"
dotenvy = "0.15"
enum-as-inner = "0.5"
enum-assoc = "0.4"
env_logger = "0.10"
generic-array = "0.14"
hex = "0.4"
homestar-wasm = { version = "0.1", path = "../homestar-wasm" }
ipfs-api = "0.17"
ipfs-api-backend-hyper = { version = "0.6", features = ["with-builder"] }
Expand All @@ -43,13 +47,14 @@ libipld = "0.16"
libipld-core = { version = "0.16", features = ["serde-codec", "serde"] }
libp2p = { version = "0.51", features = ["kad", "request-response", "macros", "identify", "mdns", "floodsub", "gossipsub", "tokio", "dns", "tcp", "noise", "yamux", "websocket"] }
proptest = { version = "1.1", optional = true }
semver = "1.0"
serde = { version = "1.0", features = ["derive"] }
signature = "2.0"
thiserror = "1.0"
tokio = { version = "1.25", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] }
tokio = { version = "1.26", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] }
tracing = "0.1"
tracing-subscriber = "0.3"
ucan = "0.1"
ucan = { version = "0.1", git = "https://github.com/ucan-wg/rs-ucan", branch = "zl/principle-derives" }
url = "2.3"
xid = "1.0"

Expand Down
4 changes: 4 additions & 0 deletions homestar/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Global constants

/// SemVer-formatted version of the UCAN Invocation Specification.
pub const VERSION: &str = "0.2.0";
8 changes: 6 additions & 2 deletions homestar/src/db/schema.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// @generated automatically by Diesel CLI.

diesel::table! {
receipts (cid) {
cid -> Text,
closure_cid -> Text,
nonce -> Text,
ran -> Text,
out -> Binary,
meta -> Binary,
iss -> Nullable<Text>,
prf -> Binary,
}
}
1 change: 1 addition & 0 deletions homestar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! [here]: https://github.com/ipvm-wg/spec.

pub mod cli;
pub mod consts;
pub mod db;
pub mod network;
pub mod workflow;
Expand Down
39 changes: 26 additions & 13 deletions homestar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ use homestar::{
swarm::{self, Topic, TopicMessage},
},
workflow::{
closure::{Action, Closure, Input},
config::Resources,
prf::UcanPrf,
receipt::{LocalReceipt, Receipt},
task::Task,
Ability, Input, Invocation, InvocationResult,
},
};
use homestar_wasm::wasmtime;
Expand All @@ -23,7 +26,7 @@ use ipfs_api::{
use itertools::Itertools;
use libipld::{
cid::{multibase::Base, Cid},
Ipld, Link,
Ipld,
};
use libp2p::{
core::PeerId,
Expand Down Expand Up @@ -149,17 +152,25 @@ async fn main() -> Result<()> {
let mut env =
wasmtime::World::instantiate(wasm_bytes, fun, wasmtime::State::default()).await?;
let res = env.execute(Ipld::List(ipld_args.clone())).await?;

let resource = Url::parse(format!("ipfs://{wasm}").as_str()).expect("IPFS URL");

let closure = Closure {
let task = Task::new(
resource,
action: Action::try_from("wasm/run")?,
inputs: Input::IpldData(Ipld::List(ipld_args)),
};

let link: Link<Closure> = Closure::try_into(closure)?;
let local_receipt = LocalReceipt::new(link, res);
Ability::from("wasm/run"),
Input::Ipld(Ipld::List(ipld_args)),
None,
);
let config = Resources::default();
let invocation =
Invocation::new(task.into(), config.clone().into(), UcanPrf::default())?;

let local_receipt = LocalReceipt::new(
invocation.try_into()?,
InvocationResult::Ok(res),
Ipld::Null,
None,
UcanPrf::default(),
);
let receipt = Receipt::try_from(&local_receipt)?;

let receipt_bytes: Vec<u8> = local_receipt.try_into()?;
Expand All @@ -179,11 +190,13 @@ async fn main() -> Result<()> {
// TODO: insert (or upsert via event handling when subscribed)
diesel::insert_into(schema::receipts::table)
.values(&receipt)
.on_conflict(schema::receipts::cid)
.do_nothing()
.execute(&mut conn)
.expect("Error saving new receipt");
println!("stored: {receipt}");

let closure_cid = receipt.closure_cid();
let invoked_cid = receipt.ran();
let output = receipt.output().clone();
let async_client = client.clone();
// We delay messages to make sure peers are within the mesh.
Expand All @@ -198,12 +211,12 @@ async fn main() -> Result<()> {
.await;
});

let _ = client.start_providing(closure_cid.clone()).await;
let _ = client.start_providing(invoked_cid.clone()).await;

loop {
match events.recv().await {
Some(Event::InboundRequest { request, channel }) => {
if request.eq(&closure_cid) {
if request.eq(&invoked_cid) {
let output = format!("{:?}", output);
client.respond_file(output.into_bytes(), channel).await?;
}
Expand Down
12 changes: 7 additions & 5 deletions homestar/src/network/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ impl EventLoop {
event: SwarmEvent<ComposedEvent, THandlerErr>,
) {
match event {
SwarmEvent::Behaviour(ComposedEvent::Floodsub(FloodsubEvent::Message(message))) =>
SwarmEvent::Behaviour(ComposedEvent::Floodsub(FloodsubEvent::Message(message))) => {
match Receipt::try_from(message.data) {
Ok(receipt) => println!("got message: {receipt}"),

Err(err) => {
println!("cannot handle_message: {err}")
},
},
}
}
}
SwarmEvent::Behaviour(ComposedEvent::Floodsub(FloodsubEvent::Subscribed {
peer_id,
topic,
Expand All @@ -95,15 +96,16 @@ impl EventLoop {
message,
propagation_source,
message_id,
})) => match Receipt::try_from(message.data) {
})) =>
match Receipt::try_from(message.data) {
Ok(receipt) => println!(
"got message: {receipt} from {propagation_source} with message id: {message_id}"
),

Err(err) => println!(
"cannot handle_message: {err}"
)
},
}
SwarmEvent::Behaviour(ComposedEvent::Gossipsub(gossipsub::Event::Subscribed {
peer_id,
topic,
Expand Down
6 changes: 4 additions & 2 deletions homestar/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
diesel::table! {
receipts (cid) {
cid -> Text,
closure_cid -> Text,
nonce -> Text,
ran -> Text,
out -> Binary,
meta -> Binary,
iss -> Nullable<Text>,
prf -> Binary,
}
}
Loading