ChainId

Struct ChainId 

Source
pub struct ChainId([u8; 32]);
Expand description

Unique identifier for a Mina blockchain network.

ChainId is a 32-byte cryptographic hash that uniquely identifies a specific Mina blockchain network. It ensures network isolation by preventing nodes from different chains (mainnet, devnet, custom testnets) from connecting to each other.

§Security Properties

The chain ID provides several security guarantees:

  • Deterministic: Always produces the same ID for identical protocol parameters
  • Collision Resistant: Uses Blake2b hashing to prevent ID conflicts
  • Tamper Evident: Any change to protocol parameters changes the chain ID
  • Network Isolation: Incompatible networks cannot connect accidentally

§Computation Method

Chain IDs are computed using ChainId::compute() from these inputs:

  1. Constraint System Digests: MD5 hashes of SNARK constraint systems
  2. Genesis State Hash: Hash of the initial blockchain state
  3. Genesis Constants: Protocol timing and consensus parameters
  4. Protocol Versions: Transaction and network protocol versions
  5. Transaction Pool Size: Maximum mempool configuration

The computation uses Blake2b-256 to hash these components in a specific order, ensuring reproducible results across different implementations.

§Network Usage

Chain IDs are used throughout the networking stack:

  • Peer Discovery: Nodes broadcast their chain ID during discovery
  • Connection Handshakes: WebRTC offers include chain ID for validation
  • Private Networks: preshared_key() generates libp2p private network keys
  • Protocol Compatibility: Ensures all peers use compatible protocol versions

§Serialization Formats

Chain IDs support multiple serialization formats:

  • Hex String: Human-readable format for configuration files
  • Binary: 32-byte array for network transmission
  • JSON: String representation for APIs and debugging

§Example Usage

use openmina_core::{ChainId, MAINNET_CHAIN_ID};

// Use predefined mainnet ID
let mainnet = MAINNET_CHAIN_ID;
println!("Mainnet: {}", mainnet.to_hex());

// Parse from configuration
let custom_id = ChainId::from_hex("29936104443aaf264a7f0192ac64b1c7173198c1ed404c1bcff5e562e05eb7f6")?;

// Generate private network key
let psk = mainnet.preshared_key();

Tuple Fields§

§0: [u8; 32]

Implementations§

Source§

impl ChainId

Source

pub fn compute( constraint_system_digests: &[[u8; 16]], genesis_state_hash: &StateHash, genesis_constants: &MinaBaseProtocolConstantsCheckedValueStableV1, protocol_transaction_version: u8, protocol_network_version: u8, tx_max_pool_size: &UnsignedExtendedUInt32StableV1, ) -> ChainId

Computes a chain ID from protocol parameters and network configuration.

This method creates a deterministic 32-byte chain identifier by hashing all the fundamental parameters that define a Mina blockchain network. Any change to these parameters will result in a different chain ID, ensuring network isolation and protocol compatibility.

§Parameters
  • constraint_system_digests - MD5 hashes of the SNARK constraint systems used for transaction and block verification
  • genesis_state_hash - Hash of the initial blockchain state
  • genesis_constants - Protocol constants including timing parameters, consensus settings, and economic parameters
  • protocol_transaction_version - Version number of the transaction protocol
  • protocol_network_version - Version number of the network protocol
  • tx_max_pool_size - Maximum number of transactions in the mempool
§Returns

A new ChainId representing the unique identifier for this network configuration.

§Algorithm

The computation process:

  1. Hash all constraint system digests into a combined string
  2. Hash the genesis constants with transaction pool size
  3. Create Blake2b-256 hash of:
    • Genesis state hash (as string)
    • Combined constraint system hash
    • Genesis constants hash (as hex)
    • Protocol transaction version (as MD5 hash)
    • Protocol network version (as MD5 hash)
§Example
use openmina_core::ChainId;
use mina_p2p_messages::v2::UnsignedExtendedUInt32StableV1;

let chain_id = ChainId::compute(
    &constraint_digests,
    &genesis_hash,
    &protocol_constants,
    1,  // transaction version
    1,  // network version
    &UnsignedExtendedUInt32StableV1::from(3000),
);
Source

pub fn preshared_key(&self) -> [u8; 32]

Generates a preshared key for libp2p private networking.

