Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
remove UnknownPeerError
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rade committed Nov 30, 2015
1 parent a13351a commit 29cb566
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
11 changes: 10 additions & 1 deletion mesh/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func randUint16() (r uint16) {
type PeerUID uint64

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) {
Expand Down Expand Up @@ -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 {
return NewPeerFromSummary(peer.PeerSummary)
}
Expand Down
20 changes: 4 additions & 16 deletions mesh/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mesh
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"math/rand"
"sync"
Expand Down Expand Up @@ -32,14 +31,6 @@ type ShortIDPeers struct {
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 ConnectionSummary struct {
Expand Down Expand Up @@ -334,9 +325,7 @@ func (peers *Peers) ApplyUpdate(update []byte) (PeerNameSet, PeerNameSet, error)
return nil, nil, err
}

// By this point, we know the update doesn't refer to any peers we
// have no knowledge of. We can now apply the update. Start by
// adding in any new peers.
// Add new peers
for name, newPeer := range newPeers {
peers.byName[name] = newPeer
peers.addByShortID(newPeer, &pending)
Expand Down Expand Up @@ -446,10 +435,8 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de
if _, found := peers.byName[remoteName]; found {
continue
}
// Update refers to a peer which we have no knowledge
// of. Thus we can't apply the update. Abort.
err = UnknownPeerError{remoteName}
return
// Update refers to a peer which we have no knowledge of.
newPeers[remoteName] = NewPeerPlaceholder(remoteName)
}
}
return
Expand Down Expand Up @@ -481,6 +468,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti
}
peer.Version = newPeer.Version
peer.UID = newPeer.UID
peer.NickName = newPeer.NickName
peer.connections = makeConnsMap(peer, connSummaries, peers.byName)

if newPeer.ShortID != peer.ShortID {
Expand Down
8 changes: 0 additions & 8 deletions mesh/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,6 @@ func (router *Router) OnGossip(update []byte) (GossipData, error) {

func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameSet, error) {
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 {
return nil, nil, err
}
Expand Down

0 comments on commit 29cb566

Please sign in to comment.