mod follow;
mod mithril_snapshot;
use std::str::FromStr;
pub use follow::*;
pub use pallas::network::miniprotocols::Point;
use pallas::{
ledger::traverse::{wellknown::GenesisValues, MultiEraBlock},
network::miniprotocols::{
chainsync, MAINNET_MAGIC, PREVIEW_MAGIC, PRE_PRODUCTION_MAGIC, TESTNET_MAGIC,
},
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Codec error: {0:?}")]
Codec(pallas::ledger::traverse::Error),
#[error("Client error: {0:?}")]
Client(pallas::network::facades::Error),
#[error("Blockfetch error: {0:?}")]
Blockfetch(pallas::network::miniprotocols::blockfetch::ClientError),
#[error("Chainsync error: {0:?}")]
Chainsync(chainsync::ClientError),
#[error("Failed to set follower read pointer")]
SetReadPointer,
#[error("Follower follow task is not running")]
FollowTaskNotRunning,
#[error("Failed to read block(s) from Mithril snapshot")]
MithrilSnapshot,
#[error("Failed to parse network")]
ParseNetwork,
#[error("Internal error")]
InternalError,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum PointOrTip {
Point(Point),
Tip,
}
impl From<Point> for PointOrTip {
fn from(point: Point) -> Self {
Self::Point(point)
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct MultiEraBlockData(Vec<u8>);
impl MultiEraBlockData {
pub fn decode(&self) -> Result<MultiEraBlock> {
let block = MultiEraBlock::decode(&self.0).map_err(Error::Codec)?;
Ok(block)
}
#[must_use]
pub fn into_raw_data(self) -> Vec<u8> {
self.0
}
}
impl AsRef<[u8]> for MultiEraBlockData {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Network {
Mainnet,
Preprod,
Preview,
Testnet,
}
const MAINNET_NAME: &str = "mainnet";
const PREPROD_NAME: &str = "preprod";
const PREVIEW_NAME: &str = "preview";
const TESTNET_NAME: &str = "testnet";
impl FromStr for Network {
type Err = Error;
fn from_str(input: &str) -> std::result::Result<Network, Self::Err> {
match input {
MAINNET_NAME => Ok(Network::Mainnet),
PREPROD_NAME => Ok(Network::Preprod),
PREVIEW_NAME => Ok(Network::Preview),
TESTNET_NAME => Ok(Network::Testnet),
_ => Err(Error::ParseNetwork),
}
}
}
impl std::fmt::Display for Network {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Network::Mainnet => write!(f, "{MAINNET_NAME}"),
Network::Preprod => write!(f, "{PREPROD_NAME}"),
Network::Preview => write!(f, "{PREVIEW_NAME}"),
Network::Testnet => write!(f, "{TESTNET_NAME}"),
}
}
}
impl From<Network> for u64 {
fn from(network: Network) -> Self {
match network {
Network::Mainnet => MAINNET_MAGIC,
Network::Preprod => PRE_PRODUCTION_MAGIC,
Network::Preview => PREVIEW_MAGIC,
Network::Testnet => TESTNET_MAGIC,
}
}
}
#[must_use]
pub fn network_genesis_values(network: &Network) -> Option<GenesisValues> {
match network {
Network::Mainnet => GenesisValues::from_magic(MAINNET_MAGIC),
Network::Preprod => GenesisValues::from_magic(PRE_PRODUCTION_MAGIC),
Network::Preview => GenesisValues::from_magic(PREVIEW_MAGIC),
Network::Testnet => GenesisValues::from_magic(TESTNET_MAGIC),
}
}
#[allow(dead_code)]
fn validate_multiera_block(_block: &MultiEraBlock) {
}