Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pallas-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hex = "0.4.3"
itertools = "0.13.0"
pallas-codec = { version = "=1.0.0-alpha.2", path = "../pallas-codec" }
pallas-crypto = { version = "=1.0.0-alpha.2", path = "../pallas-crypto" }
pallas-primitives = { version = "=1.0.0-alpha.2", path = "../pallas-primitives" }
rand = "0.8.5"
socket2 = "0.5.5"
thiserror = "1.0.31"
Expand Down
181 changes: 90 additions & 91 deletions pallas-network/src/miniprotocols/common.rs
Original file line number Diff line number Diff line change
@@ -1,144 +1,143 @@
use std::fmt::Debug;

use pallas_codec::minicbor::{decode, encode, Decode, Decoder, Encode, Encoder};

/// Well-known magic for testnet
pub const TESTNET_MAGIC: u64 = 1097911063;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::TESTNET_MAGIC` instead")]
pub const TESTNET_MAGIC: u64 = pallas_primitives::types::network_constant::TESTNET_MAGIC;

/// Well-known magic for mainnet
pub const MAINNET_MAGIC: u64 = 764824073;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::MAINNET_MAGIC` instead")]
pub const MAINNET_MAGIC: u64 = pallas_primitives::types::network_constant::MAINNET_MAGIC;

/// Well-known magic for preview
pub const PREVIEW_MAGIC: u64 = 2;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::PREVIEW_MAGIC` instead")]
pub const PREVIEW_MAGIC: u64 = pallas_primitives::types::network_constant::PREVIEW_MAGIC;

/// Well-known magic for preprod
pub const PREPROD_MAGIC: u64 = 1;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::PREPROD_MAGIC` instead")]
pub const PREPROD_MAGIC: u64 = pallas_primitives::types::network_constant::PREPROD_MAGIC;

/// Alias for PREPROD_MAGIC
pub const PRE_PRODUCTION_MAGIC: u64 = 1;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PRE_PRODUCTION_MAGIC` instead"
)]
pub const PRE_PRODUCTION_MAGIC: u64 =
pallas_primitives::types::network_constant::PRE_PRODUCTION_MAGIC;

/// Well-known magic for preprod
pub const SANCHONET_MAGIC: u64 = 4;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::SANCHONET_MAGIC` instead")]
pub const SANCHONET_MAGIC: u64 = pallas_primitives::types::network_constant::SANCHONET_MAGIC;

/// Bitflag for client-side version of a known protocol
/// # Example
/// ```
/// use pallas_network::miniprotocols::*;
/// let channel = PROTOCOL_CLIENT | PROTOCOL_N2N_HANDSHAKE;
/// ```
pub const PROTOCOL_CLIENT: u16 = 0x0;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::PROTOCOL_CLIENT` instead")]
pub const PROTOCOL_CLIENT: u16 = pallas_primitives::types::network_constant::PROTOCOL_CLIENT;

/// Bitflag for server-side version of a known protocol
/// # Example
/// ```
/// use pallas_network::miniprotocols::*;
/// let channel = PROTOCOL_SERVER | PROTOCOL_N2N_CHAIN_SYNC;
/// ```
pub const PROTOCOL_SERVER: u16 = 0x8000;
#[deprecated(note = "Use `pallas_primitives::types::network_constant::PROTOCOL_SERVER` instead")]
pub const PROTOCOL_SERVER: u16 = pallas_primitives::types::network_constant::PROTOCOL_SERVER;

/// Protocol channel number for node-to-node handshakes
pub const PROTOCOL_N2N_HANDSHAKE: u16 = 0;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_HANDSHAKE` instead"
)]
pub const PROTOCOL_N2N_HANDSHAKE: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_HANDSHAKE;

/// Protocol channel number for node-to-node chain-sync
pub const PROTOCOL_N2N_CHAIN_SYNC: u16 = 2;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_CHAIN_SYNC` instead"
)]
pub const PROTOCOL_N2N_CHAIN_SYNC: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_CHAIN_SYNC;

/// Protocol channel number for node-to-node block-fetch
pub const PROTOCOL_N2N_BLOCK_FETCH: u16 = 3;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_BLOCK_FETCH` instead"
)]
pub const PROTOCOL_N2N_BLOCK_FETCH: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_BLOCK_FETCH;

/// Protocol channel number for node-to-node tx-submission
pub const PROTOCOL_N2N_TX_SUBMISSION: u16 = 4;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_TX_SUBMISSION` instead"
)]
pub const PROTOCOL_N2N_TX_SUBMISSION: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_TX_SUBMISSION;

