-
Notifications
You must be signed in to change notification settings - Fork 18
feat(lib-blockchain): Add DifficultyConfig struct for adaptive diffic… #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5736c3a
6aae281
bb77c49
5ecf68b
914e49e
da01af2
104ab69
ecce5b1
6a0f2f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -71,6 +71,137 @@ impl std::fmt::Display for Difficulty { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Governance-controlled difficulty adjustment parameters for PoUW consensus | ||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||
| /// This struct stores configurable parameters that control how mining difficulty | ||||||||||||||||||||||||||||||
| /// adjusts over time. These parameters can be updated through governance proposals | ||||||||||||||||||||||||||||||
| /// to adapt to changing network conditions. | ||||||||||||||||||||||||||||||
| #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||||||||||||||||||||||||||||||
| pub struct DifficultyConfig { | ||||||||||||||||||||||||||||||
| /// Target timespan for difficulty adjustment period (in seconds) | ||||||||||||||||||||||||||||||
| /// Default: 14 days (1,209,600 seconds) | ||||||||||||||||||||||||||||||
| pub target_timespan: u64, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Number of blocks between difficulty adjustments | ||||||||||||||||||||||||||||||
| /// Default: 2016 blocks (Bitcoin-style) | ||||||||||||||||||||||||||||||
| pub adjustment_interval: u64, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Minimum adjustment factor (maximum difficulty decrease per adjustment) | ||||||||||||||||||||||||||||||
| /// Value of 4 means difficulty can decrease by at most 4x per adjustment | ||||||||||||||||||||||||||||||
| /// Default: 4 | ||||||||||||||||||||||||||||||
| pub min_adjustment_factor: u64, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Maximum adjustment factor (maximum difficulty increase per adjustment) | ||||||||||||||||||||||||||||||
| /// Value of 4 means difficulty can increase by at most 4x per adjustment | ||||||||||||||||||||||||||||||
| /// Default: 4 | ||||||||||||||||||||||||||||||
| pub max_adjustment_factor: u64, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Block height at which this configuration was last updated | ||||||||||||||||||||||||||||||
| /// Used for governance tracking and auditability | ||||||||||||||||||||||||||||||
| pub last_updated_at_height: u64, | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| impl DifficultyConfig { | ||||||||||||||||||||||||||||||
| /// Create a new DifficultyConfig with default values | ||||||||||||||||||||||||||||||
| pub fn new() -> Self { | ||||||||||||||||||||||||||||||
| Self::default() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Create a new DifficultyConfig with custom parameters | ||||||||||||||||||||||||||||||
| pub fn with_params( | ||||||||||||||||||||||||||||||
| target_timespan: u64, | ||||||||||||||||||||||||||||||
| adjustment_interval: u64, | ||||||||||||||||||||||||||||||
| min_adjustment_factor: u64, | ||||||||||||||||||||||||||||||
| max_adjustment_factor: u64, | ||||||||||||||||||||||||||||||
| last_updated_at_height: u64, | ||||||||||||||||||||||||||||||
| ) -> Self { | ||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||
| target_timespan, | ||||||||||||||||||||||||||||||
| adjustment_interval, | ||||||||||||||||||||||||||||||
| min_adjustment_factor, | ||||||||||||||||||||||||||||||
| max_adjustment_factor, | ||||||||||||||||||||||||||||||
| last_updated_at_height, | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
umwelt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// Get the target block time in seconds | ||||||||||||||||||||||||||||||
| /// Calculated as target_timespan / adjustment_interval | ||||||||||||||||||||||||||||||
| pub fn target_block_time(&self) -> u64 { | ||||||||||||||||||||||||||||||
| if self.adjustment_interval == 0 { | ||||||||||||||||||||||||||||||
| return 600; // Default to 10 minutes if misconfigured | ||||||||||||||||||||||||||||||
|
Comment on lines
139
to
143
|
||||||||||||||||||||||||||||||
| /// Calculated as target_timespan / adjustment_interval | |
| pub fn target_block_time(&self) -> u64 { | |
| if self.adjustment_interval == 0 { | |
| return 600; // Default to 10 minutes if misconfigured | |
| /// | |
| /// Calculated as `target_timespan / adjustment_interval`. | |
| /// | |
| /// # Panics | |
| /// | |
| /// Panics if `adjustment_interval` is zero. Call [`validate`] on this | |
| /// configuration before using it to ensure parameters are well-formed. | |
| pub fn target_block_time(&self) -> u64 { | |
| if self.adjustment_interval == 0 { | |
| panic!("DifficultyConfig is invalid: adjustment_interval must be greater than 0"); |
umwelt marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation test only checks for zero values but doesn't test the upper bound validations defined in the validate method. Specifically, it should test cases where target_timespan exceeds 1 year, adjustment_interval exceeds 1,000,000, or adjustment factors exceed 100 to ensure those validation rules work correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing blockchain serialization test (test_blockchain_serialization) does not verify that the difficulty_config field is properly serialized and deserialized. Adding a blockchain field requires updating the test to verify backward compatibility and proper serialization of the new field, especially since this field is critical for governance and consensus operations.