Skip to content

Conversation

@ZocoLini
Copy link
Collaborator

@ZocoLini ZocoLini commented Dec 30, 2025

The peers and peers reputation storage logic moved into the storage module following the trait pattern introduced by PR #311. Also moved PeerReputation values validation logic into serde's pipeline

This PR is built on top of #311

Summary by CodeRabbit

  • New Features

    • Persistent storage for block headers, filters, chain state, and masternode data.
    • Enhanced peer reputation management with validation and persistent storage.
  • Bug Fixes

    • Improved chain state consistency through storage-backed persistence.
  • Refactor

    • Reorganized storage architecture for better reliability and scalability.
    • Simplified chain state synchronization logic.
    • Migrated from in-memory state to persistent storage operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@ZocoLini ZocoLini marked this pull request as draft December 30, 2025 16:59
@github-actions github-actions bot added the merge-conflict The PR conflicts with the target branch. label Dec 30, 2025
@github-actions
Copy link

This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 30, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This PR refactors the storage architecture from a monolithic DiskStorageManager to a modular design with specialized storage traits (BlockHeaderStorage, FilterHeaderStorage, ChainStateStorage, etc.). Sync and client code is updated to read state from storage rather than in-memory state. The peer persistence layer is restructured, and related tests are updated for new APIs.

Changes

Cohort / File(s) Summary
FFI & Type Changes
dash-spv-ffi/src/types.rs, dash-spv-ffi/tests/unit/test_type_conversions.rs
Removed header_height and filter_header_height fields from FFIChainState struct and updated From conversion and tests.
Storage Layer — Core Refactoring
dash-spv/src/storage/mod.rs, dash-spv/src/storage/manager.rs, dash-spv/src/storage/state.rs, dash-spv/src/storage/headers.rs
Removed old monolithic DiskStorageManager; introduced new modular DiskStorageManager with background worker coordinating multiple persistent storage implementations. Removed legacy headers.rs methods. Removed entire state.rs persistence layer.
Storage Layer — New Modular Implementations
dash-spv/src/storage/blocks.rs, dash-spv/src/storage/filters.rs, dash-spv/src/storage/chainstate.rs, dash-spv/src/storage/masternode.rs, dash-spv/src/storage/metadata.rs, dash-spv/src/storage/transactions.rs, dash-spv/src/storage/peers.rs, dash-spv/src/storage/segments.rs
Added specialized BlockHeaderStorage, FilterHeaderStorage, ChainStateStorage, MasternodeStateStorage, MetadataStorage, TransactionStorage, and PeerStorage trait implementations with corresponding PersistentXxxStorage structs.
Peer & Reputation System
dash-spv/src/network/persist.rs, dash-spv/src/network/reputation.rs, dash-spv/src/network/manager.rs, dash-spv/src/network/mod.rs, dash-spv/peers/reputations.json, dash-spv/src/network/reputation_tests.rs
Replaced file-based PeerStore with storage-backed PersistentPeerStorage; updated ReputationManager to use storage abstraction with serde (de)serialization and validation hooks; removed public persist module export; added peer reputation seed data.
Client & Sync — Core Updates
dash-spv/src/client/core.rs, dash-spv/src/client/lifecycle.rs, dash-spv/src/client/progress.rs, dash-spv/src/client/status_display.rs, dash-spv/src/client/sync_coordinator.rs
Changed state retrieval from in-memory to storage-backed; removed clear_filters method; updated tip_hash/tip_height calculation to use storage; removed explicit error handling for storage queries (replaced with unwrap_or fallbacks).
Sync Manager & Message Handling
dash-spv/src/sync/manager.rs, dash-spv/src/sync/headers/manager.rs, dash-spv/src/sync/filters/headers.rs, dash-spv/src/sync/filters/retry.rs, dash-spv/src/sync/masternodes/manager.rs, dash-spv/src/sync/message_handlers.rs, dash-spv/src/sync/phase_execution.rs, dash-spv/src/sync/transitions.rs
Updated HeaderSyncManager and sync coordination to accept storage parameter; removed in-memory state tracking (reorg_config, total_headers_synced); changed header tip retrieval to use storage; simplified error handling with unwrap_or(0) fallbacks.
Types & Domain Model
dash-spv/src/types.rs
Removed headers and filter_headers Vec fields from ChainState; removed navigation/manipulation methods (tip_height, header_at_height, add_headers, etc.); updated Debug impl.
Examples & Core
dash-spv/examples/filter_sync.rs, dash-spv/examples/simple_sync.rs, dash-spv/examples/spv_with_wallet.rs, dash-spv/src/lib.rs
Updated DiskStorageManager::new calls to accept &str path directly (removed .into() conversions).
Client — Storage Integration
dash-spv/src/client/block_processor_test.rs
Expanded imports to include BlockHeaderStorage alongside DiskStorageManager.
Tests — Storage & Integration
dash-spv/tests/edge_case_filter_sync_test.rs, dash-spv/tests/filter_header_verification_test.rs, dash-spv/tests/header_sync_test.rs, dash-spv/tests/integration_real_node_test.rs, dash-spv/tests/peer_test.rs, dash-spv/tests/reverse_index_test.rs, dash-spv/tests/rollback_test.rs, dash-spv/tests/segmented_storage_debug.rs, dash-spv/tests/segmented_storage_test.rs, dash-spv/tests/simple_header_test.rs, dash-spv/tests/simple_segmented_test.rs, dash-spv/tests/storage_consistency_test.rs, dash-spv/tests/storage_test.rs, dash-spv/tests/wallet_integration_test.rs
Updated test imports to use new storage trait types; changed DiskStorageManager::new to accept Path directly; adjusted get_tip_height() usage (changed from Result<Option, E> to Option); updated assertions and error handling patterns.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant SyncMgr as SyncManager
    participant HeaderSync
    participant Storage
    participant Disk

    rect rgb(200, 220, 240)
    Note over Client,Disk: Old Flow: In-Memory State
    Client->>SyncMgr: load_headers_from_storage()
    SyncMgr->>SyncMgr: Read chain_state.tip_height (in-memory)
    SyncMgr->>SyncMgr: Return count of synced headers
    SyncMgr->>Client: Result<u32>
    end

    rect rgb(240, 220, 200)
    Note over Client,Disk: New Flow: Storage-Backed State
    Client->>SyncMgr: load_headers_from_storage(storage)
    SyncMgr->>HeaderSync: load_headers_from_storage(storage)
    HeaderSync->>Storage: get_tip_height().await
    Storage->>Disk: Read persisted header state
    Disk-->>Storage: Option<u32>
    Storage-->>HeaderSync: Option<u32>
    HeaderSync->>HeaderSync: Load headers from storage via BlockHeaderStorage
    HeaderSync-->>SyncMgr: (void)
    SyncMgr-->>Client: (void)
    end
