Skip to content

Commit 3847963

Browse files
committed
Make CardanoTransactionSnapshot entity use a ChainPoint beacon
1 parent b005c51 commit 3847963

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

mithril-common/src/entities/cardano_chain_point.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cmp::Ordering;
22
use std::fmt::{Display, Formatter};
33

44
use serde::{Deserialize, Serialize};
5+
use sha2::{Digest, Sha256};
56

67
cfg_fs! {
78
use pallas_network::miniprotocols::{chainsync::Tip, Point};
@@ -50,6 +51,12 @@ impl ChainPoint {
5051
self.slot_number == 0 && self.block_number == 0 && self.block_hash.is_empty()
5152
}
5253

54+
pub(crate) fn feed_hash(&self, hasher: &mut Sha256) {
55+
hasher.update(self.slot_number.to_be_bytes());
56+
hasher.update(self.block_number.to_be_bytes());
57+
hasher.update(self.block_hash.as_bytes());
58+
}
59+
5360
cfg_test_tools! {
5461
/// Create a dummy ChainPoint
5562
pub fn dummy() -> Self {
@@ -192,4 +199,44 @@ mod tests {
192199

193200
assert_eq!(Ordering::Less, chain_point1.cmp(&chain_point2));
194201
}
202+
203+
#[test]
204+
fn feed_hash_use_all_properties() {
205+
fn hash(chain_point: &ChainPoint) -> String {
206+
let mut hasher = Sha256::new();
207+
chain_point.feed_hash(&mut hasher);
208+
hex::encode(hasher.finalize())
209+
}
210+
211+
let chain_point = ChainPoint {
212+
slot_number: 5,
213+
block_number: 10,
214+
block_hash: "block_hash".to_string(),
215+
};
216+
let reference_hash = hash(&chain_point);
217+
218+
assert_ne!(
219+
reference_hash,
220+
hash(&ChainPoint {
221+
slot_number: 10000,
222+
..chain_point.clone()
223+
})
224+
);
225+
226+
assert_ne!(
227+
reference_hash,
228+
hash(&ChainPoint {
229+
block_number: 10000,
230+
..chain_point.clone()
231+
})
232+
);
233+
234+
assert_ne!(
235+
reference_hash,
236+
hash(&ChainPoint {
237+
block_hash: "other_block_hash".to_string(),
238+
..chain_point.clone()
239+
})
240+
);
241+
}
195242
}

mithril-common/src/entities/cardano_transactions_snapshot.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::signable_builder::Artifact;
21
use serde::{Deserialize, Serialize};
32
use sha2::{Digest, Sha256};
43

5-
use super::CardanoDbBeacon;
4+
use crate::signable_builder::Artifact;
5+
6+
use super::ChainPoint;
67

78
/// Snapshot of a set of Cardano transactions
8-
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
9+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
910
pub struct CardanoTransactionsSnapshot {
1011
/// Hash of the Cardano transactions set
1112
pub hash: String,
@@ -14,15 +15,15 @@ pub struct CardanoTransactionsSnapshot {
1415
pub merkle_root: String,
1516

1617
/// Beacon of the Cardano transactions set
17-
pub beacon: CardanoDbBeacon,
18+
pub chain_point: ChainPoint,
1819
}
1920

2021
impl CardanoTransactionsSnapshot {
2122
/// Creates a new [CardanoTransactionsSnapshot]
22-
pub fn new(merkle_root: String, beacon: CardanoDbBeacon) -> Self {
23+
pub fn new(merkle_root: String, chain_point: ChainPoint) -> Self {
2324
let mut cardano_transactions_snapshot = Self {
2425
merkle_root,
25-
beacon,
26+
chain_point,
2627
hash: "".to_string(),
2728
};
2829
cardano_transactions_snapshot.hash = cardano_transactions_snapshot.compute_hash();
@@ -33,7 +34,7 @@ impl CardanoTransactionsSnapshot {
3334
fn compute_hash(&self) -> String {
3435
let mut hasher = Sha256::new();
3536
hasher.update(self.merkle_root.clone().as_bytes());
36-
hasher.update(self.beacon.compute_hash().as_bytes());
37+
self.chain_point.feed_hash(&mut hasher);
3738

3839
hex::encode(hasher.finalize())
3940
}
@@ -52,18 +53,24 @@ mod tests {
5253

5354
#[test]
5455
fn test_cardano_transactions_snapshot_compute_hash() {
55-
let hash_expected = "66a1d7aa3995e9a0dce15fae3f6b91640824ecd1f81991df5ce4ddff62b34df4";
56+
let hash_expected = "701a0fec8e7c6afca90f3f94b1f912ba6e179d891023465858abcad7db108730";
5657

5758
assert_eq!(
5859
hash_expected,
59-
CardanoTransactionsSnapshot::new("mk-root-123".to_string(), CardanoDbBeacon::default())
60-
.compute_hash()
60+
CardanoTransactionsSnapshot::new(
61+
"mk-root-123".to_string(),
62+
ChainPoint::new(100, 50, "block_hash-50")
63+
)
64+
.compute_hash()
6165
);
6266

6367
assert_ne!(
6468
hash_expected,
65-
CardanoTransactionsSnapshot::new("mk-root-456".to_string(), CardanoDbBeacon::default())
66-
.compute_hash()
69+
CardanoTransactionsSnapshot::new(
70+
"mk-root-456".to_string(),
71+
ChainPoint::new(100, 50, "block_hash-50")
72+
)
73+
.compute_hash()
6774
);
6875
}
6976
}

mithril-common/src/entities/signed_entity.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ impl SignedEntity<CardanoTransactionsSnapshot> {
6868
cfg_test_tools! {
6969
/// Create a dummy [SignedEntity] for [CardanoTransactionsSnapshot] entity
7070
pub fn dummy() -> Self {
71+
let chain_point = ChainPoint::dummy();
72+
7173
SignedEntity {
7274
signed_entity_id: "snapshot-id-123".to_string(),
73-
signed_entity_type: SignedEntityType::CardanoTransactions(Epoch(5), ChainPoint::dummy()),
75+
signed_entity_type: SignedEntityType::CardanoTransactions(Epoch(5), chain_point.clone()),
7476
certificate_id: "certificate-hash-123".to_string(),
75-
artifact: CardanoTransactionsSnapshot::new("mkroot123".to_string(), CardanoDbBeacon::default()),
77+
artifact: CardanoTransactionsSnapshot::new("mkroot123".to_string(), chain_point),
7678
created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
7779
.unwrap()
7880
.with_timezone(&Utc),

mithril-common/src/entities/signed_entity_type.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ impl SignedEntityType {
144144
}
145145
SignedEntityType::CardanoTransactions(epoch, chain_point) => {
146146
hasher.update(&epoch.to_be_bytes());
147-
hasher.update(&chain_point.slot_number.to_be_bytes());
148-
hasher.update(&chain_point.block_number.to_be_bytes());
149-
hasher.update(chain_point.block_hash.as_bytes());
147+
chain_point.feed_hash(hasher);
150148
}
151149
}
152150
}

mithril-common/src/test_utils/fake_data.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ pub fn cardano_transactions_snapshot(total: u64) -> Vec<entities::CardanoTransac
245245
.map(|idx| {
246246
entities::CardanoTransactionsSnapshot::new(
247247
format!("merkleroot-{idx}"),
248-
entities::CardanoDbBeacon {
249-
immutable_file_number: idx,
250-
..beacon()
248+
entities::ChainPoint {
249+
block_number: idx,
250+
block_hash: format!("block_hash-{idx}"),
251+
..chain_point()
251252
},
252253
)
253254
})

0 commit comments

Comments
 (0)