Skip to content

Commit

Permalink
Break out relation decoding into own trait
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Sep 16, 2024
1 parent d15b46d commit 1acf046
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 25 deletions.
14 changes: 6 additions & 8 deletions data-model/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -854,13 +854,11 @@ mod encoding {

Ok(unsafe { buf.to_path(component_count) })
}
}

async fn decode_relation<P>(producer: &mut P) -> Result<Self, DecodeError<P::Error>>
where
P: BulkProducer<Item = u8>,
{
Self::decode_canonical(producer).await
}
impl<const MCL: usize, const MCC: usize, const MPL: usize> RelationDecodable
for Path<MCL, MCC, MPL>
{
}
}

Expand Down
48 changes: 46 additions & 2 deletions data-model/src/relative_encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -175,6 +178,11 @@ pub(super) mod encoding {
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize>
RelativeRelationDecodable<Path<MCL, MCC, MPL>> for Path<MCL, MCC, MPL>
{
}

// Entry <> Entry

impl<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>
Expand Down Expand Up @@ -334,7 +342,15 @@ pub(super) mod encoding {
payload_digest,
))
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>
RelativeRelationDecodable<Entry<MCL, MCC, MPL, N, S, PD>> for Entry<MCL, MCC, MPL, N, S, PD>
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).
Expand Down Expand Up @@ -551,7 +567,15 @@ pub(super) mod encoding {
payload_digest,
))
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>
RelativeRelationDecodable<(N, Area<MCL, MCC, MPL, S>)> for Entry<MCL, MCC, MPL, N, S, PD>
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).
Expand Down Expand Up @@ -872,7 +896,15 @@ pub(super) mod encoding {
payload_digest,
))
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize, N, S, PD>
RelativeRelationDecodable<(N, Range3d<MCL, MCC, MPL, S>)> for Entry<MCL, MCC, MPL, N, S, PD>
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).
Expand Down Expand Up @@ -1209,7 +1241,13 @@ pub(super) mod encoding {

Ok(Self::new(subspace, path, times))
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize, S>
RelativeRelationDecodable<Area<MCL, MCC, MPL, S>> for Area<MCL, MCC, MPL, S>
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).
Expand Down Expand Up @@ -1822,7 +1860,13 @@ pub(super) mod encoding {
},
))
}
}

impl<const MCL: usize, const MCC: usize, const MPL: usize, S>
RelativeRelationDecodable<Range3d<MCL, MCC, MPL, S>> for Range3d<MCL, MCC, MPL, S>
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).
Expand Down
6 changes: 4 additions & 2 deletions earthstar/src/identity_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>(&self, consumer: &mut C) -> Result<(), C::Error>
Expand All @@ -48,6 +48,8 @@ pub(super) mod encoding {
}
}
}

impl RelationDecodable for IdentityIdentifier {}
}

impl SubspaceId for IdentityIdentifier {
Expand Down
8 changes: 5 additions & 3 deletions earthstar/src/namespace_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>(&self, consumer: &mut C) -> Result<(), C::Error>
Expand All @@ -40,7 +40,7 @@ pub(super) mod encoding {
}

impl Decodable for NamespaceIdentifier {
async fn decode_canonical<P>(producer: &mut P) -> Result<Self, DecodeError<<P>::Error>>
async fn decode_canonical<P>(producer: &mut P) -> Result<Self, DecodeError<P::Error>>
where
P: BulkProducer<Item = u8>,
{
Expand All @@ -50,6 +50,8 @@ pub(super) mod encoding {
}
}
}

impl RelationDecodable for NamespaceIdentifier {}
}

impl NamespaceId for NamespaceIdentifier {}
10 changes: 10 additions & 0 deletions encoding/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ pub trait Decodable {
where
Producer: BulkProducer<Item = u8>,
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
Expand Down Expand Up @@ -97,7 +102,12 @@ pub trait RelativeDecodable<R> {
where
Producer: BulkProducer<Item = u8>,
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<R>: RelativeDecodable<R> {
/// 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
Expand Down
10 changes: 10 additions & 0 deletions encoding/src/traits_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ pub trait Decodable {
where
Producer: BulkProducer<Item = u8>,
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
Expand Down Expand Up @@ -92,7 +97,12 @@ pub trait RelativeDecodable<R> {
where
Producer: BulkProducer<Item = u8>,
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<R>: RelativeDecodable<R> {
/// 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
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
6 changes: 3 additions & 3 deletions fuzz/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ufotofu::{
};

use willow_encoding::{
sync::{Decodable, Encodable, RelativeDecodable, RelativeEncodable},
sync::{Decodable, Encodable, RelativeDecodable, RelativeEncodable, RelativeRelationDecodable},
DecodeError,
};

Expand Down Expand Up @@ -169,7 +169,7 @@ pub fn relative_encoding_canonical_roundtrip<T, R, C>(

pub fn relative_encoding_random<R, T>(reference: &R, data: &[u8])
where
T: RelativeEncodable<R> + RelativeDecodable<R> + std::fmt::Debug + Eq,
T: RelativeEncodable<R> + RelativeRelationDecodable<R> + std::fmt::Debug + Eq,
R: std::fmt::Debug,
{
let res_canonical = relative_encoding_canonical_random::<R, T>(reference, data);
Expand Down Expand Up @@ -227,7 +227,7 @@ where

pub fn relative_encoding_relation_random<R, T>(reference: &R, data: &[u8]) -> Option<T>
where
T: RelativeEncodable<R> + RelativeDecodable<R> + std::fmt::Debug + Eq,
T: RelativeEncodable<R> + RelativeRelationDecodable<R> + std::fmt::Debug + Eq,
R: std::fmt::Debug,
{
let mut producer = FromSlice::new(data);
Expand Down
11 changes: 9 additions & 2 deletions fuzz/src/placeholder_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -63,4 +66,8 @@ impl DecodableSync for FakePayloadDigest {
}
}

impl RelationDecodable for FakePayloadDigest {}

impl RelationDecodableSync for FakePayloadDigest {}

impl PayloadDigest for FakePayloadDigest {}
8 changes: 6 additions & 2 deletions fuzz/src/silly_sigs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,4 +137,8 @@ pub mod encoding {
Ok(SillySig(num))
}
}

impl RelationDecodable for SillyPublicKey {}

impl RelationDecodable for SillySig {}
}

0 comments on commit 1acf046

Please sign in to comment.