mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-06 21:36:26 +00:00
p2p: reorder some checks in addPeer; add comments to NodeInfo
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
14
p2p/types.go
14
p2p/types.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user