Skip to content

Commit 33060ae

Browse files
committed
Add slot_number and block_hash to CardanoTransaction
1 parent c721515 commit 33060ae

File tree

10 files changed

+419
-197
lines changed

10 files changed

+419
-197
lines changed

mithril-aggregator/src/database/cardano_transaction_migration.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,19 @@ create index cardano_tx_immutable_file_number_index on cardano_tx(immutable_file
3737
vacuum;
3838
"#,
3939
),
40+
// Migration 3
41+
// Add `slot_number` and `block_hash` columns to `cardano_tx`.
42+
SqlMigration::new(
43+
3,
44+
r#"
45+
-- remove all data from the cardano tx table since the new columns are mandatory
46+
delete from cardano_tx;
47+
48+
alter table cardano_tx add column slot_number integer not null;
49+
alter table cardano_tx add column block_hash text not null;
50+
51+
vacuum;
52+
"#,
53+
),
4054
]
4155
}

mithril-aggregator/src/database/provider/cardano_transaction.rs

Lines changed: 131 additions & 88 deletions
Large diffs are not rendered by default.

mithril-aggregator/src/services/prover.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,14 @@ mod tests {
140140
let mut transactions = vec![];
141141

142142
for i in 1..=total_transactions {
143-
let hash = format!("tx-{}", i);
144-
transactions.push(CardanoTransaction::new(&hash, 10 * i as u64, i as u64));
143+
let hash = format!("tx-{i}");
144+
transactions.push(CardanoTransaction::new(
145+
&hash,
146+
10 * i as u64,
147+
100 * i as u64,
148+
format!("block_hash-{i}"),
149+
i as u64,
150+
));
145151
hashes.push(hash);
146152
}
147153

mithril-common/src/cardano_transaction_parser.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
use crate::{
33
digesters::ImmutableFile,
44
entities::{
5-
BlockNumber, CardanoDbBeacon, CardanoTransaction, ImmutableFileNumber, TransactionHash,
5+
BlockHash, BlockNumber, CardanoDbBeacon, CardanoTransaction, ImmutableFileNumber,
6+
SlotNumber, TransactionHash,
67
},
78
StdResult,
89
};
@@ -95,6 +96,8 @@ struct Block {
9596
pub block_number: BlockNumber,
9697
pub immutable_file_number: ImmutableFileNumber,
9798
pub transactions: Vec<TransactionHash>,
99+
pub slot_number: SlotNumber,
100+
pub block_hash: BlockHash,
98101
}
99102

100103
impl Block {
@@ -103,11 +106,12 @@ impl Block {
103106
for tx in &multi_era_block.txs() {
104107
transactions.push(tx.hash().to_string());
105108
}
106-
107109
Block {
108110
block_number: multi_era_block.number(),
109111
immutable_file_number,
110112
transactions,
113+
slot_number: multi_era_block.slot(),
114+
block_hash: multi_era_block.hash().to_string(),
111115
}
112116
}
113117
}
@@ -218,14 +222,15 @@ impl TransactionParser for CardanoTransactionParser {
218222
let mut block_transactions = blocks
219223
.into_iter()
220224
.flat_map(|block| {
221-
block
222-
.transactions
223-
.into_iter()
224-
.map(move |transaction_hash| CardanoTransaction {
225+
block.transactions.into_iter().map(move |transaction_hash| {
226+
CardanoTransaction::new(
225227
transaction_hash,
226-
block_number: block.block_number,
227-
immutable_file_number: block.immutable_file_number,
228-
})
228+
block.block_number,
229+
block.slot_number,
230+
block.block_hash.clone(),
231+
block.immutable_file_number,
232+
)
233+
})
229234
})
230235
.collect::<Vec<_>>();
231236

mithril-common/src/entities/cardano_transaction.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use super::{BlockNumber, ImmutableFileNumber};
44

55
/// TransactionHash is the unique identifier of a cardano transaction.
66
pub type TransactionHash = String;
7+
/// Hash of a Cardano Block
8+
pub type BlockHash = String;
9+
/// [Cardano Slot number](https://docs.cardano.org/learn/cardano-node/#slotsandepochs)
10+
pub type SlotNumber = u64;
711

812
#[derive(Debug, PartialEq, Clone)]
913
/// Cardano transaction representation
@@ -14,20 +18,30 @@ pub struct CardanoTransaction {
1418
/// Block number of the transaction
1519
pub block_number: BlockNumber,
1620

21+
/// Slot number of the transaction
22+
pub slot_number: SlotNumber,
23+
24+
/// Block hash of the transaction
25+
pub block_hash: BlockHash,
26+
1727
/// Immutable file number of the transaction
1828
pub immutable_file_number: ImmutableFileNumber,
1929
}
2030

2131
impl CardanoTransaction {
2232
/// CardanoTransaction factory
23-
pub fn new(
24-
hash: &str,
33+
pub fn new<T: Into<TransactionHash>, U: Into<BlockHash>>(
34+
hash: T,
2535
block_number: BlockNumber,
36+
slot_number: SlotNumber,
37+
block_hash: U,
2638
immutable_file_number: ImmutableFileNumber,
2739
) -> Self {
2840
Self {
29-
transaction_hash: hash.to_owned(),
41+
transaction_hash: hash.into(),
3042
block_number,
43+
slot_number,
44+
block_hash: block_hash.into(),
3145
immutable_file_number,
3246
}
3347
}
@@ -51,11 +65,8 @@ mod tests {
5165

5266
#[test]
5367
fn test_convert_cardano_transaction_to_merkle_tree_node() {
54-
let transaction = CardanoTransaction {
55-
transaction_hash: "tx-hash-123".to_string(),
56-
block_number: 1,
57-
immutable_file_number: 1,
58-
};
68+
let transaction = CardanoTransaction::new("tx-hash-123", 10, 4, "block_hash", 1);
69+
5970
let computed_mktree_node: MKTreeNode = transaction.into();
6071
let expected_mk_tree_node = MKTreeNode::new("tx-hash-123".as_bytes().to_vec());
6172
let non_expected_mk_tree_node = MKTreeNode::new("tx-hash-456".as_bytes().to_vec());

mithril-common/src/entities/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod type_alias;
2626
pub use block_range::{BlockNumber, BlockRange, BlockRangeLength};
2727
pub use cardano_db_beacon::CardanoDbBeacon;
2828
pub use cardano_network::CardanoNetwork;
29-
pub use cardano_transaction::{CardanoTransaction, TransactionHash};
29+
pub use cardano_transaction::{BlockHash, CardanoTransaction, SlotNumber, TransactionHash};
3030
pub use cardano_transactions_set_proof::CardanoTransactionsSetProof;
3131
pub use cardano_transactions_snapshot::CardanoTransactionsSnapshot;
3232
pub use certificate::{Certificate, CertificateSignature};

mithril-common/src/messages/cardano_transactions_proof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ mod tests {
328328
async fn verify_hashes_from_verified_cardano_transaction_and_from_signable_builder_are_equals(
329329
) {
330330
let transactions = vec![
331-
CardanoTransaction::new("tx-hash-123", 1, 1),
332-
CardanoTransaction::new("tx-hash-456", 2, 1),
331+
CardanoTransaction::new("tx-hash-123", 10, 1, "block_hash", 1),
332+
CardanoTransaction::new("tx-hash-456", 20, 2, "block_hash", 1),
333333
];
334334

335335
assert_eq!(

mithril-common/src/signable_builder/cardano_transactions.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ mod tests {
159159

160160
#[tokio::test]
161161
async fn test_compute_merkle_root() {
162-
let transaction_1 = CardanoTransaction::new("tx-hash-123", 1, 1);
163-
let transaction_2 = CardanoTransaction::new("tx-hash-456", 2, 1);
164-
let transaction_3 = CardanoTransaction::new("tx-hash-789", 3, 1);
165-
let transaction_4 = CardanoTransaction::new("tx-hash-abc", 4, 1);
162+
let transaction_1 = CardanoTransaction::new("tx-hash-123", 10, 1, "block_hash", 1);
163+
let transaction_2 = CardanoTransaction::new("tx-hash-456", 20, 2, "block_hash", 1);
164+
let transaction_3 = CardanoTransaction::new("tx-hash-789", 30, 3, "block_hash", 1);
165+
let transaction_4 = CardanoTransaction::new("tx-hash-abc", 40, 4, "block_hash", 1);
166166

167167
let transactions_set_reference = vec![
168168
transaction_1.clone(),
@@ -230,9 +230,9 @@ mod tests {
230230
..CardanoDbBeacon::default()
231231
};
232232
let transactions = vec![
233-
CardanoTransaction::new("tx-hash-123", 1, 11),
234-
CardanoTransaction::new("tx-hash-456", 2, 12),
235-
CardanoTransaction::new("tx-hash-789", 3, 13),
233+
CardanoTransaction::new("tx-hash-123", 10, 1, "block_hash-", 11),
234+
CardanoTransaction::new("tx-hash-456", 20, 2, "block_hash", 12),
235+
CardanoTransaction::new("tx-hash-789", 30, 3, "block_hash", 13),
236236
];
237237
let transaction_parser = Arc::new(DumbTransactionParser::new(transactions.clone()));
238238
let mut mock_transaction_store = MockTransactionStore::new();

mithril-signer/src/database/cardano_transaction_migration.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,19 @@ create index cardano_tx_immutable_file_number_index on cardano_tx(immutable_file
3737
vacuum;
3838
"#,
3939
),
40+
// Migration 3
41+
// Add `slot_number` and `block_hash` columns to `cardano_tx`.
42+
SqlMigration::new(
43+
3,
44+
r#"
45+
-- remove all data from the cardano tx table since the new columns are mandatory
46+
delete from cardano_tx;
47+
48+
alter table cardano_tx add column slot_number integer not null;
49+
alter table cardano_tx add column block_hash text not null;
50+
51+
vacuum;
52+
"#,
53+
),
4054
]
4155
}

0 commit comments

Comments
 (0)