mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
NodeInfo not a pointer
This commit is contained in:
parent
08f84cd712
commit
8b74a8d6ac
@ -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()
|
||||
}
|
||||
|
||||
|
27
p2p/peer.go
27
p2p/peer.go
@ -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.
|
||||
|
@ -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(),
|
||||
},
|
||||
|
@ -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",
|
||||
|
@ -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(),
|
||||
},
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
|
@ -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,
|
||||
|
12
p2p/types.go
12
p2p/types.go
@ -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 {
|
||||
|
@ -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(),
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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…
x
Reference in New Issue
Block a user