Skip to content

Commit 0f9ad8c

Browse files
revert "feat:add contexts to all peerstore methods (#2312)" (#2328)
1 parent 581dd29 commit 0f9ad8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+727
-743
lines changed

config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config
22

33
import (
4-
"context"
54
"crypto/rand"
65
"errors"
76
"fmt"
@@ -151,10 +150,10 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa
151150
return nil, err
152151
}
153152

154-
if err := cfg.Peerstore.AddPrivKey(context.Background(), pid, cfg.PeerKey); err != nil {
153+
if err := cfg.Peerstore.AddPrivKey(pid, cfg.PeerKey); err != nil {
155154
return nil, err
156155
}
157-
if err := cfg.Peerstore.AddPubKey(context.Background(), pid, cfg.PeerKey.GetPublic()); err != nil {
156+
if err := cfg.Peerstore.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
158157
return nil, err
159158
}
160159

@@ -195,7 +194,7 @@ func (cfg *Config) addTransports(h host.Host) error {
195194
fx.Supply(cfg.Muxers),
196195
fx.Supply(h.ID()),
197196
fx.Provide(func() host.Host { return h }),
198-
fx.Provide(func() crypto.PrivKey { return h.Peerstore().PrivKey(context.Background(), h.ID()) }),
197+
fx.Provide(func() crypto.PrivKey { return h.Peerstore().PrivKey(h.ID()) }),
199198
fx.Provide(func() connmgr.ConnectionGater { return cfg.ConnectionGater }),
200199
fx.Provide(func() pnet.PSK { return cfg.PSK }),
201200
fx.Provide(func() network.ResourceManager { return cfg.ResourceManager }),

core/peerstore/helpers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package peerstore
22

33
import (
4-
"context"
5-
64
"github.com/libp2p/go-libp2p/core/peer"
75
)
86

97
// AddrInfos returns an AddrInfo for each specified peer ID, in-order.
10-
func AddrInfos(ctx context.Context, ps Peerstore, peers []peer.ID) []peer.AddrInfo {
8+
func AddrInfos(ps Peerstore, peers []peer.ID) []peer.AddrInfo {
119
pi := make([]peer.AddrInfo, len(peers))
1210
for i, p := range peers {
13-
pi[i] = ps.PeerInfo(ctx, p)
11+
pi[i] = ps.PeerInfo(p)
1412
}
1513
return pi
1614
}

core/peerstore/peerstore.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ type Peerstore interface {
6060
// PeerInfo returns a peer.PeerInfo struct for given peer.ID.
6161
// This is a small slice of the information Peerstore has on
6262
// that peer, useful to other services.
63-
PeerInfo(context.Context, peer.ID) peer.AddrInfo
63+
PeerInfo(peer.ID) peer.AddrInfo
6464

6565
// Peers returns all of the peer IDs stored across all inner stores.
66-
Peers(context.Context) peer.IDSlice
66+
Peers() peer.IDSlice
6767
}
6868

6969
// PeerMetadata can handle values of any type. Serializing values is
@@ -77,47 +77,47 @@ type PeerMetadata interface {
7777
// Get / Put is a simple registry for other peer-related key/value pairs.
7878
// If we find something we use often, it should become its own set of
7979
// methods. This is a last resort.
80-
Get(ctx context.Context, p peer.ID, key string) (interface{}, error)
81-
Put(ctx context.Context, p peer.ID, key string, val interface{}) error
80+
Get(p peer.ID, key string) (interface{}, error)
81+
Put(p peer.ID, key string, val interface{}) error
8282

8383
// RemovePeer removes all values stored for a peer.
84-
RemovePeer(context.Context, peer.ID)
84+
RemovePeer(peer.ID)
8585
}
8686

8787
// AddrBook holds the multiaddrs of peers.
8888
type AddrBook interface {
8989
// AddAddr calls AddAddrs(p, []ma.Multiaddr{addr}, ttl)
90-
AddAddr(context.Context, peer.ID, ma.Multiaddr, time.Duration)
90+
AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration)
9191

9292
// AddAddrs gives this AddrBook addresses to use, with a given ttl
9393
// (time-to-live), after which the address is no longer valid.
9494
// If the manager has a longer TTL, the operation is a no-op for that address
95-
AddAddrs(context.Context, peer.ID, []ma.Multiaddr, time.Duration)
95+
AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration)
9696

9797
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
98-
SetAddr(context.Context, peer.ID, ma.Multiaddr, time.Duration)
98+
SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration)
9999

100100
// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
101101
// This is used when we receive the best estimate of the validity of an address.
102-
SetAddrs(context.Context, peer.ID, []ma.Multiaddr, time.Duration)
102+
SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration)
103103

104104
// UpdateAddrs updates the addresses associated with the given peer that have
105105
// the given oldTTL to have the given newTTL.
106-
UpdateAddrs(context.Context, peer.ID, time.Duration, time.Duration)
106+
UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration)
107107

