Browse Source

NodeInfo not a pointer

pull/1105/head
Ethan Buchman 7 years ago
parent
commit
8b74a8d6ac
11 changed files with 33 additions and 43 deletions
  1. +3
    -3
      node/node.go
  2. +11
    -16
      p2p/peer.go
  3. +1
    -1
      p2p/peer_set_test.go
  4. +2
    -2
      p2p/peer_test.go
  5. +1
    -1
      p2p/pex_reactor_test.go
  6. +4
    -9
      p2p/switch.go
  7. +1
    -1
      p2p/test_util.go
  8. +6
    -6
      p2p/types.go
  9. +1
    -1
      rpc/core/net.go
  10. +1
    -1
      rpc/core/pipe.go
  11. +2
    -2
      rpc/core/types/responses.go

+ 3
- 3
node/node.go View File

@ -537,12 +537,12 @@ func (n *Node) ProxyApp() proxy.AppConns {
return n.proxyApp
}
func (n *Node) makeNodeInfo(pubKey crypto.PubKey) *p2p.NodeInfo {
func (n *Node) makeNodeInfo(pubKey crypto.PubKey) p2p.NodeInfo {
txIndexerStatus := "on"
if _, ok := n.txIndexer.(*null.TxIndex); ok {
txIndexerStatus = "off"
}
nodeInfo := &p2p.NodeInfo{
nodeInfo := p2p.NodeInfo{
PubKey: pubKey,
Network: n.genesisDoc.ChainID,
Version: version.Version,
@ -574,7 +574,7 @@ func (n *Node) makeNodeInfo(pubKey crypto.PubKey) *p2p.NodeInfo {
//------------------------------------------------------------------------------
// NodeInfo returns the Node's Info from the Switch.
func (n *Node) NodeInfo() *p2p.NodeInfo {
func (n *Node) NodeInfo() p2p.NodeInfo {
return n.sw.NodeInfo()
}


+ 11
- 16
p2p/peer.go View File

@ -1,7 +1,6 @@
package p2p
import (
"encoding/hex"
"fmt"
"net"
"time"
@ -21,7 +20,7 @@ type Peer interface {
ID() ID
IsOutbound() bool
IsPersistent() bool
NodeInfo() *NodeInfo
NodeInfo() NodeInfo
Status() ConnectionStatus
Send(byte, interface{}) bool
@ -47,7 +46,7 @@ type peer struct {
persistent bool
config *PeerConfig
nodeInfo *NodeInfo
nodeInfo NodeInfo
Data *cmn.CMap // User data.
}
@ -128,7 +127,7 @@ func newPeerFromConnAndConfig(rawConn net.Conn, outbound bool, reactorsByCh map[
}
}
// Key and NodeInfo are set after Handshake
// NodeInfo is set after Handshake
p := &peer{
outbound: outbound,
conn: conn,
@ -169,23 +168,23 @@ func (p *peer) IsPersistent() bool {
// HandshakeTimeout performs a handshake between a given node and the peer.
// NOTE: blocking
func (p *peer) HandshakeTimeout(ourNodeInfo *NodeInfo, timeout time.Duration) error {
func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) error {
// Set deadline for handshake so we don't block forever on conn.ReadFull
if err := p.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
return errors.Wrap(err, "Error setting deadline")
}
var peerNodeInfo = new(NodeInfo)
var peerNodeInfo NodeInfo
var err1 error
var err2 error
cmn.Parallel(
func() {
var n int
wire.WriteBinary(ourNodeInfo, p.conn, &n, &err1)
wire.WriteBinary(&ourNodeInfo, p.conn, &n, &err1)
},
func() {
var n int
wire.ReadBinary(peerNodeInfo, p.conn, maxNodeInfoSize, &n, &err2)
wire.ReadBinary(&peerNodeInfo, p.conn, maxNodeInfoSize, &n, &err2)
p.Logger.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
})
if err1 != nil {
@ -213,7 +212,7 @@ func (p *peer) Addr() net.Addr {
// PubKey returns peer's public key.
func (p *peer) PubKey() crypto.PubKey {
if p.NodeInfo() != nil {
if !p.nodeInfo.PubKey.Empty() {
return p.nodeInfo.PubKey
} else if p.config.AuthEnc {
return p.conn.(*SecretConnection).RemotePubKey()
@ -300,16 +299,12 @@ func (p *peer) Set(key string, data interface{}) {
// ID returns the peer's ID - the hex encoded hash of its pubkey.
func (p *peer) ID() ID {
return ID(hex.EncodeToString(p.PubKey().Address()))
return PubKeyToID(p.PubKey())
}
// NodeInfo returns a copy of the peer's NodeInfo.
func (p *peer) NodeInfo() *NodeInfo {
if p.nodeInfo == nil {
return nil
}
n := *p.nodeInfo // copy
return &n
func (p *peer) NodeInfo() NodeInfo {
return p.nodeInfo
}
// Status returns the peer's ConnectionStatus.


+ 1
- 1
p2p/peer_set_test.go View File

@ -14,7 +14,7 @@ import (
// Returns an empty dummy peer
func randPeer() *peer {
return &peer{
nodeInfo: &NodeInfo{
nodeInfo: NodeInfo{
ListenAddr: cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(),
},


+ 2
- 2
p2p/peer_test.go View File

@ -90,7 +90,7 @@ func createOutboundPeerAndPerformHandshake(addr *NetAddress, config *PeerConfig)
if err != nil {
return nil, err
}
err = p.HandshakeTimeout(&NodeInfo{
err = p.HandshakeTimeout(NodeInfo{
PubKey: pk.PubKey(),
Moniker: "host_peer",
Network: "testing",
@ -141,7 +141,7 @@ func (p *remotePeer) accept(l net.Listener) {
if err != nil {
golog.Fatalf("Failed to create a peer: %+v", err)
}
err = peer.HandshakeTimeout(&NodeInfo{
err = peer.HandshakeTimeout(NodeInfo{
PubKey: p.PrivKey.PubKey(),
Moniker: "remote_peer",
Network: "testing",


+ 1
- 1
p2p/pex_reactor_test.go View File

@ -242,7 +242,7 @@ func createRoutableAddr() (addr string, netAddr *NetAddress) {
func createRandomPeer(outbound bool) *peer {
addr, netAddr := createRoutableAddr()
p := &peer{
nodeInfo: &NodeInfo{
nodeInfo: NodeInfo{
ListenAddr: netAddr.String(),
PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(),
},


+ 4
- 9
p2p/switch.go View File

@ -52,8 +52,8 @@ type Switch struct {
reactorsByCh map[byte]Reactor
peers *PeerSet
dialing *cmn.CMap
nodeInfo *NodeInfo // our node info
nodeKey *NodeKey // our node privkey
nodeInfo NodeInfo // our node info
nodeKey *NodeKey // our node privkey
filterConnByAddr func(net.Addr) error
filterConnByPubKey func(crypto.PubKey) error
@ -70,7 +70,6 @@ func NewSwitch(config *cfg.P2PConfig) *Switch {
reactorsByCh: make(map[byte]Reactor),
peers: NewPeerSet(),
dialing: cmn.NewCMap(),
nodeInfo: nil,
}
// Ensure we have a completely undeterministic PRNG. cmd.RandInt64() draws
@ -141,24 +140,20 @@ func (sw *Switch) IsListening() bool {
// SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes.
// NOTE: Not goroutine safe.
func (sw *Switch) SetNodeInfo(nodeInfo *NodeInfo) {
func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
sw.nodeInfo = nodeInfo
}
// NodeInfo returns the switch's NodeInfo.
// NOTE: Not goroutine safe.
func (sw *Switch) NodeInfo() *NodeInfo {
func (sw *Switch) NodeInfo() NodeInfo {
return sw.nodeInfo
}
// SetNodeKey sets the switch's private key for authenticated encryption.
// NOTE: Overwrites sw.nodeInfo.PubKey.
// NOTE: Not goroutine safe.
func (sw *Switch) SetNodeKey(nodeKey *NodeKey) {
sw.nodeKey = nodeKey
if sw.nodeInfo != nil {
sw.nodeInfo.PubKey = nodeKey.PubKey()
}
}
//---------------------------------------------------------------------


+ 1
- 1
p2p/test_util.go View File

@ -98,7 +98,7 @@ func makeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch f
PrivKey: crypto.GenPrivKeyEd25519().Wrap(),
}
s := initSwitch(i, NewSwitch(cfg))
s.SetNodeInfo(&NodeInfo{
s.SetNodeInfo(NodeInfo{
PubKey: nodeKey.PubKey(),
Moniker: cmn.Fmt("switch%d", i),
Network: network,


+ 6
- 6
p2p/types.go View File

@ -30,7 +30,7 @@ type NodeInfo struct {
// Validate checks the self-reported NodeInfo is safe.
// It returns an error if the info.PubKey doesn't match the given pubKey.
// TODO: constraints for Moniker/Other? Or is that for the UI ?
func (info *NodeInfo) Validate(pubKey crypto.PubKey) error {
func (info NodeInfo) Validate(pubKey crypto.PubKey) error {
if !info.PubKey.Equals(pubKey) {
return fmt.Errorf("info.PubKey (%v) doesn't match peer.PubKey (%v)",
info.PubKey, pubKey)
@ -39,7 +39,7 @@ func (info *NodeInfo) Validate(pubKey crypto.PubKey) error {
}
// CONTRACT: two nodes are compatible if the major/minor versions match and network match
func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
func (info NodeInfo) CompatibleWith(other NodeInfo) error {
iMajor, iMinor, _, iErr := splitVersion(info.Version)
oMajor, oMinor, _, oErr := splitVersion(other.Version)
@ -71,11 +71,11 @@ func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
return nil
}
func (info *NodeInfo) ID() ID {
func (info NodeInfo) ID() ID {
return PubKeyToID(info.PubKey)
}
func (info *NodeInfo) NetAddress() *NetAddress {
func (info NodeInfo) NetAddress() *NetAddress {
id := PubKeyToID(info.PubKey)
addr := info.ListenAddr
netAddr, err := NewNetAddressString(IDAddressString(id, addr))
@ -85,12 +85,12 @@ func (info *NodeInfo) NetAddress() *NetAddress {
return netAddr
}
func (info *NodeInfo) ListenHost() string {
func (info NodeInfo) ListenHost() string {
host, _, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
return host
}
func (info *NodeInfo) ListenPort() int {
func (info NodeInfo) ListenPort() int {
_, port, _ := net.SplitHostPort(info.ListenAddr) // nolint: errcheck, gas
port_i, err := strconv.Atoi(port)
if err != nil {


+ 1
- 1
rpc/core/net.go View File

@ -41,7 +41,7 @@ func NetInfo() (*ctypes.ResultNetInfo, error) {
peers := []ctypes.Peer{}
for _, peer := range p2pSwitch.Peers().List() {
peers = append(peers, ctypes.Peer{
NodeInfo: *peer.NodeInfo(),
NodeInfo: peer.NodeInfo(),
IsOutbound: peer.IsOutbound(),
ConnectionStatus: peer.Status(),
})


+ 1
- 1
rpc/core/pipe.go View File

@ -30,7 +30,7 @@ type P2P interface {
Listeners() []p2p.Listener
Peers() p2p.IPeerSet
NumPeers() (outbound, inbound, dialig int)
NodeInfo() *p2p.NodeInfo
NodeInfo() p2p.NodeInfo
IsListening() bool
DialPeersAsync(*p2p.AddrBook, []string, bool) error
}


+ 2
- 2
rpc/core/types/responses.go View File

@ -54,7 +54,7 @@ func NewResultCommit(header *types.Header, commit *types.Commit,
}
type ResultStatus struct {
NodeInfo *p2p.NodeInfo `json:"node_info"`
NodeInfo p2p.NodeInfo `json:"node_info"`
PubKey crypto.PubKey `json:"pub_key"`
LatestBlockHash data.Bytes `json:"latest_block_hash"`
LatestAppHash data.Bytes `json:"latest_app_hash"`
@ -64,7 +64,7 @@ type ResultStatus struct {
}
func (s *ResultStatus) TxIndexEnabled() bool {
if s == nil || s.NodeInfo == nil {
if s == nil {
return false
}
for _, s := range s.NodeInfo.Other {


Loading…
Cancel
Save