Commit 6f1d183
feat(lib-blockchain): Add DifficultyConfig struct for adaptive diffic… (#652)
* feat(lib-blockchain): Add DifficultyConfig struct for adaptive difficulty adjustment
- Add DifficultyConfig struct with governance-controlled parameters
- Fields: target_timespan, adjustment_interval, min/max_adjustment_factor, last_updated_at_height
- Default values: 14 days, 2016 blocks, 4x max adjustment (Bitcoin-style)
- Add difficulty_config field to Blockchain struct
- Initialize with defaults in Blockchain::new()
- Implement serialization/deserialization for persistence
- Add get_difficulty_config() getter method
- Add comprehensive unit tests for validation and serialization
- Ensure backward compatibility with existing genesis blocks
- Document in lib-blockchain/docs/architecture.md
Resolves #605
* fix(lib-blockchain): Improve DifficultyConfig naming and add governance setter
- Rename min/max_adjustment_factor to max_decrease/increase_factor for clarity
- Add tracing warning when adjustment_interval is zero in target_block_time()
- Add adjust_difficulty_with_config() that uses DifficultyConfig parameters
- Add set_difficulty_config() setter with validation for governance updates
- Update documentation and add test for new adjust function
Co-Authored-By: Claude Opus 4.5 <[email protected]>
* fix: Remove useless port validation on u16 type
* fix: Remove redundant DifficultyConfig re-export
* fix: Avoid unnecessary cloning of DifficultyConfig in difficulty getter
- Add get_difficulty_target_timespan() to retrieve specific field without cloning entire config
- Update blockchain.rs to use the new method instead of cloning whole DifficultyConfig
- Add documentation to get_difficulty_config() noting that it clones (intended for cases needing full config)
- Keep set_difficulty_config() for governance updates as already implemented
* fix: Make with_params validate parameters and return Result
- Changed DifficultyConfig::with_params() to return Result<Self, String>
- Validates all parameters before creating the configuration
- Prevents creation of invalid configs that would fail later
- Added test_difficulty_config_with_params_invalid() to test validation
- Updated existing test to use .expect() for valid parameters
* refactor: Rename difficulty adjustment fields for semantic clarity
- Renamed max_decrease_factor to max_difficulty_decrease_factor
- Renamed max_increase_factor to max_difficulty_increase_factor
- Updated all usages: struct fields, with_params, validate, clamp_timespan
- Updated all tests to use new field names
- Updated blockchain.rs logging to use new field names
These names make it explicit that the fields control difficulty changes,
not timespan directly, eliminating confusion in clamp_timespan docs and impl.
Tests verify all 7 difficulty config tests still pass.
---------
Co-authored-by: Hugo <[email protected]>
Co-authored-by: Claude Opus 4.5 <[email protected]>1 parent d941f14 commit 6f1d183
File tree
6 files changed
+388
-26
lines changed- lib-blockchain
- docs
- src
- integration
- types
- zhtp/src
6 files changed
+388
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
331 | 358 | | |
332 | 359 | | |
333 | 360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| |||
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
| 167 | + | |
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
| |||
798 | 801 | | |
799 | 802 | | |
800 | 803 | | |
801 | | - | |
802 | | - | |
| 804 | + | |
| 805 | + | |
803 | 806 | | |
804 | 807 | | |
805 | 808 | | |
| |||
913 | 916 | | |
914 | 917 | | |
915 | 918 | | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
916 | 946 | | |
917 | 947 | | |
918 | 948 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
263 | 267 | | |
264 | 268 | | |
265 | 269 | | |
266 | 270 | | |
267 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
268 | 278 | | |
269 | 279 | | |
270 | 280 | | |
| |||
0 commit comments