Skip to content

Commit 0c72a96

Browse files
committed
Make signer TransactionImporter store block ranges with their merkle root
1 parent 86c92e1 commit 0c72a96

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use std::path::{Path, PathBuf};
22
use std::sync::Arc;
33

44
use async_trait::async_trait;
5-
#[cfg(test)]
6-
use mockall::automock;
75
use slog::{debug, Logger};
86

97
use mithril_common::cardano_block_scanner::BlockScanner;
10-
use mithril_common::entities::{CardanoTransaction, ImmutableFileNumber};
8+
use mithril_common::crypto_helper::MKTreeNode;
9+
use mithril_common::entities::{BlockRange, CardanoTransaction, ImmutableFileNumber};
1110
use mithril_common::signable_builder::TransactionsImporter;
1211
use mithril_common::StdResult;
1312

1413
/// Cardano transactions store
15-
#[cfg_attr(test, automock)]
14+
#[cfg_attr(test, mockall::automock)]
1615
#[async_trait]
1716
pub trait TransactionStore: Send + Sync {
1817
/// Get the highest known transaction beacon
@@ -26,6 +25,12 @@ pub trait TransactionStore: Send + Sync {
2625

2726
/// Store list of transactions
2827
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()>;
28+
29+
/// Store list of block ranges with their corresponding merkle root
30+
async fn store_block_ranges(
31+
&self,
32+
block_ranges: Vec<(BlockRange, MKTreeNode)>,
33+
) -> StdResult<()>;
2934
}
3035

3136
/// Import and store [CardanoTransaction].
@@ -121,6 +126,8 @@ mod tests {
121126
use mockall::predicate::eq;
122127

123128
use mithril_common::cardano_block_scanner::ScannedBlock;
129+
use mithril_common::crypto_helper::MKTree;
130+
use mithril_common::entities::BlockNumber;
124131

125132
use crate::database::repository::CardanoTransactionRepository;
126133
use crate::database::test_utils::cardano_tx_db_connection;
@@ -166,8 +173,18 @@ mod tests {
166173
)
167174
}
168175

176+
fn block_range_with_merkle_root<T: Into<MKTreeNode> + Clone>(
177+
block_number: BlockNumber,
178+
hashes: &[T],
179+
) -> (BlockRange, MKTreeNode) {
180+
(
181+
BlockRange::from_block_number(block_number),
182+
MKTree::new(hashes).unwrap().compute_root().unwrap(),
183+
)
184+
}
185+
169186
#[tokio::test]
170-
async fn if_nothing_stored_parse_and_store_all_transactions() {
187+
async fn if_nothing_stored_parse_and_store_all_transactions_and_block_ranges() {
171188
let blocks = vec![
172189
ScannedBlock::new("block_hash-1", 10, 15, 11, vec!["tx_hash-1", "tx_hash-2"]),
173190
ScannedBlock::new("block_hash-2", 20, 25, 12, vec!["tx_hash-3", "tx_hash-4"]),
@@ -195,6 +212,14 @@ mod tests {
195212
.with(eq(expected_stored_transactions))
196213
.returning(|_| Ok(()))
197214
.once();
215+
store_mock
216+
.expect_store_block_ranges()
217+
.with(eq(vec![
218+
block_range_with_merkle_root(10, &["tx_hash-1", "tx_hash-2"]),
219+
block_range_with_merkle_root(20, &["tx_hash-3", "tx_hash-4"]),
220+
]))
221+
.returning(|_| Ok(()))
222+
.once();
198223
},
199224
);
200225

@@ -217,6 +242,7 @@ mod tests {
217242
.expect_get_highest_beacon()
218243
.returning(|| Ok(Some(12)));
219244
store_mock.expect_store_transactions().never();
245+
store_mock.expect_store_block_ranges().never();
220246
},
221247
);
222248

@@ -256,6 +282,14 @@ mod tests {
256282
.with(eq(expected_to_store_transactions))
257283
.returning(|_| Ok(()))
258284
.once();
285+
store_mock
286+
.expect_store_block_ranges()
287+
.with(eq(vec![block_range_with_merkle_root(
288+
20,
289+
&["tx_hash-3", "tx_hash-4"],
290+
)]))
291+
.returning(|_| Ok(()))
292+
.once();
259293
},
260294
);
261295

@@ -266,7 +300,7 @@ mod tests {
266300
}
267301

268302
#[tokio::test]
269-
async fn importing_twice_starting_with_nothing_in_a_real_db_should_yield_the_transactions_in_same_order(
303+
async fn importing_twice_starting_with_nothing_in_a_real_db_should_yield_transactions_and_block_ranges_in_same_order(
270304
) {
271305
let blocks = vec![
272306
ScannedBlock::new("block_hash-1", 10, 15, 11, vec!["tx_hash-1", "tx_hash-2"]),

mithril-signer/src/database/repository/cardano_transaction_repository.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use std::sync::Arc;
33
use anyhow::Context;
44
use async_trait::async_trait;
55

6+
use mithril_common::crypto_helper::MKTreeNode;
67
use mithril_common::entities::{
7-
BlockHash, BlockNumber, CardanoTransaction, ImmutableFileNumber, SlotNumber, TransactionHash,
8+
BlockHash, BlockNumber, BlockRange, CardanoTransaction, ImmutableFileNumber, SlotNumber,
9+
TransactionHash,
810
};
911
use mithril_common::StdResult;
1012
use mithril_persistence::sqlite::{Provider, SqliteConnection, WhereCondition};
@@ -145,6 +147,13 @@ impl TransactionStore for CardanoTransactionRepository {
145147
}
146148
Ok(())
147149
}
150+
151+
async fn store_block_ranges(
152+
&self,
153+
_block_ranges: Vec<(BlockRange, MKTreeNode)>,
154+
) -> StdResult<()> {
155+
todo!()
156+
}
148157
}
149158

150159
#[cfg(test)]

0 commit comments

Comments
 (0)