/// Protocol channel number for node-to-node Keep-alive
pub const PROTOCOL_N2N_KEEP_ALIVE: u16 = 8;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_KEEP_ALIVE` instead"
)]
pub const PROTOCOL_N2N_KEEP_ALIVE: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_KEEP_ALIVE;

/// Protocol channel number for node-to-node Peer-sharing
pub const PROTOCOL_N2N_PEER_SHARING: u16 = 10;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2N_PEER_SHARING` instead"
)]
pub const PROTOCOL_N2N_PEER_SHARING: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2N_PEER_SHARING;

/// Protocol channel number for node-to-client handshakes
pub const PROTOCOL_N2C_HANDSHAKE: u16 = 0;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_HANDSHAKE` instead"
)]
pub const PROTOCOL_N2C_HANDSHAKE: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_HANDSHAKE;

/// Protocol channel number for node-to-client chain-sync
pub const PROTOCOL_N2C_CHAIN_SYNC: u16 = 5;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_CHAIN_SYNC` instead"
)]
pub const PROTOCOL_N2C_CHAIN_SYNC: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_CHAIN_SYNC;

/// Protocol channel number for node-to-client tx-submission
pub const PROTOCOL_N2C_TX_SUBMISSION: u16 = 6;

// Protocol channel number for node-to-client state queries
pub const PROTOCOL_N2C_STATE_QUERY: u16 = 7;

// Protocol channel number for node-to-client mempool monitor
pub const PROTOCOL_N2C_TX_MONITOR: u16 = 9;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_TX_SUBMISSION` instead"
)]
pub const PROTOCOL_N2C_TX_SUBMISSION: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_TX_SUBMISSION;

/// Protocol channel number for node-to-client state queries
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_STATE_QUERY` instead"
)]
pub const PROTOCOL_N2C_STATE_QUERY: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_STATE_QUERY;

/// Protocol channel number for node-to-client mempool monitor
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_TX_MONITOR` instead"
)]
pub const PROTOCOL_N2C_TX_MONITOR: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_TX_MONITOR;

/// Protocol channel number for node-to-client local message submission
/// This protocol is available only on the DMQ node.
// TODO: use the final mini-protocol number once available
pub const PROTOCOL_N2C_MSG_SUBMISSION: u16 = 1;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_MSG_SUBMISSION` instead"
)]
pub const PROTOCOL_N2C_MSG_SUBMISSION: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_MSG_SUBMISSION;

/// Protocol channel number for node-to-client local message notification
/// This protocol is available only on the DMQ node.
// TODO: use the final mini-protocol number once available
pub const PROTOCOL_N2C_MSG_NOTIFICATION: u16 = 2;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::PROTOCOL_N2C_MSG_NOTIFICATION` instead"
)]
pub const PROTOCOL_N2C_MSG_NOTIFICATION: u16 =
pallas_primitives::types::network_constant::PROTOCOL_N2C_MSG_NOTIFICATION;

/// A point within a chain
#[derive(Clone, Eq, PartialEq, Hash)]
pub enum Point {
Origin,
Specific(u64, Vec<u8>),
}

impl Point {
pub fn slot_or_default(&self) -> u64 {
match self {
Point::Origin => 0,
Point::Specific(slot, _) => *slot,
}
}
}

impl Debug for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Origin => write!(f, "Origin"),
Self::Specific(arg0, arg1) => write!(f, "({}, {})", arg0, hex::encode(arg1)),
}
}
}

impl Point {
pub fn new(slot: u64, hash: Vec<u8>) -> Self {
Point::Specific(slot, hash)
}
}

impl Encode<()> for Point {
fn encode<W: encode::Write>(
&self,
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), encode::Error<W::Error>> {
match self {
Point::Origin => e.array(0)?,
Point::Specific(slot, hash) => e.array(2)?.u64(*slot)?.bytes(hash)?,
};

Ok(())
}
}

impl<'b> Decode<'b, ()> for Point {
fn decode(d: &mut Decoder<'b>, _ctx: &mut ()) -> Result<Self, decode::Error> {
let size = d.array()?;

match size {
Some(0) => Ok(Point::Origin),
Some(2) => {
let slot = d.u64()?;
let hash = d.bytes()?;
Ok(Point::Specific(slot, Vec::from(hash)))
}
_ => Err(decode::Error::message(
"can't decode Point from array of size",
)),
}
}
}
#[deprecated(note = "Use `pallas_primitives::types::point::Point` instead")]
pub use pallas_primitives::types::point::Point;
6 changes: 5 additions & 1 deletion pallas-network/src/multiplexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ impl Plexer {
}

/// Protocol value that defines max segment length
pub const MAX_SEGMENT_PAYLOAD_LENGTH: usize = 65535;
#[deprecated(
note = "Use `pallas_primitives::types::network_constant::MAX_SEGMENT_PAYLOAD_LENGTH` instead"
)]
pub const MAX_SEGMENT_PAYLOAD_LENGTH: usize =
pallas_primitives::types::network_constant::MAX_SEGMENT_PAYLOAD_LENGTH;

fn try_decode_message<M>(buffer: &mut Vec<u8>) -> Result<Option<M>, Error>
where
Expand Down
3 changes: 2 additions & 1 deletion pallas-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ pub mod alonzo;
pub mod babbage;
pub mod byron;
pub mod conway;
pub use plutus_data::*;
pub mod types;

pub use plutus_data::*;
pub use framework::*;

pub use pallas_codec::codec_by_datatype;
Expand Down
2 changes: 2 additions & 0 deletions pallas-primitives/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod network_constant;
pub mod point;
85 changes: 85 additions & 0 deletions pallas-primitives/src/types/network_constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! Network constants

/// Well-known magic for testnet
pub const TESTNET_MAGIC: u64 = 1097911063;
pub const TESTNET_NETWORK_ID: u64 = 0;

/// Well-known magic for mainnet
pub const MAINNET_MAGIC: u64 = 764824073;
pub const MAINNET_NETWORK_ID: u64 = 1;

/// Well-known magic for preview
pub const PREVIEW_MAGIC: u64 = 2;
pub const PREVIEW_NETWORK_ID: u64 = 0;

/// Well-known magic for preprod
pub const PREPROD_MAGIC: u64 = 1;

/// Alias for PREPROD_MAGIC
pub const PRE_PRODUCTION_MAGIC: u64 = 1;
pub const PRE_PRODUCTION_NETWORK_ID: u64 = 0;

/// Well-known magic for preprod
pub const SANCHONET_MAGIC: u64 = 4;

/// Bitflag for client-side version of a known protocol
/// # Example
/// ```
/// use pallas_network::miniprotocols::*;
/// let channel = PROTOCOL_CLIENT | PROTOCOL_N2N_HANDSHAKE;
/// ```
pub const PROTOCOL_CLIENT: u16 = 0x0;
Comment thread
bkioshn marked this conversation as resolved.

