Skip to content

Commit

Permalink
Remove TransactionStore::get_up_to from signer & aggregator
Browse files Browse the repository at this point in the history
As the `CardanoTransactionsImporter` doesn't need it anymore since
In test it's replaced with a call to `get_all`.
  • Loading branch information
Alenar committed May 7, 2024
1 parent eaba1ae commit da5817d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,6 @@ impl TransactionStore for CardanoTransactionRepository {
}
}

async fn get_up_to(&self, beacon: ImmutableFileNumber) -> StdResult<Vec<CardanoTransaction>> {
self.get_transactions_up_to(beacon).await.map(|v| {
v.into_iter()
.map(|record| record.into())
.collect::<Vec<CardanoTransaction>>()
})
}

async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()> {
const DB_TRANSACTION_SIZE: usize = 100000;
for transactions_in_db_transaction_chunk in transactions.chunks(DB_TRANSACTION_SIZE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ pub trait TransactionStore: Send + Sync {
/// Get the highest known transaction beacon
async fn get_highest_beacon(&self) -> StdResult<Option<ImmutableFileNumber>>;

/// Get stored transactions up to the given beacon
async fn get_up_to(
&self,
immutable_file_number: ImmutableFileNumber,
) -> StdResult<Vec<CardanoTransaction>>;

/// Store list of transactions
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()>;

Expand Down
14 changes: 4 additions & 10 deletions mithril-signer/src/cardano_transactions_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ pub trait TransactionStore: Send + Sync {
/// Get the highest known transaction beacon
async fn get_highest_beacon(&self) -> StdResult<Option<ImmutableFileNumber>>;

/// Get stored transactions up to the given beacon
async fn get_up_to(
&self,
immutable_file_number: ImmutableFileNumber,
) -> StdResult<Vec<CardanoTransaction>>;

/// Store list of transactions
async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()>;

Expand Down Expand Up @@ -282,7 +276,7 @@ mod tests {
.await
.expect("Transactions Importer should succeed");

let stored_transactions = repository.get_up_to(10000).await.unwrap();
let stored_transactions = repository.get_all().await.unwrap();
assert_eq!(expected_transactions, stored_transactions);
}

Expand Down Expand Up @@ -404,7 +398,7 @@ mod tests {
.await
.expect("Transactions Importer should succeed");

let transactions = repository.get_up_to(10000).await.unwrap();
let transactions = repository.get_all().await.unwrap();
assert_eq!(vec![last_tx], transactions);
}

Expand Down Expand Up @@ -442,15 +436,15 @@ mod tests {
CardanoTransactionsImporter::new_for_test(Arc::new(scanner_mock), repository.clone())
};

let stored_transactions = repository.get_up_to(10000).await.unwrap();
let stored_transactions = repository.get_all().await.unwrap();
assert_eq!(stored_block.into_transactions(), stored_transactions);

importer
.import_transactions(up_to_beacon)
.await
.expect("Transactions Importer should succeed");

let stored_transactions = repository.get_up_to(10000).await.unwrap();
let stored_transactions = repository.get_all().await.unwrap();
assert_eq!(expected_transactions, stored_transactions);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use sqlite::Value;
use std::ops::Range;

use mithril_common::entities::{BlockNumber, ImmutableFileNumber, TransactionHash};
use sqlite::Value;

use mithril_common::entities::{BlockNumber, TransactionHash};
#[cfg(test)]
use mithril_persistence::sqlite::GetAllCondition;
use mithril_persistence::sqlite::{
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
};

use crate::database::record::CardanoTransactionRecord;

#[cfg(test)]
use mithril_persistence::sqlite::GetAllCondition;

/// Simple queries to retrieve [CardanoTransaction] from the sqlite database.
pub struct GetCardanoTransactionProvider<'client> {
connection: &'client SqliteConnection,
Expand All @@ -33,16 +33,6 @@ impl<'client> GetCardanoTransactionProvider<'client> {
)
}

pub fn get_transaction_up_to_beacon_condition(
&self,
beacon: ImmutableFileNumber,
) -> WhereCondition {
WhereCondition::new(
"immutable_file_number <= ?*",
vec![Value::Integer(beacon as i64)],
)
}

pub fn get_transaction_between_blocks_condition(
&self,
range: Range<BlockNumber>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ impl CardanoTransactionRepository {
Ok(transactions.collect())
}

/// Return all the [CardanoTransactionRecord]s in the database up to the given beacon using
/// order of insertion.
/// Note: until we rely on block number based beacons, this function needs to compute the highest block number for the given immutable file number.
pub async fn get_transactions_up_to(
&self,
beacon: ImmutableFileNumber,
) -> StdResult<Vec<CardanoTransactionRecord>> {
let provider = GetCardanoTransactionProvider::new(&self.connection);
let filters = provider.get_transaction_up_to_beacon_condition(beacon);
let transactions = provider.find(filters)?;

Ok(transactions.collect())
}

/// Return the [CardanoTransactionRecord] for the given transaction hash.
pub async fn get_transaction<T: Into<TransactionHash>>(
&self,
Expand Down Expand Up @@ -227,14 +213,6 @@ impl TransactionStore for CardanoTransactionRepository {
}
}

async fn get_up_to(&self, beacon: ImmutableFileNumber) -> StdResult<Vec<CardanoTransaction>> {
self.get_transactions_up_to(beacon).await.map(|v| {
v.into_iter()
.map(|record| record.into())
.collect::<Vec<CardanoTransaction>>()
})
}

async fn store_transactions(&self, transactions: Vec<CardanoTransaction>) -> StdResult<()> {
const DB_TRANSACTION_SIZE: usize = 100000;
for transactions_in_db_transaction_chunk in transactions.chunks(DB_TRANSACTION_SIZE) {
Expand Down Expand Up @@ -419,35 +397,6 @@ mod tests {
);
}

#[tokio::test]
async fn repository_get_up_to_beacon_transactions() {
let connection = Arc::new(cardano_tx_db_connection().unwrap());
let repository = CardanoTransactionRepository::new(connection);

let cardano_transactions: Vec<CardanoTransactionRecord> = (20..=40)
.map(|i| CardanoTransactionRecord {
transaction_hash: format!("tx-hash-{i}"),
block_number: i % 10,
slot_number: i * 100,
block_hash: format!("block-hash-{i}"),
immutable_file_number: i,
})
.collect();
repository
.create_transactions(cardano_transactions.clone())
.await
.unwrap();

let transaction_result = repository.get_transactions_up_to(34).await.unwrap();
assert_eq!(cardano_transactions[0..=14].to_vec(), transaction_result);

let transaction_result = repository.get_transactions_up_to(300).await.unwrap();
assert_eq!(cardano_transactions.clone(), transaction_result);

let transaction_result = repository.get_transactions_up_to(19).await.unwrap();
assert_eq!(Vec::<CardanoTransactionRecord>::new(), transaction_result);
}

#[tokio::test]
async fn repository_get_all_stored_transactions() {
let connection = Arc::new(cardano_tx_db_connection().unwrap());
Expand Down

0 comments on commit da5817d

Please sign in to comment.