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

Monero trait methods do not appear in documentation #620

Open
hinto-janai opened this issue Sep 30, 2024 · 0 comments
Open

Monero trait methods do not appear in documentation #620

hinto-janai opened this issue Sep 30, 2024 · 0 comments

Comments

@hinto-janai
Copy link
Contributor

What

Some traits within monero-serai are sealed in a manner that prevent public visibility, i.e. they do not appear in cargo doc:

mod sealed {
use core::fmt::Debug;
use crate::ringct::*;
use super::*;
pub(crate) trait RingSignatures: Clone + PartialEq + Eq + Default + Debug {
fn signatures_to_write(&self) -> &[RingSignature];
fn read_signatures(inputs: &[Input], r: &mut impl Read) -> io::Result<Self>;
}
impl RingSignatures for Vec<RingSignature> {
fn signatures_to_write(&self) -> &[RingSignature] {
self
}
fn read_signatures(inputs: &[Input], r: &mut impl Read) -> io::Result<Self> {
let mut signatures = Vec::with_capacity(inputs.len());
for input in inputs {
match input {
Input::ToKey { key_offsets, .. } => {
signatures.push(RingSignature::read(key_offsets.len(), r)?)
}
_ => Err(io::Error::other("reading signatures for a transaction with non-ToKey inputs"))?,
}
}
Ok(signatures)
}
}
impl RingSignatures for () {
fn signatures_to_write(&self) -> &[RingSignature] {
&[]
}
fn read_signatures(_: &[Input], _: &mut impl Read) -> io::Result<Self> {
Ok(())
}
}
pub(crate) trait RctProofsTrait: Clone + PartialEq + Eq + Debug {
fn write(&self, w: &mut impl Write) -> io::Result<()>;
fn read(
ring_length: usize,
inputs: usize,
outputs: usize,
r: &mut impl Read,
) -> io::Result<Option<Self>>;
fn rct_type(&self) -> RctType;
fn base(&self) -> &RctBase;
}
impl RctProofsTrait for RctProofs {
fn write(&self, w: &mut impl Write) -> io::Result<()> {
self.write(w)
}
fn read(
ring_length: usize,
inputs: usize,
outputs: usize,
r: &mut impl Read,
) -> io::Result<Option<Self>> {
RctProofs::read(ring_length, inputs, outputs, r)
}
fn rct_type(&self) -> RctType {
self.rct_type()
}
fn base(&self) -> &RctBase {
&self.base
}
}
impl RctProofsTrait for PrunedRctProofs {
fn write(&self, w: &mut impl Write) -> io::Result<()> {
self.base.write(w, self.rct_type)
}
fn read(
_ring_length: usize,
inputs: usize,
outputs: usize,
r: &mut impl Read,
) -> io::Result<Option<Self>> {
Ok(RctBase::read(inputs, outputs, r)?.map(|(rct_type, base)| Self { rct_type, base }))
}
fn rct_type(&self) -> RctType {
self.rct_type
}
fn base(&self) -> &RctBase {
&self.base
}
}
pub(crate) trait PotentiallyPruned {
type RingSignatures: RingSignatures;
type RctProofs: RctProofsTrait;
}
/// A transaction which isn't pruned.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct NotPruned;
impl PotentiallyPruned for NotPruned {
type RingSignatures = Vec<RingSignature>;
type RctProofs = RctProofs;
}
/// A transaction which is pruned.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Pruned;
impl PotentiallyPruned for Pruned {
type RingSignatures = ();
type RctProofs = PrunedRctProofs;
}
}
pub use sealed::*;

Problem

These traits/methods do not appear in cargo doc; users cannot know they exist without looking at source code.

Fix

These traits/methods can be safely exposed by using a sealed trait: https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed.

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

No branches or pull requests

1 participant