From 91775b22eac544132b4a0cc2ffc9efedd674a390 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Thu, 26 Oct 2023 15:22:45 +0200 Subject: [PATCH] sealable-trie: remove dependency on CryptoHash being borsh-serialisable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need to change sealable-trie to support multiple borsh crate versions. Since it’s borsh-serialising CryptoHash, this by extension means that lib needs to support all the same versions of borsh. To avoid the latter, rather than serialising and deserialising CryptoHash, deal with the underlying arrays of bytes. This makes sealable-trie code slightly more verbose but removes the need for lib to support multiple borsh versions. (We’ll see whether the support will need to be eventually added. So far this simplifies stuff a bit). --- common/lib/src/hash.rs | 4 ++++ common/sealable-trie/Cargo.toml | 3 --- .../sealable-trie/src/proof/serialisation.rs | 21 ++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/common/lib/src/hash.rs b/common/lib/src/hash.rs index 8e375f4d..707397e8 100644 --- a/common/lib/src/hash.rs +++ b/common/lib/src/hash.rs @@ -85,6 +85,10 @@ impl CryptoHash { Self(buf) } + /// Returns a shared reference to the underlying bytes array. + #[inline] + pub fn as_array(&self) -> &[u8; Self::LENGTH] { &self.0 } + /// Returns a shared reference to the hash as slice of bytes. #[inline] pub fn as_slice(&self) -> &[u8] { &self.0[..] } diff --git a/common/sealable-trie/Cargo.toml b/common/sealable-trie/Cargo.toml index 43c32cc7..78ff3013 100644 --- a/common/sealable-trie/Cargo.toml +++ b/common/sealable-trie/Cargo.toml @@ -22,6 +22,3 @@ rand.workspace = true lib = { workspace = true, features = ["test_utils"] } memory = { workspace = true, features = ["test_utils"] } - -[features] -borsh = ["dep:borsh", "lib/borsh"] diff --git a/common/sealable-trie/src/proof/serialisation.rs b/common/sealable-trie/src/proof/serialisation.rs index 31e28b79..e70c3fee 100644 --- a/common/sealable-trie/src/proof/serialisation.rs +++ b/common/sealable-trie/src/proof/serialisation.rs @@ -102,8 +102,10 @@ impl BorshDeserialize for Proof { impl BorshSerialize for Item { fn serialize(&self, wr: &mut W) -> io::Result<()> { match self { - Self::Branch(child) => (u8::from(child.is_value) << 4, &child.hash), - Self::Value(hash) => (0x30, hash), + Self::Branch(child) => { + (u8::from(child.is_value) << 4, child.hash.as_array()) + } + Self::Value(hash) => (0x30, hash.as_array()), Self::Extension(key_len) => { // to_be_bytes rather than borsh’s serialise because it’s part // of tag so we need to keep most significant byte first. @@ -141,7 +143,7 @@ fn deserialize_item_cont( .ok_or_else(|| invalid_data("empty Item::Extension".into())) .map(Item::Extension) } - 0x30 => CryptoHash::deserialize_reader(rd).map(Item::Value), + 0x30 => Ok(Item::Value(CryptoHash(<_>::deserialize_reader(rd)?))), _ => Err(invalid_data(format!("invalid Item tag: {first}"))), } } @@ -155,7 +157,8 @@ impl BorshSerialize for Actual { match self { Self::Branch(left, right) => { let vv = u8::from(left.is_value) * 2 + u8::from(right.is_value); - ((0x80 | vv), &left.hash, &right.hash).serialize(wr) + ((0x80 | vv), left.hash.as_array(), right.hash.as_array()) + .serialize(wr) } Self::Extension(left, key, child) => { (0x84 | u8::from(child.is_value)).serialize(wr)?; @@ -163,10 +166,10 @@ impl BorshSerialize for Actual { // Note: We’re not encoding length of the bytes slice since it // can be recovered from the contents of the bytes slice. wr.write_all(key)?; - child.hash.serialize(wr) + child.hash.as_array().serialize(wr) } Self::LookupKeyLeft(left, hash) => { - (0x86u8, left, &hash).serialize(wr) + (0x86u8, left, hash.as_array()).serialize(wr) } } } @@ -224,7 +227,7 @@ fn deserialize_actual_cont( Ok(Actual::Extension(left, key, child)) } 0x86 => BorshDeserialize::deserialize_reader(rd) - .map(|(left, hash)| Actual::LookupKeyLeft(left, hash)), + .map(|(left, hash)| Actual::LookupKeyLeft(left, CryptoHash(hash))), _ => Err(invalid_data(format!("invalid Actual tag: {first}"))), } } @@ -276,7 +279,9 @@ fn deserialize_owned_ref( rd: &mut impl io::Read, is_value: bool, ) -> io::Result { - CryptoHash::deserialize_reader(rd).map(|hash| OwnedRef { is_value, hash }) + <_>::deserialize_reader(rd) + .map(CryptoHash) + .map(|hash| OwnedRef { is_value, hash }) } /// Returns an `io::Error` of kind `InvalidData` with specified message.