Loading
sequenceDiagram
    participant SyncMgr as Sync Flow
    participant NetMgr as NetworkManager
    participant Storage
    participant PeerStor as PeerStorage

    rect rgb(200, 240, 220)
    Note over SyncMgr,PeerStor: Old Peer Persistence
    NetMgr->>NetMgr: Load PeerStore from file path
    NetMgr->>NetMgr: Read peers.dat, reputations.json (file I/O)
    NetMgr->>NetMgr: Manage reputation in-memory
    NetMgr->>NetMgr: Save reputation back to file
    end

    rect rgb(240, 240, 200)
    Note over SyncMgr,PeerStor: New Peer Persistence (Storage-Backed)
    NetMgr->>Storage: PersistentPeerStorage::open(path)
    Storage->>PeerStor: Initialize storage layer
    NetMgr->>Storage: load_peers() via PeerStorage trait
    Storage->>PeerStor: Read from peers.dat
    PeerStor-->>Storage: Vec<SocketAddr>
    Storage-->>NetMgr: Vec<SocketAddr>
    NetMgr->>Storage: load_peers_reputation() via PeerStorage
    Storage->>PeerStor: Deserialize reputations.json
    PeerStor-->>Storage: HashMap<SocketAddr, PeerReputation>
    Storage-->>NetMgr: HashMap (with validation & clamping)
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Suggested reviewers

  • xdustinface
  • QuantumExplorer

Poem

🐰 Hops of refactoring delight!
Storage traits now shine so bright,
Modular, clean, and organized true,
State flows through storage, tried and new! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 61.86% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Refactor/peer persistence' directly describes the core objective of the PR: reorganizing peer and peer-reputation storage logic within the storage module following a trait-based pattern.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ZocoLini ZocoLini changed the base branch from v0.41-dev to refactor/storage-manager-trait December 30, 2025 16:59
@ZocoLini ZocoLini force-pushed the refactor/peer-persistence branch from 5d78f43 to 2d4550e Compare December 30, 2025 17:01
@github-actions github-actions bot removed the merge-conflict The PR conflicts with the target branch. label Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants