chore: Da full replication unit tests update (#675)

* test: add metadata getter test for VidCertificate

* fix: uncomment assertion for SeqCst

* fix: formatting

* test: get Metadata for Certificate
- generate signature for Certificate

* test: generate hash for Attestation

* fix: add assertion for generate hash

* fix: wrap in test module
- add helper functions

* fix: undo wrap in test module

* test: Hash implementation for Certificate with default Hasher
This commit is contained in:
Roman Zajic 2024-07-22 11:33:18 +02:00 committed by GitHub
parent 8bbbfcfc5f
commit 50f3078e6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 3 deletions

View File

@ -29,6 +29,9 @@ tracing = "0.1"
time = "0.3"
rand = "0.8"
[dev-dependencies]
blake2 = { version = "0.10" }
[features]
default = []
libp2p = []

View File

@ -1,14 +1,22 @@
use std::hash::{DefaultHasher, Hash};
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::Arc;
use std::time::Duration;
use crate::common::*;
use blake2::{
digest::{Update, VariableOutput},
Blake2bVar,
};
use bytes::Bytes;
use cryptarchia_consensus::TimeConfig;
use cryptarchia_ledger::{Coin, LedgerState};
use full_replication::attestation::Attestation;
use full_replication::{Certificate, VidCertificate};
use nomos_core::da::attestation::Attestation as TraitAttestation;
use nomos_core::da::certificate::metadata::Metadata;
use nomos_core::da::certificate::vid::VidCertificate as _;
use nomos_core::da::certificate::Certificate as _;
use nomos_core::da::certificate::CertificateStrategy;
@ -31,8 +39,6 @@ use rand::{thread_rng, Rng};
use tempfile::{NamedTempFile, TempDir};
use time::OffsetDateTime;
use crate::common::*;
#[derive(Services)]
struct IndexerNode {
network: ServiceHandle<NetworkService<NetworkBackend>>,
@ -105,8 +111,27 @@ fn new_node(
.unwrap()
}
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
}
fn signature(attestation: &Attestation) -> [u8; 32] {
let mut attestations = vec![attestation];
attestations.sort();
let mut signatures = Vec::new();
for attestation in &attestations {
signatures.extend_from_slice(attestation.signature());
}
hash(signatures)
}
// TODO: When verifier is implemented this test should be removed and a new one
// performed in integration tests crate using the real node.
#[test]
fn test_indexer() {
let performed_tx = Arc::new(AtomicBool::new(false));
@ -184,11 +209,33 @@ fn test_indexer() {
let attestation = Attestation::new_signed(blob_hash, ids[0], &MockKeyPair);
let certificate_strategy = full_replication::AbsoluteNumber::new(1);
let cert = certificate_strategy.build(vec![attestation], app_id, index);
let cert = certificate_strategy.build(vec![attestation.clone()], app_id, index);
let cert_id = cert.id();
let vid: VidCertificate = cert.clone().into();
let range = 0.into()..1.into(); // get idx 0 and 1.
// Test generate hash for Attestation
let hash2 = attestation.hash();
let expected_hash = hash([blob_hash, ids[0]].concat());
assert_eq!(hash2, expected_hash);
// Test generate signature for Certificate
let sig2 = cert.signature();
let expected_signature = signature(&attestation);
assert_eq!(sig2, expected_signature);
// Test get Metadata for Certificate
let (app_id2, index2) = cert.metadata();
assert_eq!(app_id2, app_id);
assert_eq!(index2, index);
// Test generate hash for Certificate with default Hasher
let mut default_hasher = DefaultHasher::new();
let _hash3 = <Certificate as Hash>::hash(&cert, &mut default_hasher);
// Mock attestation step where blob is persisted in nodes blob storage.
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(write_blob(