Merge pull request #1 from fluencelabs/call

Introduce the early version of the call instruction
This commit is contained in:
vms 2020-09-30 10:41:32 +03:00 committed by GitHub
commit 0956f1ab0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 257 additions and 21 deletions

57
Cargo.lock generated
View File

@ -6,6 +6,9 @@ version = "0.1.0"
dependencies = [
"fluence",
"log",
"serde",
"serde_derive",
"serde_sexpr",
]
[[package]]
@ -16,7 +19,8 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "fluence"
version = "0.2.6"
version = "0.2.7"
source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924"
dependencies = [
"fluence-sdk-macro",
"fluence-sdk-main",
@ -24,14 +28,16 @@ dependencies = [
[[package]]
name = "fluence-sdk-macro"
version = "0.2.6"
version = "0.2.7"
source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924"
dependencies = [
"fluence-sdk-wit",
]
[[package]]
name = "fluence-sdk-main"
version = "0.2.6"
version = "0.2.7"
source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924"
dependencies = [
"fluence-sdk-macro",
"log",
@ -40,7 +46,8 @@ dependencies = [
[[package]]
name = "fluence-sdk-wit"
version = "0.2.6"
version = "0.2.7"
source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924"
dependencies = [
"proc-macro2",
"quote",
@ -82,6 +89,22 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "nom"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
dependencies = [
"memchr",
"version_check",
]
[[package]]
name = "ppv-lite86"
version = "0.2.9"
@ -90,9 +113,9 @@ checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro2"
version = "1.0.21"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c"
checksum = "51ef7cd2518ead700af67bf9d1a658d90b6037d77110fd9c0445429d0ba1c6c9"
dependencies = [
"unicode-xid",
]
@ -185,10 +208,20 @@ dependencies = [
]
[[package]]
name = "syn"
version = "1.0.41"
name = "serde_sexpr"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b"
checksum = "5318bfeed779c64075ce317c81462ed54dc00021be1c6b34957d798e11a68bdb"
dependencies = [
"nom",
"serde",
]
[[package]]
name = "syn"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c51d92969d209b54a98397e1b91c8ae82d8c87a7bb87df0b29aa2ad81454228"
dependencies = [
"proc-macro2",
"quote",
@ -210,6 +243,12 @@ dependencies = [
"rand",
]
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"

View File

@ -8,6 +8,13 @@ edition = "2018"
name = "aquamarine"
path = "src/main.rs"
[[bin]]
name = "aqua_test_module"
path = "test_module/main.rs"
[dependencies]
fluence = { path = "/Users/mike/dev/work/fluence/wasm/rust-sdk", features = ["logger"] }
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
serde = { version = "1.0.116", features = ["derive"] }
serde_derive = "1.0.116"
serde_sexpr = "0.1.0"
log = "0.4.11"

View File

@ -1,6 +1,11 @@
modules_dir = "artifacts/"
modules_dir = "./target/wasm32-wasi/release/"
[[module]]
name = "aquamarine"
name = "aqua_test_module"
mem_pages_count = 100
logger_enabled = true
[[module]]
name = "aquamarine"
mem_pages_count = 100
logger_enabled = true

38
src/instructions/call.rs Normal file
View File

@ -0,0 +1,38 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub(crate) struct Call {
pub peer_part: (String, Option<String>),
pub fn_part: (Option<String>, String),
pub args: String,
pub result_name: String,
}
impl super::ExecutableInstruction for Call {
fn execute(self, _data: &mut HashMap<String, Vec<u8>>) {
let service_id = match (self.peer_part.1, self.fn_part.0) {
(Some(service_id), None) => service_id,
(None, Some(service_id)) => service_id,
_ => unimplemented!(),
};
let _result = unsafe { crate::call_service(service_id, self.fn_part.1, self.args) };
}
}

46
src/instructions/mod.rs Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod call;
pub(crate) use call::Call;
pub(self) use crate::stepper::ExecutableInstruction;
use serde_derive::{Deserialize, Serialize};
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum Instruction {
Null,
Call(Call),
/*
Par(Box<Instruction>, Box<Instruction>),
Seq(Box<Instruction>, Box<Instruction>),
*/
}
impl ExecutableInstruction for Instruction {
fn execute(self, data: &mut HashMap<String, Vec<u8>, RandomState>) {
match self {
Instruction::Null => {},
Instruction::Call(call) => call.execute(data),
}
}
}

View File

@ -14,25 +14,26 @@
* limitations under the License.
*/
mod air;
mod instructions;
mod stepper;
mod stepper_outcome;
use instructions::Instruction;
use stepper_outcome::StepperOutcome;
use fluence::fce;
use log::info;
pub fn main() {
fluence::WasmLogger::init_with_level(log::Level::Info).unwrap();
}
#[fce]
pub struct StepperOutcome {
pub data: String,
pub next_peer_pks: Vec<String>,
}
#[fce]
pub fn invoke(init_user_id: String, aqua: String, data: String) -> StepperOutcome {
info!(
log::info!(
"stepper invoked with user_id = {}, aqua = {:?}, data = {:?}",
init_user_id, aqua, data
init_user_id,
aqua,
data
);
let outcome = StepperOutcome {
@ -40,5 +41,29 @@ pub fn invoke(init_user_id: String, aqua: String, data: String) -> StepperOutcom
next_peer_pks: vec![init_user_id],
};
let parsed_aqua = match serde_sexpr::from_str::<Vec<Instruction>>(&aqua) {
Ok(parsed) => parsed,
Err(e) => {
log::error!("supplied aqua script can't be parsed: {:?}", e);
return outcome;
}
};
log::info!("parsed_aqua: {:?}", parsed_aqua);
crate::stepper::execute(parsed_aqua);
outcome
}
#[fce]
pub struct CallServiceResult {
pub result: i32,
pub outcome: String,
}
#[fce]
#[link(wasm_import_module = "aqua_test_module")]
extern "C" {
pub fn call_service(service_id: String, fn_name: String, args: String) -> CallServiceResult;
}

31
src/stepper.rs Normal file
View File

@ -0,0 +1,31 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::Instruction;
use std::collections::HashMap;
pub(crate) trait ExecutableInstruction {
fn execute(self, data: &mut HashMap<String, Vec<u8>>);
}
pub(crate) fn execute(instructions: Vec<Instruction>) {
let mut data = HashMap::new();
for instruction in instructions {
instruction.execute(&mut data);
}
}

23
src/stepper_outcome.rs Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use fluence::fce;
#[fce]
pub struct StepperOutcome {
pub data: String,
pub next_peer_pks: Vec<String>,
}

22
test_module/main.rs Normal file
View File

@ -0,0 +1,22 @@
use fluence::fce;
fn main() {}
#[fce]
pub struct CallServiceResult {
pub result: i32,
pub outcome: Vec<u8>,
}
#[fce]
pub fn call_service(service_id: String, fn_name: String, args: Vec<u8>) -> CallServiceResult {
println!(
"call service invoked with:\n service_id: {}\n fn_name: {}\n args: {:?}",
service_id, fn_name, args
);
CallServiceResult {
result: 0,
outcome: vec![1, 2, 3],
}
}