Skip to content

Commit

Permalink
feat(avm-server,air-interpreter,aquavm-air)!: secret key and particle…
Browse files Browse the repository at this point in the history
… ID arguments (#593)

* feat(avm-server)!: keypair and particle ID arguments

Add `&fluence_keypair::KeyPair` argument to `AVM::call` and
`AVMRunner::call`.  This value is further forwarded in a deconstructed
form to WASM Air interpreter, but is not used there yet. Also,
`AVMRunner::call` gets `particle_id: String` argument.

feat(air-interpreter)!: `invoke` methods have three new arguments:
`key_format: u8`, `secret_key_bytes: Vec<u8>` and `paritcle_id: String`.

feat(aquavm-air): `air::execute_air` has two three arguments:
`key_format: u8`, `secret_key_bytes: Vec<u8>` and `paritcle_id: String`.

feat(aquavm-air-cli)!: add `--random-key`/`--ed25519-key file` options to AIR CLI.

* feat(avm-server)!: Add `RunnerError::KeypairError`

* chore(bench): Add signature performance benchmarks

These benchmarks contain valid signature, so they should work with
verification out of the box.

---------

Co-authored-by: Artsiom Shamsutdzinau <[email protected]>
Co-authored-by: folex <[email protected]>
  • Loading branch information
3 people authored Jun 22, 2023
1 parent 93c6899 commit 8ce8af3
Show file tree
Hide file tree
Showing 73 changed files with 896 additions and 253 deletions.
23 changes: 10 additions & 13 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 air-interpreter/src/marine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn invoke(
execute_air(air, prev_data, data, params, call_results)
}

#[allow(clippy::too_many_arguments)]
#[marine]
pub fn invoke_tracing(
air: String,
Expand Down
1 change: 1 addition & 0 deletions air-interpreter/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn main() {
log::set_max_level(LevelFilter::Info);
}

#[allow(clippy::too_many_arguments)]
#[wasm_bindgen]
pub fn invoke(
air: String,
Expand Down
1 change: 1 addition & 0 deletions air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tracing = "0.1.37"
air-test-utils = { path = "../crates/air-lib/test-utils" }
air-testing-framework = { path = "../crates/testing-framework" }
fluence-app-service = "0.25.0"
fluence-keypair = "0.10.1"
marine-rs-sdk = { version = "0.7.0", features = ["logger"] }

# the feature just silence a warning in the criterion 0.3.x.
Expand Down
2 changes: 1 addition & 1 deletion air/benches/call_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ thread_local!(static SCRIPT: String = String::from(
);

fn current_peer_id_call() -> Result<RawAVMOutcome, String> {
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1);
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1, "");
VM.with(|vm| SCRIPT.with(|script| vm.borrow_mut().call(script, "", "", run_parameters)))
}

Expand Down
2 changes: 1 addition & 1 deletion air/benches/chat_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn chat_sent_message_benchmark() -> Result<RawAVMOutcome, String> {
)
"#;

let run_parameters = TestRunParameters::new("A", 0, 1);
let run_parameters = TestRunParameters::new("A", 0, 1, "");
let result = CLIENT_1_VM
.with(|vm| vm.borrow_mut().call(script, "", "", run_parameters.clone()))
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion air/benches/create_service_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn create_service_benchmark() -> Result<RawAVMOutcome, String> {
)
)"#;

let run_parameters1 = TestRunParameters::new("set_variables", 0, 1);
let run_parameters1 = TestRunParameters::new("set_variables", 0, 1, "");
let run_parameters2 = run_parameters1.clone();
let result = SET_VARIABLES_VM
.with(|vm| vm.borrow_mut().call(script, "", "", run_parameters1))
Expand Down
2 changes: 1 addition & 1 deletion air/benches/data_big_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SCRIPT: &str = include_str!("data/big.air");
const VALUES_DATA: &str = include_str!("data/anomaly_big.json");

fn data_big_calls(prev_data: &str, current_data: &str) -> Result<RawAVMOutcome, String> {
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1);
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1, "");
VM.with(|vm| vm.borrow_mut().call(SCRIPT, prev_data, current_data, run_parameters))
}

Expand Down
2 changes: 1 addition & 1 deletion air/benches/data_long_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SCRIPT: &str = "(par (null) (null))";
const VALUES_DATA: &str = include_str!("data/anomaly_long.json");

fn data_long_calls(prev_data: &str, current_data: &str) -> Result<RawAVMOutcome, String> {
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1);
let run_parameters = TestRunParameters::new("test_peer_id", 0, 1, "");
VM.with(|vm| vm.borrow_mut().call(SCRIPT, prev_data, current_data, run_parameters))
}

