@@ -44,6 +44,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
4444use std:: cmp:: Reverse ;
4545use std:: collections:: BTreeMap ;
4646use std:: fmt:: { Debug , Display , Formatter } ;
47+ use std:: str:: FromStr ;
4748use std:: sync:: Arc ;
4849
4950/// This data type models the events sent by the ChainSync mini-protocol with simplified data for the tests.
@@ -315,6 +316,30 @@ pub fn random_walk(
315316#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
316317pub struct Ratio ( pub u32 , pub u32 ) ;
317318
319+ impl Display for Ratio {
320+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
321+ write ! ( f, "{}/{}" , self . 0 , self . 1 )
322+ }
323+ }
324+
325+ impl FromStr for Ratio {
326+ type Err = String ;
327+
328+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
329+ let parts: Vec < & str > = s. split ( '/' ) . collect ( ) ;
330+ if parts. len ( ) != 2 {
331+ return Err ( "Ratio must be in the form 'numerator/denominator'" . to_string ( ) ) ;
332+ }
333+ let numerator = parts[ 0 ]
334+ . parse :: < u32 > ( )
335+ . map_err ( |_| format ! ( "Invalid numerator in ratio: {}" , parts[ 0 ] ) ) ?;
336+ let denominator = parts[ 1 ]
337+ . parse :: < u32 > ( )
338+ . map_err ( |_| format ! ( "Invalid denominator in ratio: {}" , parts[ 1 ] ) ) ?;
339+ Ok ( Ratio ( numerator, denominator) )
340+ }
341+ }
342+
318343/// Generate random walks for a fixed number of peers on a given tree of headers.
319344///
320345/// The returned list of actions is transposed so that the actions from different peers are interleaved.
@@ -358,8 +383,9 @@ pub fn any_select_chains(
358383 peers_nb : usize ,
359384 depth : usize ,
360385 rollback_ratio : Ratio ,
386+ branching_ratio : Ratio ,
361387) -> impl Strategy < Value = Vec < Action > > {
362- any_tree_of_headers ( depth, Ratio ( 1 , 2 ) ) . prop_flat_map ( move |tree| {
388+ any_tree_of_headers ( depth, branching_ratio ) . prop_flat_map ( move |tree| {
363389 ( 1 ..u64:: MAX )
364390 . prop_map ( move |seed| generate_random_walks ( & tree, peers_nb, rollback_ratio, seed) )
365391 } )
0 commit comments