/// Bitflag for server-side version of a known protocol
/// # Example
/// ```
/// use pallas_network::miniprotocols::*;
/// let channel = PROTOCOL_SERVER | PROTOCOL_N2N_CHAIN_SYNC;
/// ```
pub const PROTOCOL_SERVER: u16 = 0x8000;
Comment thread
bkioshn marked this conversation as resolved.

/// Protocol channel number for node-to-node handshakes
pub const PROTOCOL_N2N_HANDSHAKE: u16 = 0;

/// Protocol channel number for node-to-node chain-sync
pub const PROTOCOL_N2N_CHAIN_SYNC: u16 = 2;

/// Protocol channel number for node-to-node block-fetch
pub const PROTOCOL_N2N_BLOCK_FETCH: u16 = 3;

/// Protocol channel number for node-to-node tx-submission
pub const PROTOCOL_N2N_TX_SUBMISSION: u16 = 4;

/// Protocol channel number for node-to-node Keep-alive
pub const PROTOCOL_N2N_KEEP_ALIVE: u16 = 8;

/// Protocol channel number for node-to-node Peer-sharing
pub const PROTOCOL_N2N_PEER_SHARING: u16 = 10;

/// Protocol channel number for node-to-client handshakes
pub const PROTOCOL_N2C_HANDSHAKE: u16 = 0;

/// Protocol channel number for node-to-client chain-sync
pub const PROTOCOL_N2C_CHAIN_SYNC: u16 = 5;

/// Protocol channel number for node-to-client tx-submission
pub const PROTOCOL_N2C_TX_SUBMISSION: u16 = 6;

// Protocol channel number for node-to-client state queries
pub const PROTOCOL_N2C_STATE_QUERY: u16 = 7;

// Protocol channel number for node-to-client mempool monitor
pub const PROTOCOL_N2C_TX_MONITOR: u16 = 9;

/// Protocol channel number for node-to-client local message submission
/// This protocol is available only on the DMQ node.
// TODO: use the final mini-protocol number once available
pub const PROTOCOL_N2C_MSG_SUBMISSION: u16 = 1;

/// Protocol channel number for node-to-client local message notification
/// This protocol is available only on the DMQ node.
// TODO: use the final mini-protocol number once available
pub const PROTOCOL_N2C_MSG_NOTIFICATION: u16 = 2;

/// Protocol value that defines max segment length
pub const MAX_SEGMENT_PAYLOAD_LENGTH: usize = 65535;
Loading