-
Notifications
You must be signed in to change notification settings - Fork 16
Refactor: Move difficulty adjustment ownership to lib-consensus #641
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
Refactor: Move difficulty adjustment ownership to lib-consensus #641
Conversation
PeterGRutherford
commented
Jan 6, 2026
- 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
- 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
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.
Pull request overview
This PR refactors difficulty adjustment ownership from lib-blockchain to lib-consensus, establishing the consensus layer as the authoritative source for difficulty policy. The changes implement a new DifficultyManager that encapsulates Bitcoin-style difficulty adjustment logic and makes parameters governable via DAO proposals.
Key Changes:
- Created
DifficultyManagerandDifficultyConfigin lib-consensus with Bitcoin-compatible defaults - Updated
BlockchainConsensusCoordinatorto own and manage difficulty calculations - Modified
Blockchain::adjust_difficulty()to delegate to consensus coordinator while maintaining backward compatibility - Extended DAO governance to support blockchain difficulty parameter updates
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| lib-consensus/src/lib.rs | Adds difficulty module export and re-exports key types |
| lib-consensus/src/difficulty.rs | New module implementing DifficultyManager with Bitcoin-style adjustment algorithm, validation, and DAO governance support |
| lib-consensus/src/dao/dao_types.rs | Adds governance parameters for blockchain difficulty (InitialDifficulty, AdjustmentInterval, TargetTimespan) |
| lib-consensus/src/dao/dao_engine.rs | Adds validation for blockchain difficulty parameters in DAO governance proposals |
| lib-consensus/docs/api-reference.md | Documents DifficultyManager API, configuration options, and DAO integration patterns |
| lib-consensus/README.md | Adds difficulty management section highlighting consensus ownership and DAO governance |
| lib-blockchain/src/lib.rs | Re-exports difficulty types from lib-consensus for convenience |
| lib-blockchain/src/integration/consensus_integration.rs | Integrates DifficultyManager into BlockchainConsensusCoordinator with delegation methods |
| lib-blockchain/src/blockchain.rs | Delegates difficulty adjustment to consensus coordinator with legacy fallback |
| lib-blockchain/docs/architecture.md | Updates architecture to include difficulty management coordination |
| lib-blockchain/docs/api-reference.md | Documents that DIFFICULTY_ADJUSTMENT_INTERVAL is now a fallback constant |
| Cargo.toml | Changes package.metadata to workspace.metadata (appears unrelated to PR scope) |
| Cargo.lock | Adds new dependencies (rand_chacha, turmoil, rand_distr, libm) not mentioned in PR description |
- 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.
…culty-adjustment-ownership-to-lib-consensus Resolved conflict in Cargo.toml by keeping [workspace.metadata.dev-tools]
Code reviewFound 1 issue:
When the consensus coordinator path is used,
Both paths should handle zero timespan consistently—either both error out or both silently return current difficulty (but not mixed). The-Sovereign-Network/lib-blockchain/src/blockchain.rs Lines 104 to 112 in 9350cf7
|
…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).
|


