1
- use std:: { path:: PathBuf , time:: SystemTime } ;
1
+ use std:: { num :: NonZeroU64 , path:: PathBuf , time:: SystemTime } ;
2
2
3
3
use edr_eth:: {
4
4
block:: BlobGas , spec:: HardforkActivations , AccountInfo , Address , HashMap , SpecId , B256 , U256 ,
@@ -7,36 +7,51 @@ use edr_evm::{alloy_primitives::ChainId, MineOrdering};
7
7
use rand:: Rng ;
8
8
use serde:: { Deserialize , Serialize } ;
9
9
10
- use crate :: { requests:: hardhat:: rpc_types:: ForkConfig , OneUsizeOrTwo } ;
10
+ use crate :: requests:: { hardhat:: rpc_types:: ForkConfig , IntervalConfig as IntervalConfigRequest } ;
11
11
12
12
/// Configuration for interval mining.
13
13
#[ derive( Clone , Debug , Deserialize , Serialize ) ]
14
14
pub enum IntervalConfig {
15
- Fixed ( u64 ) ,
15
+ Fixed ( NonZeroU64 ) ,
16
16
Range { min : u64 , max : u64 } ,
17
17
}
18
18
19
19
impl IntervalConfig {
20
20
/// Generates a (random) interval based on the configuration.
21
21
pub fn generate_interval ( & self ) -> u64 {
22
22
match self {
23
- IntervalConfig :: Fixed ( interval) => * interval,
23
+ IntervalConfig :: Fixed ( interval) => interval. get ( ) ,
24
24
IntervalConfig :: Range { min, max } => rand:: thread_rng ( ) . gen_range ( * min..=* max) ,
25
25
}
26
26
}
27
27
}
28
28
29
- impl TryFrom < OneUsizeOrTwo > for IntervalConfig {
30
- type Error = ( ) ;
31
-
32
- fn try_from ( value : OneUsizeOrTwo ) -> Result < Self , Self :: Error > {
33
- match value {
34
- OneUsizeOrTwo :: One ( 0 ) => Err ( ( ) ) ,
35
- OneUsizeOrTwo :: One ( value) => Ok ( Self :: Fixed ( value as u64 ) ) ,
36
- OneUsizeOrTwo :: Two ( [ min, max] ) => Ok ( Self :: Range {
37
- min : min as u64 ,
38
- max : max as u64 ,
39
- } ) ,
29
+ /// An error that occurs when trying to convert [`IntervalConfigRequest`] to an
30
+ /// `Option<IntervalConfig>`.
31
+ #[ derive( Debug , thiserror:: Error ) ]
32
+ pub enum IntervalConfigConversionError {
33
+ /// The minimum value in the range is greater than the maximum value.
34
+ #[ error( "Minimum value in range is greater than maximum value" ) ]
35
+ MinGreaterThanMax ,
36
+ }
37
+
38
+ impl TryInto < Option < IntervalConfig > > for IntervalConfigRequest {
39
+ type Error = IntervalConfigConversionError ;
40
+
41
+ fn try_into ( self ) -> Result < Option < IntervalConfig > , Self :: Error > {
42
+ match self {
43
+ Self :: FixedOrDisabled ( 0 ) => Ok ( None ) ,
44
+ Self :: FixedOrDisabled ( value) => {
45
+ // Zero implies disabled
46
+ Ok ( NonZeroU64 :: new ( value) . map ( IntervalConfig :: Fixed ) )
47
+ }
48
+ Self :: Range ( [ min, max] ) => {
49
+ if max >= min {
50
+ Ok ( Some ( IntervalConfig :: Range { min, max } ) )
51
+ } else {
52
+ Err ( IntervalConfigConversionError :: MinGreaterThanMax )
53
+ }
54
+ }
40
55
}
41
56
}
42
57
}
@@ -65,7 +80,7 @@ pub struct ProviderConfig {
65
80
pub bail_on_call_failure : bool ,
66
81
/// Whether to return an `Err` when a `eth_sendTransaction` fails
67
82
pub bail_on_transaction_failure : bool ,
68
- pub block_gas_limit : u64 ,
83
+ pub block_gas_limit : NonZeroU64 ,
69
84
pub cache_dir : PathBuf ,
70
85
pub chain_id : ChainId ,
71
86
pub chains : HashMap < ChainId , HardforkActivations > ,
0 commit comments