Skip to content

Commit

Permalink
chore: allow Blue Oak license (need for minicbor crate)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Rosa committed Dec 8, 2023
1 parent e970f84 commit 914f97f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 182 deletions.
20 changes: 0 additions & 20 deletions hermes/crates/cardano-chain-follower/examples/fetch_block.rs

This file was deleted.

25 changes: 0 additions & 25 deletions hermes/crates/cardano-chain-follower/examples/fetch_block_range.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

use std::error::Error;

use cardano_chain_follower::{ChainUpdate, Client, Follower, Network};
use cardano_chain_follower::{ChainUpdate, Follower, Network};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client =
Client::connect_n2n("relays-new.cardano-mainnet.iohk.io:3001", Network::Mainnet).await?;
let mut follower = Follower::new(client);
let mut follower =
Follower::connect_n2n("relays-new.cardano-mainnet.iohk.io:3001", Network::Mainnet).await?;

loop {
let chain_update = follower.next().await?;
Expand Down
187 changes: 55 additions & 132 deletions hermes/crates/cardano-chain-follower/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
//! Cardano chain follower.
//!
//! # Follower
//!
//! The follower can be used to follow the chain and to fetch a single block or a range of
//! blocks.
//!
//! Follower will maintain the state of the read-pointer (the point at which the chain is
//! being read by the chainsync miniprotocol).
//!
//! The read-pointer state will not be modified when fetching blocks.
//!
//! # Client
//!
//! The client can be used to fetch a single block or a range of blocks and
//! is used by the follower to follow the chain.
//!
//! # Example
//!
//! Following chain updates from the tip of the chain can be done like:
//!
//! ```rust,no_run
//! use cardano_chain_follower::{Client, Follower, Network, PointOrTip};
//! use cardano_chain_follower::{Follower, Network, PointOrTip};
//!
//! #[tokio::main]
//! async fn main() {
//! let mut follower = Follower::new(
//! Client::connect_n2n("node.address", Network::Preprod)
//! .await
//! .unwrap(),
//! );
//! let mut follower = Follower::connect_n2n("node.address", Network::Preprod)
//! .await
//! .unwrap();
//!
//! follower.set_read_pointer(PointOrTip::Tip).await.unwrap();
//!
Expand All @@ -48,10 +31,7 @@ use pallas::{
ledger::traverse::MultiEraBlock,
network::{
facades::{NodeClient, PeerClient},
miniprotocols::{
chainsync::NextResponse, MAINNET_MAGIC, PREVIEW_MAGIC, PRE_PRODUCTION_MAGIC,
TESTNET_MAGIC,
},
miniprotocols::{MAINNET_MAGIC, PREVIEW_MAGIC, PRE_PRODUCTION_MAGIC, TESTNET_MAGIC},
},
};

Expand All @@ -75,95 +55,6 @@ pub type Error = Box<dyn std::error::Error>;
/// Crate result type.
pub type Result<T> = std::result::Result<T, Error>;

/// Enum of possible types of clients used by the follower.
pub enum Client {
/// Pallas node-to-node client.
N2n(PeerClient),
/// Pallas node-to-client client.
N2c(NodeClient),
}

impl Client {
/// Connects the follower to a producer using the node-to-node protocol.
///
/// # Arguments
///
/// * `address`: Address of the node to connect to.
/// * `network`: The [Network] the client is assuming it's connecting to.
///
/// # Errors
///
/// Returns Err if the connection could not be estabilished.
pub async fn connect_n2n(address: &str, network: Network) -> Result<Self> {
let client = PeerClient::connect(address, network.into())
.await
.map_err(Box::new)?;

Ok(Client::N2n(client))
}

/// Connects the follower to a producer using the node-to-client protocol.
///
/// # Arguments
///
/// * `path`: Path to the UDS to use for node communication.
/// * `network`: The [Network] the client is assuming it's connecting to.
///
/// # Errors
///
/// Returns Err if the connection could not be estabilished.
#[cfg(unix)]
pub async fn connect_n2c<P>(path: P, network: Network) -> Result<Self>
where P: AsRef<std::path::Path> {
let client = NodeClient::connect(path, network.into())
.await
.map_err(Box::new)?;

Ok(Client::N2c(client))
}

/// Fetches a single block from the producer.
///
/// # Arguments
///
/// * `at`: Point at which to fetch the block from.
///
/// # Errors
///
/// Returns Err if the block could not be fetched (e.g. producer communication
/// failed).
pub async fn fetch_block(&mut self, _at: Point) -> Result<MultiEraBlockData> {
todo!()
}

/// Fetches a range of blocks from the producer.
///
/// # Arguments
///
/// * `from`: Point defining the start of the range.
/// * `to`: Point defining the end of the range.
///
/// # Errors
///
/// Returns Err if the block range could not be fetched (e.g. producer communication
/// failed).
pub async fn fetch_block_range(
&mut self, _from: Point, _to: Point,
) -> Result<Vec<MultiEraBlockData>> {
todo!()
}

// (fsgr): These will be used by the Follower to get the chain updates.

async fn set_read_pointer(&mut self, _at: PointOrTip) -> Result<Point> {
todo!()
}

async fn next(&mut self) -> Result<NextResponse<MultiEraBlockData>> {
todo!()
}
}

/// A point in the chain or the tip.
pub enum PointOrTip {
/// Represents a specific point of the chain.
Expand Down Expand Up @@ -218,6 +109,14 @@ impl From<Network> for u64 {
}
}

/// Enum of possible types of clients used by the follower.
enum Client {
/// Pallas node-to-node client.
N2n(PeerClient),
/// Pallas node-to-client client.
N2c(NodeClient),
}

/// Enum of chain updates received by the follower.
pub enum ChainUpdate {
/// New block inserted on chain.
Expand All @@ -230,27 +129,51 @@ pub enum ChainUpdate {
pub struct Follower {
/// Client used to get chain data.
client: Client,
/// Point at which the follower is reading the chain at.
read_pointer: Point,
}

impl Follower {
/// Creates a new follower.
/// Connects the follower to a producer using the node-to-node protocol.
///
/// # Arguments
///
/// * `client`: The client the follower will use.
pub fn new(client: Client) -> Self {
Self {
client,
read_pointer: Point::Origin,
}
/// * `address`: Address of the node to connect to.
/// * `network`: The [Network] the client is assuming it's connecting to.
///
/// # Errors
///
/// Returns Err if the connection could not be estabilished.
pub async fn connect_n2n(address: &str, network: Network) -> Result<Self> {
let client = Client::N2n(
PeerClient::connect(address, network.into())
.await
.map_err(Box::new)?,
);

Ok(Self { client })
}

/// Returns the follower's client.
#[must_use]
pub fn client(&mut self) -> &mut Client {
&mut self.client
/// Connects the follower to a producer using the node-to-client protocol.
///
/// # Arguments
///
/// * `path`: Path to the UDS to use for node communication.
/// * `network`: The [Network] the client is assuming it's connecting to.
///
/// # Errors
///
/// Returns Err if the connection could not be estabilished.
#[cfg(unix)]
pub async fn connect_n2c<P>(path: P, network: Network) -> Result<Self>
where
P: AsRef<std::path::Path>,
{
let client = Client::N2c(
NodeClient::connect(path, network.into())
.await
.map_err(Box::new)?,
);

Ok(Follower { client })
}

/// Set the follower's chain read-pointer.
Expand All @@ -262,11 +185,11 @@ impl Follower {
/// # Errors
///
/// Returns Err if something went wrong while communicating with the producer.
pub async fn set_read_pointer<P>(&mut self, at: P) -> Result<()>
where P: Into<PointOrTip> {
self.read_pointer = self.client.set_read_pointer(at.into()).await?;

Ok(())
pub async fn set_read_pointer<P>(&mut self, _at: P) -> Result<()>
where
P: Into<PointOrTip>,
{
todo!()
}

/// Receive the next chain update from the producer.
Expand Down
3 changes: 2 additions & 1 deletion hermes/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ allow = [
"MIT",
"Apache-2.0",
"Unicode-DFS-2016",
"BSD-3-Clause"
"BSD-3-Clause",
"BlueOak-1.0.0"
]
# List of explicitly disallowed licenses
# See https://spdx.org/licenses/ for list of possible licenses
Expand Down

0 comments on commit 914f97f

Please sign in to comment.