Skip to content

Commit 19138cf

Browse files
committed
addressing review remarks
1 parent ae18826 commit 19138cf

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

src/vit-testing/mainnet-lib/src/cardano_node/in_memory.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ impl InMemoryNode {
9191
let handle = tokio::spawn(async move {
9292
loop {
9393
tokio::time::sleep(Duration::from_secs(u64::from(slot_duration))).await;
94-
let block = ledger.write().unwrap().mint_block();
94+
let block = ledger
95+
.write()
96+
.unwrap()
97+
.mint_block()
98+
.expect("leadership event failed");
9599
block_notifier.notify(block).await.unwrap();
96100
}
97101
});

src/vit-testing/mainnet-lib/src/cardano_node/ledger.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,24 @@ impl Ledger {
4545
}
4646

4747
/// Mint new block
48-
pub fn mint_block(&mut self) -> Block {
49-
let last_block = self
50-
.blocks
51-
.last()
52-
.expect("internal error no block0 in blockchain");
48+
///
49+
/// # Errors
50+
///
51+
/// On blockchain inconsistency
52+
pub fn mint_block(&mut self) -> Result<Block, Error> {
53+
let last_block = self.blocks.last().ok_or(Error::MissingBlock0)?;
5354
let next_block = BlockBuilder::next_block(Some(last_block), &self.mempool);
5455
self.blocks.push(next_block.clone());
5556
self.mempool.clear();
56-
next_block
57+
Ok(next_block)
5758
}
5859
}
60+
61+
#[derive(thiserror::Error, Debug, Clone)]
62+
pub enum Error {
63+
#[error("no block0 in blockchain")]
64+
MissingBlock0,
65+
}
66+
67+
unsafe impl Send for Error {}
68+
unsafe impl Send for Ledger {}

src/vit-testing/mainnet-lib/src/cardano_node/settings.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::CARDANO_MAINNET_SLOTS_PER_EPOCH;
12
use chain_impl_mockchain::testing::TestGen;
23
use std::time::SystemTime;
34

@@ -11,7 +12,7 @@ pub struct Settings {
1112
/// slot duration
1213
pub slot_duration: u32,
1314
/// slots per epoch
14-
pub slots_per_epoch: u32,
15+
pub slots_per_epoch: u64,
1516
}
1617

1718
impl Default for Settings {
@@ -20,7 +21,7 @@ impl Default for Settings {
2021
block0_hash: TestGen::hash().to_string(),
2122
block0_time: SystemTime::now(),
2223
slot_duration: 1,
23-
slots_per_epoch: 43200,
24+
slots_per_epoch: CARDANO_MAINNET_SLOTS_PER_EPOCH,
2425
}
2526
}
2627
}

src/vit-testing/mainnet-lib/src/db_sync/in_memory.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Block0, InMemoryNode};
1+
use crate::{Block0, InMemoryNode, CARDANO_MAINNET_SLOTS_PER_EPOCH};
22
use cardano_serialization_lib::metadata::GeneralTransactionMetadata;
33
use cardano_serialization_lib::utils::BigNum;
44
use cardano_serialization_lib::{Block, Transaction, TransactionWitnessSet};
@@ -15,8 +15,6 @@ use std::path::Path;
1515
use std::sync::{Arc, RwLock};
1616
use tokio::task::JoinHandle;
1717

18-
const CARDANO_MAINNET_SLOTS_PER_EPOCH: u64 = 43200;
19-
2018
pub type BlockNo = u32;
2119
pub type Address = String;
2220

@@ -52,7 +50,6 @@ impl Debug for InMemoryDbSync {
5250
}
5351

5452
impl InMemoryDbSync {
55-
5653
/// Creates new instance out of block0
5754
#[must_use]
5855
pub fn from_block0(block0: &Block0) -> Self {
@@ -135,16 +132,15 @@ impl InMemoryDbSync {
135132

136133
/// Query transaction by it's hash representation
137134
#[must_use]
138-
pub fn transaction_by_hash(&self, hash: &str) -> Vec<(&Block, &Transaction)> {
135+
pub fn transaction_by_hash(&self, hash: &str) -> Vec<(Option<&Block>, &Transaction)> {
139136
self.transactions
140137
.iter()
141138
.filter_map(|(block, txs)| {
142139
if let Some(tx) = txs.iter().find(|tx| tx.to_hex() == hash) {
143140
let block = self
144141
.blocks
145142
.iter()
146-
.find(|x| x.header().header_body().block_number() == *block)
147-
.expect("data inconsitency. cannot find block with block no from tx");
143+
.find(|x| x.header().header_body().block_number() == *block);
148144

149145
return Some((block, tx));
150146
}

src/vit-testing/mainnet-lib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ mod db_sync;
2020
mod network;
2121
mod wallet;
2222

23+
/// Const defining caradno mainnet slot per epoch setting
24+
pub const CARDANO_MAINNET_SLOTS_PER_EPOCH: u64 = 43200;
25+
2326
pub use exports::*;
2427
mod exports {
2528
pub use crate::blockfrost::{CatalystBlockFrostApi, Error as CatalystBlockFrostApiError};

src/vit-testing/mainnet-lib/src/network/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl MainnetNetworkBuilder {
7171
pub fn build_shared(self) -> (SharedInMemoryDbSync, InMemoryNode, HashSet<Identifier>) {
7272
let (db_sync, ledger, reps) = self.build();
7373
let mut node = InMemoryNode::start_from_ledger(ledger);
74-
(db_sync.connect_to_node(&mut node),node, reps)
74+
(db_sync.connect_to_node(&mut node), node, reps)
7575
}
7676
}
7777

0 commit comments

Comments
 (0)