Skip to content

Commit

Permalink
style(common): adjust naming and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Alenar committed Oct 25, 2024
1 parent e7de8b2 commit d4518bc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ impl BlockStreamer for ChainReaderBlockStreamer {
}
}
Some(BlockStreamerNextAction::ChainBlockNextAction(
ChainBlockNextAction::RollBackward {
point: rollback_chain_point,
},
ChainBlockNextAction::RollBackward { rollback_point },
)) => {
self.last_polled_point = Some(rollback_chain_point.clone());
let rollback_slot_number = rollback_chain_point.slot_number;
self.last_polled_point = Some(rollback_point.clone());
let rollback_slot_number = rollback_point.slot_number;
let index_rollback = roll_forwards
.iter()
.position(|block| block.slot_number == rollback_slot_number);
Expand Down Expand Up @@ -146,10 +144,8 @@ impl ChainReaderBlockStreamer {
)))
}
}
Some(ChainBlockNextAction::RollBackward {
point: rollback_chain_point,
}) => {
let rollback_slot_number = rollback_chain_point.slot_number;
Some(ChainBlockNextAction::RollBackward { rollback_point }) => {
let rollback_slot_number = rollback_point.slot_number;
trace!(
self.logger,
"Received a RollBackward({rollback_slot_number:?})"
Expand All @@ -158,9 +154,7 @@ impl ChainReaderBlockStreamer {
BlockStreamerNextAction::SkipToNextAction
} else {
BlockStreamerNextAction::ChainBlockNextAction(
ChainBlockNextAction::RollBackward {
point: rollback_chain_point,
},
ChainBlockNextAction::RollBackward { rollback_point },
)
};
Ok(Some(block_streamer_next_action))
Expand Down Expand Up @@ -429,7 +423,7 @@ mod tests {
async fn test_parse_expected_nothing_when_rollbackward_on_same_point() {
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
ChainBlockNextAction::RollBackward {
point: RawCardanoPoint::new(SlotNumber(100), "hash-123"),
rollback_point: RawCardanoPoint::new(SlotNumber(100), "hash-123"),
},
])));
let mut block_streamer = ChainReaderBlockStreamer::try_new(
Expand All @@ -452,7 +446,7 @@ mod tests {
{
let chain_reader = Arc::new(Mutex::new(FakeChainReader::new(vec![
ChainBlockNextAction::RollBackward {
point: RawCardanoPoint::new(SlotNumber(100), "hash-10"),
rollback_point: RawCardanoPoint::new(SlotNumber(100), "hash-10"),
},
])));
let mut block_streamer = ChainReaderBlockStreamer::try_new(
Expand Down Expand Up @@ -513,7 +507,7 @@ mod tests {
),
},
ChainBlockNextAction::RollBackward {
point: RawCardanoPoint::new(SlotNumber(9), "hash-9"),
rollback_point: RawCardanoPoint::new(SlotNumber(9), "hash-9"),
},
])));
let mut block_streamer = ChainReaderBlockStreamer::try_new(
Expand Down Expand Up @@ -562,7 +556,7 @@ mod tests {
),
},
ChainBlockNextAction::RollBackward {
point: RawCardanoPoint::new(SlotNumber(3), "hash-3"),
rollback_point: RawCardanoPoint::new(SlotNumber(3), "hash-3"),
},
])));
let mut block_streamer = ChainReaderBlockStreamer::try_new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use std::fmt::{Debug, Formatter};
use crate::cardano_block_scanner::ScannedBlock;
use crate::entities::{ChainPoint, SlotNumber};

