Skip to content

Commit

Permalink
Add serde support (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad authored and preston-evans98 committed Sep 14, 2023
1 parent bb1aebf commit 8afae70
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 21 deletions.
10 changes: 9 additions & 1 deletion module-system/sov-state/src/containers/accessory_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V, VC = BorshCodec> {
_phantom: (PhantomData<K>, PhantomData<V>),
value_codec: VC,
Expand Down
11 changes: 10 additions & 1 deletion module-system/sov-state/src/containers/accessory_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V, VC = BorshCodec> {
_phantom: PhantomData<V>,
codec: VC,
Expand Down
23 changes: 16 additions & 7 deletions module-system/sov-state/src/containers/accessory_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V, VC = BorshCodec>
where
VC: StateValueCodec<V>,
{
_phantom: PhantomData<V>,
prefix: Prefix,
len_value: AccessoryStateValue<usize>,
len_value: AccessoryStateValue<usize, VC>,
elems: AccessoryStateMap<usize, V, VC>,
}

Expand All @@ -30,15 +38,16 @@ where

impl<V, VC> AccessoryStateVec<V, VC>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
{
/// Creates a new [`AccessoryStateVec`] with the given prefix and codec.
pub fn with_codec(prefix: Prefix, codec: VC) -> Self {
// Differentiating the prefixes for the length and the elements
// 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::<usize>::new(prefix.extended(b"l"));
let len_value =
AccessoryStateValue::<usize, VC>::with_codec(prefix.extended(b"l"), codec.clone());
let elems = AccessoryStateMap::with_codec(prefix.extended(b"e"), codec);
Self {
_phantom: PhantomData,
Expand Down Expand Up @@ -187,7 +196,7 @@ where

impl<'a, 'ws, V, VC, S> Iterator for AccessoryStateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
type Item = V;
Expand All @@ -204,7 +213,7 @@ where

impl<'a, 'ws, V, VC, S> ExactSizeIterator for AccessoryStateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
fn len(&self) -> usize {
Expand All @@ -214,7 +223,7 @@ where

impl<'a, 'ws, V, VC, S> FusedIterator for AccessoryStateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
}
Expand Down
10 changes: 9 additions & 1 deletion module-system/sov-state/src/containers/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V, VC = BorshCodec> {
_phantom: (PhantomData<K>, PhantomData<V>),
value_codec: VC,
Expand Down
11 changes: 9 additions & 2 deletions module-system/sov-state/src/containers/value.rs
Original file line number Diff line number Diff line change
@@ -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<V, VC = BorshCodec> {
_phantom: PhantomData<V>,
codec: VC,
Expand Down
22 changes: 15 additions & 7 deletions module-system/sov-state/src/containers/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<V, VC = BorshCodec>
where
VC: StateValueCodec<V>,
{
_phantom: PhantomData<V>,
prefix: Prefix,
len_value: StateValue<usize>,
len_value: StateValue<usize, VC>,
elems: StateMap<usize, V, VC>,
}

Expand All @@ -39,15 +47,15 @@ where

impl<V, VC> StateVec<V, VC>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
{
/// Creates a new [`StateVec`] with the given prefix and codec.
pub fn with_codec(prefix: Prefix, codec: VC) -> Self {
// Differentiating the prefixes for the length and the elements
// 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::<usize>::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,
Expand Down Expand Up @@ -192,7 +200,7 @@ where

impl<'a, 'ws, V, VC, S> Iterator for StateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
type Item = V;
Expand All @@ -209,7 +217,7 @@ where

impl<'a, 'ws, V, VC, S> ExactSizeIterator for StateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
fn len(&self) -> usize {
Expand All @@ -219,7 +227,7 @@ where

impl<'a, 'ws, V, VC, S> FusedIterator for StateVecIter<'a, 'ws, V, VC, S>
where
VC: StateValueCodec<V>,
VC: StateValueCodec<V> + StateValueCodec<usize> + Clone,
S: Storage,
{
}
Expand Down
11 changes: 10 additions & 1 deletion module-system/sov-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion module-system/sov-state/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
}
Expand Down

0 comments on commit 8afae70

Please sign in to comment.