Skip to content

Commit

Permalink
Merge branch 'master' into 235-add-mock-host-iface-for-non-wasm32-tar…
Browse files Browse the repository at this point in the history
…gets
  • Loading branch information
its-saeed authored Jun 27, 2024
2 parents e3e746e + 0fe9ad4 commit e58ad3b
Show file tree
Hide file tree
Showing 23 changed files with 597 additions and 44 deletions.
59 changes: 33 additions & 26 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ members = [
"./crates/primitives",
"./crates/runtime",
"./crates/sdk",
"./crates/sdk/libs/near",
"./crates/sdk/macros",
"./crates/server",
"./crates/server-primitives",
"./crates/store",

"./apps/kv-store",
"./apps/only-peers",
"./apps/gen-ext",

"./contracts/package-manager",
]
Expand All @@ -46,8 +48,10 @@ futures-util = "0.3.30"
hex = "0.4.3"
libp2p = "0.53.2"
libp2p-stream = "0.1.0-alpha.1"
libp2p-identity = "0.2.9"
multiaddr = "0.18.1"
# multibase = "0.9.1"
near-account-id = "1.0.0"
near-jsonrpc-client = "0.8.0"
near-jsonrpc-primitives = "0.20.0"
near-primitives = "0.20.0"
Expand All @@ -67,6 +71,7 @@ sha3 = "0.10.8"
semver = "1.0.22"
serde = "1.0.196"
serde_json = "1.0.113"
serde_with = "3.8.1"
syn = "2.0"
thiserror = "1.0.56"
tokio = "1.35.1"
Expand Down
14 changes: 14 additions & 0 deletions apps/gen-ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
authors.workspace = true
edition.workspace = true
license.workspace = true
name = "gen-ext"
repository.workspace = true
version = "0.1.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
calimero-sdk = {path = "../../crates/sdk"}
calimero-sdk-near = { path = "../../crates/sdk/libs/near" }
18 changes: 18 additions & 0 deletions apps/gen-ext/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -e

cd "$(dirname $0)"

TARGET="${CARGO_TARGET_DIR:-../../target}"

rustup target add wasm32-unknown-unknown

cargo build --target wasm32-unknown-unknown --profile app-release

mkdir -p res

cp $TARGET/wasm32-unknown-unknown/app-release/gen_ext.wasm ./res/

if command -v wasm-opt > /dev/null; then
wasm-opt -Oz ./res/gen_ext.wasm -o ./res/gen_ext.wasm
fi
27 changes: 27 additions & 0 deletions apps/gen-ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use calimero_sdk::app;
use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk_near::query::RpcQueryRequest;
use calimero_sdk_near::views::QueryRequest;
use calimero_sdk_near::Client;

#[app::state]
#[derive(Default, BorshSerialize, BorshDeserialize)]
#[borsh(crate = "calimero_sdk::borsh")]
struct GenExt;

#[app::logic]
impl GenExt {
pub fn view_account(&mut self, account_id: &str, block_height: u64) -> String {
let client = Client::testnet();
let request = RpcQueryRequest {
block_id: calimero_sdk_near::BlockId::Height(block_height),
request: QueryRequest::ViewAccount {
account_id: account_id.parse().unwrap(),
},
};
match client.call(request) {
Ok(r) => format!("{:?}", r),
Err(e) => format!("{:?}", e),
}
}
}
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license.workspace = true
[dependencies]
bs58.workspace = true
ed25519-dalek.workspace = true
libp2p = { workspace = true, features = ["serde"] }
libp2p-identity = { workspace = true, features = ["peerid", "serde"] }
semver = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand Down
20 changes: 20 additions & 0 deletions crates/primitives/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
use serde::{Deserialize, Serialize};

pub const fn bool_true() -> bool {
true
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "Result")]
pub enum ResultAlt<T, E> {
#[serde(rename = "result")]
Ok(T),
#[serde(rename = "error")]
Err(E),
}

impl<T, E> From<ResultAlt<T, E>> for Result<T, E> {
fn from(result: ResultAlt<T, E>) -> Self {
match result {
ResultAlt::Ok(value) => Ok(value),
ResultAlt::Err(err) => Err(err),
}
}
}
2 changes: 1 addition & 1 deletion crates/primitives/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct ExecutedTransactionPayload {
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PeerJoinedPayload {
pub peer_id: libp2p::PeerId,
pub peer_id: libp2p_identity::PeerId,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub mod serde_signing_key {
pub mod serde_identity {
use std::fmt;

use libp2p::identity::Keypair;
use libp2p_identity::Keypair;
use serde::de::{self, MapAccess};
use serde::ser::{self, SerializeMap};
use serde::{Deserializer, Serializer};
Expand Down
9 changes: 6 additions & 3 deletions crates/runtime/examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use calimero_runtime::{logic, run, store, Constraint};
use serde_json::json;

fn main() -> eyre::Result<()> {
let file = include_bytes!("../../../apps/only-peers/res/only_peers.wasm");
let file = include_bytes!("../../../apps/gen-ext/res/gen_ext.wasm");

let mut storage = store::InMemoryStorage::default();

Expand All @@ -22,9 +22,12 @@ fn main() -> eyre::Result<()> {
};

let cx = logic::VMContext {
input: serde_json::to_vec(&json!({}))?,
input: serde_json::to_vec(&json!({
"block_height": 167345193,
"account_id": "nearkat.testnet",
}))?,
};
let get_outcome = run(file, "fetch", cx, &mut storage, &limits)?;
let get_outcome = run(file, "view_account", cx, &mut storage, &limits)?;
let returns = String::from_utf8(get_outcome.returns.unwrap().unwrap()).unwrap();
println!("{returns}");

Expand Down
6 changes: 5 additions & 1 deletion crates/runtime/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ impl<'a> VMHostFunctions<'a> {
let url = self.get_string(url_ptr, url_len)?;
let method = self.get_string(method_ptr, method_len)?;
let headers = self.read_guest_memory(headers_ptr, headers_len)?;
let headers: Vec<(String, String)> = borsh::from_slice(&headers).unwrap(); // safety: headers are coming from an inner source. Safe to deserialize.

// Safety: The `fetch` function cannot be directly called by applications.
// Therefore, the headers are generated exclusively by our code, ensuring
// that it is safe to deserialize them.
let headers: Vec<(String, String)> = borsh::from_slice(&headers).unwrap();
let body = self.read_guest_memory(body_ptr, body_len)?;
let mut request = ureq::request(&method, &url);

Expand Down
17 changes: 17 additions & 0 deletions crates/sdk/libs/near/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "calimero-sdk-near"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
near-account-id = { workspace = true, features = ["serde"]}
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_with = {workspace = true, features = ["base64"]}
thiserror.workspace = true

calimero-sdk = { path = "../../" }
calimero-primitives = { path = "../../../primitives" }
Loading

0 comments on commit e58ad3b

Please sign in to comment.