Skip to content

Commit

Permalink
Merge pull request #1727 from input-output-hk/ensemble/1697/chain_poi…
Browse files Browse the repository at this point in the history
…nt_beacon_in_cardano_transaction

Use a Block Number beacon for Cardano transaction
  • Loading branch information
Alenar authored Jun 5, 2024
2 parents 3b542fd + 70a8a61 commit c6f32d0
Show file tree
Hide file tree
Showing 83 changed files with 2,817 additions and 1,823 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ As a minor extension, we have adopted a slightly different versioning convention

- Implement a Resource Pool and use it for caching Block Range Merkle maps used by the Cardano transactions prover and improving the throughput.

- Change the beacon of the Cardano Transactions to a block number instead of an immutable file number.

- Crates versions:

| Crate | Version |
Expand Down
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ Here is a list of the available parameters:
| `snapshot_compression_algorithm` | `--snapshot-compression-algorithm` | - | `SNAPSHOT_COMPRESSION_ALGORITHM` | Compression algorithm of the snapshot archive | `zstandard` | `gzip` or `zstandard` | - |
| `zstandard_parameters` | - | - | `ZSTANDARD_PARAMETERS__LEVEL` and `ZSTANDARD_PARAMETERS__NUMBER_OF_WORKERS` | Zstandard specific parameters | - | `{ level: 9, number_of_workers: 4 }` | - |
| `allow_unparsable_block` | `--allow-unparsable-block` | - | `ALLOW_UNPARSABLE_BLOCK` | If set no error is returned in case of unparsable block and an error log is written instead. Will be ignored on (pre)production networks. | `false` | - | - |
| `cardano_transactions_signing_config` | - | - | `CARDANO_TRANSACTIONS_SIGNING_CONFIG__SECURITY_PARAMETER` and `CARDANO_TRANSACTIONS_SIGNING_CONFIG__STEP` | Cardano transactions signing configuration | - | `{ security_parameter: 3000, step: 120 }` | - |

`genesis bootstrap` command:

Expand Down
2 changes: 1 addition & 1 deletion internal/mithril-persistence/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-persistence"
version = "0.2.1"
version = "0.2.2"
description = "Common types, interfaces, and utilities to persist data for Mithril nodes."
authors = { workspace = true }
edition = { workspace = true }
Expand Down
36 changes: 31 additions & 5 deletions internal/mithril-persistence/src/database/hydrator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Shared hydrator helpers for persistence

use serde::Deserialize;

use mithril_common::entities::{
CardanoDbBeacon, Epoch, SignedEntityType, SignedEntityTypeDiscriminants,
BlockNumber, CardanoDbBeacon, Epoch, SignedEntityType, SignedEntityTypeDiscriminants,
};

use crate::sqlite::HydrationError;
Expand Down Expand Up @@ -69,15 +71,39 @@ impl Hydrator {
SignedEntityType::CardanoImmutableFilesFull(beacon)
}
SignedEntityTypeDiscriminants::CardanoTransactions => {
let beacon: CardanoDbBeacon = serde_json::from_str(beacon_str).map_err(|e| {
HydrationError::InvalidData(format!(
#[derive(Deserialize)]
struct CardanoTransactionsBeacon {
epoch: Epoch,
block_number: BlockNumber,
}

let beacon: CardanoTransactionsBeacon =
serde_json::from_str(beacon_str).map_err(|e| {
HydrationError::InvalidData(format!(
"Invalid Beacon JSON in open_message.beacon: '{beacon_str}'. Error: {e}"
))
})?;
SignedEntityType::CardanoTransactions(beacon)
})?;
SignedEntityType::CardanoTransactions(beacon.epoch, beacon.block_number)
}
};

Ok(signed_entity)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hydrate_cardano_transaction_signed_entity_type() {
let expected = SignedEntityType::CardanoTransactions(Epoch(35), 77);
let signed_entity = Hydrator::hydrate_signed_entity_type(
SignedEntityTypeDiscriminants::CardanoTransactions.index(),
&expected.get_json_beacon().unwrap(),
)
.unwrap();

assert_eq!(expected, signed_entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ impl GetCardanoTransactionQuery {

Self { condition }
}

pub fn with_highest_block_number() -> Self {
Self {
condition: WhereCondition::new(
"block_number = (select max(block_number) from cardano_tx)",
vec![],
),
}
}
}

impl Query for GetCardanoTransactionQuery {
Expand All @@ -80,3 +89,49 @@ impl Query for GetCardanoTransactionQuery {
format!("select {projection} from cardano_tx where {condition} order by block_number, transaction_hash")
}
}

#[cfg(test)]
mod tests {
use crate::database::query::InsertCardanoTransactionQuery;
use crate::database::test_helper::cardano_tx_db_connection;
use crate::sqlite::{ConnectionExtensions, SqliteConnection};

use super::*;

fn insert_transactions(connection: &SqliteConnection, records: Vec<CardanoTransactionRecord>) {
connection
.fetch_first(InsertCardanoTransactionQuery::insert_many(records).unwrap())
.unwrap();
}

#[test]
fn with_highest_block_number() {
let connection = cardano_tx_db_connection().unwrap();

let cursor = connection
.fetch(GetCardanoTransactionQuery::with_highest_block_number())
.unwrap();
assert_eq!(0, cursor.count());

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),
],
);

let records: Vec<CardanoTransactionRecord> = connection
.fetch_collect(GetCardanoTransactionQuery::with_highest_block_number())
.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),
],
records
);
}
}
Loading

0 comments on commit c6f32d0

Please sign in to comment.