From 0e80059e79de3b1e27a5255f4f64d590ddcfc0f4 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 22 Apr 2015 13:50:37 -0700 Subject: [PATCH] versioning --- node/node.go | 1 + p2p/peer.go | 6 ------ p2p/switch.go | 5 +++-- types/node.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/node/node.go b/node/node.go index 415796367..52e2d26fd 100644 --- a/node/node.go +++ b/node/node.go @@ -188,6 +188,7 @@ func makeNodeInfo(sw *p2p.Switch) *types.NodeInfo { nodeInfo := &types.NodeInfo{ Moniker: config.App().GetString("Moniker"), Network: config.App().GetString("Network"), + Version: "0.0.1", } if !sw.IsListening() { return nodeInfo diff --git a/p2p/peer.go b/p2p/peer.go index 1a63b52e8..549200e24 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -12,12 +12,6 @@ import ( "github.com/tendermint/tendermint/types" ) -type nodeInfo struct { - Host string - RPCPort uint16 - P2PPort uint16 -} - type Peer struct { outbound bool mconn *MConnection diff --git a/p2p/switch.go b/p2p/switch.go index c873874af..00d48c0e7 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -161,9 +161,10 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er if err != nil { return nil, err } - if peerNodeInfo.Network != sw.nodeInfo.Network { - return nil, fmt.Errorf("Peer is on different network %v", peerNodeInfo.Network) + if err := sw.nodeInfo.CompatibleWith(peerNodeInfo); err != nil { + return nil, err } + peer := newPeer(conn, peerNodeInfo, outbound, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError) // Add the peer to .peers diff --git a/types/node.go b/types/node.go index 7a3c41a73..0498e33e9 100644 --- a/types/node.go +++ b/types/node.go @@ -1,9 +1,56 @@ package types +import ( + "fmt" + "strings" +) + type NodeInfo struct { Moniker string Network string + Version string + Host string P2PPort uint16 RPCPort uint16 } + +func (ni *NodeInfo) CompatibleWith(no *NodeInfo) error { + iM, im, _, ie := splitVersion(ni.Version) + oM, om, _, oe := splitVersion(no.Version) + + // if our own version number is not formatted right, we messed up + if ie != nil { + return ie + } + + // version number must be formatted correctly ("x.x.x") + if oe != nil { + return oe + } + + // major version must match + if iM != oM { + return fmt.Errorf("Peer is on a different major version. Got %v, expected %v", oM, iM) + } + + // minor version must match + if im != om { + return fmt.Errorf("Peer is on a different minor version. Got %v, expected %v", om, im) + } + + // nodes must be on the same network + if ni.Network != no.Network { + return fmt.Errorf("Peer is on a different network. Got %v, expected %v", no.Network, ni.Network) + } + + return nil +} + +func splitVersion(version string) (string, string, string, error) { + spl := strings.Split(version, ".") + if len(spl) != 3 { + return "", "", "", fmt.Errorf("Invalid version format %v", version) + } + return spl[0], spl[1], spl[2], nil +}