|
| 1 | +package bootstrap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "math/rand" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + "time" |
| 11 | + |
| 12 | + logging "github.com/ipfs/go-log" |
| 13 | + "github.com/jbenet/goprocess" |
| 14 | + goprocessctx "github.com/jbenet/goprocess/context" |
| 15 | + periodicproc "github.com/jbenet/goprocess/periodic" |
| 16 | + "github.com/libp2p/go-libp2p/core/host" |
| 17 | + "github.com/libp2p/go-libp2p/core/network" |
| 18 | + "github.com/libp2p/go-libp2p/core/peer" |
| 19 | + "github.com/libp2p/go-libp2p/core/peerstore" |
| 20 | + "github.com/libp2p/go-libp2p/core/routing" |
| 21 | +) |
| 22 | + |
| 23 | +var log = logging.Logger("bootstrap") |
| 24 | + |
| 25 | +// ErrNotEnoughBootstrapPeers signals that we do not have enough bootstrap |
| 26 | +// peers to bootstrap correctly. |
| 27 | +var ErrNotEnoughBootstrapPeers = errors.New("not enough bootstrap peers to bootstrap") |
| 28 | + |
| 29 | +// BootstrapConfig specifies parameters used in an IpfsNode's network |
| 30 | +// bootstrapping process. |
| 31 | +type BootstrapConfig struct { |
| 32 | + // MinPeerThreshold governs whether to bootstrap more connections. If the |
| 33 | + // node has less open connections than this number, it will open connections |
| 34 | + // to the bootstrap nodes. From there, the routing system should be able |
| 35 | + // to use the connections to the bootstrap nodes to connect to even more |
| 36 | + // peers. Routing systems like the IpfsDHT do so in their own Bootstrap |
| 37 | + // process, which issues random queries to find more peers. |
| 38 | + MinPeerThreshold int |
| 39 | + |
| 40 | + // Period governs the periodic interval at which the node will |
| 41 | + // attempt to bootstrap. The bootstrap process is not very expensive, so |
| 42 | + // this threshold can afford to be small (<=30s). |
| 43 | + Period time.Duration |
| 44 | + |
| 45 | + // ConnectionTimeout determines how long to wait for a bootstrap |
| 46 | + // connection attempt before cancelling it. |
| 47 | + ConnectionTimeout time.Duration |
| 48 | + |
| 49 | + // BootstrapPeers is a function that returns a set of bootstrap peers |
| 50 | + // for the bootstrap process to use. This makes it possible for clients |
| 51 | + // to control the peers the process uses at any moment. |
| 52 | + BootstrapPeers func() []peer.AddrInfo |
| 53 | + |
| 54 | + // BackupBootstrapInterval governs the periodic interval at which the node will |
| 55 | + // attempt to save connected nodes to use as temporary bootstrap peers. |
| 56 | + BackupBootstrapInterval time.Duration |
| 57 | + |
| 58 | + // MaxBackupBootstrapSize controls the maximum number of peers we're saving |
| 59 | + // as backup bootstrap peers. |
| 60 | + MaxBackupBootstrapSize int |
| 61 | + |
| 62 | + saveBackupBootstrapPeers func(context.Context, []peer.AddrInfo) |
| 63 | + loadBackupBootstrapPeers func(context.Context) []peer.AddrInfo |
| 64 | +} |
| 65 | + |
| 66 | +// DefaultBootstrapConfig specifies default sane parameters for bootstrapping. |
| 67 | +var DefaultBootstrapConfig = BootstrapConfig{ |
| 68 | + MinPeerThreshold: 4, |
| 69 | + Period: 30 * time.Second, |
| 70 | + ConnectionTimeout: (30 * time.Second) / 3, // Period / 3 |
| 71 | + BackupBootstrapInterval: 1 * time.Hour, |
| 72 | + MaxBackupBootstrapSize: 20, |
| 73 | +} |
| 74 | + |
| 75 | +// BootstrapConfigWithPeers creates a default BootstrapConfig configured with |
| 76 | +// the specified peers, and optional functions to load and save backup peers. |
| 77 | +func BootstrapConfigWithPeers(pis []peer.AddrInfo, options ...func(*BootstrapConfig)) BootstrapConfig { |
| 78 | + cfg := DefaultBootstrapConfig |
| 79 | + cfg.BootstrapPeers = func() []peer.AddrInfo { |
| 80 | + return pis |
| 81 | + } |
| 82 | + for _, opt := range options { |
| 83 | + opt(&cfg) |
| 84 | + } |
| 85 | + return cfg |
| 86 | +} |
| 87 | + |
| 88 | +// WithBackupPeers configures functions to load and save backup bootstrap peers. |
| 89 | +func WithBackupPeers(load func(context.Context) []peer.AddrInfo, save func(context.Context, []peer.AddrInfo)) func(*BootstrapConfig) { |
| 90 | + if save == nil && load != nil || save != nil && load == nil { |
| 91 | + panic("both load and save backup bootstrap peers functions must be defined") |
| 92 | + } |
| 93 | + return func(cfg *BootstrapConfig) { |
| 94 | + cfg.loadBackupBootstrapPeers = load |
| 95 | + cfg.saveBackupBootstrapPeers = save |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// BackupPeers returns the load and save backup peers functions. |
| 100 | +func (cfg *BootstrapConfig) BackupPeers() (func(context.Context) []peer.AddrInfo, func(context.Context, []peer.AddrInfo)) { |
| 101 | + return cfg.loadBackupBootstrapPeers, cfg.saveBackupBootstrapPeers |
| 102 | +} |
| 103 | + |
| 104 | +// SetBackupPeers sets the load and save backup peers functions. |
| 105 | +func (cfg *BootstrapConfig) SetBackupPeers(load func(context.Context) []peer.AddrInfo, save func(context.Context, []peer.AddrInfo)) { |
| 106 | + opt := WithBackupPeers(load, save) |
| 107 | + opt(cfg) |
| 108 | +} |
| 109 | + |
| 110 | +// Bootstrap kicks off IpfsNode bootstrapping. This function will periodically |
| 111 | +// check the number of open connections and -- if there are too few -- initiate |
| 112 | +// connections to well-known bootstrap peers. It also kicks off subsystem |
| 113 | +// bootstrapping (i.e. routing). |
| 114 | +func Bootstrap(id peer.ID, host host.Host, rt routing.Routing, cfg BootstrapConfig) (io.Closer, error) { |
| 115 | + // make a signal to wait for one bootstrap round to complete. |
| 116 | + doneWithRound := make(chan struct{}) |
| 117 | + |
| 118 | + if len(cfg.BootstrapPeers()) == 0 { |
| 119 | + // We *need* to bootstrap but we have no bootstrap peers |
| 120 | + // configured *at all*, inform the user. |
| 121 | + log.Warn("no bootstrap nodes configured: go-ipfs may have difficulty connecting to the network") |
| 122 | + } |
| 123 | + |
| 124 | + // the periodic bootstrap function -- the connection supervisor |
| 125 | + periodic := func(worker goprocess.Process) { |
| 126 | + ctx := goprocessctx.OnClosingContext(worker) |
| 127 | + |
| 128 | + if err := bootstrapRound(ctx, host, cfg); err != nil { |
| 129 | + log.Debugf("%s bootstrap error: %s", id, err) |
| 130 | + } |
| 131 | + |
| 132 | + // Exit the first call (triggered independently by `proc.Go`, not `Tick`) |
| 133 | + // only after being done with the *single* Routing.Bootstrap call. Following |
| 134 | + // periodic calls (`Tick`) will not block on this. |
| 135 | + <-doneWithRound |
| 136 | + } |
| 137 | + |
| 138 | + // kick off the node's periodic bootstrapping |
| 139 | + proc := periodicproc.Tick(cfg.Period, periodic) |
| 140 | + proc.Go(periodic) // run one right now. |
| 141 | + |
| 142 | + // kick off Routing.Bootstrap |
| 143 | + if rt != nil { |
| 144 | + ctx := goprocessctx.OnClosingContext(proc) |
| 145 | + if err := rt.Bootstrap(ctx); err != nil { |
| 146 | + proc.Close() |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + doneWithRound <- struct{}{} |
| 152 | + close(doneWithRound) // it no longer blocks periodic |
| 153 | + |
| 154 | + // If loadBackupBootstrapPeers is not nil then saveBackupBootstrapPeers |
| 155 | + // must also not be nil. |
| 156 | + if cfg.loadBackupBootstrapPeers != nil { |
| 157 | + startSavePeersAsTemporaryBootstrapProc(cfg, host, proc) |
| 158 | + } |
| 159 | + |
| 160 | + return proc, nil |
| 161 | +} |
| 162 | + |
| 163 | +// Aside of the main bootstrap process we also run a secondary one that saves |
| 164 | +// connected peers as a backup measure if we can't connect to the official |
| 165 | +// bootstrap ones. These peers will serve as *temporary* bootstrap nodes. |
| 166 | +func startSavePeersAsTemporaryBootstrapProc(cfg BootstrapConfig, host host.Host, bootstrapProc goprocess.Process) { |
| 167 | + savePeersFn := func(worker goprocess.Process) { |
| 168 | + ctx := goprocessctx.OnClosingContext(worker) |
| 169 | + |
| 170 | + if err := saveConnectedPeersAsTemporaryBootstrap(ctx, host, cfg); err != nil { |
| 171 | + log.Debugf("saveConnectedPeersAsTemporaryBootstrap error: %s", err) |
| 172 | + } |
| 173 | + } |
| 174 | + savePeersProc := periodicproc.Tick(cfg.BackupBootstrapInterval, savePeersFn) |
| 175 | + |
| 176 | + // When the main bootstrap process ends also terminate the 'save connected |
| 177 | + // peers' ones. Coupling the two seems the easiest way to handle this backup |
| 178 | + // process without additional complexity. |
| 179 | + go func() { |
| 180 | + <-bootstrapProc.Closing() |
| 181 | + savePeersProc.Close() |
| 182 | + }() |
| 183 | + |
| 184 | + // Run the first round now (after the first bootstrap process has finished) |
| 185 | + // as the SavePeersPeriod can be much longer than bootstrap. |
| 186 | + savePeersProc.Go(savePeersFn) |
| 187 | +} |
| 188 | + |
| 189 | +func saveConnectedPeersAsTemporaryBootstrap(ctx context.Context, host host.Host, cfg BootstrapConfig) error { |
| 190 | + // Randomize the list of connected peers, we don't prioritize anyone. |
| 191 | + connectedPeers := randomizeList(host.Network().Peers()) |
| 192 | + |
| 193 | + bootstrapPeers := cfg.BootstrapPeers() |
| 194 | + backupPeers := make([]peer.AddrInfo, 0, cfg.MaxBackupBootstrapSize) |
| 195 | + foundPeers := make(map[peer.ID]struct{}, cfg.MaxBackupBootstrapSize+len(bootstrapPeers)) |
| 196 | + |
| 197 | + // Don't record bootstrap peers |
| 198 | + for _, b := range bootstrapPeers { |
| 199 | + foundPeers[b.ID] = struct{}{} |
| 200 | + } |
| 201 | + |
| 202 | + // Choose peers to save and filter out the ones that are already bootstrap nodes. |
| 203 | + for _, p := range connectedPeers { |
| 204 | + if _, found := foundPeers[p]; found { |
| 205 | + continue |
| 206 | + } |
| 207 | + foundPeers[p] = struct{}{} |
| 208 | + |
| 209 | + backupPeers = append(backupPeers, peer.AddrInfo{ |
| 210 | + ID: p, |
| 211 | + Addrs: host.Network().Peerstore().Addrs(p), |
| 212 | + }) |
| 213 | + |
| 214 | + if len(backupPeers) >= cfg.MaxBackupBootstrapSize { |
| 215 | + break |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + // If we didn't reach the target number use previously stored connected peers. |
| 220 | + if len(backupPeers) < cfg.MaxBackupBootstrapSize { |
| 221 | + oldSavedPeers := cfg.loadBackupBootstrapPeers(ctx) |
| 222 | + log.Debugf("missing %d peers to reach backup bootstrap target of %d, trying from previous list of %d saved peers", |
| 223 | + cfg.MaxBackupBootstrapSize-len(backupPeers), cfg.MaxBackupBootstrapSize, len(oldSavedPeers)) |
| 224 | + |
| 225 | + // Add some of the old saved peers. Ensure we don't duplicate them. |
| 226 | + for _, p := range oldSavedPeers { |
| 227 | + if _, found := foundPeers[p.ID]; found { |
| 228 | + continue |
| 229 | + } |
| 230 | + foundPeers[p.ID] = struct{}{} |
| 231 | + |
| 232 | + backupPeers = append(backupPeers, p) |
| 233 | + |
| 234 | + if len(backupPeers) >= cfg.MaxBackupBootstrapSize { |
| 235 | + break |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + cfg.saveBackupBootstrapPeers(ctx, backupPeers) |
| 241 | + log.Debugf("saved %d peers (of %d target) as bootstrap backup in the config", len(backupPeers), cfg.MaxBackupBootstrapSize) |
| 242 | + return nil |
| 243 | +} |
| 244 | + |
| 245 | +// Connect to as many peers needed to reach the BootstrapConfig.MinPeerThreshold. |
| 246 | +// Peers can be original bootstrap or temporary ones (drawn from a list of |
| 247 | +// persisted previously connected peers). |
| 248 | +func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) error { |
| 249 | + ctx, cancel := context.WithTimeout(ctx, cfg.ConnectionTimeout) |
| 250 | + defer cancel() |
| 251 | + id := host.ID() |
| 252 | + |
| 253 | + // get bootstrap peers from config. retrieving them here makes |
| 254 | + // sure we remain observant of changes to client configuration. |
| 255 | + peers := cfg.BootstrapPeers() |
| 256 | + // determine how many bootstrap connections to open |
| 257 | + connected := host.Network().Peers() |
| 258 | + if len(connected) >= cfg.MinPeerThreshold { |
| 259 | + log.Debugf("%s core bootstrap skipped -- connected to %d (> %d) nodes", |
| 260 | + id, len(connected), cfg.MinPeerThreshold) |
| 261 | + return nil |
| 262 | + } |
| 263 | + numToDial := cfg.MinPeerThreshold - len(connected) // numToDial > 0 |
| 264 | + |
| 265 | + if len(peers) > 0 { |
| 266 | + numToDial -= int(peersConnect(ctx, host, peers, numToDial, true)) |
| 267 | + if numToDial <= 0 { |
| 268 | + return nil |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + if cfg.loadBackupBootstrapPeers == nil { |
| 273 | + log.Debugf("not enough bootstrap peers to fill the remaining target of %d connections", numToDial) |
| 274 | + return ErrNotEnoughBootstrapPeers |
| 275 | + } |
| 276 | + |
| 277 | + log.Debugf("not enough bootstrap peers to fill the remaining target of %d connections, trying backup list", numToDial) |
| 278 | + |
| 279 | + tempBootstrapPeers := cfg.loadBackupBootstrapPeers(ctx) |
| 280 | + if len(tempBootstrapPeers) > 0 { |
| 281 | + numToDial -= int(peersConnect(ctx, host, tempBootstrapPeers, numToDial, false)) |
| 282 | + if numToDial <= 0 { |
| 283 | + return nil |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + log.Debugf("tried both original bootstrap peers and temporary ones but still missing target of %d connections", numToDial) |
| 288 | + |
| 289 | + return ErrNotEnoughBootstrapPeers |
| 290 | +} |
| 291 | + |
| 292 | +// Attempt to make `needed` connections from the `availablePeers` list. Mark |
| 293 | +// peers as either `permanent` or temporary when adding them to the Peerstore. |
| 294 | +// Return the number of connections completed. We eagerly over-connect in parallel, |
| 295 | +// so we might connect to more than needed. |
| 296 | +// (We spawn as many routines and attempt connections as the number of availablePeers, |
| 297 | +// but this list comes from restricted sets of original or temporary bootstrap |
| 298 | +// nodes which will keep it under a sane value.) |
| 299 | +func peersConnect(ctx context.Context, ph host.Host, availablePeers []peer.AddrInfo, needed int, permanent bool) uint64 { |
| 300 | + peers := randomizeList(availablePeers) |
| 301 | + |
| 302 | + // Monitor the number of connections and stop if we reach the target. |
| 303 | + var connected uint64 |
| 304 | + ctx, cancel := context.WithCancel(ctx) |
| 305 | + defer cancel() |
| 306 | + go func() { |
| 307 | + for { |
| 308 | + select { |
| 309 | + case <-ctx.Done(): |
| 310 | + return |
| 311 | + case <-time.After(1 * time.Second): |
| 312 | + if int(atomic.LoadUint64(&connected)) >= needed { |
| 313 | + cancel() |
| 314 | + return |
| 315 | + } |
| 316 | + } |
| 317 | + } |
| 318 | + }() |
| 319 | + |
| 320 | + var wg sync.WaitGroup |
| 321 | + for _, p := range peers { |
| 322 | + |
| 323 | + // performed asynchronously because when performed synchronously, if |
| 324 | + // one `Connect` call hangs, subsequent calls are more likely to |
| 325 | + // fail/abort due to an expiring context. |
| 326 | + // Also, performed asynchronously for dial speed. |
| 327 | + |
| 328 | + if int(atomic.LoadUint64(&connected)) >= needed { |
| 329 | + cancel() |
| 330 | + break |
| 331 | + } |
| 332 | + |
| 333 | + wg.Add(1) |
| 334 | + go func(p peer.AddrInfo) { |
| 335 | + defer wg.Done() |
| 336 | + |
| 337 | + // Skip addresses belonging to a peer we're already connected to. |
| 338 | + // (Not a guarantee but a best-effort policy.) |
| 339 | + if ph.Network().Connectedness(p.ID) == network.Connected { |
| 340 | + return |
| 341 | + } |
| 342 | + log.Debugf("%s bootstrapping to %s", ph.ID(), p.ID) |
| 343 | + |
| 344 | + if err := ph.Connect(ctx, p); err != nil { |
| 345 | + if ctx.Err() != context.Canceled { |
| 346 | + log.Debugf("failed to bootstrap with %v: %s", p.ID, err) |
| 347 | + } |
| 348 | + return |
| 349 | + } |
| 350 | + if permanent { |
| 351 | + // We're connecting to an original bootstrap peer, mark it as |
| 352 | + // a permanent address (Connect will register it as TempAddrTTL). |
| 353 | + ph.Peerstore().AddAddrs(p.ID, p.Addrs, peerstore.PermanentAddrTTL) |
| 354 | + } |
| 355 | + |
| 356 | + log.Infof("bootstrapped with %v", p.ID) |
| 357 | + atomic.AddUint64(&connected, 1) |
| 358 | + }(p) |
| 359 | + } |
| 360 | + wg.Wait() |
| 361 | + |
| 362 | + return connected |
| 363 | +} |
| 364 | + |
| 365 | +func randomizeList[T any](in []T) []T { |
| 366 | + out := make([]T, len(in)) |
| 367 | + for i, val := range rand.Perm(len(in)) { |
| 368 | + out[i] = in[val] |
| 369 | + } |
| 370 | + return out |
| 371 | +} |
0 commit comments