From de4625b8d304d4ea63721e06c920914e763667b7 Mon Sep 17 00:00:00 2001 From: bkolad Date: Thu, 7 Sep 2023 11:16:26 +0200 Subject: [PATCH] Add serde support --- .../sov-state/src/containers/accessory_map.rs | 10 +++++++- .../src/containers/accessory_value.rs | 11 ++++++++- .../sov-state/src/containers/accessory_vec.rs | 23 +++++++++++++------ module-system/sov-state/src/containers/map.rs | 10 +++++++- .../sov-state/src/containers/value.rs | 11 +++++++-- module-system/sov-state/src/containers/vec.rs | 22 ++++++++++++------ module-system/sov-state/src/lib.rs | 11 ++++++++- module-system/sov-state/src/utils.rs | 11 ++++++++- 8 files changed, 88 insertions(+), 21 deletions(-) diff --git a/module-system/sov-state/src/containers/accessory_map.rs b/module-system/sov-state/src/containers/accessory_map.rs index bb487b7b3..0f13fb023 100644 --- a/module-system/sov-state/src/containers/accessory_map.rs +++ b/module-system/sov-state/src/containers/accessory_map.rs @@ -15,7 +15,15 @@ use crate::{AccessoryWorkingSet, Prefix, StateReaderAndWriter, Storage}; /// - a key type `K`; /// - a value type `V`; /// - a [`StateValueCodec`] `VC`. -#[derive(Debug, Clone, PartialEq, borsh::BorshDeserialize, borsh::BorshSerialize)] +#[derive( + Debug, + Clone, + PartialEq, + borsh::BorshDeserialize, + borsh::BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct AccessoryStateMap { _phantom: (PhantomData, PhantomData), value_codec: VC, diff --git a/module-system/sov-state/src/containers/accessory_value.rs b/module-system/sov-state/src/containers/accessory_value.rs index bc43e578e..2c95a8200 100644 --- a/module-system/sov-state/src/containers/accessory_value.rs +++ b/module-system/sov-state/src/containers/accessory_value.rs @@ -8,7 +8,16 @@ use crate::{AccessoryWorkingSet, Prefix, StateReaderAndWriter, Storage}; /// Container for a single value stored as "accessory" state, outside of the /// JMT. -#[derive(Debug, PartialEq, Eq, Clone, BorshDeserialize, BorshSerialize)] +#[derive( + Debug, + PartialEq, + Eq, + Clone, + BorshDeserialize, + BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct AccessoryStateValue { _phantom: PhantomData, codec: VC, diff --git a/module-system/sov-state/src/containers/accessory_vec.rs b/module-system/sov-state/src/containers/accessory_vec.rs index bd96eb873..b7ff94fd3 100644 --- a/module-system/sov-state/src/containers/accessory_vec.rs +++ b/module-system/sov-state/src/containers/accessory_vec.rs @@ -6,14 +6,22 @@ use crate::{ AccessoryStateMap, AccessoryStateValue, AccessoryWorkingSet, Prefix, StateVecError, Storage, }; -#[derive(Debug, Clone)] +#[derive( + Debug, + Clone, + PartialEq, + borsh::BorshDeserialize, + borsh::BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct AccessoryStateVec where VC: StateValueCodec, { _phantom: PhantomData, prefix: Prefix, - len_value: AccessoryStateValue, + len_value: AccessoryStateValue, elems: AccessoryStateMap, } @@ -30,7 +38,7 @@ where impl AccessoryStateVec where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, { /// Creates a new [`AccessoryStateVec`] with the given prefix and codec. pub fn with_codec(prefix: Prefix, codec: VC) -> Self { @@ -38,7 +46,8 @@ where // shouldn't be necessary, but it's best not to rely on implementation // details of `StateValue` and `StateMap` as they both have the right to // reserve the whole key space for themselves. - let len_value = AccessoryStateValue::::new(prefix.extended(b"l")); + let len_value = + AccessoryStateValue::::with_codec(prefix.extended(b"l"), codec.clone()); let elems = AccessoryStateMap::with_codec(prefix.extended(b"e"), codec); Self { _phantom: PhantomData, @@ -187,7 +196,7 @@ where impl<'a, 'ws, V, VC, S> Iterator for AccessoryStateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { type Item = V; @@ -204,7 +213,7 @@ where impl<'a, 'ws, V, VC, S> ExactSizeIterator for AccessoryStateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { fn len(&self) -> usize { @@ -214,7 +223,7 @@ where impl<'a, 'ws, V, VC, S> FusedIterator for AccessoryStateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { } diff --git a/module-system/sov-state/src/containers/map.rs b/module-system/sov-state/src/containers/map.rs index f64d04206..f596b293d 100644 --- a/module-system/sov-state/src/containers/map.rs +++ b/module-system/sov-state/src/containers/map.rs @@ -15,7 +15,15 @@ use crate::{Prefix, StateReaderAndWriter, Storage, WorkingSet}; /// - a key type `K`; /// - a value type `V`; /// - a [`StateValueCodec`] `VC`. -#[derive(Debug, Clone, PartialEq, borsh::BorshDeserialize, borsh::BorshSerialize)] +#[derive( + Debug, + Clone, + PartialEq, + borsh::BorshDeserialize, + borsh::BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct StateMap { _phantom: (PhantomData, PhantomData), value_codec: VC, diff --git a/module-system/sov-state/src/containers/value.rs b/module-system/sov-state/src/containers/value.rs index 832ae499e..c4d0c26ab 100644 --- a/module-system/sov-state/src/containers/value.rs +++ b/module-system/sov-state/src/containers/value.rs @@ -1,13 +1,20 @@ use std::marker::PhantomData; -use borsh::{BorshDeserialize, BorshSerialize}; use thiserror::Error; use crate::codec::{BorshCodec, StateValueCodec}; use crate::{Prefix, StateReaderAndWriter, Storage, WorkingSet}; /// Container for a single value. -#[derive(Debug, PartialEq, Eq, Clone, BorshDeserialize, BorshSerialize)] +#[derive( + Debug, + Clone, + PartialEq, + borsh::BorshDeserialize, + borsh::BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct StateValue { _phantom: PhantomData, codec: VC, diff --git a/module-system/sov-state/src/containers/vec.rs b/module-system/sov-state/src/containers/vec.rs index 95ff45903..1d0b3b8c9 100644 --- a/module-system/sov-state/src/containers/vec.rs +++ b/module-system/sov-state/src/containers/vec.rs @@ -6,14 +6,22 @@ use thiserror::Error; use crate::codec::{BorshCodec, StateValueCodec}; use crate::{Prefix, StateMap, StateValue, Storage, WorkingSet}; -#[derive(Debug, Clone)] +#[derive( + Debug, + Clone, + PartialEq, + borsh::BorshDeserialize, + borsh::BorshSerialize, + serde::Serialize, + serde::Deserialize, +)] pub struct StateVec where VC: StateValueCodec, { _phantom: PhantomData, prefix: Prefix, - len_value: StateValue, + len_value: StateValue, elems: StateMap, } @@ -39,7 +47,7 @@ where impl StateVec where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, { /// Creates a new [`StateVec`] with the given prefix and codec. pub fn with_codec(prefix: Prefix, codec: VC) -> Self { @@ -47,7 +55,7 @@ where // shouldn't be necessary, but it's best not to rely on implementation // details of `StateValue` and `StateMap` as they both have the right to // reserve the whole key space for themselves. - let len_value = StateValue::::new(prefix.extended(b"l")); + let len_value = StateValue::with_codec(prefix.extended(b"l"), codec.clone()); let elems = StateMap::with_codec(prefix.extended(b"e"), codec); Self { _phantom: PhantomData, @@ -192,7 +200,7 @@ where impl<'a, 'ws, V, VC, S> Iterator for StateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { type Item = V; @@ -209,7 +217,7 @@ where impl<'a, 'ws, V, VC, S> ExactSizeIterator for StateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { fn len(&self) -> usize { @@ -219,7 +227,7 @@ where impl<'a, 'ws, V, VC, S> FusedIterator for StateVecIter<'a, 'ws, V, VC, S> where - VC: StateValueCodec, + VC: StateValueCodec + StateValueCodec + Clone, S: Storage, { } diff --git a/module-system/sov-state/src/lib.rs b/module-system/sov-state/src/lib.rs index 0fe0974d5..9be0bfc5a 100644 --- a/module-system/sov-state/src/lib.rs +++ b/module-system/sov-state/src/lib.rs @@ -40,7 +40,16 @@ pub use crate::witness::{ArrayWitness, TreeWitnessReader, Witness}; // All the collection types in this crate are backed by the same storage instance, this means that insertions of the same key // to two different `StorageMaps` would collide with each other. We solve it by instantiating every collection type with a unique // prefix that is prepended to each key. -#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Eq, Clone)] +#[derive( + borsh::BorshDeserialize, + borsh::BorshSerialize, + Debug, + PartialEq, + Eq, + Clone, + serde::Serialize, + serde::Deserialize, +)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct Prefix { prefix: AlignedVec, diff --git a/module-system/sov-state/src/utils.rs b/module-system/sov-state/src/utils.rs index 1dfc152a7..985c990ce 100644 --- a/module-system/sov-state/src/utils.rs +++ b/module-system/sov-state/src/utils.rs @@ -2,7 +2,16 @@ // This makes certain operations cheaper in zk-context (concatenation) // TODO: Currently the implementation defaults to `stc::vec::Vec` see: // https://github.com/Sovereign-Labs/sovereign-sdk/issues/47 -#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Eq, Clone)] +#[derive( + borsh::BorshDeserialize, + borsh::BorshSerialize, + Debug, + PartialEq, + Eq, + Clone, + serde::Serialize, + serde::Deserialize, +)] pub struct AlignedVec { inner: Vec, }