|
| 1 | +use std::{ |
| 2 | + path::{Path, PathBuf}, |
| 3 | + sync::Arc, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::Context; |
| 7 | +use async_trait::async_trait; |
| 8 | +use slog::{info, Logger}; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + digesters::ImmutableDigester, |
| 12 | + entities::{CardanoDbBeacon, ProtocolMessage, ProtocolMessagePartKey}, |
| 13 | + logging::LoggerExtensions, |
| 14 | + signable_builder::SignableBuilder, |
| 15 | + StdResult, |
| 16 | +}; |
| 17 | + |
| 18 | +/// This structure is responsible for calculating the message for incremental Cardano database. |
| 19 | +pub struct CardanoDatabaseSignableBuilder { |
| 20 | + digester: Arc<dyn ImmutableDigester>, |
| 21 | + logger: Logger, |
| 22 | + dirpath: PathBuf, |
| 23 | +} |
| 24 | + |
| 25 | +impl CardanoDatabaseSignableBuilder { |
| 26 | + /// Constructor |
| 27 | + pub fn new(digester: Arc<dyn ImmutableDigester>, dirpath: &Path, logger: Logger) -> Self { |
| 28 | + Self { |
| 29 | + digester, |
| 30 | + logger: logger.new_with_component_name::<Self>(), |
| 31 | + dirpath: dirpath.to_owned(), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[async_trait] |
| 37 | +impl SignableBuilder<CardanoDbBeacon> for CardanoDatabaseSignableBuilder { |
| 38 | + async fn compute_protocol_message( |
| 39 | + &self, |
| 40 | + beacon: CardanoDbBeacon, |
| 41 | + ) -> StdResult<ProtocolMessage> { |
| 42 | + let merkle_tree = self |
| 43 | + .digester |
| 44 | + .compute_merkle_tree(&self.dirpath, &beacon) |
| 45 | + .await |
| 46 | + .with_context(|| { |
| 47 | + format!( |
| 48 | + "Cardano Database Signable Builder can not compute merkle tree of '{}'", |
| 49 | + &self.dirpath.display() |
| 50 | + ) |
| 51 | + })?; |
| 52 | + |
| 53 | + let merkle_root = merkle_tree.compute_root()?.to_hex(); |
| 54 | + info!( |
| 55 | + self.logger, |
| 56 | + "Computed Cardano database Merkle root = '{merkle_root}'" |
| 57 | + ); |
| 58 | + |
| 59 | + let mut protocol_message = ProtocolMessage::new(); |
| 60 | + protocol_message.set_message_part( |
| 61 | + ProtocolMessagePartKey::CardanoDatabaseMerkleRoot, |
| 62 | + merkle_root, |
| 63 | + ); |
| 64 | + |
| 65 | + Ok(protocol_message) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use std::path::Path; |
| 72 | + |
| 73 | + use crate::{ |
| 74 | + crypto_helper::{MKTree, MKTreeStoreInMemory}, |
| 75 | + digesters::ImmutableDigesterError, |
| 76 | + entities::{CardanoDbBeacon, ProtocolMessagePartKey}, |
| 77 | + test_utils::TestLogger, |
| 78 | + }; |
| 79 | + |
| 80 | + use super::*; |
| 81 | + |
| 82 | + #[derive(Default)] |
| 83 | + pub struct ImmutableDigesterImpl { |
| 84 | + digests: Vec<String>, |
| 85 | + } |
| 86 | + |
| 87 | + impl ImmutableDigesterImpl { |
| 88 | + pub fn new(digests: Vec<String>) -> Self { |
| 89 | + Self { digests } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + #[async_trait] |
| 94 | + impl ImmutableDigester for ImmutableDigesterImpl { |
| 95 | + async fn compute_digest( |
| 96 | + &self, |
| 97 | + _dirpath: &Path, |
| 98 | + _beacon: &CardanoDbBeacon, |
| 99 | + ) -> Result<String, ImmutableDigesterError> { |
| 100 | + Ok("whatever".to_string()) |
| 101 | + } |
| 102 | + |
| 103 | + async fn compute_merkle_tree( |
| 104 | + &self, |
| 105 | + _dirpath: &Path, |
| 106 | + _beacon: &CardanoDbBeacon, |
| 107 | + ) -> Result<MKTree<MKTreeStoreInMemory>, ImmutableDigesterError> { |
| 108 | + Ok(MKTree::new(&self.digests).unwrap()) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + #[tokio::test] |
| 113 | + async fn compute_signable() { |
| 114 | + let digests = vec!["digest-1".to_string(), "digest-2".to_string()]; |
| 115 | + let digester = ImmutableDigesterImpl::new(digests.clone()); |
| 116 | + let signable_builder = CardanoDatabaseSignableBuilder::new( |
| 117 | + Arc::new(digester), |
| 118 | + Path::new(""), |
| 119 | + TestLogger::stdout(), |
| 120 | + ); |
| 121 | + |
| 122 | + let protocol_message = signable_builder |
| 123 | + .compute_protocol_message(CardanoDbBeacon::default()) |
| 124 | + .await |
| 125 | + .unwrap(); |
| 126 | + |
| 127 | + let expected_mktree: MKTree<MKTreeStoreInMemory> = MKTree::new(&digests).unwrap(); |
| 128 | + let mut expected_message = ProtocolMessage::new(); |
| 129 | + expected_message.set_message_part( |
| 130 | + ProtocolMessagePartKey::CardanoDatabaseMerkleRoot, |
| 131 | + expected_mktree.compute_root().unwrap().to_hex(), |
| 132 | + ); |
| 133 | + assert_eq!(expected_message, protocol_message); |
| 134 | + } |
| 135 | +} |
0 commit comments