remove UnknownPeerError

when encountering an unknown peer in received gossip, rather than
ignoring the entire gossip we create a placeholder Peer. This has
version 0 and uid 0, which means it will be replaced by any complete
peer information subsequently received via gossip.
This commit is contained in:
Matthias Radestock committed 2015-11-30 16:17:16 +00:00
1 parent eb9b5b535f
commit 7e15eabe65
3 files changed
+14 -25

No files matched your search

+10 -1
View File
@@ -26,7 +26,12 @@ func randUint16() (r uint16) {
type PeerUID uint64 type PeerUID uint64
func randomPeerUID() PeerUID { func randomPeerUID() PeerUID {
return PeerUID(randUint64()) for {
uid := randUint64()
if uid != 0 { // uid 0 is reserved for peer placeholder
return PeerUID(uid)
}
}
} }
func ParsePeerUID(s string) (PeerUID, error) { func ParsePeerUID(s string) (PeerUID, error) {
@@ -95,6 +100,10 @@ func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64, shortI
}) })
} }
func NewPeerPlaceholder(name PeerName) *Peer {
return NewPeerFromSummary(PeerSummary{NameByte: name.Bin()})
}
func NewPeerFrom(peer *Peer) *Peer { func NewPeerFrom(peer *Peer) *Peer {
return NewPeerFromSummary(peer.PeerSummary) return NewPeerFromSummary(peer.PeerSummary)
} }
+4 -16
View File
@@ -3,7 +3,6 @@ package mesh
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"fmt"
"io" "io"
"math/rand" "math/rand"
"sync" "sync"
@@ -32,14 +31,6 @@ type ShortIDPeers struct {
others []*Peer others []*Peer
} }
type UnknownPeerError struct {
Name PeerName
}
func (upe UnknownPeerError) Error() string {
return fmt.Sprint("Reference to unknown peer ", upe.Name)
}
type PeerNameSet map[PeerName]struct{} type PeerNameSet map[PeerName]struct{}
type ConnectionSummary struct { type ConnectionSummary struct {
@@ -334,9 +325,7 @@ func (peers *Peers) ApplyUpdate(update []byte) (PeerNameSet, PeerNameSet, error)
return nil, nil, err return nil, nil, err
} }
// By this point, we know the update doesn't refer to any peers we // Add new peers
// have no knowledge of. We can now apply the update. Start by
// adding in any new peers.
for name, newPeer := range newPeers { for name, newPeer := range newPeers {
peers.byName[name] = newPeer peers.byName[name] = newPeer
peers.addByShortID(newPeer, &pending) peers.addByShortID(newPeer, &pending)
@@ -446,10 +435,8 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de
if _, found := peers.byName[remoteName]; found { if _, found := peers.byName[remoteName]; found {
continue continue
} }
// Update refers to a peer which we have no knowledge // Update refers to a peer which we have no knowledge of.
// of. Thus we can't apply the update. Abort. newPeers[remoteName] = NewPeerPlaceholder(remoteName)
err = UnknownPeerError{remoteName}
return
} }
} }
return return
@@ -484,6 +471,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti
} }
peer.Version = newPeer.Version peer.Version = newPeer.Version
peer.UID = newPeer.UID peer.UID = newPeer.UID
peer.NickName = newPeer.NickName
peer.connections = makeConnsMap(peer, connSummaries, peers.byName) peer.connections = makeConnsMap(peer, connSummaries, peers.byName)
if newPeer.ShortID != peer.ShortID || newPeer.HasShortID != peer.HasShortID { if newPeer.ShortID != peer.ShortID || newPeer.HasShortID != peer.HasShortID {
-8
View File
@@ -172,14 +172,6 @@ func (router *Router) OnGossip(update []byte) (GossipData, error) {
func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameSet, error) { func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameSet, error) {
origUpdate, newUpdate, err := router.Peers.ApplyUpdate(update) origUpdate, newUpdate, err := router.Peers.ApplyUpdate(update)
if _, ok := err.(UnknownPeerError); err != nil && ok {
// That update contained a reference to a peer which wasn't
// itself included in the update, and we didn't know about
// already. We ignore this; eventually we should receive an
// update containing a complete topology.
log.Println("Topology gossip:", err)
return nil, nil, nil
}
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }