diff --git a/nodes/nomos-node/src/bridges/mod.rs b/nodes/nomos-node/src/bridges/mod.rs index 26707427..1ed4a9d7 100644 --- a/nodes/nomos-node/src/bridges/mod.rs +++ b/nodes/nomos-node/src/bridges/mod.rs @@ -150,7 +150,7 @@ where &mempool_channel, res_tx, payload.unwrap_or_default(), - |cert| cert.blob(), + |cert| cert.hash(), ) .await { diff --git a/nodes/nomos-node/src/main.rs b/nodes/nomos-node/src/main.rs index 374645a3..e81ed600 100644 --- a/nodes/nomos-node/src/main.rs +++ b/nodes/nomos-node/src/main.rs @@ -107,5 +107,5 @@ fn main() -> Result<()> { fn cert_id(cert: &Certificate) -> ::Hash { use certificate::Certificate; - cert.blob() + cert.hash() } diff --git a/nomos-core/src/da/attestation/mod.rs b/nomos-core/src/da/attestation/mod.rs index 52b24167..713596b9 100644 --- a/nomos-core/src/da/attestation/mod.rs +++ b/nomos-core/src/da/attestation/mod.rs @@ -4,5 +4,6 @@ use bytes::Bytes; pub trait Attestation { type Blob: Blob; fn blob(&self) -> ::Hash; + fn hash(&self) -> ::Hash; fn as_bytes(&self) -> Bytes; } diff --git a/nomos-core/src/da/certificate/mod.rs b/nomos-core/src/da/certificate/mod.rs index 92425f62..2d055f3d 100644 --- a/nomos-core/src/da/certificate/mod.rs +++ b/nomos-core/src/da/certificate/mod.rs @@ -6,7 +6,7 @@ use bytes::Bytes; pub trait Certificate { type Blob: Blob; fn blob(&self) -> ::Hash; - + fn hash(&self) -> ::Hash; fn as_bytes(&self) -> Bytes; } diff --git a/nomos-da/full-replication/src/lib.rs b/nomos-da/full-replication/src/lib.rs index 398de11b..da0bfa9b 100644 --- a/nomos-da/full-replication/src/lib.rs +++ b/nomos-da/full-replication/src/lib.rs @@ -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) -> ::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) -> ::Hash { + let mut input = self + .attestations + .iter() + .map(|a| a.hash()) + .collect::>(); + // 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> { .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 +}