introduce xor (#8)

This commit is contained in:
vms 2020-10-09 09:52:36 +03:00 committed by GitHub
parent b1b875db53
commit e107d8e716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 136 additions and 15 deletions

30
Cargo.lock generated
View File

@ -440,14 +440,6 @@ dependencies = [
"wasmer-wasi-fl",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-wit 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.2.8"
@ -458,13 +450,11 @@ dependencies = [
]
[[package]]
name = "fluence-sdk-main"
name = "fluence-sdk-macro"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
"fluence-sdk-wit 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
]
[[package]]
@ -479,9 +469,20 @@ dependencies = [
]
[[package]]
name = "fluence-sdk-wit"
name = "fluence-sdk-main"
version = "0.2.8"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"fluence-sdk-macro 0.2.8 (git+https://github.com/fluencelabs/rust-sdk)",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "560baf91197ded38a99a5c94ff366a3dd971ebf33f5d987ecce31d3dedf86d17"
dependencies = [
"proc-macro2",
"quote",
@ -494,8 +495,7 @@ dependencies = [
[[package]]
name = "fluence-sdk-wit"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "560baf91197ded38a99a5c94ff366a3dd971ebf33f5d987ecce31d3dedf86d17"
source = "git+https://github.com/fluencelabs/rust-sdk#4d6c4f6b862c22ebd8db7244daac0adf3f1bd2fd"
dependencies = [
"proc-macro2",
"quote",

View File

@ -19,6 +19,7 @@ mod fold;
mod null;
mod par;
mod seq;
mod xor;
use crate::AquaData;
use crate::Result;
@ -29,6 +30,7 @@ use fold::Next;
use null::Null;
use par::Par;
use seq::Seq;
use xor::Xor;
use serde_derive::Deserialize;
use serde_derive::Serialize;
@ -62,6 +64,7 @@ pub(crate) enum Instruction {
Next(Next),
Par(Par),
Seq(Seq),
Xor(Xor),
}
pub(crate) trait ExecutableInstruction {
@ -77,6 +80,7 @@ impl ExecutableInstruction for Instruction {
Instruction::Next(next) => next.execute(ctx),
Instruction::Par(par) => par.execute(ctx),
Instruction::Seq(seq) => seq.execute(ctx),
Instruction::Xor(xor) => xor.execute(ctx),
}
}
}

117
stepper/src/air/xor.rs Normal file
View File

@ -0,0 +1,117 @@
/*
* 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 super::ExecutionContext;
use super::Instruction;
use crate::AquamarineError;
use crate::Result;
use serde_derive::Deserialize;
use serde_derive::Serialize;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub(crate) struct Xor(Box<Instruction>, Box<Instruction>);
impl super::ExecutableInstruction for Xor {
fn execute(&self, ctx: &mut ExecutionContext) -> Result<()> {
log::info!("xor is called with context: {:?}", ctx);
match self.0.execute(ctx) {
Err(AquamarineError::LocalServiceError(_)) => self.1.execute(ctx),
res => res,
}
}
}
#[cfg(test)]
mod tests {
use crate::JValue;
use aqua_test_utils::create_aqua_vm;
use aquamarine_vm::vec1::Vec1;
use aquamarine_vm::HostExportedFunc;
use aquamarine_vm::IValue;
use serde_json::json;
#[test]
fn xor() {
let call_service: HostExportedFunc = Box::new(|_, args| -> Option<IValue> {
let builtin_service = match &args[0] {
IValue::String(str) => str,
_ => unreachable!(),
};
if builtin_service == "service_id_1" {
// return a error for service with id service_id_1
Some(IValue::Record(
Vec1::new(vec![IValue::S32(1), IValue::String(String::from("{}"))]).unwrap(),
))
} else {
// return success for services with other ids
Some(IValue::Record(
Vec1::new(vec![
IValue::S32(0),
IValue::String(String::from("\"res\"")),
])
.unwrap(),
))
}
});
let mut vm = create_aqua_vm(call_service);
let script = String::from(
r#"
(xor (
(call (%current_peer_id% (service_id_1 local_fn_name) () result_1))
(call (%current_peer_id% (service_id_2 local_fn_name) () result_2))
))"#,
);
let res = vm
.call(json!([
String::from("asd"),
script,
json!({"arg3": "arg3_value"}).to_string(),
]))
.expect("call should be successful");
let jdata: JValue = serde_json::from_str(&res.data).expect("should be valid json");
assert_eq!(jdata["result_2"], json!("res"));
let script = String::from(
r#"
(xor (
(call (%current_peer_id% (service_id_2 local_fn_name) () result_1))
(call (%current_peer_id% (service_id_1 local_fn_name) () result_2))
))"#,
);
let res = vm
.call(json!([
String::from("asd"),
script,
json!({"arg3": "arg3_value"}).to_string(),
]))
.expect("call should be successful");
let jdata: JValue = serde_json::from_str(&res.data).expect("should be valid json");
assert_eq!(jdata["result_1"], json!("res"));
}
}