Skip to content

Commit 819dd93

Browse files
author
Felipe Rosa
committed
chore: Add chain reader struct and refactor some methods
1 parent 69e217e commit 819dd93

File tree

4 files changed

+127
-86
lines changed

4 files changed

+127
-86
lines changed

hermes/crates/cardano-chain-follower/examples/fetch_block.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
//! This example shows how to use the chain follower to download arbitrary blocks
1+
//! This example shows how to use the chain reader to download arbitrary blocks
22
//! from the chain.
33
44
use std::error::Error;
55

6-
use cardano_chain_follower::{ConfigBuilder, Follower, Network, Point};
6+
use cardano_chain_follower::{Network, Point, Reader};
77

88
#[tokio::main]
99
async fn main() -> Result<(), Box<dyn Error>> {
10-
let config = ConfigBuilder::default().build();
10+
let mut reader =
11+
Reader::connect("relays-new.cardano-mainnet.iohk.io:3001", Network::Mainnet).await?;
1112

12-
let mut follower = Follower::connect(
13-
"relays-new.cardano-mainnet.iohk.io:3001",
14-
Network::Mainnet,
15-
config,
16-
)
17-
.await?;
18-
19-
let data = follower
20-
.fetch_block(Point::Specific(
13+
let data = reader
14+
.read_block(Point::Specific(
2115
110908236,
2216
hex::decode("ad3798a1db2b6097c71f35609399e4b2ff834f0f45939803d563bf9d660df2f2")?,
2317
))

hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
//! This example shows how to use the chain follower to download arbitrary blocks
1+
//! This example shows how to use the chain reader to download arbitrary blocks
22
//! from the chain.
33
44
use std::error::Error;
55

6-
use cardano_chain_follower::{ConfigBuilder, Follower, Network, Point};
6+
use cardano_chain_follower::{Network, Point, Reader};
77

88
#[tokio::main]
99
async fn main() -> Result<(), Box<dyn Error>> {
10-
let config = ConfigBuilder::default().build();
10+
let mut reader =
11+
Reader::connect("relays-new.cardano-mainnet.iohk.io:3001", Network::Mainnet).await?;
1112

12-
let mut follower = Follower::connect(
13-
"relays-new.cardano-mainnet.iohk.io:3001",
14-
Network::Mainnet,
15-
config,
16-
)
17-
.await?;
18-
19-
let datas = follower
20-
.fetch_block_range(
13+
let datas = reader
14+
.read_block_range(
2115
Point::Specific(
2216
110908236,
2317
hex::decode("ad3798a1db2b6097c71f35609399e4b2ff834f0f45939803d563bf9d660df2f2")?,

hermes/crates/cardano-chain-follower/examples/follow_chain_updates.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
44
use std::error::Error;
55

6-
use cardano_chain_follower::{ChainUpdate, ConfigBuilder, Follower, Network, PointOrTip};
6+
use cardano_chain_follower::{ChainUpdate, Follower, FollowerConfigBuilder, Network};
77

88
#[tokio::main]
99
async fn main() -> Result<(), Box<dyn Error>> {
10-
let config = ConfigBuilder::default().build();
10+
// Defaults to start following from the tip.
11+
let config = FollowerConfigBuilder::default().build();
1112

1213
let mut follower = Follower::connect(
1314
"relays-new.cardano-mainnet.iohk.io:3001",
@@ -16,8 +17,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
1617
)
1718
.await?;
1819

19-
follower.set_read_pointer(PointOrTip::Tip).await?;
20-
2120
loop {
2221
let chain_update = follower.next().await?;
2322

hermes/crates/cardano-chain-follower/src/lib.rs

Lines changed: 112 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use pallas::{
1414
};
1515

1616
/// Default [`Follower`] block buffer size.
17-
const DEFAULT_BLOCK_BUFFER_SIZE: usize = 32;
17+
const DEFAULT_CHAINUPDATE_BUFFER_SIZE: usize = 32;
1818
/// Default [`Follower`] max await retries.
1919
const DEFAULT_MAX_AWAIT_RETRIES: u32 = 3;
2020

@@ -92,6 +92,58 @@ impl From<Network> for u64 {
9292
}
9393
}
9494

95+
/// Cardano chain Reader.
96+
pub struct Reader {}
97+
98+
impl Reader {
99+
/// Connects the Reader to a producer using the node-to-node protocol.
100+
///
101+
/// # Arguments
102+
///
103+
/// * `address`: Address of the node to connect to.
104+
/// * `network`: The [Network] the client is assuming it's connecting to.
105+
///
106+
/// # Errors
107+
///
108+
/// Returns Err if the connection could not be established.
109+
pub async fn connect(_address: &str, _network: Network) -> Result<Self> {
110+
todo!()
111+
}
112+
113+
/// Reads a single block from the chain.
114+
///
115+
/// # Arguments
116+
///
117+
/// * `at`: The point at which to read the block.
118+
///
119+
/// # Errors
120+
///
121+
/// Returns Err if the block was not found or if some communication error ocurred.
122+
pub async fn read_block(&mut self, _at: Point) -> Result<MultiEraBlockData> {
123+
todo!()
124+
}
125+
126+
/// Reads a range of blocks from the chain.
127+
///
128+
/// # Arguments
129+
///
130+
/// * `from`: The point at which to start reading block from.
131+
/// * `to`: The point up to which the blocks will be read.
132+
///
133+
/// # Errors
134+
///
135+
/// Returns Err if the block range was not found or if some communication error
136+
/// ocurred.
137+
pub async fn read_block_range<P>(
138+
&mut self, _from: Point, _to: P,
139+
) -> Result<Vec<MultiEraBlockData>>
140+
where
141+
P: Into<PointOrTip>,
142+
{
143+
todo!()
144+
}
145+
}
146+
95147
/// Enum of chain updates received by the follower.
96148
pub enum ChainUpdate {
97149
/// New block inserted on chain.
@@ -100,56 +152,84 @@ pub enum ChainUpdate {
100152
Rollback(MultiEraBlockData),
101153
}
102154

103-
/// Builder used to create [`Config`]s.
104-
#[derive(Default)]
105-
pub struct ConfigBuilder {
155+
/// Builder used to create [`FollowerConfig`]s.
156+
pub struct FollowerConfigBuilder {
106157
/// Block buffer size option.
107-
block_buffer_size: Option<usize>,
158+
chain_update_buffer_size: usize,
108159
/// Maximum await retries option.
109-
max_await_retries: Option<u32>,
160+
max_await_retries: u32,
161+
/// Where to start following from.
162+
follow_from: PointOrTip,
110163
}
111164

112-
impl ConfigBuilder {
113-
/// Sets the size of the block buffer used by the [`Follower`].
165+
impl Default for FollowerConfigBuilder {
166+
fn default() -> Self {
167+
Self {
168+
chain_update_buffer_size: DEFAULT_CHAINUPDATE_BUFFER_SIZE,
169+
max_await_retries: DEFAULT_MAX_AWAIT_RETRIES,
170+
follow_from: PointOrTip::Tip,
171+
}
172+
}
173+
}
174+
175+
impl FollowerConfigBuilder {
176+
/// Sets the size of the chain updates buffer used by the [`Follower`].
114177
///
115178
/// # Arguments
116179
///
117-
/// * `block_buffer_size`: Size of the block buffer.
180+
/// * `chain_update_buffer_size`: Size of the chain updates buffer.
118181
#[must_use]
119-
pub fn with_block_buffer_size(mut self, block_buffer_size: usize) -> Self {
120-
self.block_buffer_size = Some(block_buffer_size);
182+
pub fn chain_update_buffer_size(mut self, block_buffer_size: usize) -> Self {
183+
self.chain_update_buffer_size = block_buffer_size;
121184
self
122185
}
123186

124187
/// Sets the maximum number of retries the [`Follower`] will execute when remote node
125188
/// sends an AWAIT message when the [`Follower`] is already in the "must reply"
126189
/// state.
127190
///
128-
/// # Argument
191+
/// # Arguments
129192
///
130193
/// * `max_await_retries`: Maxium number of retries.
131194
#[must_use]
132-
pub fn with_max_await_retries(mut self, max_await_retries: u32) -> Self {
133-
self.max_await_retries = Some(max_await_retries);
195+
pub fn max_await_retries(mut self, max_await_retries: u32) -> Self {
196+
self.max_await_retries = max_await_retries;
134197
self
135198
}
136199

137-
/// Builds a [`Config`].
200+
/// Sets the point at which the follower will start following from.
201+
///
202+
/// # Arguments
203+
///
204+
/// * `from`: Sync starting point.
138205
#[must_use]
139-
pub fn build(self) -> Config {
140-
Config {
141-
block_buffer_size: self.block_buffer_size.unwrap_or(DEFAULT_BLOCK_BUFFER_SIZE),
142-
max_await_retries: self.max_await_retries.unwrap_or(DEFAULT_MAX_AWAIT_RETRIES),
206+
pub fn follow_from<P>(mut self, from: P) -> Self
207+
where
208+
P: Into<PointOrTip>,
209+
{
210+
self.follow_from = from.into();
211+
self
212+
}
213+
214+
/// Builds a [`FollowerConfig`].
215+
#[must_use]
216+
pub fn build(self) -> FollowerConfig {
217+
FollowerConfig {
218+
chain_update_buffer_size: self.chain_update_buffer_size,
219+
max_await_retries: self.max_await_retries,
220+
follow_from: self.follow_from,
143221
}
144222
}
145223
}
146224

147225
/// Configuration for the Cardano chain follower.
148-
pub struct Config {
149-
/// Configured block buffer size.
150-
block_buffer_size: usize,
226+
pub struct FollowerConfig {
227+
/// Configured chain update buffer size.
228+
pub chain_update_buffer_size: usize,
151229
/// Configured maximum await retry count.
152-
max_await_retries: u32,
230+
pub max_await_retries: u32,
231+
/// Where to start following from.
232+
pub follow_from: PointOrTip,
153233
}
154234

155235
/// Cardano chain follower.
@@ -162,42 +242,14 @@ impl Follower {
162242
///
163243
/// * `address`: Address of the node to connect to.
164244
/// * `network`: The [Network] the client is assuming it's connecting to.
165-
/// * `config`: Follower's configuration (see [`ConfigBuilder`]).
166-
///
167-
/// # Errors
168-
///
169-
/// Returns Err if the connection could not be estabilished.
170-
pub async fn connect(_address: &str, _network: Network, _config: Config) -> Result<Self> {
171-
todo!()
172-
}
173-
174-
/// Fetches a single block from the chain.
175-
///
176-
/// # Arguments
177-
///
178-
/// * `at`: The point at which to fetch the block.
245+
/// * `config`: Follower's configuration (see [`FollowerConfigBuilder`]).
179246
///
180247
/// # Errors
181248
///
182-
/// Returns Err if the block was not found or if some communication error ocurred.
183-
pub async fn fetch_block(&mut self, _at: Point) -> Result<MultiEraBlockData> {
184-
todo!()
185-
}
186-
187-
/// Fetches a range of blocks from the chain.
188-
///
189-
/// # Arguments
190-
///
191-
/// * `from`: The point at which to start fetching block from.
192-
/// * `to`: The point up to which the blocks will be fetched.
193-
///
194-
/// # Errors
195-
///
196-
/// Returns Err if the block range was not found or if some communication error
197-
/// ocurred.
198-
pub async fn fetch_block_range(
199-
&mut self, _from: Point, _to: Point,
200-
) -> Result<Vec<MultiEraBlockData>> {
249+
/// Returns Err if the connection could not be established.
250+
pub async fn connect(
251+
_address: &str, _network: Network, _config: FollowerConfig,
252+
) -> Result<Self> {
201253
todo!()
202254
}
203255

@@ -212,7 +264,9 @@ impl Follower {
212264
///
213265
/// Returns Err if something went wrong while communicating with the producer.
214266
pub async fn set_read_pointer<P>(&mut self, _at: P) -> Result<Option<Point>>
215-
where P: Into<PointOrTip> {
267+
where
268+
P: Into<PointOrTip>,
269+
{
216270
todo!()
217271
}
218272

@@ -226,7 +280,7 @@ impl Follower {
226280
}
227281
}
228282

229-
/// Validate a multiera block.
283+
/// Validate a multi-era block.
230284
///
231285
/// This does not execute Plutus scripts nor validates ledger state.
232286
/// It only checks that the block is correctly formatted for its era.

0 commit comments

Comments
 (0)