Change iteration over connection decoding to abort after the first error, and let the UnknownPeersError know which peer was unknown
This commit is contained in:
1 parent
1748b6e790
commit
978d374820
3 files changed
+17
-14
No files matched your search
+15
-13
@@ -177,24 +177,22 @@ func (cache *PeerCache) decodeUpdate(update []byte, router *Router) (newPeers ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unknownPeers := false
|
|
||||||
for _, connsBuf := range decodedConns {
|
for _, connsBuf := range decodedConns {
|
||||||
connsIterator(connsBuf, func(remoteNameByte []byte, _ string) {
|
err = connsIterator(connsBuf, func(remoteNameByte []byte, _ string) error {
|
||||||
remoteName := PeerNameFromBin(remoteNameByte)
|
remoteName := PeerNameFromBin(remoteNameByte)
|
||||||
if _, found := newPeers[remoteName]; found {
|
if _, found := newPeers[remoteName]; found {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
if _, found := cache.table[remoteName]; found {
|
if _, found := cache.table[remoteName]; found {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
// Update refers to a peer which we have no knowledge
|
// Update refers to a peer which we have no knowledge
|
||||||
// of. Thus we can't apply the update. Abort.
|
// of. Thus we can't apply the update. Abort.
|
||||||
unknownPeers = true
|
return UnknownPeersError{remoteName}
|
||||||
})
|
})
|
||||||
}
|
if err != nil {
|
||||||
if unknownPeers {
|
break
|
||||||
err = UnknownPeersError{}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -282,7 +280,7 @@ func decodePeerNoConns(dec *gob.Decoder) (nameByte []byte, uid uint64, version u
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func connsIterator(input []byte, fun func([]byte, string)) {
|
func connsIterator(input []byte, fun func([]byte, string) error) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.Write(input)
|
buf.Write(input)
|
||||||
dec := gob.NewDecoder(buf)
|
dec := gob.NewDecoder(buf)
|
||||||
@@ -290,22 +288,26 @@ func connsIterator(input []byte, fun func([]byte, string)) {
|
|||||||
var nameByte []byte
|
var nameByte []byte
|
||||||
err := dec.Decode(&nameByte)
|
err := dec.Decode(&nameByte)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
checkFatal(err)
|
checkFatal(err)
|
||||||
var foundAt string
|
var foundAt string
|
||||||
checkFatal(dec.Decode(&foundAt))
|
checkFatal(dec.Decode(&foundAt))
|
||||||
fun(nameByte, string(foundAt))
|
if err = fun(nameByte, string(foundAt)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConnsMap(peer *Peer, buf []byte, table map[PeerName]*Peer) map[PeerName]Connection {
|
func readConnsMap(peer *Peer, buf []byte, table map[PeerName]*Peer) map[PeerName]Connection {
|
||||||
conns := make(map[PeerName]Connection)
|
conns := make(map[PeerName]Connection)
|
||||||
connsIterator(buf, func(nameByte []byte, remoteTCPAddr string) {
|
connsIterator(buf, func(nameByte []byte, remoteTCPAddr string) error {
|
||||||
name := PeerNameFromBin(nameByte)
|
name := PeerNameFromBin(nameByte)
|
||||||
remotePeer := table[name]
|
remotePeer := table[name]
|
||||||
conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr)
|
conn := NewRemoteConnection(peer, remotePeer, remoteTCPAddr)
|
||||||
conns[name] = conn
|
conns[name] = conn
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
return conns
|
return conns
|
||||||
}
|
}
|
||||||
@@ -123,6 +123,7 @@ type FrameTooBigError struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UnknownPeersError struct {
|
type UnknownPeersError struct {
|
||||||
|
Name PeerName
|
||||||
}
|
}
|
||||||
|
|
||||||
type NameCollisionError struct {
|
type NameCollisionError struct {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func (ftbe FrameTooBigError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (upe UnknownPeersError) Error() string {
|
func (upe UnknownPeersError) Error() string {
|
||||||
return fmt.Sprint("Reference to unknown peers")
|
return fmt.Sprint("Reference to unknown peer: ", upe.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nce NameCollisionError) Error() string {
|
func (nce NameCollisionError) Error() string {
|
||||||
|
|||||||
Reference in new issue
Block a user