108108
// Addrs returns all known (and valid) addresses for a given peer.
109-
Addrs(context.Context, peer.ID) []ma.Multiaddr
109+
Addrs(p peer.ID) []ma.Multiaddr
110110

111111
// AddrStream returns a channel that gets all addresses for a given
112112
// peer sent on it. If new addresses are added after the call is made
113113
// they will be sent along through the channel as well.
114114
AddrStream(context.Context, peer.ID) <-chan ma.Multiaddr
115115

116116
// ClearAddresses removes all previously stored addresses.
117-
ClearAddrs(context.Context, peer.ID)
117+
ClearAddrs(p peer.ID)
118118

119119
// PeersWithAddrs returns all of the peer IDs stored in the AddrBook.
120-
PeersWithAddrs(context.Context) peer.IDSlice
120+
PeersWithAddrs() peer.IDSlice
121121
}
122122

123123
// CertifiedAddrBook manages "self-certified" addresses for remote peers.
@@ -172,12 +172,12 @@ type CertifiedAddrBook interface {
172172
// AddrBook.SetAddrs will be ignored. AddrBook.SetAddrs may still be used
173173
// to update the TTL of certified addresses that have previously been
174174
// added via ConsumePeerRecord.
175-
ConsumePeerRecord(context.Context, *record.Envelope, time.Duration) (accepted bool, err error)
175+
ConsumePeerRecord(s *record.Envelope, ttl time.Duration) (accepted bool, err error)
176176

177177
// GetPeerRecord returns a Envelope containing a PeerRecord for the
178178
// given peer id, if one exists.
179179
// Returns nil if no signed PeerRecord exists for the peer.
180-
GetPeerRecord(context.Context, peer.ID) *record.Envelope
180+
GetPeerRecord(p peer.ID) *record.Envelope
181181
}
182182

