fix(peerstore): restore signed peer records from datastore on restart#39
Open
bhuvan-somisetty wants to merge 1 commit into
Open
Conversation
Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In libp2p, a signed peer record is more than just an address list. It is a cryptographically certified statement: "I am peer X, I live at these addresses, and here is my signature to prove it." The certified address book exists precisely so nodes can trust addresses they receive without re-verifying them every time. Without it, the DHT and GossipSub have to fall back to uncertified paths, and every restart breaks continuity.
The persistent peerstore has been saving and loading addresses, protocols, and metadata correctly since libp2p#946. But signed peer records (Envelopes) were being silently lost on every restart. The write path called
serialize_record_state, which stored only aState.VALIDbyte marker and discarded the envelope and sequence number entirely. The read path calleddeserialize_record_state, which returned a dummyPeerRecordStateconstructed from a 32-byte zero key. Every peer whose record had been consumed and stored would come back as a ghost on restart: present in the map, but with no actual data.The fix is two lines per peerstore. The
serialize_envelopeanddeserialize_envelopefunctions already exist in the same serialization module and handle the full envelope round-trip correctly — the local peer record already uses them. The peer record load and save paths now use the same approach: serialize the envelope directly, and on restore reconstruct thePeerRecordStatefrom the envelope and its embedded sequence number.Four tests cover the new behavior: SQLite-backed peer record persistence for the sync and async implementations, a control case confirming the memory backend does not persist records across restarts, and a seq-ordering test verifying that after a restart, the peerstore still correctly rejects older records that arrive with a lower sequence number. All existing tests pass.