Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Genesis parsing #273

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion chain/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ itertools = "0.10.1"
getrandom = { version = "0.2.3", features = ["js"] }
rand = "0.8.5"
fraction = "0.10.0"
base64 = "0.13"
base64 = "0.21.5"
num-bigint = "0.4.0"
num-integer = "0.1.45"
#rand_os = "0.1"
Expand Down
5 changes: 2 additions & 3 deletions chain/rust/src/byron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use std::io::{BufRead, Write};

use schemars::JsonSchema;

use cml_crypto::{chain_crypto::hash::Blake2b224, Bip32PublicKey, PublicKey};

use crate::Coin;
Expand All @@ -15,9 +13,10 @@ use crate::Coin;
use cbor_event::{self, de::Deserializer, se::Serializer};

pub use self::crc32::Crc32;
pub use cml_core::network::ProtocolMagic;
pub use utils::{
make_daedalus_bootstrap_witness, make_icarus_bootstrap_witness, AddressId, ByronAddressError,
ByronScript, ProtocolMagic, StakeholderId,
ByronScript, ParseExtendedAddrError, StakeholderId,
};

mod base58;
Expand Down
65 changes: 3 additions & 62 deletions chain/rust/src/byron/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,73 +255,14 @@ impl ::std::str::FromStr for ByronAddress {
}
}

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum ParseExtendedAddrError {
#[error("Deserialize: {0:?}")]
DeserializeError(DeserializeError),
#[error("Base58: {0:?}")]
Base58Error(base58::Error),
}

impl fmt::Display for ProtocolMagic {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(
Clone,
Copy,
Debug,
Eq,
Ord,
PartialEq,
PartialOrd,
Hash,
serde::Serialize,
serde::Deserialize,
JsonSchema,
)]
pub struct ProtocolMagic(u32);

impl From<ProtocolMagic> for u32 {
fn from(val: ProtocolMagic) -> Self {
val.0
}
}

impl From<u32> for ProtocolMagic {
fn from(inner: u32) -> Self {
ProtocolMagic(inner)
}
}

impl ::std::ops::Deref for ProtocolMagic {
type Target = u32;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Default for ProtocolMagic {
fn default() -> Self {
NetworkInfo::mainnet().protocol_magic()
}
}

impl cbor_event::se::Serialize for ProtocolMagic {
fn serialize<'se, W: Write>(
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
serializer.write_unsigned_integer(self.0 as u64)
}
}

impl Deserialize for ProtocolMagic {
fn deserialize<R: BufRead>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
Ok(Self(raw.unsigned_integer()? as u32))
}
}

pub fn make_daedalus_bootstrap_witness(
tx_body_hash: TransactionHash,
addr: ByronAddress,
Expand Down
67 changes: 67 additions & 0 deletions chain/rust/src/genesis/byron/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! Blockchain network specific config (ProtocolMagic)
//!
//! there are some settings that need to be set in order to guarantee
//! operability with the appropriate network or different option.
//!

use std::{
collections::BTreeMap,
time::{Duration, SystemTime},
};

use crate::{
byron::{ByronAddress, ProtocolMagic, StakeholderId},
fees::LinearFee,
Coin,
};
use cml_crypto::{
chain_crypto::{self, Ed25519, Ed25519Bip32},
BlockHeaderHash,
};

/// Configuration for the wallet-crypto
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "generic-serialization", derive(Serialize, Deserialize))]
pub struct Config {
pub protocol_magic: ProtocolMagic,
}
impl Config {
pub fn new(protocol_magic: ProtocolMagic) -> Self {
Config { protocol_magic }
}
}
impl Default for Config {
fn default() -> Self {
Config::new(ProtocolMagic::default())
}
}

/// A subset of the genesis data. The genesis data is a JSON file
/// whose canonicalized form has the hash 'genesis_prev', which is the
/// parent of the genesis block of epoch 0. (Note that "genesis data"
/// is something completely different from a epoch genesis block. The
/// genesis data is not stored in the chain as a block.)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenesisData {
// FIXME: genesis_prev shouldn't be here since it's computed *from* the GenesisData.
pub genesis_prev: BlockHeaderHash,
pub epoch_stability_depth: usize, // a.k.a. 'k'
pub start_time: SystemTime,
pub slot_duration: Duration,
pub protocol_magic: ProtocolMagic,
pub fee_policy: LinearFee,
pub avvm_distr: BTreeMap<chain_crypto::PublicKey<Ed25519>, Coin>, // AVVM = Ada Voucher Vending Machine
// note: order of the keys here is unspecified in the spec (anything order is valid)
pub non_avvm_balances: BTreeMap<ByronAddress, Coin>,
pub boot_stakeholders: BTreeMap<StakeholderId, BootStakeholder>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BootStakeholder {
pub weight: BootStakeWeight,
pub issuer_pk: chain_crypto::PublicKey<Ed25519Bip32>,
pub delegate_pk: chain_crypto::PublicKey<Ed25519Bip32>,
pub cert: chain_crypto::Signature<(), Ed25519Bip32>,
}

pub type BootStakeWeight = u16;
3 changes: 3 additions & 0 deletions chain/rust/src/genesis/byron/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod config;
pub mod parse;
pub mod raw;
Loading
Loading