p2p: reorder some checks in addPeer; add comments to NodeInfo

This commit is contained in:
Ethan Buchman
2018-01-01 20:21:11 -05:00
parent 528154f1a2
commit f2e0abf1dc
3 changed files with 18 additions and 17 deletions

View File

@@ -304,6 +304,7 @@ func (p *peer) Set(key string, data interface{}) {
}
// Key returns the peer's id key.
// TODO: call this ID
func (p *peer) Key() string {
return p.nodeInfo.ListenAddr // XXX: should probably be PubKey.KeyString()
}

View File

@@ -232,10 +232,15 @@ func (sw *Switch) OnStop() {
// NOTE: If error is returned, caller is responsible for calling peer.CloseConn()
func (sw *Switch) addPeer(peer *peer) error {
// Avoid self
if sw.nodeInfo.PubKey.Equals(peer.PubKey().Wrap()) {
return errors.New("Ignoring connection from self")
}
// Filter peer against white list
if err := sw.FilterConnByAddr(peer.Addr()); err != nil {
return err
}
if err := sw.FilterConnByPubKey(peer.PubKey()); err != nil {
return err
}
@@ -244,9 +249,10 @@ func (sw *Switch) addPeer(peer *peer) error {
return err
}
// Avoid self
if sw.nodeInfo.PubKey.Equals(peer.PubKey().Wrap()) {
return errors.New("Ignoring connection from self")
// Avoid duplicate
if sw.peers.Has(peer.Key()) {
return ErrSwitchDuplicatePeer
}
// Check version, chain id
@@ -254,12 +260,6 @@ func (sw *Switch) addPeer(peer *peer) error {
return err
}
// Check for duplicate peer
if sw.peers.Has(peer.Key()) {
return ErrSwitchDuplicatePeer
}
// Start peer
if sw.IsRunning() {
sw.startInitPeer(peer)

View File

@@ -12,13 +12,13 @@ import (
const maxNodeInfoSize = 10240 // 10Kb
type NodeInfo struct {
PubKey crypto.PubKey `json:"pub_key"`
Moniker string `json:"moniker"`
Network string `json:"network"`
RemoteAddr string `json:"remote_addr"`
ListenAddr string `json:"listen_addr"`
Version string `json:"version"` // major.minor.revision
Other []string `json:"other"` // other application specific data
PubKey crypto.PubKey `json:"pub_key"` // authenticated pubkey
Moniker string `json:"moniker"` // arbitrary moniker
Network string `json:"network"` // network/chain ID
RemoteAddr string `json:"remote_addr"` // address for the connection
ListenAddr string `json:"listen_addr"` // accepting incoming
Version string `json:"version"` // major.minor.revision
Other []string `json:"other"` // other application specific data
}
// CONTRACT: two nodes are compatible if the major/minor versions match and network match