Skip to content

Commit

Permalink
fix(configs): fix Shelley genesis parsing (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt authored Jan 15, 2025
1 parent 1e521e4 commit 307495b
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 2 deletions.
102 changes: 100 additions & 2 deletions pallas-configs/src/shelley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,74 @@ pub struct ProtocolParams {
pub a0: pallas_primitives::alonzo::RationalNumber,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
pub hash: String,
pub url: String
}

#[derive(Debug, Deserialize, Clone)]
pub struct SingleHostAddr {
pub port: Option<u32>,
pub IPv6: Option<String>,
pub IPv4: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SingleHostName {
pub port: Option<u32>,
pub dns_name: String,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MultiHostName {
pub dns_name: String,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase", untagged)]
pub enum Relay {
SingleHostAddr(SingleHostAddr),
SingleHostName(SingleHostName),
MultiHostName(MultiHostName),
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Credential {
KeyHash(String),
ScriptHash(String),
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RewardAccount {
pub credential: Credential,
pub network: String,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Pool {
pub cost: u64,
#[serde(deserialize_with = "deserialize_rational")]
pub margin: pallas_primitives::alonzo::RationalNumber,
pub metadata: Option<Metadata>,
pub owners: Vec<String>,
pub pledge: u64,
pub public_key: String, // pool ID
pub relays: Vec<HashMap<String, Relay>>,
pub reward_account: RewardAccount,
pub vrf: String,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Staking {
pub pools: Option<HashMap<String, String>>,
pub pools: Option<HashMap<String, Pool>>,
pub stake: Option<HashMap<String, String>>,
}

Expand All @@ -119,7 +183,7 @@ pub struct GenesisFile {
pub active_slots_coeff: Option<f32>,
pub epoch_length: Option<u32>,
pub gen_delegs: Option<HashMap<String, GenDelegs>>,
pub initial_funds: Option<HashMap<String, String>>,
pub initial_funds: Option<HashMap<String, u64>>,
pub max_kes_evolutions: Option<u32>,
pub max_lovelace_supply: Option<u64>,
pub network_id: Option<String>,
Expand All @@ -141,6 +205,21 @@ pub fn from_file(path: &std::path::Path) -> Result<GenesisFile, std::io::Error>
Ok(parsed)
}

pub type GenesisUtxo = (Hash<32>, pallas_addresses::Address, u64);

pub fn shelley_utxos(config: &GenesisFile) -> Vec<GenesisUtxo> {
match &config.initial_funds {
None => Vec::new(),
Some(funds) => funds.iter().map(|(addr, amount)| {
let addr = pallas_addresses::Address::from_hex(addr).unwrap();

let txid = pallas_crypto::hash::Hasher::<256>::hash(&addr.to_vec());

(txid, addr, *amount)
}).collect()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -154,6 +233,25 @@ mod tests {
from_file(&path).unwrap()
}

#[test]
fn calc_address_txid() {
let config = load_test_data_config("golden");
let utxos = shelley_utxos(&config);
let utxo = utxos.first().unwrap();
assert_eq!(
utxo.0.to_string(),
"f9ec23569778d1c5f7f43e0e98464335f02fb98b57683faa1c6b18c82921d2da"
);
assert_eq!(
utxo.1.to_bech32().unwrap(),
"addr_test1qrsm4h32h9r95f8at64ykuugxqu3wvu0s5ay3vg6tlyevjh4e2flkegka00r69gt8c4vkxgf2vnnph3nsvhlkg5ukgxslee3tf"
);
assert_eq!(
utxo.2,
12157196
);
}

#[test]
fn test_preview_json_loads() {
load_test_data_config("preview");
Expand Down
93 changes: 93 additions & 0 deletions test_data/golden-shelley-genesis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"maxKESEvolutions": 28899,
"updateQuorum": 16991,
"protocolParams": {
"extraEntropy": {
"tag": "NeutralNonce"
},
"maxBlockHeaderSize": 17569,
"poolDeposit": 0,
"minUTxOValue": 0,
"minFeeB": 0,
"a0": 0.0,
"minPoolCost": 0,
"rho": 0.0,
"tau": 0.0,
"nOpt": 100,
"protocolVersion": {
"minor": 0,
"major": 2
},
"eMax": 0,
"keyDeposit": 0,
"decentralisationParam": 1.9e-2,
"maxTxSize": 2048,
"maxBlockBodySize": 239857,
"minFeeA": 0
},
"slotsPerKESPeriod": 8541,
"genDelegs": {
"38e7c5986a34f334e19b712c0aa525146dab8f0ff889b2ad16894241": {
"vrf": "fce31c6f3187531ee4a39aa743c24d22275f415a8895e9cd22c30c8a25cdef0d",
"delegate": "e6960dd671ee8d73de1a83d1345b661165dcddeba99623beef2f157a"
}
},
"staking": {
"stake": {
"83a192dec0e8da2188e520d0c536a69a747cf173a3df16a6daa94d86": "649eda82bf644d34a6925f24ea4c4c36d27e51de1b44ef47e3560be7"
},
"pools": {
"f583a45e4947c102091b96170ef50ef0cf8edb62666193a2163247bb": {
"rewardAccount": {
"network": "Testnet",
"credential": {
"keyHash": "4e88cc2d27c364aaf90648a87dfb95f8ee103ba67fa1f12f5e86c42a"
}
},
"relays": [
{
"single host address": {
"port": 1234,
"IPv6": "2001:db8:a::123",
"IPv4": "0.0.0.0"
}
},
{
"single host name": {
"port": null,
"dnsName": "cool.domain.com"
}
},
{
"multi host name": {
"dnsName": "cool.domain.com"
}
}
],
"vrf": "68f9cfd33ac8f044facc664db5aa1b73c0b0f5435b85e7b520bb2f1a92f02999",
"publicKey": "4e130c0bdeb7768edf2e8f85007fd52073e3dc1871f4c47f9dfca92e",
"pledge": 1,
"cost": 5,
"margin": 0.25,
"metadata": {
"hash": "31303061627b7d31303061627b7d",
"url": "best.pool.com"
},
"owners": [
"4e88cc2d27c364aaf90648a87dfb95f8ee103ba67fa1f12f5e86c42a"
]
}
}
},
"networkMagic": 4036000900,
"maxLovelaceSupply": 71,
"systemStart": "2009-02-13T23:13:09Z",
"epochLength": 1215,
"networkId": "Testnet",
"slotLength": 8,
"activeSlotsCoeff": 0.259,
"securityParam": 120842,
"initialFunds": {
"00e1bade2ab9465a24fd5eaa4b7388303917338f853a48b11a5fc9964af5ca93fb6516ebde3d150b3e2acb1909532730de33832ffb229cb20d": 12157196
}
}

0 comments on commit 307495b

Please sign in to comment.