Skip to content

Commit

Permalink
Remove immutable_file_number from CardanoTransactionRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Jul 17, 2024
1 parent 7059534 commit ef079c4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ mod tests {

fn test_transaction_set() -> Vec<CardanoTransactionRecord> {
vec![
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10", 1),
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10", 1),
CardanoTransactionRecord::new("tx-hash-2", 11, 52, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-3", 11, 53, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-4", 12, 54, "block-hash-12", 1),
CardanoTransactionRecord::new("tx-hash-5", 12, 55, "block-hash-12", 1),
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10"),
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10"),
CardanoTransactionRecord::new("tx-hash-2", 11, 52, "block-hash-11"),
CardanoTransactionRecord::new("tx-hash-3", 11, 53, "block-hash-11"),
CardanoTransactionRecord::new("tx-hash-4", 12, 54, "block-hash-12"),
CardanoTransactionRecord::new("tx-hash-5", 12, 55, "block-hash-12"),
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ mod tests {
insert_transactions(
&connection,
vec![
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10", 1),
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10", 1),
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-0", 10, 50, "block-hash-10"),
CardanoTransactionRecord::new("tx-hash-1", 10, 51, "block-hash-10"),
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11"),
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11"),
],
);

Expand All @@ -144,8 +144,8 @@ mod tests {
.unwrap();
assert_eq!(
vec![
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11", 1),
CardanoTransactionRecord::new("tx-hash-2", 11, 54, "block-hash-11"),
CardanoTransactionRecord::new("tx-hash-3", 11, 55, "block-hash-11"),
],
records
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl InsertCardanoTransactionQuery {
Value::Integer(record.block_number.try_into()?),
Value::Integer(record.slot_number.try_into()?),
Value::String(record.block_hash.clone()),
Value::Integer(record.immutable_file_number.try_into()?),
Value::Integer(0),
]);
Ok(vec)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sqlite::Row;

use mithril_common::entities::{
BlockHash, BlockNumber, CardanoTransaction, ImmutableFileNumber, SlotNumber, TransactionHash,
BlockHash, BlockNumber, CardanoTransaction, SlotNumber, TransactionHash,
};

use crate::database::Hydrator;
Expand All @@ -21,9 +21,6 @@ pub struct CardanoTransactionRecord {

/// Block hash of the transaction
pub block_hash: BlockHash,

/// Immutable file number of the transaction
pub immutable_file_number: ImmutableFileNumber,
}

impl CardanoTransactionRecord {
Expand All @@ -33,14 +30,12 @@ impl CardanoTransactionRecord {
block_number: BlockNumber,
slot_number: SlotNumber,
block_hash: U,
immutable_file_number: ImmutableFileNumber,
) -> Self {
Self {
transaction_hash: hash.into(),
block_number,
slot_number,
block_hash: block_hash.into(),
immutable_file_number,
}
}
}
Expand All @@ -52,7 +47,6 @@ impl From<CardanoTransaction> for CardanoTransactionRecord {
block_number: transaction.block_number,
slot_number: transaction.slot_number,
block_hash: transaction.block_hash,
immutable_file_number: 0,
}
}
}
Expand All @@ -77,15 +71,12 @@ impl SqLiteEntity for CardanoTransactionRecord {
let block_number = Hydrator::try_to_u64("cardano_tx.block_number", row.read::<i64, _>(1))?;
let slot_number = Hydrator::try_to_u64("cardano_tx.slot_number", row.read::<i64, _>(2))?;
let block_hash = row.read::<&str, _>(3);
let immutable_file_number =
Hydrator::try_to_u64("cardano_tx.immutable_file_number", row.read::<i64, _>(4))?;

Ok(Self {
transaction_hash: transaction_hash.to_string(),
block_number,
slot_number,
block_hash: block_hash.to_string(),
immutable_file_number,
})
}

Expand All @@ -99,11 +90,6 @@ impl SqLiteEntity for CardanoTransactionRecord {
("block_number", "{:cardano_tx:}.block_number", "int"),
("slot_number", "{:cardano_tx:}.slot_number", "int"),
("block_hash", "{:cardano_tx:}.block_hash", "text"),
(
"immutable_file_number",
"{:cardano_tx:}.immutable_file_number",
"int",
),
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use async_trait::async_trait;

use mithril_common::crypto_helper::MKTreeNode;
use mithril_common::entities::{
BlockHash, BlockNumber, BlockRange, CardanoTransaction, ChainPoint, ImmutableFileNumber,
SlotNumber, TransactionHash,
BlockHash, BlockNumber, BlockRange, CardanoTransaction, ChainPoint, SlotNumber, TransactionHash,
};
use mithril_common::signable_builder::BlockRangeRootRetriever;
use mithril_common::StdResult;
Expand Down Expand Up @@ -68,14 +67,12 @@ impl CardanoTransactionRepository {
block_number: BlockNumber,
slot_number: SlotNumber,
block_hash: U,
immutable_file_number: ImmutableFileNumber,
) -> StdResult<Option<CardanoTransactionRecord>> {
let query = InsertCardanoTransactionQuery::insert_one(&CardanoTransactionRecord {
transaction_hash: transaction_hash.into(),
block_number,
slot_number,
block_hash: block_hash.into(),
immutable_file_number,
})?;

self.connection_pool.connection()?.fetch_first(query)
Expand Down Expand Up @@ -190,22 +187,6 @@ impl CardanoTransactionRepository {
.fetch_collect(GetBlockRangeRootQuery::all())
}

/// Get the highest [ImmutableFileNumber] of the cardano transactions stored in the database.
pub async fn get_transaction_highest_immutable_file_number(
&self,
) -> StdResult<Option<ImmutableFileNumber>> {
let highest: Option<i64> = self.connection_pool.connection()?.query_single_cell(
"select max(immutable_file_number) as highest from cardano_tx;",
&[],
)?;
highest
.map(u64::try_from)
.transpose()
.with_context(||
format!("Integer field max(immutable_file_number) (value={highest:?}) is incompatible with u64 representation.")
)
}

/// Store the given transactions in the database.
///
/// The storage is done in chunks to avoid exceeding sqlite binding limitations.
Expand Down Expand Up @@ -354,7 +335,6 @@ mod tests {
block_number: 10,
slot_number: 50,
block_hash: "block_hash-123".to_string(),
immutable_file_number: 0
}),
transaction_result
);
Expand All @@ -373,10 +353,10 @@ mod tests {

repository
.create_transactions(vec![
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123", 1234),
CardanoTransactionRecord::new("tx_hash-456", 11, 51, "block_hash-456", 1234),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789", 1234),
CardanoTransactionRecord::new("tx_hash-000", 101, 100, "block_hash-000", 1234),
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123"),
CardanoTransactionRecord::new("tx_hash-456", 11, 51, "block_hash-456"),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789"),
CardanoTransactionRecord::new("tx_hash-000", 101, 100, "block_hash-000"),
])
.await
.unwrap();
Expand All @@ -389,8 +369,8 @@ mod tests {

assert_eq!(
vec![
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123", 1234),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789", 1234),
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123"),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789"),
],
transactions
);
Expand All @@ -403,8 +383,8 @@ mod tests {

assert_eq!(
vec![
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123", 1234),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789", 1234),
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123"),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789"),
],
transactions
);
Expand All @@ -417,9 +397,9 @@ mod tests {

assert_eq!(
vec![
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123", 1234),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789", 1234),
CardanoTransactionRecord::new("tx_hash-000", 101, 100, "block_hash-000", 1234),
CardanoTransactionRecord::new("tx_hash-123", 10, 50, "block_hash-123"),
CardanoTransactionRecord::new("tx_hash-789", 12, 52, "block_hash-789"),
CardanoTransactionRecord::new("tx_hash-000", 101, 100, "block_hash-000"),
],
transactions
);
Expand All @@ -442,11 +422,11 @@ mod tests {
));

repository
.create_transaction("tx-hash-123", 10, 50, "block_hash-123", 99)
.create_transaction("tx-hash-123", 10, 50, "block_hash-123")
.await
.unwrap();
repository
.create_transaction("tx-hash-123", 11, 51, "block_hash-123-bis", 100)
.create_transaction("tx-hash-123", 11, 51, "block_hash-123-bis")
.await
.unwrap();
let transaction_result = repository.get_transaction("tx-hash-123").await.unwrap();
Expand All @@ -457,7 +437,6 @@ mod tests {
block_number: 10,
slot_number: 50,
block_hash: "block_hash-123".to_string(),
immutable_file_number: 99
}),
transaction_result
);
Expand Down Expand Up @@ -487,7 +466,6 @@ mod tests {
block_number: 10,
slot_number: 50,
block_hash: "block-hash-123".to_string(),
immutable_file_number: 0
}),
transaction_result
);
Expand All @@ -500,7 +478,6 @@ mod tests {
block_number: 11,
slot_number: 51,
block_hash: "block-hash-456".to_string(),
immutable_file_number: 0,
}),
transaction_result
);
Expand Down Expand Up @@ -539,7 +516,7 @@ mod tests {
));

