Add a canonical way to get an id for Attestation and Certificate (#448)

* add hash() method on Attestation and Certificate

* clippy
This commit is contained in:
Giacomo Pasini 2023-10-02 15:59:57 +02:00 committed by GitHub
parent 4cf57eee74
commit f9c446e48c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 4 deletions

View File

@ -150,7 +150,7 @@ where
&mempool_channel,
res_tx,
payload.unwrap_or_default(),
|cert| cert.blob(),
|cert| cert.hash(),
)
.await
{

View File

@ -107,5 +107,5 @@ fn main() -> Result<()> {
fn cert_id(cert: &Certificate) -> <Blob as blob::Blob>::Hash {
use certificate::Certificate;
cert.blob()
cert.hash()
}

View File

@ -4,5 +4,6 @@ use bytes::Bytes;
pub trait Attestation {
type Blob: Blob;
fn blob(&self) -> <Self::Blob as Blob>::Hash;
fn hash(&self) -> <Self::Blob as Blob>::Hash;
fn as_bytes(&self) -> Bytes;
}

View File

@ -6,7 +6,7 @@ use bytes::Bytes;
pub trait Certificate {
type Blob: Blob;
fn blob(&self) -> <Self::Blob as Blob>::Hash;
fn hash(&self) -> <Self::Blob as Blob>::Hash;
fn as_bytes(&self) -> Bytes;
}

View File

@ -1,6 +1,6 @@
// internal
use nomos_core::da::{
attestation,
attestation::{self, Attestation as _},
blob::{self, BlobHasher},
certificate, DaProtocol,
};
@ -120,6 +120,11 @@ impl attestation::Attestation for Attestation {
fn blob(&self) -> [u8; 32] {
self.blob
}
fn hash(&self) -> <Self::Blob as blob::Blob>::Hash {
hash([self.blob, self.voter].concat())
}
fn as_bytes(&self) -> Bytes {
wire::serialize(self)
.expect("Attestation shouldn't fail to be serialized")
@ -145,6 +150,17 @@ impl certificate::Certificate for Certificate {
self.attestations[0].blob
}
fn hash(&self) -> <Self::Blob as blob::Blob>::Hash {
let mut input = self
.attestations
.iter()
.map(|a| a.hash())
.collect::<Vec<_>>();
// sort to make the hash deterministic
input.sort();
hash(input.concat())
}
fn as_bytes(&self) -> Bytes {
wire::serialize(self)
.expect("Certificate shouldn't fail to be serialized")
@ -208,3 +224,11 @@ impl DaProtocol for FullReplication<AbsoluteNumber<Attestation, Certificate>> {
.can_build(&certificate.attestations)
}
}
fn hash(item: impl AsRef<[u8]>) -> [u8; 32] {
let mut hasher = Blake2bVar::new(32).unwrap();
hasher.update(item.as_ref());
let mut output = [0; 32];
hasher.finalize_variable(&mut output).unwrap();
output
}