From 1acf04611f8f16374d5acfd907e98b36b4c650c4 Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Mon, 16 Sep 2024 11:34:06 +0100 Subject: [PATCH] Break out relation decoding into own trait --- data-model/src/path.rs | 14 +++--- data-model/src/relative_encodings.rs | 48 ++++++++++++++++++- earthstar/src/identity_id.rs | 6 ++- earthstar/src/namespace_id.rs | 8 ++-- encoding/src/traits.rs | 10 ++++ encoding/src/traits_sync.rs | 10 ++++ .../mc_capability_rel_area_encoding_random.rs | 6 +-- fuzz/src/encode.rs | 6 +-- fuzz/src/placeholder_params.rs | 11 ++++- fuzz/src/silly_sigs.rs | 8 +++- 10 files changed, 102 insertions(+), 25 deletions(-) diff --git a/data-model/src/path.rs b/data-model/src/path.rs index bf490c3..2fdd86e 100644 --- a/data-model/src/path.rs +++ b/data-model/src/path.rs @@ -791,8 +791,8 @@ mod encoding { use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use willow_encoding::DecodeError; - #[syncify_replace(use willow_encoding::sync::{Decodable, Encodable};)] - use willow_encoding::{Decodable, Encodable}; + #[syncify_replace(use willow_encoding::sync::{Decodable, Encodable, RelationDecodable};)] + use willow_encoding::{Decodable, Encodable, RelationDecodable}; #[syncify_replace(use willow_encoding::sync::{decode_max_power, encode_max_power};)] use willow_encoding::{decode_max_power, encode_max_power}; @@ -854,13 +854,11 @@ mod encoding { Ok(unsafe { buf.to_path(component_count) }) } + } - async fn decode_relation

(producer: &mut P) -> Result> - where - P: BulkProducer, - { - Self::decode_canonical(producer).await - } + impl RelationDecodable + for Path + { } } diff --git a/data-model/src/relative_encodings.rs b/data-model/src/relative_encodings.rs index 4860392..e92e175 100644 --- a/data-model/src/relative_encodings.rs +++ b/data-model/src/relative_encodings.rs @@ -10,8 +10,11 @@ pub(super) mod encoding { use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use willow_encoding::{is_bitflagged, CompactWidth, DecodeError}; - #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable, RelativeDecodable, RelativeEncodable};)] - use willow_encoding::{Decodable, Encodable, RelativeDecodable, RelativeEncodable}; + #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable, RelativeDecodable, RelativeEncodable, RelationDecodable, RelativeRelationDecodable};)] + use willow_encoding::{ + Decodable, Encodable, RelationDecodable, RelativeDecodable, RelativeEncodable, + RelativeRelationDecodable, + }; #[syncify_replace(use willow_encoding::sync::{decode_max_power, encode_max_power};)] use willow_encoding::{decode_max_power, encode_max_power}; @@ -175,6 +178,11 @@ pub(super) mod encoding { } } + impl + RelativeRelationDecodable> for Path + { + } + // Entry <> Entry impl @@ -334,7 +342,15 @@ pub(super) mod encoding { payload_digest, )) } + } + impl + RelativeRelationDecodable> for Entry + where + N: NamespaceId + RelationDecodable + std::fmt::Debug, + S: SubspaceId + RelationDecodable + std::fmt::Debug, + PD: PayloadDigest + RelationDecodable, + { /// Decodes an [`Entry`] relative to the given reference [`Entry`]. /// /// [Definition](https://willowprotocol.org/specs/encodings/index.html#enc_etry_relative_entry). @@ -551,7 +567,15 @@ pub(super) mod encoding { payload_digest, )) } + } + impl + RelativeRelationDecodable<(N, Area)> for Entry + where + N: NamespaceId + RelationDecodable, + S: SubspaceId + RelationDecodable + std::fmt::Debug, + PD: PayloadDigest + RelationDecodable, + { /// Decodes an [`Entry`] relative to a reference [`NamespaceId`] and [`Area`]. /// /// [Definition](https://willowprotocol.org/specs/encodings/index.html#enc_entry_in_namespace_area). @@ -872,7 +896,15 @@ pub(super) mod encoding { payload_digest, )) } + } + impl + RelativeRelationDecodable<(N, Range3d)> for Entry + where + N: NamespaceId + RelationDecodable, + S: SubspaceId + RelationDecodable + std::fmt::Debug, + PD: PayloadDigest + RelationDecodable, + { /// Decodes an [`Entry`] relative to a reference [`NamespaceId`] and [`Range3d`]. /// /// [Definition](https://willowprotocol.org/specs/encodings/index.html#enc_entry_in_namespace_3drange). @@ -1209,7 +1241,13 @@ pub(super) mod encoding { Ok(Self::new(subspace, path, times)) } + } + impl + RelativeRelationDecodable> for Area + where + S: SubspaceId + RelationDecodable, + { /// Decodes an [`Area`] relative to another [`Area`] which [includes](https://willowprotocol.org/specs/grouping-entries/index.html#area_include_area) it. /// /// [Definition](https://willowprotocol.org/specs/encodings/index.html#enc_area_in_area). @@ -1822,7 +1860,13 @@ pub(super) mod encoding { }, )) } + } + impl + RelativeRelationDecodable> for Range3d + where + S: SubspaceId + RelationDecodable + std::fmt::Debug, + { /// Decodes a [`Range3d`] relative to another [`Range3d`] which [includes](https://willowprotocol.org/specs/grouping-entries/index.html#area_include_area) it. /// /// [Definition](https://willowprotocol.org/specs/encodings/index.html#enc_area_in_area). diff --git a/earthstar/src/identity_id.rs b/earthstar/src/identity_id.rs index aede138..7c9c501 100644 --- a/earthstar/src/identity_id.rs +++ b/earthstar/src/identity_id.rs @@ -24,8 +24,8 @@ pub(super) mod encoding { #[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)] use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use willow_encoding::DecodeError; - #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)] - use willow_encoding::{Decodable, Encodable}; + #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable, RelationDecodable};)] + use willow_encoding::{Decodable, Encodable, RelationDecodable}; impl Encodable for IdentityIdentifier { async fn encode(&self, consumer: &mut C) -> Result<(), C::Error> @@ -48,6 +48,8 @@ pub(super) mod encoding { } } } + + impl RelationDecodable for IdentityIdentifier {} } impl SubspaceId for IdentityIdentifier { diff --git a/earthstar/src/namespace_id.rs b/earthstar/src/namespace_id.rs index 7c8a309..23b58c4 100644 --- a/earthstar/src/namespace_id.rs +++ b/earthstar/src/namespace_id.rs @@ -26,8 +26,8 @@ pub(super) mod encoding { #[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)] use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use willow_encoding::DecodeError; - #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)] - use willow_encoding::{Decodable, Encodable}; + #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable, RelationDecodable};)] + use willow_encoding::{Decodable, Encodable, RelationDecodable}; impl Encodable for NamespaceIdentifier { async fn encode(&self, consumer: &mut C) -> Result<(), C::Error> @@ -40,7 +40,7 @@ pub(super) mod encoding { } impl Decodable for NamespaceIdentifier { - async fn decode_canonical

(producer: &mut P) -> Result::Error>> + async fn decode_canonical

(producer: &mut P) -> Result> where P: BulkProducer, { @@ -50,6 +50,8 @@ pub(super) mod encoding { } } } + + impl RelationDecodable for NamespaceIdentifier {} } impl NamespaceId for NamespaceIdentifier {} diff --git a/encoding/src/traits.rs b/encoding/src/traits.rs index 049795e..f9e9771 100644 --- a/encoding/src/traits.rs +++ b/encoding/src/traits.rs @@ -40,7 +40,12 @@ pub trait Decodable { where Producer: BulkProducer, Self: Sized; +} +/// A type which can be asynchronously decoded from bytes produced by a [`ufotofu::local_nb::BulkProducer`] +/// +/// [Definition](https://willowprotocol.org/specs/encodings/index.html#encodings_what) +pub trait RelationDecodable: Decodable { /// Decode a bytestring belonging to the **encoding relation** on the set of `Self` and the set of bytestrings, such that: /// /// - for every `s` in `Self`, there is at least one bytestring in relation with `s`, and @@ -97,7 +102,12 @@ pub trait RelativeDecodable { where Producer: BulkProducer, Self: Sized; +} +/// A type relative to a value of type `R` which can be asynchronously decoded from bytes (belonging to an encoding relation) produced by a [`ufotofu::local_nb::BulkProducer`] +/// +/// [Definition](https://willowprotocol.org/specs/encodings/index.html#encodings_what) +pub trait RelativeRelationDecodable: RelativeDecodable { /// Decode a bytestring belonging to the **encoding relation** on the set of `Self` relative to a value of the set of `R`, and the set of bytestrings, such that: /// /// - for every pair of `s` in `Self` and `r` in `R`, there is at least one bytestring in relation with the pair of `s` and `r`, and diff --git a/encoding/src/traits_sync.rs b/encoding/src/traits_sync.rs index 1317f5d..08fcd6a 100644 --- a/encoding/src/traits_sync.rs +++ b/encoding/src/traits_sync.rs @@ -35,7 +35,12 @@ pub trait Decodable { where Producer: BulkProducer, Self: Sized; +} +/// A type which can be decoded from bytes (belonging to an encoding relation) produced by a [`ufotofu::local_nb::BulkProducer`] +/// +/// [Definition](https://willowprotocol.org/specs/encodings/index.html#encodings_what) +pub trait RelationDecodable: Decodable { /// Decode a bytestring belonging to the **encoding relation** on the set of `Self` and the set of bytestrings, such that: /// /// - for every `s` in `Self`, there is at least one bytestring in relation with `s`, and @@ -92,7 +97,12 @@ pub trait RelativeDecodable { where Producer: BulkProducer, Self: Sized; +} +/// A type relative to a value of type `R` which can be decoded from bytes (belonging to an encoding relation) produced by a [`ufotofu::local_nb::BulkProducer`] +/// +/// [Definition](https://willowprotocol.org/specs/encodings/index.html#encodings_what) +pub trait RelativeRelationDecodable: RelativeDecodable { /// Decode a bytestring belonging to the **encoding relation** on the set of `Self` relative to a value of the set of `R`, and the set of bytestrings, such that: /// /// - for every pair of `s` in `Self` and `r` in `R`, there is at least one bytestring in relation with the pair of `s` and `r`, and diff --git a/fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs b/fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs index 2929927..d0cd633 100644 --- a/fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs +++ b/fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs @@ -3,14 +3,14 @@ use libfuzzer_sys::fuzz_target; use meadowcap::McCapability; use willow_data_model::grouping::Area; -use willow_fuzz::encode::relative_encoding_random; +use willow_fuzz::encode::relative_encoding_canonical_random; use willow_fuzz::silly_sigs::{SillyPublicKey, SillySig}; fuzz_target!(|data: (&[u8], Area<3, 3, 3, SillyPublicKey>,)| { let (random_bytes, out) = data; - relative_encoding_random::< + relative_encoding_canonical_random::< Area<3, 3, 3, SillyPublicKey>, McCapability<3, 3, 3, SillyPublicKey, SillySig, SillyPublicKey, SillySig>, - >(&out, random_bytes) + >(&out, random_bytes); }); diff --git a/fuzz/src/encode.rs b/fuzz/src/encode.rs index c7f8399..15ae568 100644 --- a/fuzz/src/encode.rs +++ b/fuzz/src/encode.rs @@ -8,7 +8,7 @@ use ufotofu::{ }; use willow_encoding::{ - sync::{Decodable, Encodable, RelativeDecodable, RelativeEncodable}, + sync::{Decodable, Encodable, RelativeDecodable, RelativeEncodable, RelativeRelationDecodable}, DecodeError, }; @@ -169,7 +169,7 @@ pub fn relative_encoding_canonical_roundtrip( pub fn relative_encoding_random(reference: &R, data: &[u8]) where - T: RelativeEncodable + RelativeDecodable + std::fmt::Debug + Eq, + T: RelativeEncodable + RelativeRelationDecodable + std::fmt::Debug + Eq, R: std::fmt::Debug, { let res_canonical = relative_encoding_canonical_random::(reference, data); @@ -227,7 +227,7 @@ where pub fn relative_encoding_relation_random(reference: &R, data: &[u8]) -> Option where - T: RelativeEncodable + RelativeDecodable + std::fmt::Debug + Eq, + T: RelativeEncodable + RelativeRelationDecodable + std::fmt::Debug + Eq, R: std::fmt::Debug, { let mut producer = FromSlice::new(data); diff --git a/fuzz/src/placeholder_params.rs b/fuzz/src/placeholder_params.rs index 1e84369..d1b1278 100644 --- a/fuzz/src/placeholder_params.rs +++ b/fuzz/src/placeholder_params.rs @@ -3,8 +3,11 @@ use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use ufotofu::sync::{BulkConsumer as BulkConsumerSync, BulkProducer as BulkProducerSync}; use willow_data_model::PayloadDigest; use willow_encoding::{ - sync::{Decodable as DecodableSync, Encodable as EncodableSync}, - Decodable, DecodeError, Encodable, + sync::{ + Decodable as DecodableSync, Encodable as EncodableSync, + RelationDecodable as RelationDecodableSync, + }, + Decodable, DecodeError, Encodable, RelationDecodable, }; #[derive(Arbitrary, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)] @@ -63,4 +66,8 @@ impl DecodableSync for FakePayloadDigest { } } +impl RelationDecodable for FakePayloadDigest {} + +impl RelationDecodableSync for FakePayloadDigest {} + impl PayloadDigest for FakePayloadDigest {} diff --git a/fuzz/src/silly_sigs.rs b/fuzz/src/silly_sigs.rs index 9fb32f5..aed8be1 100644 --- a/fuzz/src/silly_sigs.rs +++ b/fuzz/src/silly_sigs.rs @@ -84,8 +84,8 @@ pub mod encoding { use ufotofu::local_nb::{BulkConsumer, BulkProducer}; use willow_encoding::DecodeError; - #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)] - use willow_encoding::{Decodable, Encodable}; + #[syncify_replace(use willow_encoding::sync::{Encodable, Decodable, RelationDecodable};)] + use willow_encoding::{Decodable, Encodable, RelationDecodable}; #[syncify_replace(use willow_encoding::sync::produce_byte;)] use willow_encoding::produce_byte; @@ -137,4 +137,8 @@ pub mod encoding { Ok(SillySig(num)) } } + + impl RelationDecodable for SillyPublicKey {} + + impl RelationDecodable for SillySig {} }