repository
.create_transaction("tx-hash-000", 1, 5, "block-hash", 9)
.create_transaction("tx-hash-000", 1, 5, "block-hash")
.await
.unwrap();

Expand All @@ -562,7 +539,6 @@ mod tests {
block_number: 1,
slot_number: 5,
block_hash: "block-hash".to_string(),
immutable_file_number: 9
}),
transaction_result
);
Expand Down Expand Up @@ -644,43 +620,6 @@ mod tests {
);
}

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

let highest_beacon = repository
.get_transaction_highest_immutable_file_number()
.await
.unwrap();
assert_eq!(None, highest_beacon);
}

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

let cardano_transactions = vec![
CardanoTransactionRecord::new("tx-hash-123".to_string(), 10, 50, "block-hash-123", 50),
CardanoTransactionRecord::new("tx-hash-456".to_string(), 11, 51, "block-hash-456", 100),
];
repository
.create_transactions(cardano_transactions)
.await
.unwrap();

let highest_beacon = repository
.get_transaction_highest_immutable_file_number()
.await
.unwrap();
assert_eq!(Some(100), highest_beacon);
}

#[tokio::test]
async fn repository_get_transactions_in_range_blocks() {
let connection = cardano_tx_db_connection().unwrap();
Expand All @@ -689,9 +628,9 @@ mod tests {
));

