@@ -21,7 +21,7 @@ use std::time::Duration;
2121/// Chain Parameters for Proof of Work.
2222///
2323/// See in Bitcoin's [chainparams.cpp](https://github.com/bitcoin/bitcoin/blob/eca694a4e78d54ce4e29b388b3e81b06e55c2293/src/chainparams.cpp)
24- #[ derive( Debug ) ]
24+ #[ derive( Debug , Clone ) ]
2525pub struct PoWChainConfig {
2626 no_retargeting : bool ,
2727 /// Checks whether minimum difficulty can be used for the block
@@ -33,14 +33,8 @@ pub struct PoWChainConfig {
3333}
3434
3535impl PoWChainConfig {
36- pub ( crate ) const fn new ( chain_type : ChainType ) -> Self {
37- PoWChainConfig {
38- no_retargeting : no_retargeting ( chain_type) ,
39- allow_min_difficulty_blocks : allow_min_difficulty_blocks ( chain_type) ,
40- limit : limit ( chain_type) ,
41- // If block time is 2 minutes (which is my goal eventually), then 500 is equivalent to 100 in bitcoin's 10 minutes.
42- reward_maturity_distance : BlockDistance :: new ( 500 ) ,
43- }
36+ pub ( crate ) fn new ( chain_type : ChainType ) -> Self {
37+ PoWChainConfigBuilder :: new ( chain_type) . build ( )
4438 }
4539
4640 pub const fn no_retargeting ( & self ) -> bool {
@@ -77,6 +71,49 @@ impl PoWChainConfig {
7771 }
7872}
7973
74+ #[ derive( Copy , Clone ) ]
75+ pub struct PoWChainConfigBuilder {
76+ chain_type : ChainType ,
77+ no_retargeting : Option < bool > ,
78+ allow_min_difficulty_blocks : Option < bool > ,
79+ limit : Option < Uint256 > ,
80+ reward_maturity_distance : Option < BlockDistance > ,
81+ }
82+
83+ impl PoWChainConfigBuilder {
84+ pub fn new ( chain_type : ChainType ) -> Self {
85+ Self {
86+ chain_type,
87+ no_retargeting : None ,
88+ allow_min_difficulty_blocks : None ,
89+ limit : None ,
90+ reward_maturity_distance : None ,
91+ }
92+ }
93+
94+ pub fn limit ( mut self , value : Option < Uint256 > ) -> Self {
95+ self . limit = value;
96+ self
97+ }
98+
99+ pub fn build ( self ) -> PoWChainConfig {
100+ PoWChainConfig {
101+ no_retargeting : self . no_retargeting . unwrap_or_else ( || no_retargeting ( self . chain_type ) ) ,
102+ allow_min_difficulty_blocks : self
103+ . allow_min_difficulty_blocks
104+ . unwrap_or_else ( || allow_min_difficulty_blocks ( self . chain_type ) ) ,
105+ limit : self . limit . unwrap_or_else ( || limit ( self . chain_type ) ) ,
106+
107+ // If block time is 2 minutes (which is my goal
108+ // eventually), then 500 is equivalent to 100 in bitcoin's
109+ // 10 minutes.
110+ reward_maturity_distance : self
111+ . reward_maturity_distance
112+ . unwrap_or_else ( || BlockDistance :: new ( 500 ) ) ,
113+ }
114+ }
115+ }
116+
80117const fn no_retargeting ( chain_type : ChainType ) -> bool {
81118 match chain_type {
82119 ChainType :: Mainnet | ChainType :: Testnet | ChainType :: Signet => false ,
0 commit comments