This method creates a cryptographic key used by libp2p’s private network (Pnet) protocol to ensure only nodes with the same chain ID can connect. The preshared key provides an additional layer of network isolation beyond basic chain ID validation.

§Algorithm

The preshared key is computed as:

Blake2b-256("/coda/0.0.1/" + chain_id_hex)

The “/coda/0.0.1/” prefix is a protocol identifier that ensures the preshared key is unique to the Mina protocol and not accidentally compatible with other systems.

§Returns

A 32-byte array containing the preshared key for this chain ID.

§Usage

This key is used to configure libp2p’s private network transport, which encrypts all network traffic and prevents unauthorized nodes from joining the network even if they know peer addresses.

§Example
use openmina_core::MAINNET_CHAIN_ID;

let psk = MAINNET_CHAIN_ID.preshared_key();
// Use psk to configure libp2p Pnet transport
Source

pub fn to_hex(&self) -> String

Converts the chain ID to a hexadecimal string representation.

This method creates a lowercase hex string of the 32-byte chain ID, suitable for display, logging, configuration files, and JSON serialization.

§Returns

A 64-character hexadecimal string representing the chain ID.

§Example
use openmina_core::MAINNET_CHAIN_ID;

let hex_id = MAINNET_CHAIN_ID.to_hex();
assert_eq!(hex_id.len(), 64);
println!("Mainnet ID: {}", hex_id);
Source

pub fn from_hex(s: &str) -> Result<ChainId, FromHexError>

Parses a chain ID from a hexadecimal string.

This method converts a hex string back into a ChainId instance. The input string must represent exactly 32 bytes (64 hex characters). Case-insensitive parsing is supported.

§Parameters
  • s - A hexadecimal string representing the chain ID
§Returns
  • Ok(ChainId) if the string is valid 64-character hex
  • Err(hex::FromHexError) if the string is invalid or wrong length
§Errors

This method returns an error if:

  • The string contains non-hexadecimal characters
  • The string length is not exactly 64 characters
  • The string represents fewer than 32 bytes
§Example
use openmina_core::ChainId;

let chain_id = ChainId::from_hex(
    "a7351abc7ddf2ea92d1b38cc8e636c271c1dfd2c081c637f62ebc2af34eb7cc1"
)?;
Source

pub fn from_bytes(bytes: &[u8]) -> ChainId

Creates a chain ID from raw bytes.

This method constructs a ChainId from a byte slice, taking the first 32 bytes as the chain identifier. If the input has fewer than 32 bytes, the remaining bytes are zero-padded.

§Parameters
  • bytes - A byte slice containing at least 32 bytes
§Returns

A new ChainId instance created from the input bytes.

§Panics

This method will panic if the input slice has fewer than 32 bytes.

§Example
use openmina_core::ChainId;

let bytes = [0u8; 32]; // All zeros for testing
let chain_id = ChainId::from_bytes(&bytes);

Trait Implementations§

Source§

impl AsRef<[u8]> for ChainId

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl BinProtRead for ChainId

Source§

fn binprot_read<R: Read + ?Sized>(r: &mut R) -> Result<Self, Error>
where Self: Sized,

Source§

impl BinProtWrite for ChainId

Source§

impl Clone for ChainId

Source§

fn clone(&self) -> ChainId

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChainId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ChainId

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for ChainId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ChainId

Source§

fn eq(&self, other: &ChainId) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ChainId

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for ChainId

Source§

impl StructuralPartialEq for ChainId

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<A, T> AsBits<T> for A
where A: AsRef<[T]>, T: BitStore,

§

fn as_bits<O>(&self) -> &BitSlice<T, O>
where O: BitOrder,

Views self as an immutable bit-slice region with the O ordering.
§

fn try_as_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
where O: BitOrder,

Attempts to view self as an immutable bit-slice region with the O ordering. Read more
§

impl<T> BinProtSize for T
where T: BinProtWrite,

§

fn binprot_size(&self) -> usize

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromBinProtStream for T
where T: BinProtRead,

Source§

fn read_from_stream<R>(r: &mut R) -> Result<Self, Error>
where R: Read,

Decodes bytes from reader of byte stream into the specified type T. This function assumes that the data is prepended with 8-bytes little endian integer specirying the size. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,