From 2a2f8c03094139653e4fbe93bdf8c8c0906c7f5b Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Mon, 27 May 2019 13:59:01 +0200 Subject: [PATCH] CRDT: Fix protecting of nodes before loading the peerstore Addresses comments from review #792 --- consensus/crdt/consensus.go | 12 ++++++------ pstoremgr/pstoremgr.go | 7 ++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/consensus/crdt/consensus.go b/consensus/crdt/consensus.go index 798a2b89..fe447298 100644 --- a/consensus/crdt/consensus.go +++ b/consensus/crdt/consensus.go @@ -102,12 +102,6 @@ func New( readyCh: make(chan struct{}, 1), } - // Set up a fast-lookup trusted peers cache. - // Protect these peers in the ConnMgr - for _, p := range css.config.TrustedPeers { - css.Trust(ctx, p) - } - go css.setup() return css, nil } @@ -119,6 +113,12 @@ func (css *Consensus) setup() { case <-css.rpcReady: } + // Set up a fast-lookup trusted peers cache. + // Protect these peers in the ConnMgr + for _, p := range css.config.TrustedPeers { + css.Trust(css.ctx, p) + } + // Hash the cluster name and produce the topic name from there // as a way to avoid pubsub topic collisions with other // pubsub applications potentially when both potentially use diff --git a/pstoremgr/pstoremgr.go b/pstoremgr/pstoremgr.go index 4b4e26df..81ee2cda 100644 --- a/pstoremgr/pstoremgr.go +++ b/pstoremgr/pstoremgr.go @@ -256,7 +256,7 @@ func (pm *Manager) SavePeerstoreForPeers(peers []peer.ID) { // Bootstrap attempts to get as much as count connected peers by selecting // randomly from those in the libp2p host peerstore. It returns the number -// if peers it sucessfully connected to. +// of peers it successfully connected to. func (pm *Manager) Bootstrap(count int) int { knownPeers := pm.host.Peerstore().PeersWithAddrs() toSort := &peerSort{ @@ -296,6 +296,11 @@ func (pm *Manager) SetPriority(pid peer.ID, prio int) error { return pm.host.Peerstore().Put(pid, PriorityTag, prio) } +// peerSort is used to sort a slice of PinInfos given the PriorityTag in the +// peerstore, from the lowest tag value (0 is the highest priority) to the +// highest, Peers without a valid priority tag are considered as having a tag +// with value 0, so they will be among the first elements in the resulting +// slice. type peerSort struct { pinfos []peerstore.PeerInfo pstore peerstore.Peerstore