Adopt a sans-IO style for DaProtocol (#395)

* Adopt a sans-IO style for DaProtocol

Remove async streams from the DaProtocol trait to avoid creating
dependencies on actual I/O. The protocol it's now transformed into
a state machine with an input and output buffer.

In addition, a method to create an attestation was added to the
trait.

* add forgotten &mut to certify_dispersal

* add forgotten &self param to validate certificate
This commit is contained in:
Giacomo Pasini 2023-09-12 16:34:13 +02:00 committed by GitHub
parent 7cabddc71f
commit 1eeeeb1834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 12 deletions

View File

@ -1,8 +1,5 @@
// std
use std::error::Error;
// crates // crates
use bytes::Bytes; use bytes::Bytes;
use futures::Stream;
// internal // internal
use crate::da::attestation::Attestation; use crate::da::attestation::Attestation;
use crate::da::blob::Blob; use crate::da::blob::Blob;
@ -17,14 +14,24 @@ pub trait DaProtocol {
type Attestation: Attestation; type Attestation: Attestation;
type Certificate: Certificate; type Certificate: Certificate;
fn encode<T: AsRef<[u8]>>(&self, data: T) -> Box<dyn Stream<Item = Self::Blob>>; /// Encode bytes into blobs
fn decode<S: Stream<Item = Self::Blob>>(&self, s: S) -> Result<Bytes, Box<dyn Error>>; fn encode<T: AsRef<[u8]>>(&self, data: T) -> Vec<Self::Blob>;
/// Feed a blob for decoding.
/// Depending on the protocol, it may be necessary to feed multiple blobs to
/// recover the initial data.
fn recv_blob(&mut self, blob: Self::Blob);
/// Attempt to recover the initial data from feeded blobs.
/// If the protocol is not yet ready to return the data, return None.
fn extract(&mut self) -> Option<Bytes>;
/// Attest that we have received and stored a blob.
fn attest(&self, blob: &Self::Blob) -> Self::Attestation;
/// Validate that an attestation is valid for a blob.
fn validate_attestation(&self, blob: &Self::Blob, attestation: &Self::Attestation) -> bool; fn validate_attestation(&self, blob: &Self::Blob, attestation: &Self::Attestation) -> bool;
/// Buffer attestations to produce a certificate of correct dispersal.
fn certificate_dispersal<S: Stream<Item = Self::Attestation>>( fn recv_attestation(&mut self, attestation: Self::Attestation);
&self, /// Attempt to produce a certificate of correct disperal for a blob.
attestations: S, /// If the protocol is not yet ready to return the certificate, return None.
) -> Self::Certificate; fn certify_dispersal(&mut self) -> Option<Self::Certificate>;
/// Validate a certificate.
fn validate_certificate(certificate: &Self::Certificate) -> bool; fn validate_certificate(&self, certificate: &Self::Certificate) -> bool;
} }