/// Point in the chain that can be intersected.
///
/// Internally the point used in Cardano doesn't have a block number like our [ChainPoint]
/// does, so we need to use a different struct to represent it. Else converting from one to the other
/// would be lossy.
/// Point internal representation in the Cardano chain.
#[derive(Clone, PartialEq)]
pub struct RawCardanoPoint {
/// The [slot number](https://docs.cardano.org/learn/cardano-node/#slotsandepochs)
Expand Down
6 changes: 3 additions & 3 deletions mithril-common/src/cardano_block_scanner/scanned_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub struct ScannedBlock {

impl ScannedBlock {
/// Scanned block factory
pub fn new<BlkHash: Into<Vec<u8>>, TxHash: Into<TransactionHash>>(
block_hash: BlkHash,
pub fn new<B: Into<Vec<u8>>, T: Into<TransactionHash>>(
block_hash: B,
block_number: BlockNumber,
slot_number: SlotNumber,
transaction_hashes: Vec<TxHash>,
transaction_hashes: Vec<T>,
) -> Self {
Self {
block_hash: block_hash.into(),
Expand Down
2 changes: 1 addition & 1 deletion mithril-common/src/chain_reader/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pub enum ChainBlockNextAction {
/// RollBackward event (we are on an incorrect fork, we need to get back a point to roll forward again)
RollBackward {
/// The rollback point in the chain to read (as a new valid chain point to read from on the main chain, which has already been seen)
point: RawCardanoPoint,
rollback_point: RawCardanoPoint,
},
}
22 changes: 11 additions & 11 deletions mithril-common/src/chain_reader/fake_chain_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ use super::{ChainBlockNextAction, ChainBlockReader};

/// [FakeChainReader] is a fake implementation of [ChainBlockReader] for testing purposes.
pub struct FakeChainReader {
chain_point_next_actions: Mutex<VecDeque<ChainBlockNextAction>>,
chain_block_next_actions: Mutex<VecDeque<ChainBlockNextAction>>,
}

impl FakeChainReader {
/// Creates a new [FakeChainReader] instance.
pub fn new(chain_point_next_actions: Vec<ChainBlockNextAction>) -> Self {
pub fn new(chain_block_next_actions: Vec<ChainBlockNextAction>) -> Self {
Self {
chain_point_next_actions: Mutex::new(chain_point_next_actions.into()),
chain_block_next_actions: Mutex::new(chain_block_next_actions.into()),
}
}

/// Total remaining next actions
pub fn get_total_remaining_next_actions(&self) -> usize {
self.chain_point_next_actions.lock().unwrap().len()
self.chain_block_next_actions.lock().unwrap().len()
}
}

Expand All @@ -33,7 +33,7 @@ impl ChainBlockReader for FakeChainReader {
}

async fn get_next_chain_block(&mut self) -> StdResult<Option<ChainBlockNextAction>> {
Ok(self.chain_point_next_actions.lock().unwrap().pop_front())
Ok(self.chain_block_next_actions.lock().unwrap().pop_front())
}
}

Expand All @@ -46,7 +46,7 @@ mod tests {

#[tokio::test]
async fn test_get_next_chain_block() {
let expected_chain_point_next_actions = vec![
let expected_chain_block_next_actions = vec![
ChainBlockNextAction::RollForward {
parsed_block: ScannedBlock::new(
"hash-1",
Expand All @@ -64,18 +64,18 @@ mod tests {
),
},
ChainBlockNextAction::RollBackward {
point: RawCardanoPoint::new(SlotNumber(1), "point-hash-1".as_bytes()),
rollback_point: RawCardanoPoint::new(SlotNumber(1), "point-hash-1".as_bytes()),
},
];

let mut chain_reader = FakeChainReader::new(expected_chain_point_next_actions.clone());
let mut chain_reader = FakeChainReader::new(expected_chain_block_next_actions.clone());

let mut chain_point_next_actions = vec![];
let mut chain_block_next_actions = vec![];
while let Some(chain_block_next_action) = chain_reader.get_next_chain_block().await.unwrap()
{
chain_point_next_actions.push(chain_block_next_action);
chain_block_next_actions.push(chain_block_next_action);
}

assert_eq!(expected_chain_point_next_actions, chain_point_next_actions);
assert_eq!(expected_chain_block_next_actions, chain_block_next_actions);
}
}
9 changes: 5 additions & 4 deletions mithril-common/src/chain_reader/pallas_chain_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ impl PallasChainReader {
Ok(Some(ChainBlockNextAction::RollForward { parsed_block }))
}
NextResponse::RollBackward(rollback_point, _) => {
let point = RawCardanoPoint::from(rollback_point);
Ok(Some(ChainBlockNextAction::RollBackward { point }))
Ok(Some(ChainBlockNextAction::RollBackward {
rollback_point: RawCardanoPoint::from(rollback_point),
}))
}
NextResponse::Await => Ok(None),
}
Expand Down Expand Up @@ -281,8 +282,8 @@ mod tests {
let (_, client_res) = tokio::join!(server, client);
let chain_block = client_res.expect("Client failed to get next chain block");
match chain_block {
ChainBlockNextAction::RollBackward { point: chain_point } => {
assert_eq!(chain_point, get_fake_raw_point_backwards());
ChainBlockNextAction::RollBackward { rollback_point } => {
assert_eq!(rollback_point, get_fake_raw_point_backwards());
}
_ => panic!("Unexpected chain block action"),
}
Expand Down

0 comments on commit d4518bc

Please sign in to comment.