You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor: Move difficulty adjustment ownership to lib-consensus (#641)
* Refactor: Move difficulty adjustment ownership to lib-consensus
- Create DifficultyManager in lib-consensus
- Move DifficultyConfig to lib-consensus
- Update BlockchainConsensusCoordinator to use DifficultyManager
- Update Blockchain to delegate difficulty adjustment to Coordinator
- Add governance parameters for difficulty adjustment
- Update documentation and architecture diagrams
* fix: address all Copilot review comments for difficulty refactoring PR
- Remove redundant height > 0 condition in should_adjust()
- Fix double lock acquisition in blockchain.rs adjust_difficulty()
- Add division by zero checks in legacy difficulty calculation
- Make config_mut() private to prevent bypassing validation
- Add min_timespan safeguard against integer division returning 0
- Document complete governance application flow for difficulty params
- Remove duplicate DifficultyManager documentation
- Add comprehensive tests for setter methods
- Add 4 new integration tests for difficulty manager
- Export initialize_consensus_integration_with_difficulty_config()
All tests passing. Fixes address security, correctness, and maintainability issues.
* Fix: Make DifficultyManager handle zero actual_timespan consistently with legacy fallback
- Changed DifficultyManager::adjust_difficulty() to return Ok(None) instead of error when actual_timespan is zero
- This matches the behavior of calculate_difficulty_legacy() which silently returns current difficulty
- Both code paths now handle the edge case identically, preventing consensus divergence
- Added warning log to trace when this rare condition occurs
Fixes inconsistent error handling between coordinator and fallback paths that could cause chain forks when actual_timespan equals zero (identical block timestamps).
---------
Co-authored-by: supertramp <hugo@kode.zone>
0 commit comments