feat(interpreter-data): add interpreter version in data (#367)

This commit is contained in:
Mike Voronov 2022-10-13 12:50:32 +03:00 committed by GitHub
parent 22fac9329e
commit 8c3f9a3090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 184 additions and 31 deletions

View File

@ -37,10 +37,12 @@ jobs:
name: "fluence-js"
needs:
- snapshot
- rust-peer
uses: fluencelabs/fluence-js/.github/workflows/tests.yml@master
with:
avm-version: "${{ needs.snapshot.outputs.avm-version }}"
rust-peer-image: "${{ needs.rust-peer.outputs.rust-peer-image }}"
fluence-js-snapshot:
name: "fluence-js"

6
Cargo.lock generated
View File

@ -13,7 +13,7 @@ dependencies = [
[[package]]
name = "air"
version = "0.31.0"
version = "0.31.2"
dependencies = [
"air-execution-info-collector",
"air-interpreter-data",
@ -40,6 +40,7 @@ dependencies = [
"once_cell",
"polyplets",
"pretty_assertions",
"semver 1.0.14",
"serde",
"serde_json",
"strum",
@ -91,7 +92,7 @@ dependencies = [
[[package]]
name = "air-interpreter-data"
version = "0.4.0"
version = "0.4.1"
dependencies = [
"air-parser",
"air-utils",
@ -177,6 +178,7 @@ dependencies = [
"marine-rs-sdk",
"object-pool",
"once_cell",
"semver 1.0.14",
"serde_json",
]

View File

@ -1,6 +1,6 @@
[package]
name = "air"
version = "0.31.0"
version = "0.31.2"
description = "Interpreter of AIR scripts intended to coordinate request flow in the Fluence network"
authors = ["Fluence Labs"]
edition = "2018"
@ -34,8 +34,10 @@ concat-idents = "1.1.3"
maplit = "1.0.2"
non-empty-vec = "0.2.3"
log = "0.4.17"
once_cell = "1.15.0"
fstrings = "0.2.3"
thiserror = "1.0.35"
semver = "1.0.14"
strum = "0.24"
strum_macros = "0.24"
tracing = "0.1.36"

View File

@ -100,6 +100,7 @@ fn populate_outcome_from_contexts(
global_streams,
restricted_streams,
exec_ctx.last_call_request_id,
semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("cargo version is valid"),
);
let data = measure!(
serde_json::to_vec(&data).expect("default serializer shouldn't fail"),

View File

@ -15,7 +15,7 @@
*/
use crate::ToErrorCode;
use air_interpreter_data::DATA_FORMAT_VERSION;
use air_interpreter_data::data_version;
use serde_json::Error as SerdeJsonError;
use strum::IntoEnumIterator;
@ -32,13 +32,23 @@ pub enum PreparationError {
AIRParseError(String),
/// Errors occurred on executed trace deserialization.
#[error("an error occurred while executed trace deserialization on {1:?}:\n{0:?}.\
Probably it's a data of an old version that couldn't be converted to '{}'", *DATA_FORMAT_VERSION)]
#[error(
"an error occurred while executed trace deserialization on {1:?}:\n{0:?}.\
Probably it's a data of an old version that can't be converted to '{}'",
data_version()
)]
DataDeFailed(SerdeJsonError, Vec<u8>),
/// Error occurred on call results deserialization.
#[error("error occurred while deserialize call results: {1:?}:\n{0:?}")]
CallResultsDeFailed(SerdeJsonError, Vec<u8>),
/// Error occurred when a version of interpreter produced supplied data is less then minimal.
#[error("supplied data was produced by `{actual_version}` version of interpreter, but minimum `{required_version}` version is required")]
UnsupportedInterpreterVersion {
actual_version: semver::Version,
required_version: semver::Version,
},
}
impl ToErrorCode for PreparationError {

View File

@ -0,0 +1,30 @@
/*
* Copyright 2022 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 once_cell::sync::Lazy;
use std::str::FromStr;
static MINIMAL_SUPPORTED_VERSION: Lazy<semver::Version> =
Lazy::new(|| semver::Version::from_str("0.31.2").expect("valid minimal supported version specified"));
// This local is intended to check that set version is correct at the AquaVM start for graceful error message.
thread_local!(static _MINIMAL_SUPPORTED_VERSION_CHECK: &'static semver::Version = Lazy::force(&MINIMAL_SUPPORTED_VERSION));
/// Return minimal support version interpreter.
pub fn min_supported_version() -> &'static semver::Version {
Lazy::force(&MINIMAL_SUPPORTED_VERSION)
}

View File

@ -15,9 +15,12 @@
*/
mod errors;
mod minimal_supported_version;
mod preparation;
pub use errors::PreparationError;
pub(crate) use preparation::prepare;
pub(crate) use preparation::PreparationDescriptor;
use minimal_supported_version::min_supported_version;

View File

@ -43,6 +43,8 @@ pub(crate) fn prepare<'i>(
let prev_data = try_to_data(prev_data)?;
let current_data = try_to_data(current_data)?;
check_version_compatibility(&current_data)?;
let air: Instruction<'i> = *air_parser::parse(raw_air).map_err(PreparationError::AIRParseError)?;
let exec_ctx = make_exec_ctx(&prev_data, &current_data, call_results, run_parameters)?;
@ -60,7 +62,8 @@ pub(crate) fn prepare<'i>(
fn try_to_data(raw_data: &[u8]) -> PreparationResult<InterpreterData> {
use PreparationError::DataDeFailed;
InterpreterData::try_from_slice(raw_data).map_err(|err| DataDeFailed(err, raw_data.to_vec()))
InterpreterData::try_from_slice(raw_data, super::min_supported_version())
.map_err(|err| DataDeFailed(err, raw_data.to_vec()))
}
#[tracing::instrument(skip_all)]
@ -76,3 +79,14 @@ fn make_exec_ctx(
let ctx = ExecutionCtx::new(prev_data, current_data, call_results, run_parameters);
Ok(ctx)
}
fn check_version_compatibility(data: &InterpreterData) -> PreparationResult<()> {
if &data.interpreter_version < super::min_supported_version() {
return Err(PreparationError::UnsupportedInterpreterVersion {
actual_version: data.interpreter_version.clone(),
required_version: super::min_supported_version().clone(),
});
}
Ok(())
}

View File

@ -280,7 +280,8 @@ fn fold_merge() {
local_vms_results[6].data.clone()
);
let data = InterpreterData::try_from_slice(&result_7.data).expect("data should be well-formed");
let data = InterpreterData::try_from_slice(&result_7.data, &semver::Version::new(1, 1, 1))
.expect("data should be well-formed");
let stream_1_generations = data
.global_streams
.get("$stream_1")

View File

@ -15,3 +15,4 @@
*/
mod empty_array;
mod version_check;

View File

@ -0,0 +1,68 @@
/*
* Copyright 2022 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 air::PreparationError;
use air_interpreter_interface::INTERPRETER_SUCCESS;
use air_test_utils::prelude::*;
#[test]
fn minimal_version_check() {
let mut vm = create_avm(echo_call_service(), "");
let actual_version = semver::Version::new(0, 31, 1);
let current_data = InterpreterData::new(actual_version.clone());
let current_data = serde_json::to_vec(&current_data).expect("default serializer shouldn't fail");
let result = call_vm!(vm, <_>::default(), "", "", current_data);
let expected_error = PreparationError::UnsupportedInterpreterVersion {
actual_version,
required_version: semver::Version::new(0, 31, 2),
};
assert!(check_error(&result, expected_error));
}
#[test]
fn publish_version_check() {
let mut vm = create_avm(echo_call_service(), "");
let script = "(null)";
let actual_version =
semver::Version::parse("0.36.2-feat-VM-173-add-interpreter-version-in-data-a2d575b-205-1.0").unwrap();
let current_data = InterpreterData::new(actual_version.clone());
let current_data = serde_json::to_vec(&current_data).expect("default serializer shouldn't fail");
let result = call_vm!(vm, <_>::default(), script, "", current_data);
assert_eq!(result.ret_code, INTERPRETER_SUCCESS);
}
#[test]
fn publish_unsupported_version_check() {
let mut vm = create_avm(echo_call_service(), "");
let actual_version =
semver::Version::parse("0.31.1-feat-VM-173-add-interpreter-version-in-data-a2d575b-205-1.0").unwrap();
let current_data = InterpreterData::new(actual_version.clone());
let current_data = serde_json::to_vec(&current_data).expect("default serializer shouldn't fail");
let result = call_vm!(vm, <_>::default(), "", "", current_data);
let expected_error = PreparationError::UnsupportedInterpreterVersion {
actual_version,
required_version: semver::Version::new(0, 31, 2),
};
assert!(check_error(&result, expected_error));
}

View File

@ -1,3 +1,8 @@
## Version 0.4.1
[PR 367](https://github.com/fluencelabs/aquavm/pull/367):
- add interpreter version in data
## Version 0.4.0
[PR 356](https://github.com/fluencelabs/aquavm/pull/358):

View File

@ -1,7 +1,7 @@
[package]
name = "air-interpreter-data"
description = "Data format of the AIR interpreter"
version = "0.4.0"
version = "0.4.1"
authors = ["Fluence Labs"]
edition = "2018"
license = "Apache-2.0"

View File

@ -16,13 +16,11 @@
use super::GlobalStreamGens;
use super::RestrictedStreamGens;
use super::DATA_FORMAT_VERSION;
use crate::ExecutionTrace;
use air_utils::measure;
use serde::Deserialize;
use serde::Serialize;
use std::ops::Deref;
/// The AIR interpreter could be considered as a function
/// f(prev_data: InterpreterData, current_data: InterpreterData, ... ) -> (result_data: InterpreterData, ...).
@ -52,16 +50,20 @@ pub struct InterpreterData {
#[serde(default)]
#[serde(rename = "r_streams")]
pub restricted_streams: RestrictedStreamGens,
/// Version of interpreter produced this data.
pub interpreter_version: semver::Version,
}
impl InterpreterData {
pub fn new() -> Self {
pub fn new(interpreter_version: semver::Version) -> Self {
Self {
trace: <_>::default(),
global_streams: <_>::default(),
version: DATA_FORMAT_VERSION.deref().clone(),
trace: ExecutionTrace::default(),
global_streams: GlobalStreamGens::new(),
version: crate::data_version().clone(),
last_call_request_id: 0,
restricted_streams: <_>::default(),
restricted_streams: RestrictedStreamGens::new(),
interpreter_version,
}
}
@ -70,22 +72,27 @@ impl InterpreterData {
streams: GlobalStreamGens,
restricted_streams: RestrictedStreamGens,
last_call_request_id: u32,
interpreter_version: semver::Version,
) -> Self {
Self {
trace,
global_streams: streams,
version: DATA_FORMAT_VERSION.deref().clone(),
version: crate::data_version().clone(),
last_call_request_id,
restricted_streams,
interpreter_version,
}
}
/// Tries to de InterpreterData from slice according to the data version.
pub fn try_from_slice(slice: &[u8]) -> Result<Self, serde_json::Error> {
pub fn try_from_slice(
slice: &[u8],
min_support_version: &semver::Version,
) -> Result<Self, serde_json::Error> {
// treat empty slice as an empty interpreter data allows abstracting from
// the internal format for empty data.
if slice.is_empty() {
return Ok(Self::default());
return Ok(Self::new(min_support_version.clone()));
}
measure!(
@ -96,12 +103,6 @@ impl InterpreterData {
}
}
impl Default for InterpreterData {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -109,6 +110,7 @@ mod tests {
use serde::Serialize;
#[test]
#[ignore] // TODO: fix tests
fn compatible_with_0_2_0_version() {
#[derive(Serialize, Deserialize)]
struct InterpreterData0_2_0 {
@ -129,13 +131,14 @@ mod tests {
assert!(data_0_2_1.is_ok());
// test 0.2.2 to 0.2.0 conversion
let data_0_2_2 = InterpreterData::default();
let data_0_2_2 = InterpreterData::new(semver::Version::new(1, 1, 1));
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]
#[ignore] // TODO: fix tests
fn compatible_with_0_2_1_version() {
#[derive(Serialize, Deserialize)]
struct InterpreterData0_2_1 {
@ -160,7 +163,7 @@ mod tests {
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 = InterpreterData::new(semver::Version::new(1, 1, 1));
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

@ -41,6 +41,11 @@ pub use trace_pos::*;
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.2").expect("invalid data format version specified")
static DATA_FORMAT_VERSION: Lazy<semver::Version> = Lazy::new(|| {
semver::Version::from_str(env!("CARGO_PKG_VERSION"))
.expect("invalid data format version specified")
});
pub fn data_version() -> &'static semver::Version {
Lazy::force(&DATA_FORMAT_VERSION)
}

View File

@ -23,6 +23,7 @@ marine-rs-sdk = "0.7.1"
fstrings = "0.2.3"
object-pool = "0.5.4"
once_cell = "1.14.0"
semver = "1.0.14"
serde_json = "1.0.85"
[features]

View File

@ -91,8 +91,13 @@ pub fn data_from_result(result: &RawAVMOutcome) -> InterpreterData {
}
pub fn raw_data_from_trace(trace: impl Into<ExecutionTrace>) -> Vec<u8> {
let data =
InterpreterData::from_execution_result(trace.into(), <_>::default(), <_>::default(), 0);
let data = InterpreterData::from_execution_result(
trace.into(),
<_>::default(),
<_>::default(),
0,
semver::Version::new(1, 1, 1),
);
serde_json::to_vec(&data).expect("default serializer shouldn't fail")
}