← 输入框|github.com|刷新
Skip to content

Persist peers accross restarts - #114

Merged
rustaceanrob merged 3 commits into
kernel-node:masterfrom
rustaceanrob:persist-peers
Oct 26, 2026
Merged

rustaceanrob merged 3 commits into
kernel-node:masterfrom
rustaceanrob:persist-peers

Conversation

@rustaceanrob

@rustaceanrob rustaceanrob commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

We are continually hitting DNS every time the node starts. This can be a one-time bootstrap on the first run, with subsequent runs just using the peers on disk. Note this uses the addrman crate io extensions.

I have the current flushing interval at every 10 minutes. The flushing occurs on a minimal background thread, but still needs to temporarily hold the lock of the address book. This table should be no more than a few thousand bytes, so this shouldn't be noticeable at all. Also this fixes a bug where the "source" of the address gossip was using the DNS resolver IP instead of the peer's address. Part of #97

@pzafonte pzafonte left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--connect and addrman comments are the more consequential ones

Comment thread src/bin/node.rs Outdated
Arc::clone(&addrman),
Arc::clone(&node_state),
network,
peers_file,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using --connect the flush thread overwrites peers.bin with only the connect addresses, and on a restart uses those instead of DNS. Not starting the flush thread under --connect fixes it in the same way Core skips anchors.dat.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see this by seeding peers.bin, running once with --connect, and restarting without it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using --connect the flush thread overwrites peers.bin with only the connect addresses, and on a restart uses those instead of DNS. Not starting the flush thread under --connect fixes it in the same way Core skips anchors.dat

I made the peers_file optional and do not pass it when we have dedicated connections. How is that?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Comment thread src/net/addr.rs
}

impl AddrTableExt for AddrTable {
fn load(path: &Path) -> Self {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could test this to save Core's address to peers.bin, start without --connect, and wait for Core's getconnectioncount to reach 1.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test to check a seeded peers file creates a connection

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good.

Comment thread src/net/addr.rs
Comment on lines +44 to +48
fn save(&self, path: &Path) -> std::io::Result<()> {
let tmp = path.with_file_name("peers.tmp");
File::create(&tmp)?.write_table(self)?;
fs::rename(&tmp, path)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sync peers.tmp before the rename, or a power loss / interruption can leave peers.bin empty.

Suggested change
fn save(&self, path: &Path) -> std::io::Result<()> {
let tmp = path.with_file_name("peers.tmp");
File::create(&tmp)?.write_table(self)?;
fs::rename(&tmp, path)
}
fn save(&self, path: &Path) -> std::io::Result<()> {
let tmp = path.with_file_name("peers.tmp");
let mut file = File::create(&tmp)?;
file.write_table(self)?;
file.sync_data()?;
fs::rename(&tmp, path)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sync_data is called in the write_table method: https://github.com/2140-dev/bitcoin-address-book/blob/master/src/io.rs#L46

Do we need the extra one here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that, thanks!

Comment thread Cargo.toml Resolved
Make a test here that ensures if a live peer is available the node is
able to file it from the address book file.
@rustaceanrob

Copy link
Copy Markdown
Contributor Author

Rebased after copying bitcoin-address-book into the source tree

@pzafonte

Copy link
Copy Markdown
Collaborator

Tested & ACK 43d8dc0

@rustaceanrob
rustaceanrob merged commit 098c6ce into kernel-node:master Oct 26, 2026
2 checks passed
@rustaceanrob
rustaceanrob deleted the persist-peers branch October 26, 2026 13:38
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