Browse Source

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

pull/1048/head
Ethan Buchman 7 years ago
parent
commit
f2e0abf1dc
3 changed files with 18 additions and 17 deletions
  1. +1
    -0
      p2p/peer.go
  2. +10
    -10
      p2p/switch.go
  3. +7
    -7
      p2p/types.go

+ 1
- 0
p2p/peer.go View File

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


+ 10
- 10
p2p/switch.go View File

@ -232,10 +232,15 @@ func (sw *Switch) OnStop() {
// NOTE: If error is returned, caller is responsible for calling peer.CloseConn() // NOTE: If error is returned, caller is responsible for calling peer.CloseConn()
func (sw *Switch) addPeer(peer *peer) error { 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 { if err := sw.FilterConnByAddr(peer.Addr()); err != nil {
return err return err
} }
if err := sw.FilterConnByPubKey(peer.PubKey()); err != nil { if err := sw.FilterConnByPubKey(peer.PubKey()); err != nil {
return err return err
} }
@ -244,9 +249,10 @@ func (sw *Switch) addPeer(peer *peer) error {
return err 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 // Check version, chain id
@ -254,12 +260,6 @@ func (sw *Switch) addPeer(peer *peer) error {
return err return err
} }
// Check for duplicate peer
if sw.peers.Has(peer.Key()) {
return ErrSwitchDuplicatePeer
}
// Start peer // Start peer
if sw.IsRunning() { if sw.IsRunning() {
sw.startInitPeer(peer) sw.startInitPeer(peer)


+ 7
- 7
p2p/types.go View File

@ -12,13 +12,13 @@ import (
const maxNodeInfoSize = 10240 // 10Kb const maxNodeInfoSize = 10240 // 10Kb
type NodeInfo struct { 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 // CONTRACT: two nodes are compatible if the major/minor versions match and network match


Loading…
Cancel
Save