let transactions = vec![
CardanoTransactionRecord::new("tx-hash-1", 10, 50, "block-hash-1", 99),
CardanoTransactionRecord::new("tx-hash-2", 11, 51, "block-hash-2", 100),
CardanoTransactionRecord::new("tx-hash-3", 12, 52, "block-hash-3", 101),
CardanoTransactionRecord::new("tx-hash-1", 10, 50, "block-hash-1"),
CardanoTransactionRecord::new("tx-hash-2", 11, 51, "block-hash-2"),
CardanoTransactionRecord::new("tx-hash-3", 12, 52, "block-hash-3"),
];
repository
.create_transactions(transactions.clone())
Expand Down Expand Up @@ -743,12 +682,12 @@ mod tests {
));

let transactions = vec![
CardanoTransactionRecord::new("tx-hash-1", 10, 50, "block-hash-1", 99),
CardanoTransactionRecord::new("tx-hash-2", 11, 51, "block-hash-2", 100),
CardanoTransactionRecord::new("tx-hash-3", 20, 52, "block-hash-3", 101),
CardanoTransactionRecord::new("tx-hash-4", 31, 53, "block-hash-4", 102),
CardanoTransactionRecord::new("tx-hash-5", 35, 54, "block-hash-5", 103),
CardanoTransactionRecord::new("tx-hash-6", 46, 55, "block-hash-6", 104),
CardanoTransactionRecord::new("tx-hash-1", 10, 50, "block-hash-1"),
CardanoTransactionRecord::new("tx-hash-2", 11, 51, "block-hash-2"),
CardanoTransactionRecord::new("tx-hash-3", 20, 52, "block-hash-3"),
CardanoTransactionRecord::new("tx-hash-4", 31, 53, "block-hash-4"),
CardanoTransactionRecord::new("tx-hash-5", 35, 54, "block-hash-5"),
CardanoTransactionRecord::new("tx-hash-6", 46, 55, "block-hash-6"),
];
repository
.create_transactions(transactions.clone())
Expand Down Expand Up @@ -802,9 +741,9 @@ mod tests {
));

let transactions = vec![
CardanoTransactionRecord::new("tx-1", 100, 500, "block-1", 99),
CardanoTransactionRecord::new("tx-2", 100, 500, "block-1", 99),
CardanoTransactionRecord::new("tx-3", 101, 501, "block-1", 99),
CardanoTransactionRecord::new("tx-1", 100, 500, "block-1"),
CardanoTransactionRecord::new("tx-2", 100, 500, "block-1"),
CardanoTransactionRecord::new("tx-3", 101, 501, "block-1"),
];
repository
.create_transactions(transactions.clone())
Expand Down

0 comments on commit ef079c4

Please sign in to comment.