@@ -2,6 +2,7 @@ use cml_core::DeserializeError;
2
2
use cml_crypto:: {
3
3
chain_crypto:: Blake2b256 , Ed25519KeyHash , PoolMetadataHash , TransactionHash , VRFKeyHash ,
4
4
} ;
5
+ use num:: traits:: Pow as _;
5
6
use serde_json;
6
7
use std:: collections:: BTreeMap ;
7
8
use std:: io:: Read ;
@@ -36,8 +37,7 @@ pub enum GenesisJSONError {
36
37
pub fn parse_genesis_data < R : Read > (
37
38
json : R ,
38
39
) -> 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) ?;
41
41
42
42
let mut initial_funds = BTreeMap :: new ( ) ;
43
43
for ( addr_hex, balance) in & data. initialFunds {
@@ -55,7 +55,7 @@ pub fn parse_genesis_data<R: Read>(
55
55
// 1) Get stake pools
56
56
let mut pools: BTreeMap < Ed25519KeyHash , PoolParams > = BTreeMap :: new ( ) ;
57
57
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 ) ;
59
59
let mut owners = Vec :: < Ed25519KeyHash > :: new ( ) ;
60
60
for owner in & params. owners {
61
61
owners. push ( Ed25519KeyHash :: from_hex ( owner) ?) ;
@@ -136,7 +136,7 @@ pub fn parse_genesis_data<R: Read>(
136
136
) ;
137
137
}
138
138
Ok ( config:: ShelleyGenesisData {
139
- active_slots_coeff : fraction :: Fraction :: from_str ( & data. activeSlotsCoeff ) . unwrap ( ) ,
139
+ active_slots_coeff : json_number_to_fraction ( & data. activeSlotsCoeff ) ,
140
140
epoch_length : data. epochLength ,
141
141
gen_delegs,
142
142
initial_funds,
@@ -145,11 +145,10 @@ pub fn parse_genesis_data<R: Read>(
145
145
network_id,
146
146
network_magic : data. networkMagic ,
147
147
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 (
150
150
& data. protocolParams . decentralisationParam ,
151
- )
152
- . unwrap ( ) ,
151
+ ) ,
153
152
e_max : data. protocolParams . eMax ,
154
153
extra_entropy : config:: ShelleyGenesisExtraEntropy {
155
154
tag : data. protocolParams . extraEntropy . tag ,
@@ -168,18 +167,33 @@ pub fn parse_genesis_data<R: Read>(
168
167
data. protocolParams . protocolVersion . major ,
169
168
data. protocolParams . protocolVersion . minor ,
170
169
) ,
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 ) ,
173
172
} ,
174
173
security_param : data. securityParam ,
175
- slot_length : fraction :: Fraction :: from_str ( & data. slotLength ) . unwrap ( ) ,
174
+ slot_length : json_number_to_fraction ( & data. slotLength ) ,
176
175
slots_per_kes_period : data. slotsPerKESPeriod ,
177
176
staking,
178
177
system_start : data. systemStart . parse ( ) . expect ( "Failed to parse date" ) ,
179
178
update_quorum : data. updateQuorum ,
180
179
} )
181
180
}
182
181
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
+
183
197
pub fn redeem_address_to_txid ( pubkey : & Address ) -> TransactionHash {
184
198
let txid = Blake2b256 :: new ( & pubkey. to_raw_bytes ( ) ) ;
185
199
TransactionHash :: from ( * txid. as_hash_bytes ( ) )
0 commit comments