Expand Down
6 changes: 3 additions & 3 deletions air/src/execution_step/execution_context/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'i> ExecutionCtx<'i> {
prev_ingredients: ExecCtxIngredients,
current_ingredients: ExecCtxIngredients,
call_results: CallResults,
run_parameters: RunParameters,
run_parameters: &RunParameters,
) -> Self {
let run_parameters = RcRunParameters::from_run_parameters(run_parameters);
let streams = Streams::from_data(
Expand Down Expand Up @@ -184,10 +184,10 @@ pub(crate) struct RcRunParameters {
}

impl RcRunParameters {
pub(crate) fn from_run_parameters(run_parameters: RunParameters) -> Self {
pub(crate) fn from_run_parameters(run_parameters: &RunParameters) -> Self {
Self {
init_peer_id: run_parameters.init_peer_id.as_str().into(),
current_peer_id: Rc::new(run_parameters.current_peer_id),
current_peer_id: Rc::new(run_parameters.current_peer_id.clone()),
timestamp: run_parameters.timestamp,
ttl: run_parameters.ttl,
}
Expand Down
7 changes: 7 additions & 0 deletions air/src/preparation_step/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ pub enum PreparationError {
actual_version: semver::Version,
required_version: semver::Version,
},

/// Malformed keypair format data.
#[error("malformed keypair format: {error:?}")]
MalformedKeyPairData {
#[from]
error: fluence_keypair::error::DecodingError,
},
}

impl ToErrorCode for PreparationError {
Expand Down
13 changes: 11 additions & 2 deletions air/src/preparation_step/preparation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::execution_step::TraceHandler;
use air_interpreter_data::InterpreterData;
use air_interpreter_interface::RunParameters;
use air_parser::ast::Instruction;
use fluence_keypair::KeyFormat;
use fluence_keypair::KeyPair;

use std::convert::TryFrom;

type PreparationResult<T> = Result<T, PreparationError>;

Expand All @@ -30,6 +34,7 @@ pub(crate) struct PreparationDescriptor<'ctx, 'i> {
pub(crate) exec_ctx: ExecutionCtx<'ctx>,
pub(crate) trace_handler: TraceHandler,
pub(crate) air: Instruction<'i>,
pub(crate) keypair: KeyPair,
}

/// Parse and prepare supplied data and AIR script.
Expand Down Expand Up @@ -64,13 +69,17 @@ pub(crate) fn prepare<'i>(
signature_store: current_data.signatures,
};

let exec_ctx = make_exec_ctx(prev_ingredients, current_ingredients, call_results, run_parameters)?;
let exec_ctx = make_exec_ctx(prev_ingredients, current_ingredients, call_results, &run_parameters)?;
let trace_handler = TraceHandler::from_trace(prev_data.trace, current_data.trace);

let key_format = KeyFormat::try_from(run_parameters.key_format)?;
let keypair = KeyPair::from_secret_key(run_parameters.secret_key_bytes, key_format)?;

let result = PreparationDescriptor {
exec_ctx,
trace_handler,
air,
keypair,
};

Ok(result)
Expand Down Expand Up @@ -98,7 +107,7 @@ fn make_exec_ctx(
prev_ingredients: ExecCtxIngredients,
current_ingredients: ExecCtxIngredients,
call_results: &[u8],
run_parameters: RunParameters,
run_parameters: &RunParameters,
) -> PreparationResult<ExecutionCtx<'static>> {
let call_results = serde_json::from_slice(call_results)
.map_err(|e| PreparationError::call_results_de_failed(call_results.to_vec(), e))?;
Expand Down
5 changes: 1 addition & 4 deletions air/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::preparation_step::PreparationDescriptor;

use air_interpreter_interface::InterpreterOutcome;
use air_interpreter_interface::RunParameters;
use air_interpreter_signatures::derive_dummy_keypair;
use air_log_targets::RUN_PARAMS;
use air_utils::measure;

Expand Down Expand Up @@ -56,13 +55,11 @@ fn execute_air_impl(
params: RunParameters,
call_results: Vec<u8>,
) -> Result<InterpreterOutcome, InterpreterOutcome> {
// TODO STUB this is a stub key that is to be replaced by external one in other PR
let (keypair, _) = derive_dummy_keypair(&params.current_peer_id);

let PreparationDescriptor {
mut exec_ctx,
mut trace_handler,
air,
keypair,
} = match prepare(&prev_data, &data, air.as_str(), &call_results, params) {
Ok(descriptor) => descriptor,
// return the prev data in case of errors
Expand Down
6 changes: 4 additions & 2 deletions air/tests/test_module/features/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

use air_interpreter_signatures::{derive_dummy_keypair, SignatureTracker};
use air_test_framework::{ephemeral::PeerId, AirScriptExecutor};
use air_interpreter_signatures::SignatureTracker;
use air_test_framework::ephemeral::PeerId;
use air_test_framework::AirScriptExecutor;
use air_test_utils::key_utils::derive_dummy_keypair;
use air_test_utils::prelude::*;
use air_test_utils::test_runner::TestRunParameters;

Expand Down
2 changes: 1 addition & 1 deletion air/tests/test_module/instructions/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn call_with_timestamp() {

let script = r#"(call %init_peer_id% ("" "") [%timestamp%] result_name)"#;

let test_params = TestRunParameters::new(vm_peer_id, 1337, 0);
let test_params = TestRunParameters::new(vm_peer_id, 1337, 0, "");
let result = checked_call_vm!(vm, test_params.clone(), script, "", "");

let actual_trace = trace_from_result(&result);
Expand Down
Loading

0 comments on commit 8ce8af3

Please sign in to comment.