peerstore: make it possible to use an empty peer ID (#2006)

This commit is contained in:
Marten Seemann 2023-01-21 23:28:57 -08:00 committed by GitHub
parent 5b5eea45d3
commit e004bd3fc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -46,7 +46,10 @@ type addrSegment struct {
} }
func (segments *addrSegments) get(p peer.ID) *addrSegment { func (segments *addrSegments) get(p peer.ID) *addrSegment {
return segments[byte(p[len(p)-1])] if len(p) == 0 { // it's not terribly useful to use an empty peer ID, but at least we should not panic
return segments[0]
}
return segments[uint8(p[len(p)-1])]
} }
type clock interface { type clock interface {

View File

@ -135,6 +135,12 @@ func testAddAddress(ab pstore.AddrBook, clk *mockClock.Mock) func(*testing.T) {
ab.UpdateAddrs(id, 4*time.Second, 0) ab.UpdateAddrs(id, 4*time.Second, 0)
AssertAddressesEqual(t, nil, ab.Addrs(id)) AssertAddressesEqual(t, nil, ab.Addrs(id))
}) })
t.Run("accessing an empty peer ID", func(t *testing.T) {
addrs := GenerateAddrs(5)
ab.AddAddrs("", addrs, time.Hour)
AssertAddressesEqual(t, addrs, ab.Addrs(""))
})
} }
} }