mirror of
https://github.com/fluencelabs/aquavm
synced 2025-07-03 08:31:34 +00:00
feat(data)!: flexible serialization formats (#757)
New crate air_interpreter_sede introduces a flexible serialization and deserialization infrastructure. Low level API: the Format trait and related. It includes simple and universal mechanisms for serializing any possible (e.g. implementing the Serde traits) value. High level API: Representation trait and related. It allows declaring representation types that can serialize and deserialize only certain types (for example, InterpreterDataRepr can serialize only the InterpreterData, but deserialize InterpreterData and Versions` types), producing newtype values as serialization results. The serialized representation of CallResults now contains byte arrays, not strings, because the new infrastructure serializes to byte arrays only. Also, the basic support for multicodecs is added.
This commit is contained in:
@ -14,14 +14,70 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use air_interpreter_sede::define_simple_representation;
|
||||
use air_interpreter_sede::derive_serialized_type;
|
||||
use air_interpreter_sede::Format;
|
||||
use air_interpreter_sede::FromSerialized;
|
||||
use air_interpreter_sede::JsonFormat;
|
||||
use air_interpreter_sede::Representation;
|
||||
|
||||
use marine_call_parameters::SecurityTetraplet;
|
||||
#[cfg(feature = "marine")]
|
||||
use marine_rs_sdk::marine;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub type CallRequests = HashMap<u32, CallRequestParams>;
|
||||
|
||||
derive_serialized_type!(SerializedCallArguments);
|
||||
derive_serialized_type!(SerializedTetraplets);
|
||||
derive_serialized_type!(SerializedCallRequests);
|
||||
|
||||
pub type CallArgumentsFormat = JsonFormat;
|
||||
pub type TetrapletsFormat = JsonFormat;
|
||||
pub type CallRequestsFormat = JsonFormat;
|
||||
|
||||
define_simple_representation! {
|
||||
CallArgumentsRepr,
|
||||
Vec<serde_json::Value>,
|
||||
CallArgumentsFormat,
|
||||
SerializedCallArguments
|
||||
}
|
||||
|
||||
pub type CallArgumentsDeserializeError = <CallArgumentsRepr as Representation>::DeserializeError;
|
||||
|
||||
define_simple_representation! {
|
||||
TetrapletsRepr,
|
||||
// additional implementation for Vec<Vec<SecurityTetraplet>> is defined below
|
||||
// TODO allow this macro to define implementations for multiple types
|
||||
Vec<Vec<Rc<SecurityTetraplet>>>,
|
||||
TetrapletsFormat,
|
||||
SerializedTetraplets
|
||||
}
|
||||
|
||||
pub type TetrapletDeserializeError = <TetrapletsRepr as Representation>::DeserializeError;
|
||||
|
||||
define_simple_representation! {
|
||||
CallRequestsRepr,
|
||||
CallRequests,
|
||||
CallRequestsFormat,
|
||||
SerializedCallRequests
|
||||
}
|
||||
|
||||
pub type CallRequestsDeserializeError = <CallRequestsRepr as Representation>::DeserializeError;
|
||||
|
||||
impl FromSerialized<Vec<Vec<SecurityTetraplet>>> for TetrapletsRepr {
|
||||
fn deserialize(
|
||||
&self,
|
||||
repr: &[u8],
|
||||
) -> Result<Vec<Vec<SecurityTetraplet>>, Self::DeserializeError> {
|
||||
CallArgumentsRepr.get_format().from_slice(repr)
|
||||
}
|
||||
}
|
||||
|
||||
/// Contains arguments of a call instruction and all other necessary information
|
||||
/// required for calling a service.
|
||||
#[cfg_attr(feature = "marine", marine)]
|
||||
@ -34,18 +90,18 @@ pub struct CallRequestParams {
|
||||
pub function_name: String,
|
||||
|
||||
/// Serialized to JSON string Vec<JValue> of arguments that should be passed to a service.
|
||||
pub arguments: String,
|
||||
pub arguments: SerializedCallArguments,
|
||||
|
||||
/// Serialized to JSON string Vec<Vec<SecurityTetraplet>> that should be passed to a service.
|
||||
pub tetraplets: String,
|
||||
pub tetraplets: SerializedTetraplets,
|
||||
}
|
||||
|
||||
impl CallRequestParams {
|
||||
pub fn new(
|
||||
service_id: String,
|
||||
function_name: String,
|
||||
arguments: String,
|
||||
tetraplets: String,
|
||||
arguments: SerializedCallArguments,
|
||||
tetraplets: SerializedTetraplets,
|
||||
) -> Self {
|
||||
Self {
|
||||
service_id,
|
||||
|
@ -14,6 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use air_interpreter_sede::define_simple_representation;
|
||||
use air_interpreter_sede::derive_serialized_type;
|
||||
use air_interpreter_sede::JsonFormat;
|
||||
use air_interpreter_sede::Representation;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value as JValue;
|
||||
@ -22,6 +26,20 @@ use std::collections::HashMap;
|
||||
pub type CallResults = HashMap<u32, CallServiceResult>;
|
||||
pub const CALL_SERVICE_SUCCESS: i32 = 0;
|
||||
|
||||
pub type CallResultsFormat = JsonFormat;
|
||||
|
||||
derive_serialized_type!(SerializedCallResults);
|
||||
|
||||
define_simple_representation! {
|
||||
CallResultsRepr,
|
||||
CallResults,
|
||||
CallResultsFormat,
|
||||
SerializedCallResults
|
||||
}
|
||||
|
||||
pub type CallResultsDeserializeError = <CallResultsRepr as Representation>::DeserializeError;
|
||||
pub type CallResultsSerializeError = <CallResultsRepr as Representation>::SerializeError;
|
||||
|
||||
/// Represents an executed host function result.
|
||||
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
||||
pub struct CallServiceResult {
|
||||
|
@ -51,8 +51,9 @@ impl InterpreterOutcome {
|
||||
error_message: String,
|
||||
data: Vec<u8>,
|
||||
next_peer_pks: Vec<String>,
|
||||
call_requests: Vec<u8>,
|
||||
call_requests: SerializedCallRequests,
|
||||
) -> Self {
|
||||
let call_requests = call_requests.into();
|
||||
Self {
|
||||
ret_code,
|
||||
error_message,
|
||||
@ -81,7 +82,13 @@ impl InterpreterOutcome {
|
||||
let error_message = try_as_string(record_values.pop().unwrap(), "error_message")?;
|
||||
let ret_code = try_as_i64(record_values.pop().unwrap(), "ret_code")?;
|
||||
|
||||
let outcome = Self::new(ret_code, error_message, data, next_peer_pks, call_requests);
|
||||
let outcome = Self::new(
|
||||
ret_code,
|
||||
error_message,
|
||||
data,
|
||||
next_peer_pks,
|
||||
call_requests.into(),
|
||||
);
|
||||
|
||||
Ok(outcome)
|
||||
}
|
||||
@ -90,6 +97,8 @@ impl InterpreterOutcome {
|
||||
#[cfg(feature = "marine")]
|
||||
use fluence_it_types::ne_vec::NEVec;
|
||||
|
||||
use crate::SerializedCallRequests;
|
||||
|
||||
#[cfg(feature = "marine")]
|
||||
fn try_as_record(ivalue: IValue) -> Result<NEVec<IValue>, String> {
|
||||
match ivalue {
|
||||
|
Reference in New Issue
Block a user