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:
parent
4cf57eee74
commit
f9c446e48c
@ -150,7 +150,7 @@ where
|
|||||||
&mempool_channel,
|
&mempool_channel,
|
||||||
res_tx,
|
res_tx,
|
||||||
payload.unwrap_or_default(),
|
payload.unwrap_or_default(),
|
||||||
|cert| cert.blob(),
|
|cert| cert.hash(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
@ -107,5 +107,5 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
fn cert_id(cert: &Certificate) -> <Blob as blob::Blob>::Hash {
|
fn cert_id(cert: &Certificate) -> <Blob as blob::Blob>::Hash {
|
||||||
use certificate::Certificate;
|
use certificate::Certificate;
|
||||||
cert.blob()
|
cert.hash()
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,6 @@ use bytes::Bytes;
|
|||||||
pub trait Attestation {
|
pub trait Attestation {
|
||||||
type Blob: Blob;
|
type Blob: Blob;
|
||||||
fn blob(&self) -> <Self::Blob as Blob>::Hash;
|
fn blob(&self) -> <Self::Blob as Blob>::Hash;
|
||||||
|
fn hash(&self) -> <Self::Blob as Blob>::Hash;
|
||||||
fn as_bytes(&self) -> Bytes;
|
fn as_bytes(&self) -> Bytes;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use bytes::Bytes;
|
|||||||
pub trait Certificate {
|
pub trait Certificate {
|
||||||
type Blob: Blob;
|
type Blob: Blob;
|
||||||
fn blob(&self) -> <Self::Blob as Blob>::Hash;
|
fn blob(&self) -> <Self::Blob as Blob>::Hash;
|
||||||
|
fn hash(&self) -> <Self::Blob as Blob>::Hash;
|
||||||
fn as_bytes(&self) -> Bytes;
|
fn as_bytes(&self) -> Bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// internal
|
// internal
|
||||||
use nomos_core::da::{
|
use nomos_core::da::{
|
||||||
attestation,
|
attestation::{self, Attestation as _},
|
||||||
blob::{self, BlobHasher},
|
blob::{self, BlobHasher},
|
||||||
certificate, DaProtocol,
|
certificate, DaProtocol,
|
||||||
};
|
};
|
||||||
@ -120,6 +120,11 @@ impl attestation::Attestation for Attestation {
|
|||||||
fn blob(&self) -> [u8; 32] {
|
fn blob(&self) -> [u8; 32] {
|
||||||
self.blob
|
self.blob
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn hash(&self) -> <Self::Blob as blob::Blob>::Hash {
|
||||||
|
hash([self.blob, self.voter].concat())
|
||||||
|
}
|
||||||
|
|
||||||
fn as_bytes(&self) -> Bytes {
|
fn as_bytes(&self) -> Bytes {
|
||||||
wire::serialize(self)
|
wire::serialize(self)
|
||||||
.expect("Attestation shouldn't fail to be serialized")
|
.expect("Attestation shouldn't fail to be serialized")
|
||||||
@ -145,6 +150,17 @@ impl certificate::Certificate for Certificate {
|
|||||||
self.attestations[0].blob
|
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 {
|
fn as_bytes(&self) -> Bytes {
|
||||||
wire::serialize(self)
|
wire::serialize(self)
|
||||||
.expect("Certificate shouldn't fail to be serialized")
|
.expect("Certificate shouldn't fail to be serialized")
|
||||||
@ -208,3 +224,11 @@ impl DaProtocol for FullReplication<AbsoluteNumber<Attestation, Certificate>> {
|
|||||||
.can_build(&certificate.attestations)
|
.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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user