OSDN Git Service

File rename p2p/types.go to p2p/node_info.go
authorYahtoo Ma <yahtoo.ma@gmail.com>
Thu, 17 May 2018 08:23:02 +0000 (16:23 +0800)
committerYahtoo Ma <yahtoo.ma@gmail.com>
Mon, 21 May 2018 02:54:19 +0000 (10:54 +0800)
p2p/node_info.go [moved from p2p/types.go with 82% similarity]
p2p/switch.go

similarity index 82%
rename from p2p/types.go
rename to p2p/node_info.go
index 1d3770b..02245f0 100644 (file)
@@ -21,7 +21,9 @@ type NodeInfo struct {
        Other      []string             `json:"other"`   // other application specific data
 }
 
-// CONTRACT: two nodes are compatible if the major/minor versions match and network match
+// CompatibleWith checks if two NodeInfo are compatible with eachother.
+// CONTRACT: two nodes are compatible if the major version matches and network match
+// and they have at least one channel in common.
 func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
        iMajor, iMinor, _, iErr := splitVersion(info.Version)
        oMajor, oMinor, _, oErr := splitVersion(other.Version)
@@ -68,8 +70,13 @@ func (info *NodeInfo) ListenPort() int {
        return port_i
 }
 
+func (info *NodeInfo) RemoteAddrHost() string {
+       host, _, _ := net.SplitHostPort(info.RemoteAddr)
+       return host
+}
+
 func (info NodeInfo) String() string {
-       return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [remote %v, listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.RemoteAddr, info.ListenAddr, info.Version, info.Other)
+       return fmt.Sprintf("NodeInfo{pk: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}", info.PubKey, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other)
 }
 
 func splitVersion(version string) (string, string, string, error) {
index 313872d..3cafff9 100644 (file)
@@ -301,7 +301,7 @@ func (sw *Switch) filterConnByIP(ip string) error {
 }
 
 func (sw *Switch) filterConnByPeer(peer *Peer) error {
-       if err := sw.checkBannedPeer(peer.mconn.RemoteAddress.IP.String()); err != nil {
+       if err := sw.checkBannedPeer(peer.NodeInfo.RemoteAddrHost()); err != nil {
                return ErrConnectBannedPeer
        }
 
@@ -428,7 +428,7 @@ func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
 func (sw *Switch) AddBannedPeer(peer *Peer) error {
        sw.mtx.Lock()
        defer sw.mtx.Unlock()
-       key := peer.mconn.RemoteAddress.IP.String()
+       key := peer.NodeInfo.RemoteAddrHost()
        sw.bannedPeer[key] = time.Now().Add(defaultBanDuration)
        datajson, err := json.Marshal(sw.bannedPeer)
        if err != nil {