Implement RocksDB-backed state persistence for accounts and bonds#81
Implement RocksDB-backed state persistence for accounts and bonds#81
Conversation
- Add data_dir config option for persistent storage - Update StateManager to use storage fallback for get operations - Add Blockchain::with_storage() constructor - Update ValidatorNode and MinerNode to use persistent storage when data_dir is set - Add comprehensive persistence tests for accounts and bonds - Storage persists across node restarts ensuring no data loss Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
- Add test_blockchain_with_persistent_storage test - Add tempfile as dev dependency for bitcell-node - Verify accounts persist correctly across blockchain restarts Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
- Only compute hex encoding when trace level is enabled - Addresses code review feedback for performance optimization Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR implements RocksDB-backed persistent storage for blockchain state, addressing the critical issue of data loss across node restarts. The implementation adds storage-backed variants of state access methods (get_account_owned(), get_bond_owned()), a new Blockchain::with_storage() constructor, and CLI configuration via the --data-dir flag. When data_dir is set, nodes use persistent RocksDB storage; when omitted, they fall back to in-memory storage for testing.
Key Changes
- StateManager and Blockchain now support persistent storage through RocksDB-backed constructors
- Storage fallback pattern implemented with
_owned()variants that check cache then storage - CLI configuration added for all node types (validator, miner, full node) with
--data-dirflag - Comprehensive persistence tests added verifying state survives storage reopening
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/bitcell-state/src/storage.rs | Added persistence tests for accounts and bonds that verify data survives storage reopening |
| crates/bitcell-state/src/lib.rs | Added storage-backed getter methods (get_account_owned(), get_bond_owned()) with cache-first fallback pattern and trace logging for cache misses; added comprehensive persistence tests |
| crates/bitcell-node/src/validator.rs | Implemented conditional blockchain initialization with persistent storage when data_dir is configured, with helpful console messages |
| crates/bitcell-node/src/miner.rs | Mirrored validator implementation for consistent storage initialization across node types |
| crates/bitcell-node/src/main.rs | Wired --data-dir CLI parameter to NodeConfig for all three node types (validator, miner, full node) |
| crates/bitcell-node/src/config.rs | Added data_dir field to NodeConfig with proper documentation and None default for testing |
| crates/bitcell-node/src/blockchain.rs | Added with_storage() constructor that initializes RocksDB at {data_dir}/state/ with comprehensive documentation; added persistence integration test |
| crates/bitcell-node/Cargo.toml | Added tempfile dev dependency for testing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
crates/bitcell-node/src/miner.rs
Outdated
| std::fs::create_dir_all(data_path) | ||
| .expect("Failed to create data directory"); | ||
|
|
||
| println!("📦 Using persistent storage at: {}", data_path.display()); | ||
| Blockchain::with_storage(secret_key.clone(), metrics.clone(), data_path) | ||
| .expect("Failed to initialize blockchain with storage") |
There was a problem hiding this comment.
Error handling issue: Using .expect() for directory creation and blockchain initialization will cause the process to panic instead of returning a proper error. Consider using proper error handling that returns a Result, especially since with_key() could be called from contexts that expect graceful error handling.
There was a problem hiding this comment.
Fixed in commit c180ba8. Changed both with_key() and new() methods to return Result and updated all call sites to handle errors gracefully. Errors are now properly returned instead of causing panics.
…panic - Change ValidatorNode::with_key() and MinerNode::with_key() to return Result - Change ValidatorNode::new() and MinerNode::new() to return Result - Update all call sites in main.rs to handle errors gracefully - Update test cases to unwrap Results - Addresses PR review feedback about using .expect() causing panics Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
- Remove unused import COIN from blockchain.rs - Remove unused import SecretKey from main.rs - Remove empty line after doc comment in miner.rs - Remove empty line after doc comment in blockchain.rs Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
crates/bitcell-node/src/miner.rs
Outdated
| @@ -1,5 +1,4 @@ | |||
| ///! Miner node implementation | |||
There was a problem hiding this comment.
Incorrect doc comment syntax. Module-level doc comments should use //! not ///!. The triple slash with exclamation (///!) is invalid - use either /// for item-level docs or //! for module-level docs.
| ///! Miner node implementation | |
| //! Miner node implementation |
There was a problem hiding this comment.
Fixed in commit 480c46e. Changed ///! to //! for proper module-level doc comment syntax.
crates/bitcell-node/src/validator.rs
Outdated
| Self { | ||
| Ok(Self { | ||
| config, | ||
| state: StateManager::new(), |
There was a problem hiding this comment.
Inconsistent state management: ValidatorNode creates a separate in-memory StateManager (line 62) while Blockchain has its own StateManager with storage (lines 44-55). This creates two independent state managers that could diverge. Consider either:
- Removing the ValidatorNode's separate
statefield if Blockchain manages state, or - Sharing the same StateManager instance between ValidatorNode and Blockchain by extracting it from the blockchain after creation.
The same issue exists in MinerNode.
There was a problem hiding this comment.
Fixed in commit 480c46e. Removed the separate StateManager from ValidatorNode. The node now uses the Blockchain's StateManager which includes storage support when configured with --data-dir.
crates/bitcell-node/src/miner.rs
Outdated
| Ok(Self { | ||
| config, | ||
| secret_key, | ||
| state: StateManager::new(), |
There was a problem hiding this comment.
Inconsistent state management: MinerNode creates a separate in-memory StateManager (line 48) while Blockchain has its own StateManager with storage (lines 30-41). This creates two independent state managers that could diverge. Consider either:
- Removing the MinerNode's separate
statefield if Blockchain manages state, or - Sharing the same StateManager instance between MinerNode and Blockchain by extracting it from the blockchain after creation.
The same issue exists in ValidatorNode.
There was a problem hiding this comment.
Fixed in commit 480c46e. Removed the separate StateManager from MinerNode. The node now uses the Blockchain's StateManager which includes storage support when configured with --data-dir.
|
@copilot Please address the comments from the code review on this branch. |
- Fix doc comment syntax in miner.rs (use //! instead of ///!) - Remove duplicate StateManager from ValidatorNode and MinerNode - Both nodes now use Blockchain's StateManager with storage support - Update test to access state through blockchain.state() method - Resolves inconsistent state management between nodes and blockchain Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
|
@copilot fix the merge conflict with this branch into master, and commit the merge-ready commit to this branch |
- Resolved merge conflict in crates/bitcell-node/src/blockchain.rs - Kept both test_blockchain_with_persistent_storage and new VRF tests - Integrated wallet documentation and VRF integration test from master - All conflicts resolved and ready for merge Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.