@@ -2,6 +2,7 @@ use cml_core::DeserializeError;
22use cml_crypto:: {
33 chain_crypto:: Blake2b256 , Ed25519KeyHash , PoolMetadataHash , TransactionHash , VRFKeyHash ,
44} ;
5+ use num:: traits:: Pow as _;
56use serde_json;
67use std:: collections:: BTreeMap ;
78use std:: io:: Read ;
@@ -36,8 +37,7 @@ pub enum GenesisJSONError {
3637pub fn parse_genesis_data < R : Read > (
3738 json : R ,
3839) -> Result < config:: ShelleyGenesisData , GenesisJSONError > {
39- let data_value: serde_json:: Value = serde_json:: from_reader ( json) ?;
40- let data: raw:: ShelleyGenesisData = serde_json:: from_value ( data_value) ?;
40+ let data: raw:: ShelleyGenesisData = serde_json:: from_reader ( json) ?;
4141
4242 let mut initial_funds = BTreeMap :: new ( ) ;
4343 for ( addr_hex, balance) in & data. initialFunds {
@@ -55,7 +55,7 @@ pub fn parse_genesis_data<R: Read>(
5555 // 1) Get stake pools
5656 let mut pools: BTreeMap < Ed25519KeyHash , PoolParams > = BTreeMap :: new ( ) ;
5757 for ( pool_id, params) in & raw . pools {
58- let ration = fraction :: Fraction :: from_str ( & params. margin ) . unwrap ( ) ;
58+ let ration = json_number_to_fraction ( & params. margin ) ;
5959 let mut owners = Vec :: < Ed25519KeyHash > :: new ( ) ;
6060 for owner in & params. owners {
6161 owners. push ( Ed25519KeyHash :: from_hex ( owner) ?) ;
@@ -136,7 +136,7 @@ pub fn parse_genesis_data<R: Read>(
136136 ) ;
137137 }
138138 Ok ( config:: ShelleyGenesisData {
139- active_slots_coeff : fraction :: Fraction :: from_str ( & data. activeSlotsCoeff ) . unwrap ( ) ,
139+ active_slots_coeff : json_number_to_fraction ( & data. activeSlotsCoeff ) ,
140140 epoch_length : data. epochLength ,
141141 gen_delegs,
142142 initial_funds,
@@ -145,11 +145,10 @@ pub fn parse_genesis_data<R: Read>(
145145 network_id,
146146 network_magic : data. networkMagic ,
147147 protocol_params : config:: ShelleyGenesisProtocolParameters {
148- a0 : fraction :: Fraction :: from_str ( & data. protocolParams . a0 ) . unwrap ( ) ,
149- decentralisation_param : fraction :: Fraction :: from_str (
148+ a0 : json_number_to_fraction ( & data. protocolParams . a0 ) ,
149+ decentralisation_param : json_number_to_fraction (
150150 & data. protocolParams . decentralisationParam ,
151- )
152- . unwrap ( ) ,
151+ ) ,
153152 e_max : data. protocolParams . eMax ,
154153 extra_entropy : config:: ShelleyGenesisExtraEntropy {
155154 tag : data. protocolParams . extraEntropy . tag ,
@@ -168,18 +167,33 @@ pub fn parse_genesis_data<R: Read>(
168167 data. protocolParams . protocolVersion . major ,
169168 data. protocolParams . protocolVersion . minor ,
170169 ) ,
171- rho : fraction :: Fraction :: from_str ( & data. protocolParams . rho ) . unwrap ( ) ,
172- tau : fraction :: Fraction :: from_str ( & data. protocolParams . tau ) . unwrap ( ) ,
170+ rho : json_number_to_fraction ( & data. protocolParams . rho ) ,
171+ tau : json_number_to_fraction ( & data. protocolParams . tau ) ,
173172 } ,
174173 security_param : data. securityParam ,
175- slot_length : fraction :: Fraction :: from_str ( & data. slotLength ) . unwrap ( ) ,
174+ slot_length : json_number_to_fraction ( & data. slotLength ) ,
176175 slots_per_kes_period : data. slotsPerKESPeriod ,
177176 staking,
178177 system_start : data. systemStart . parse ( ) . expect ( "Failed to parse date" ) ,
179178 update_quorum : data. updateQuorum ,
180179 } )
181180}
182181
182+ fn json_number_to_fraction ( param : & serde_json:: Number ) -> fraction:: GenericFraction < u64 > {
183+ let s = param. to_string ( ) ;
184+
185+ if let Some ( exp_position) = s. find ( 'e' ) . or_else ( || s. find ( 'E' ) ) {
186+ let ( a, b) = s. split_at_checked ( exp_position) . unwrap ( ) ;
187+
188+ let exp = fraction:: Ratio :: from ( 10u64 ) . pow ( i32:: from_str ( & b[ 1 ..] ) . unwrap ( ) ) ;
189+
190+ fraction:: Fraction :: from_str ( a) . unwrap ( )
191+ * fraction:: Fraction :: new ( * exp. numer ( ) , * exp. denom ( ) )
192+ } else {
193+ fraction:: Fraction :: from_str ( & param. to_string ( ) ) . unwrap ( )
194+ }
195+ }
196+
183197pub fn redeem_address_to_txid ( pubkey : & Address ) -> TransactionHash {
184198 let txid = Blake2b256 :: new ( & pubkey. to_raw_bytes ( ) ) ;
185199 TransactionHash :: from ( * txid. as_hash_bytes ( ) )
0 commit comments