183183
// GetCertifiedAddrBook is a helper to "upcast" an AddrBook to a
@@ -196,24 +196,24 @@ func GetCertifiedAddrBook(ab AddrBook) (cab CertifiedAddrBook, ok bool) {
196196
// KeyBook tracks the keys of Peers.
197197
type KeyBook interface {
198198
// PubKey stores the public key of a peer.
199-
PubKey(context.Context, peer.ID) ic.PubKey
199+
PubKey(peer.ID) ic.PubKey
200200

201201
// AddPubKey stores the public key of a peer.
202-
AddPubKey(context.Context, peer.ID, ic.PubKey) error
202+
AddPubKey(peer.ID, ic.PubKey) error
203203

204204
// PrivKey returns the private key of a peer, if known. Generally this might only be our own
205205
// private key, see
206206
// https://discuss.libp2p.io/t/what-is-the-purpose-of-having-map-peer-id-privatekey-in-peerstore/74.
207-
PrivKey(context.Context, peer.ID) ic.PrivKey
207+
PrivKey(peer.ID) ic.PrivKey
208208

209209
// AddPrivKey stores the private key of a peer.
210-
AddPrivKey(context.Context, peer.ID, ic.PrivKey) error
210+
AddPrivKey(peer.ID, ic.PrivKey) error
211211

212212
// PeersWithKeys returns all the peer IDs stored in the KeyBook.
213-
PeersWithKeys(context.Context) peer.IDSlice
213+
PeersWithKeys() peer.IDSlice
214214

215215
// RemovePeer removes all keys associated with a peer.
216-
RemovePeer(context.Context, peer.ID)
216+
RemovePeer(peer.ID)
217217
}
218218

219219
// Metrics tracks metrics across a set of peers.
@@ -226,25 +226,25 @@ type Metrics interface {
226226
LatencyEWMA(peer.ID) time.Duration
227227

228228
// RemovePeer removes all metrics stored for a peer.
229-
RemovePeer(context.Context, peer.ID)
229+
RemovePeer(peer.ID)
230230
}
231231

232232
// ProtoBook tracks the protocols supported by peers.
233233
type ProtoBook interface {
234-
GetProtocols(context.Context, peer.ID) ([]protocol.ID, error)
235-
AddProtocols(context.Context, peer.ID, ...protocol.ID) error
236-
SetProtocols(context.Context, peer.ID, ...protocol.ID) error
237-
RemoveProtocols(context.Context, peer.ID, ...protocol.ID) error
234+
GetProtocols(peer.ID) ([]protocol.ID, error)
235+
AddProtocols(peer.ID, ...protocol.ID) error
236+
SetProtocols(peer.ID, ...protocol.ID) error
237+
RemoveProtocols(peer.ID, ...protocol.ID) error
238238

239239
// SupportsProtocols returns the set of protocols the peer supports from among the given protocols.
240240
// If the returned error is not nil, the result is indeterminate.
241-
SupportsProtocols(context.Context, peer.ID, ...protocol.ID) ([]protocol.ID, error)
241+
SupportsProtocols(peer.ID, ...protocol.ID) ([]protocol.ID, error)
242242

243243
// FirstSupportedProtocol returns the first protocol that the peer supports among the given protocols.
244244
// If the peer does not support any of the given protocols, this function will return an empty protocol.ID and a nil error.
245245
// If the returned error is not nil, the result is indeterminate.
246-
FirstSupportedProtocol(context.Context, peer.ID, ...protocol.ID) (protocol.ID, error)
246+
FirstSupportedProtocol(peer.ID, ...protocol.ID) (protocol.ID, error)
247247

248248
// RemovePeer removes all protocols associated with a peer.
249-
RemovePeer(context.Context, peer.ID)
249+
RemovePeer(peer.ID)
250250
}

p2p/discovery/backoff/backoffconnector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func getNetHosts(t *testing.T, n int) []host.Host {
5555
func loadCh(peers []host.Host) <-chan peer.AddrInfo {
5656
ch := make(chan peer.AddrInfo, len(peers))
5757
for _, p := range peers {
58-
ch <- p.Peerstore().PeerInfo(context.Background(), p.ID())
58+
ch <- p.Peerstore().PeerInfo(p.ID())
5959
}
6060
close(ch)
6161
return ch

p2p/host/autonat/autonat.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (as *AmbientAutoNAT) background() {
188188
as.confidence--
189189
}
190190
case event.EvtPeerIdentificationCompleted:
191-
if s, err := as.host.Peerstore().SupportsProtocols(context.Background(), e.Peer, AutoNATProto); err == nil && len(s) > 0 {
191+
if s, err := as.host.Peerstore().SupportsProtocols(e.Peer, AutoNATProto); err == nil && len(s) > 0 {
192192
currentStatus := *as.status.Load()
193193
if currentStatus == network.ReachabilityUnknown {
194194
as.tryProbe(e.Peer)
@@ -367,7 +367,7 @@ func (as *AmbientAutoNAT) tryProbe(p peer.ID) bool {
367367
}
368368
as.cleanupRecentProbes()
369369

370-
info := as.host.Peerstore().PeerInfo(context.Background(), p)
370+
info := as.host.Peerstore().PeerInfo(p)
371371

372372
if !as.config.dialPolicy.skipPeer(info.Addrs) {
373373
as.recentProbes[p] = time.Now()
@@ -402,9 +402,9 @@ func (as *AmbientAutoNAT) getPeerToProbe() peer.ID {
402402
candidates := make([]peer.ID, 0, len(peers))
403403

404404
for _, p := range peers {
405-
info := as.host.Peerstore().PeerInfo(context.Background(), p)
405+
info := as.host.Peerstore().PeerInfo(p)
406406
// Exclude peers which don't support the autonat protocol.
407-
if proto, err := as.host.Peerstore().SupportsProtocols(context.Background(), p, AutoNATProto); len(proto) == 0 || err != nil {
407+
if proto, err := as.host.Peerstore().SupportsProtocols(p, AutoNATProto); len(proto) == 0 || err != nil {
408408
continue
409409
}
410410

p2p/host/autonat/autonat_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ func makeAutoNATServicePublic(t *testing.T) host.Host {
9494

9595
func makeAutoNAT(t *testing.T, ash host.Host) (host.Host, AutoNAT) {
9696
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
97-
h.Peerstore().AddAddrs(context.Background(), ash.ID(), ash.Addrs(), time.Minute)
98-
h.Peerstore().AddProtocols(context.Background(), ash.ID(), AutoNATProto)
97+
h.Peerstore().AddAddrs(ash.ID(), ash.Addrs(), time.Minute)
98+
h.Peerstore().AddProtocols(ash.ID(), AutoNATProto)
9999
a, _ := New(h, WithSchedule(100*time.Millisecond, time.Second), WithoutStartupDelay())
100100
a.(*AmbientAutoNAT).config.dialPolicy.allowSelfDials = true
101101
a.(*AmbientAutoNAT).config.throttlePeerPeriod = 100 * time.Millisecond
102102
return h, a
103103
}
104104

105105
func identifyAsServer(server, recip host.Host) {
106-
recip.Peerstore().AddAddrs(context.Background(), server.ID(), server.Addrs(), time.Minute)
107-
recip.Peerstore().AddProtocols(context.Background(), server.ID(), AutoNATProto)
106+
recip.Peerstore().AddAddrs(server.ID(), server.Addrs(), time.Minute)
107+
recip.Peerstore().AddProtocols(server.ID(), AutoNATProto)
108108

109109
}
110110

p2p/host/autonat/svc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ func (as *autoNATService) doDial(pi peer.AddrInfo) *pb.Message_DialResponse {
223223
ctx, cancel := context.WithTimeout(context.Background(), as.config.dialTimeout)
224224
defer cancel()
225225

226-
as.config.dialer.Peerstore().ClearAddrs(ctx, pi.ID)
226+
as.config.dialer.Peerstore().ClearAddrs(pi.ID)
227227

228-
as.config.dialer.Peerstore().AddAddrs(ctx, pi.ID, pi.Addrs, peerstore.TempAddrTTL)
228+
as.config.dialer.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)
229229

230230
defer func() {
231-
as.config.dialer.Peerstore().ClearAddrs(ctx, pi.ID)
232-
as.config.dialer.Peerstore().RemovePeer(ctx, pi.ID)
231+
as.config.dialer.Peerstore().ClearAddrs(pi.ID)
232+
as.config.dialer.Peerstore().RemovePeer(pi.ID)
233233
}()
234234

235235
conn, err := as.config.dialer.DialPeer(ctx, pi.ID)

p2p/host/autonat/test/autonat_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestAutonatRoundtrip(t *testing.T) {
2929
t.Fatal(err)
3030
}
3131

32-
client.Peerstore().AddAddrs(context.Background(), service.ID(), service.Addrs(), time.Hour)
33-
require.NoError(t, client.Connect(context.Background(), service.Peerstore().PeerInfo(context.Background(), service.ID())))
32+
client.Peerstore().AddAddrs(service.ID(), service.Addrs(), time.Hour)
33+
require.NoError(t, client.Connect(context.Background(), service.Peerstore().PeerInfo(service.ID())))
3434

3535
cSub, err := client.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
3636
require.NoError(t, err)

p2p/host/autorelay/relay_finder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func (rf *relayFinder) tryNode(ctx context.Context, pi peer.AddrInfo) (supportsR
493493
return false, ctx.Err()
494494
}
495495

496-
protos, err := rf.host.Peerstore().SupportsProtocols(ctx, pi.ID, protoIDv2)
496+
protos, err := rf.host.Peerstore().SupportsProtocols(pi.ID, protoIDv2)
497497
if err != nil {
498498
return false, fmt.Errorf("error checking relay protocol support for peer %s: %w", pi.ID, err)
499499
}
@@ -734,7 +734,7 @@ func (rf *relayFinder) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
734734
// add relay specific addrs to the list
735735
relayAddrCnt := 0
736736
for p := range rf.relays {
737-
addrs := cleanupAddressSet(rf.host.Peerstore().Addrs(context.Background(), p))
737+
addrs := cleanupAddressSet(rf.host.Peerstore().Addrs(p))
738738
relayAddrCnt += len(addrs)
739739
circuit := ma.StringCast(fmt.Sprintf("/p2p/%s/p2p-circuit", p.Pretty()))
740740
for _, addr := range addrs {

p2p/host/basic/basic_host.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
207207
}
208208
h.caBook = cab
209209

210-
h.signKey = h.Peerstore().PrivKey(context.Background(), h.ID())
210+
h.signKey = h.Peerstore().PrivKey(h.ID())
211211
if h.signKey == nil {
212212
return nil, errors.New("unable to access host key")
213213
}
@@ -221,7 +221,7 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
221221
if err != nil {
222222
return nil, fmt.Errorf("failed to create signed record for self: %w", err)
223223
}
224-
if _, err := cab.ConsumePeerRecord(context.Background(), ev, peerstore.PermanentAddrTTL); err != nil {
224+
if _, err := cab.ConsumePeerRecord(ev, peerstore.PermanentAddrTTL); err != nil {
225225
return nil, fmt.Errorf("failed to persist signed record to peerstore: %w", err)
226226
}
227227
}
@@ -515,7 +515,7 @@ func (h *BasicHost) background() {
515515
changeEvt.SignedPeerRecord = sr
516516

517517
// persist the signed record to the peerstore
518-
if _, err := h.caBook.ConsumePeerRecord(context.Background(), sr, peerstore.PermanentAddrTTL); err != nil {
518+
if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil {
519519
log.Errorf("failed to persist signed peer record in peer store, err=%s", err)
520520
return
521521
}
@@ -656,7 +656,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
656656
return nil, ctx.Err()
657657
}
658658

659-
pref, err := h.preferredProtocol(ctx, p, pids)
659+
pref, err := h.preferredProtocol(p, pids)
660660
if err != nil {
661661
_ = s.Reset()
662662
return nil, err
@@ -692,12 +692,12 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
692692
}
693693

694694
s.SetProtocol(selected)
695-
h.Peerstore().AddProtocols(ctx, p, selected)
695+
h.Peerstore().AddProtocols(p, selected)
696696
return s, nil
697697
}
698698

699-
func (h *BasicHost) preferredProtocol(ctx context.Context, p peer.ID, pids []protocol.ID) (protocol.ID, error) {
700-
supported, err := h.Peerstore().SupportsProtocols(ctx, p, pids...)
699+
func (h *BasicHost) preferredProtocol(p peer.ID, pids []protocol.ID) (protocol.ID, error) {
700+
supported, err := h.Peerstore().SupportsProtocols(p, pids...)
701701
if err != nil {
702702
return "", err
703703
}
@@ -716,7 +716,7 @@ func (h *BasicHost) preferredProtocol(ctx context.Context, p peer.ID, pids []pro
716716
// It will also resolve any /dns4, /dns6, and /dnsaddr addresses.
717717
func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
718718
// absorb addresses into peerstore
719-
h.Peerstore().AddAddrs(ctx, pi.ID, pi.Addrs, peerstore.TempAddrTTL)
719+
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL)
720720

721721
forceDirect, _ := network.GetForceDirectDial(ctx)
722722
if !forceDirect {

0 commit comments

Comments
 (0)