Introduce restriction operator for streams (#172)

This commit is contained in:
Mike Voronov
2021-11-24 17:57:14 +03:00
committed by GitHub
parent 9f47eb9b83
commit 5cd45385b4
61 changed files with 2484 additions and 1224 deletions

View File

@ -15,13 +15,13 @@
*/
use super::ExecutedState;
use super::GlobalStreamGens;
use super::RestrictedStreamGens;
use super::DATA_FORMAT_VERSION;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::ops::Deref;
pub type StreamGenerations = HashMap<String, u32>;
pub type ExecutionTrace = Vec<ExecutedState>;
/// The AIR interpreter could be considered as a function
@ -33,9 +33,11 @@ pub struct InterpreterData {
/// Trace of AIR execution, which contains executed call, par and fold states.
pub trace: ExecutionTrace,
/// Contains maximum generation for each stream. This info will be used while merging
/// values in streams.
pub streams: StreamGenerations,
/// Contains maximum generation for each global stream. This info will be used while merging
/// values in streams. This field is also needed for backward compatibility with
/// <= 0.2.1 versions.
#[serde(rename = "streams")] // for compatibility with versions <= 0.2.1
pub global_streams: GlobalStreamGens,
/// Version of this data format.
pub version: semver::Version,
@ -44,28 +46,37 @@ pub struct InterpreterData {
#[serde(default)]
#[serde(rename = "lcid")]
pub last_call_request_id: u32,
/// Contains maximum generation for each private stream. This info will be used while merging
/// values in streams.
#[serde(default)]
#[serde(rename = "r_streams")]
pub restricted_streams: RestrictedStreamGens,
}
impl InterpreterData {
pub fn new() -> Self {
Self {
trace: <_>::default(),
streams: <_>::default(),
global_streams: <_>::default(),
version: DATA_FORMAT_VERSION.deref().clone(),
last_call_request_id: 0,
restricted_streams: <_>::default(),
}
}
pub fn from_execution_result(
trace: ExecutionTrace,
streams: StreamGenerations,
streams: GlobalStreamGens,
restricted_streams: RestrictedStreamGens,
last_call_request_id: u32,
) -> Self {
Self {
trace,
streams,
global_streams: streams,
version: DATA_FORMAT_VERSION.deref().clone(),
last_call_request_id,
restricted_streams,
}
}
@ -98,14 +109,14 @@ mod tests {
#[derive(Serialize, Deserialize)]
struct InterpreterData0_2_0 {
pub trace: ExecutionTrace,
pub streams: StreamGenerations,
pub streams: GlobalStreamGens,
pub version: semver::Version,
}
// test 0.2.0 to 0.2.1 conversion
// test 0.2.0 to 0.2.2 conversion
let data_0_2_0 = InterpreterData0_2_0 {
trace: ExecutionTrace::default(),
streams: StreamGenerations::default(),
streams: GlobalStreamGens::default(),
version: semver::Version::new(0, 2, 0),
};
@ -113,10 +124,41 @@ mod tests {
let data_0_2_1 = serde_json::from_slice::<InterpreterData>(&data_0_2_0_se);
assert!(data_0_2_1.is_ok());
// test 0.2.1 to 0.2.1 conversion
let data_0_2_1 = InterpreterData::default();
// test 0.2.2 to 0.2.0 conversion
let data_0_2_2 = InterpreterData::default();
let data_0_2_2_se = serde_json::to_vec(&data_0_2_2).unwrap();
let data_0_2_0 = serde_json::from_slice::<InterpreterData0_2_0>(&data_0_2_2_se);
assert!(data_0_2_0.is_ok());
}
#[test]
fn compatible_with_0_2_1_version() {
#[derive(Serialize, Deserialize)]
struct InterpreterData0_2_1 {
pub trace: ExecutionTrace,
pub streams: GlobalStreamGens,
pub version: semver::Version,
#[serde(default)]
#[serde(rename = "lcid")]
pub last_call_request_id: u32,
}
// test 0.2.1 to 0.2.2 conversion
let data_0_2_1 = InterpreterData0_2_1 {
trace: ExecutionTrace::default(),
streams: GlobalStreamGens::default(),
version: semver::Version::new(0, 2, 1),
last_call_request_id: 1,
};
let data_0_2_1_se = serde_json::to_vec(&data_0_2_1).unwrap();
let data_0_2_0 = serde_json::from_slice::<InterpreterData0_2_0>(&data_0_2_1_se);
let data_0_2_2 = serde_json::from_slice::<InterpreterData>(&data_0_2_1_se);
assert!(data_0_2_2.is_ok());
// test 0.2.2 to 0.2.1 conversion
let data_0_2_2 = InterpreterData::default();
let data_0_2_2_se = serde_json::to_vec(&data_0_2_2).unwrap();
let data_0_2_0 = serde_json::from_slice::<InterpreterData0_2_1>(&data_0_2_2_se);
assert!(data_0_2_0.is_ok());
}
}

View File

@ -14,15 +14,28 @@
* limitations under the License.
*/
#![warn(rust_2018_idioms)]
#![deny(
dead_code,
nonstandard_style,
unused_imports,
unused_mut,
unused_variables,
unused_unsafe,
unreachable_patterns
)]
mod executed_state;
mod interpreter_data;
mod stream_generations;
pub use executed_state::*;
pub use interpreter_data::*;
pub use stream_generations::*;
use once_cell::sync::Lazy;
use std::str::FromStr;
pub static DATA_FORMAT_VERSION: Lazy<semver::Version> = Lazy::new(|| {
semver::Version::from_str("0.2.1").expect("invalid data format version specified")
semver::Version::from_str("0.2.2").expect("invalid data format version specified")
});

View File

@ -0,0 +1,31 @@
/*
* Copyright 2021 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 std::collections::HashMap;
/// Mapping from a stream name to it's generation count.
/// Similar to pi-calculus non-restricted names/channels.
pub type GlobalStreamGens = HashMap<String, u32>;
/// Mapping from a stream name to
/// position of a new instruction in a script that creates a scope for a stream
/// to vector where each position represents a corresponding iteration.
///
/// Vec<u32> is needed because a new instruction could be placed into a fold instruction,
/// so it could be met several times during script execution. This field anchors iteration
/// where it was met.
/// Similar to pi-calculus restricted names/channels.
pub type RestrictedStreamGens = HashMap<String, HashMap<u32, Vec<u32>>>;