-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix: StateMap::Keys are not consistent across platforms (#804)
* Bug fix: Introduce EncodeLike trait. This PR removes our StateMap key encoding based on std::Hash, which was not consistent across platforms. Instead, this PR introduces the `EncodeLike<Ref, Target>` trait which marks that Ref can be encoded like Target by the implementing codec. This PR also removes the SingletonKey type, which required special handling in codecs. Instead, of using this placeholder, this PR implements auxiliary methods on the working set for dealing with singletons * Remove rollup config changes * fix test: qualify conversion * Add missing bounds for fuzzing * fix docs * clarify zsts in comment * allow separate codecs for keys/values * Split key and value codecs * Fix fuzzing feature * Introduce StateCodec trait to allow EncodeLike with SplitCodec * add doc comments * Fix fuzz and test targets * Fix test * fix docs
- Loading branch information
1 parent
6b93031
commit 44db227
Showing
17 changed files
with
520 additions
and
216 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,42 @@ | ||
use super::{StateCodec, StateKeyCodec}; | ||
use crate::codec::StateValueCodec; | ||
|
||
/// A [`StateValueCodec`] that uses [`bcs`] for all keys and values. | ||
#[derive(Debug, Default, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)] | ||
pub struct BcsCodec; | ||
|
||
impl<K> StateKeyCodec<K> for BcsCodec | ||
where | ||
K: serde::Serialize, | ||
{ | ||
fn encode_key(&self, key: &K) -> Vec<u8> { | ||
bcs::to_bytes(key).expect("Failed to serialize key") | ||
} | ||
} | ||
|
||
impl<V> StateValueCodec<V> for BcsCodec | ||
where | ||
V: serde::Serialize + for<'a> serde::Deserialize<'a>, | ||
{ | ||
type Error = bcs::Error; | ||
|
||
fn encode_value(&self, value: &V) -> Vec<u8> { | ||
bcs::to_bytes(value).expect("Failed to serialize key") | ||
bcs::to_bytes(value).expect("Failed to serialize value") | ||
} | ||
|
||
fn try_decode_value(&self, bytes: &[u8]) -> Result<V, Self::Error> { | ||
bcs::from_bytes(bytes) | ||
} | ||
} | ||
|
||
impl StateCodec for BcsCodec { | ||
type KeyCodec = Self; | ||
type ValueCodec = Self; | ||
fn key_codec(&self) -> &Self::KeyCodec { | ||
self | ||
} | ||
|
||
fn value_codec(&self) -> &Self::ValueCodec { | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.