Skip to content

Fix multi network persistance - #534

Open
Sansh2356 wants to merge 3 commits into
braidpool:devfrom
Sansh2356:multi-network-persistance
Open

Sansh2356 wants to merge 3 commits into
braidpool:devfrom
Sansh2356:multi-network-persistance

Conversation

@Sansh2356

Copy link
Copy Markdown
Contributor
  • Braidpool nodes previously ran every network (mainnet, signet, regtest, cpunet) on the same libp2p protocol IDs, so peers from different networks could negotiate substreams against each other. That silently produced bead/hash divergence leading to breakdown of entire network mesh.
  • This PR isolates networks end-to-end across the p2p stack and on disk by protocol negotiation during ConnectionEstablishment and adding peers only when negotiation passes as well as persistence in network specific sub-directories having --datadir as parent directory.
  • Addresses Bifurcating persistance on basis of Network type #462 and --datadir has no effect inside init_db() #533 .

Copilot AI lite review requested due to automatic review settings September 1, 2026 10:26
@Sansh2356 Sansh2356 mentioned this pull request Sep 1, 2026
32 of 38 tasks
@Sansh2356
Sansh2356 marked this pull request as draft September 1, 2026 10:28

Copilot AI left a comment

Copy link
Copy Markdown

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 addresses cross-network contamination by scoping Braidpool’s libp2p protocol identifiers and on-disk state to the selected --network, preventing peers from different networks from negotiating substreams and sharing persistence under the same paths.

Changes:

  • Scope libp2p protocol IDs (kademlia + bead-sync) and floodsub topic by PoolNetwork, and only admit peers into the mesh/peer manager after Identify confirms expected protocols.
  • Make persistence network-specific by creating/using <datadir>/<network>/ as the root for the DB and keystore, and thread the network-scoped path through DB initialization.
  • Add a multiaddr routability filter for DHT insertion and expand tests/docs to cover the new isolation model.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
node/src/utils/mod.rs Adds is_routable_multiaddr plus unit tests for filtering unroutable listen addrs before DHT insertion.
node/src/stratum.rs Updates a test to use DBHandler::new_in_memory to avoid persistent DB usage.
node/src/peer_manager/mod.rs Introduces PendingPeer and pending tracking to defer peer registration until protocol negotiation succeeds.
node/src/main.rs Implements network-scoped datadir setup, network-scoped topics/protocol checks on Identify, and pending→promoted peer flow.
node/src/db/init_db.rs Changes DB init to accept a network-scoped directory and warns if a legacy unscoped DB exists.
node/src/db/db_handlers.rs Threads network-scoped datadir into persistent DB handler initialization.
node/src/db/audit_db_handlers.rs Threads network-scoped datadir into audit DB handler initialization.
node/src/behaviour/tests.rs Adds tests asserting protocol/topic names differ across networks and updates swarm construction.
node/src/behaviour/mod.rs Replaces global protocol constants with network-scoped protocol/topic generators and updates behaviour construction.
node/src/audit.rs Updates audit DAG initialization to accept the network-scoped datadir.
docs/braidpool_setup.md Documents the per-network datadir layout and network-scoped on-wire protocol/topic naming.
Cargo.lock Updates locked dependencies as a result of the build/dependency graph changes in the workspace.
Suppressed comments (1)

node/src/main.rs:133

  • If the per-network data path (<datadir>/<network>) already exists but is not a directory, the code logs and continues. That can lead to confusing failures when creating the DB/keystore inside it; returning an error here makes the failure immediate and actionable.
    match fs::metadata(&network_datadir) {
        Ok(m) => {
            if !m.is_dir() {
                error!(path = %network_datadir.display(), "Network data path exists but is not a directory");
            }

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread node/src/main.rs Outdated
Comment thread node/src/utils/mod.rs
@Sansh2356
Sansh2356 marked this pull request as ready for review September 5, 2026 08:13
…uring negotiation

Earlier there was no bifurcation on the basis of network current node is
interacting with during protocol exchange and handshake which could
allow nodes on different `PoolNetwork` to establish connection.

This scopes each protocol versioning according to network and add
a fail case in which incoming connection maybe from different node .
@Sansh2356
Sansh2356 force-pushed the multi-network-persistance branch from 9c46fc0 to 83d1bcc Compare September 7, 2026 19:49
This adds to the network bifurcated functioning of braidpool involving
protocol negotiation between peers during Identify Event and updating
the filtering during DHT addition in local peer.

Each protocol id is based on network on which that braidpool node is
operating and it avoids the addition of peer in DHT as well as connection
if protocols are not matching preventing peers on different networks from
operating on same mesh leading to invalid hashes.

This also updates the DHT checks for filtering out unroutable peer addresses
received during the Identify even received from the peer and changing the addition
of DHT insertion from ConnectionEstablished event to Identify event.

Changing the directory structure for network wise bead persistance as well as
peer Ids stored in key-store.
@Sansh2356
Sansh2356 force-pushed the multi-network-persistance branch from 83d1bcc to 6e28829 Compare September 7, 2026 19:57

This branch has not been deployed

No deployments
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