Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changes done while porting iroh-willow #45

Merged
merged 7 commits into from
Aug 19, 2024

Conversation

Frando
Copy link
Contributor

@Frando Frando commented Aug 12, 2024

This PR contains changes I made to willow-rs while porting iroh-willow to use willow-rs.

The changes are mostly small and not all related to each other. They were mostly done by doing jump to definition from iroh-willow when touching willow-rs parts that didn't fit in well yet.

I will add review comments in the diff to explain the motivation.

My idea was to submit this here, let you review it, and if anything warrants further discussion, I will separate it out into its own PR, to keep only uncontroversial things in here that can easily be merged.

If you prefer to split this up right away, let me know.

@@ -13,7 +13,7 @@ pub type Timestamp = u64;

/// The metadata associated with each Payload.
/// [Definition](https://willowprotocol.org/specs/data-model/index.html#Entry).
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Ord, PartialOrd)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the derived Ord should work as expected, as the derive will sort fields in definition order, which is the same order I deemed logical in my impl before.

I mostly need this to stick entries into a BTreeSet or such, to have predictable ordering.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the impl of PartialOrd and Ord should have a doc comment stating that the order is not according to the spec but simply an arbitrary choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment that details the sort order, and swapped the field ordering on the struct so that payload_digest comes before payload_length, so that the derived Ord matches the "is newer" relation from the willow spec (for entries where namespace, subspace and path are equal).

@@ -16,11 +16,16 @@ pub struct AreaOfInterest<const MCL: usize, const MCC: usize, const MPL: usize,
impl<const MCL: usize, const MCC: usize, const MPL: usize, S: SubspaceId>
AreaOfInterest<MCL, MCC, MPL, S>
{
/// Creates a new [`AreaOfInterest`].
pub fn new(area: Area<MCL, MCC, MPL, S>, max_count: u64, max_size: u64) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just easier to type than initializing with let aoi = AreaOfInterest { area, max_count, max_size } and leads to less line breaks.

/// Return the intersection of this [`AreaOfInterest`] with another.
/// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#aoi_intersection).
pub fn intersection(
&self,
other: AreaOfInterest<MCL, MCC, MPL, S>,
other: &AreaOfInterest<MCL, MCC, MPL, S>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't consume other so should take by ref to not need cloning at call site

@@ -6,7 +6,7 @@ use arbitrary::{Arbitrary, Error as ArbitraryError};

use crate::path::Path;

#[derive(Debug, Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those can easily be copy

@@ -164,6 +152,11 @@ impl<T> Range<T>
where
T: Ord + Clone,
{
/// Construct a range.
pub fn new(start: T, end: RangeEnd<T>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, a constructor for when you have a RangeEnd available (eg from another range) saves line breaks at callsite

@@ -157,8 +157,8 @@ where
/// The kind of access this capability grants.
///
/// [Definition](https://willowprotocol.org/specs/meadowcap/index.html#communal_cap_mode)
pub fn access_mode(&self) -> &AccessMode {
&self.access_mode
pub fn access_mode(&self) -> AccessMode {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy types should be returned by value

namespace_key: NamespacePublicKey,
namespace_secret: NamespaceSecret,
namespace_secret: &NamespaceSecret,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • No need for this to be async.
  • NamespaceSecret isn't consumed, so take by ref to not have to clone at call site

@@ -251,7 +251,7 @@ where
/// Return a new [`McAuthorisationToken`], or an error if the resulting signature was not for the capability's receiver.
pub fn authorisation_token<UserSecret, PD>(
&self,
entry: Entry<MCL, MCC, MPL, NamespacePublicKey, UserPublicKey, PD>,
entry: &Entry<MCL, MCC, MPL, NamespacePublicKey, UserPublicKey, PD>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entry isn't consumed so take ref to avoid clone at call site

@@ -69,9 +69,9 @@ where
UserSignature: Encodable + Clone,
{
/// Generate a new [`McSubspaceCapability`] for a given user, or return an error if the given namespace secret is incorrect.
pub fn new<const MCL: usize, const MCC: usize, const MPL: usize, NamespaceSecret>(
pub fn new<NamespaceSecret>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the const generics weren't used

@Frando Frando marked this pull request as ready for review August 12, 2024 15:06
pub Entry<MCL, MCC, MPL, N, S, PD>,
pub AT,
Entry<MCL, MCC, MPL, N, S, PD>,
AT,
Copy link
Contributor Author

@Frando Frando Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the fields should not be pub: If this type is intended to symbolize an entry+token pair where the authorisation was checked (which the new function indicates, and which is also how the term is used in the willow spec) then I think the fields should be private, otherwise AuthorisedEntry(some_entry, some_token) is a too-easy-to-get-wrong constructor for an unchecked AuthorisedEntry.

@AljoschaMeyer
Copy link
Contributor

AljoschaMeyer commented Aug 13, 2024

Thank you. I agree with almost all of these, will add comments where I'm not 100% convinced.

@Frando
Copy link
Contributor Author

Frando commented Aug 19, 2024

@AljoschaMeyer wrote here:

Re Ord on Entry again: the sideloading protocol defines a total order on entries to use (entries "being prior" to others): https://willowprotocol.org/specs/sideloading/index.html#sideload_protocol

The implementation of Ord should implement that order, and refer to it in the doc comment. Sorry, I forgot about that order earlier.

Right! I changed towards a manual impl of Ord that uses the definition of ordering from the sideloading protocol.

Copy link
Contributor

@sgwilym sgwilym left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this roundup!

@sgwilym sgwilym merged commit b0a5eeb into earthstar-project:main Aug 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants