Skip to content

Commit

Permalink
sealable-trie: remove dependency on CryptoHash being borsh-serialisable
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mina86 committed Oct 26, 2023
1 parent e477a1f commit 91775b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 4 additions & 0 deletions common/lib/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..] }
Expand Down
3 changes: 0 additions & 3 deletions common/sealable-trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
21 changes: 13 additions & 8 deletions common/sealable-trie/src/proof/serialisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ impl BorshDeserialize for Proof {
impl BorshSerialize for Item {
fn serialize<W: io::Write>(&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.
Expand Down Expand Up @@ -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}"))),
}
}
Expand All @@ -155,18 +157,19 @@ 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)?;
left.serialize(wr)?;
// 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)
}
}
}
Expand Down Expand Up @@ -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}"))),
}
}
Expand Down Expand Up @@ -276,7 +279,9 @@ fn deserialize_owned_ref(
rd: &mut impl io::Read,
is_value: bool,
) -> io::Result<OwnedRef> {
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.
Expand Down

0 comments on commit 91775b2

Please sign in to comment.