tendermint/p2p/node_info.go

282 lines
8.1 KiB
Go
Raw Normal View History

2018-01-21 00:33:53 -05:00
package p2p
2015-10-25 18:21:51 -07:00
import (
"fmt"
"strings"
cmn "github.com/tendermint/tendermint/libs/common"
2018-09-26 18:47:58 -07:00
"github.com/tendermint/tendermint/version"
2015-10-25 18:21:51 -07:00
)
const (
maxNodeInfoSize = 10240 // 10Kb
maxNumChannels = 16 // plenty of room for upgrades, for now
)
2015-11-10 12:29:43 -08:00
2018-04-26 23:40:29 -04:00
// Max size of the NodeInfo struct
2018-01-20 21:12:04 -05:00
func MaxNodeInfoSize() int {
return maxNodeInfoSize
}
2018-01-13 16:14:28 -05:00
// NodeInfo is the basic node information exchanged
2018-01-13 23:48:41 -05:00
// between two peers during the Tendermint P2P handshake.
2015-10-25 18:21:51 -07:00
type NodeInfo struct {
2018-09-26 18:47:58 -07:00
Version VersionInfo `json:"version"`
Address AddressInfo `json:"id"`
Network NetworkInfo `json:"network"`
Services ServiceInfo `json:"services"`
}
// VersionInfo contains all protocol and software version information for the node.
type VersionInfo struct {
Protocol ProtocolVersion `json:"protocol"`
Software version.Software `json:"software"`
}
// ProtocolVersion contains the p2p, block, and app protocol versions.
type ProtocolVersion struct {
P2P version.Protocol `json:"p2p"`
Block version.Protocol `json:"block"`
// Don't bother with App for now until we can update it live
// App version.Protocol `json:"app"`
}
// AddressInfo contains info about the peers ID and network address.
type AddressInfo struct {
2018-01-13 23:48:41 -05:00
// Authenticate
2018-04-28 14:48:51 -04:00
// TODO: replace with NetAddress
Replaced NodeInfo's pubkey to ID (#1443) * Replaced NodeInfo PubKey to NodeID * Fixed tests and replaced NodeID with ID * Removed unnecessary method ID() * Fixed codec_test.go * Fixed codec_test.go * Removed unnecessary bracket * Fixed all tests * Fixed peer_set_test.go * Fixed peer_test.go * Fixed common_test.go * Fixed common_test.go * Renamed node_id to id * Removed peer.ID() from RPC net.go * Replaced NodeInfo pubKey to ID * Fixed codec_test.go * Fixed peer_set_test.go * Fix pex_reactor_test.go * Refactored code for privateKey initiali * Fixed peer_set_test.go * Fixed test.proto and removed orphan string in codec_test.go * Fixed pointer to a string * generate node_key when running tendermint init * [docs] prefix IPs with node IDs Refs #1429 * gen_node_key cmd * [docs/specification/secure-p2p] add a note about config * fix data race Closes #1442 ``` WARNING: DATA RACE Write at 0x00c4209de7c8 by goroutine 23: github.com/tendermint/tendermint/types.(*Block).fillHeader() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:88 +0x157 github.com/tendermint/tendermint/types.(*Block).Hash() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:104 +0x121 github.com/tendermint/tendermint/types.(*Block).HashesTo() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:135 +0x4f github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1037 +0x182d github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1425 +0x1a6c github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1318 +0x77 github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:581 +0x7a9 github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:539 +0x6c3 Previous read at 0x00c4209de7c8 by goroutine 47: github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*HexBytes).MarshalJSON() <autogenerated>:1 +0x52 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.invokeMarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:433 +0x88 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:82 +0x8d2 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).MarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/amino.go:296 +0x182 github.com/tendermint/tendermint/rpc/lib/types.NewRPCSuccessResponse() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/types/types.go:100 +0x12c github.com/tendermint/tendermint/rpc/lib/server.makeJSONRPCHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:152 +0xab7 net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.(*ServeMux).ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2254 +0xa2 github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:138 +0x4fa net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.serverHandler.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2619 +0xbc net/http.(*conn).serve() /usr/lib/go-1.9/src/net/http/server.go:1801 +0x83b Goroutine 23 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:250 +0x35b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/reactor.go:69 +0x1b4 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).Start() <autogenerated>:1 +0x43 github.com/tendermint/tendermint/p2p.(*Switch).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch.go:177 +0x124 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/node.(*Node).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/node/node.go:416 +0xa1b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/rpc/test.StartTendermint() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/test/helpers.go:100 +0x5b github.com/tendermint/tendermint/rpc/client_test.TestMain() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/client/main_test.go:17 +0x4c main.main() github.com/tendermint/tendermint/rpc/client/_test/_testmain.go:76 +0x1cd Goroutine 47 (running) created at: net/http.(*Server).Serve() /usr/lib/go-1.9/src/net/http/server.go:2720 +0x37c net/http.Serve() /usr/lib/go-1.9/src/net/http/server.go:2323 +0xe2 github.com/tendermint/tendermint/rpc/lib/server.StartHTTPServer.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:35 +0xb3 ``` * removed excessive comment Refs https://github.com/tendermint/tendermint/pull/1446#discussion_r180353446 * use the tag interface for pubsub. (#1438) * use the tag interface for pubsub. * update tmlibs. * Fix unresolved conflict. * improve `show_node_id` (#1433) * fix show_node_id * make LoadNodeKey public * make LoadNodeKey public * remove if * remove if
2018-04-11 11:11:11 +03:00
ID ID `json:"id"` // authenticated identifier
ListenAddr string `json:"listen_addr"` // accepting incoming
2018-01-13 23:48:41 -05:00
2018-09-26 18:47:58 -07:00
// ASCIIText fields
Moniker string `json:"moniker"` // arbitrary moniker
}
// NetworkInfo contains info about the network this peer is operating on.
// Currently, the only identifier is the ChainID, known here as the Name.
type NetworkInfo struct {
Name string `json:"name"`
}
// ServiceInfo describes the services this peer offers to other peers and to users.
type ServiceInfo struct {
Peers PeerServices `json:"peers`
Users UserServices `json:"users"`
}
// PeerServices describes the services this peer offers to other peers,
// in terms of active Reactor channels.
type PeerServices struct {
2018-04-26 23:40:29 -04:00
// Channels are HexBytes so easier to read as JSON
Channels cmn.HexBytes `json:"channels"` // channels this node knows about
}
2018-09-26 18:47:58 -07:00
// UserServices describes the set of services exposed to the user.
type UserServices struct {
TxIndex string `json:"tx_index"`
RPCAddress string `json:"rpc_address"`
}
2018-09-26 18:47:58 -07:00
//--------------------------------------------------------------------------
func (info NodeInfo) ID() ID {
return info.Address.ID
2018-01-13 23:48:41 -05:00
}
// Validate checks the self-reported NodeInfo is safe.
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
2018-02-27 06:54:40 -05:00
// It returns an error if there
2018-04-28 14:48:51 -04:00
// are too many Channels, if there are any duplicate Channels,
// if the ListenAddr is malformed, or if the ListenAddr is a host name
// that can not be resolved to some IP.
2018-01-13 23:48:41 -05:00
// TODO: constraints for Moniker/Other? Or is that for the UI ?
// JAE: It needs to be done on the client, but to prevent ambiguous
// unicode characters, maybe it's worth sanitizing it here.
// In the future we might want to validate these, once we have a
// name-resolution system up.
// International clients could then use punycode (or we could use
// url-encoding), and we just need to be careful with how we handle that in our
// clients. (e.g. off by default).
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
2018-02-27 06:54:40 -05:00
func (info NodeInfo) Validate() error {
2018-09-26 18:47:58 -07:00
if err := info.Version.Validate(); err != nil {
return err
}
if err := info.Address.Validate(); err != nil {
return err
}
if err := info.Network.Validate(); err != nil {
return err
}
if err := info.Services.Validate(); err != nil {
return err
}
return nil
}
// Validate checks that the protocol versions are non-zero and that the software versions are ASCII.
func (info VersionInfo) Validate() error {
// TODO
// ProtocolVersion - {P2P, Block} greater than 0
// SoftwareVersion - ASCII
return nil
}
// Validate checks that the ListenAddr is well formed and that the moniker is ASCII.
// The ID should have already been checked.
func (info AddressInfo) Validate() error {
if _, err := sanitizeASCII(info.Moniker); err != nil {
return fmt.Errorf("Moniker %v", err)
}
// ensure ListenAddr is good
_, err := NewNetAddressString(IDAddressString(info.ID, info.ListenAddr))
return err
}
// Validate checks that the NetworkInfo.Name is ASCII.
func (info NetworkInfo) Validate() error {
if _, err := sanitizeASCII(info.Name); err != nil {
return fmt.Errorf("Name %v", err)
}
2018-09-26 18:47:58 -07:00
return nil
}
// Validate validates the PeerServices and UserServices
func (info ServiceInfo) Validate() error {
if err := info.Peers.Validate(); err != nil {
return err
}
2018-09-26 18:47:58 -07:00
if err := info.Users.Validate(); err != nil {
return err
}
return nil
}
// Validate checks that there are not too many channels or any duplicate channels.
func (services PeerServices) Validate() error {
channelBytes := services.Channels
if len(channelBytes) > maxNumChannels {
return fmt.Errorf("Channels is too long (%v). Max is %v", len(channelBytes), maxNumChannels)
}
channels := make(map[byte]struct{})
2018-09-26 18:47:58 -07:00
for _, ch := range channelBytes {
_, ok := channels[ch]
if ok {
2018-09-26 18:47:58 -07:00
return fmt.Errorf("Channels contains duplicate channel id %v", ch)
}
channels[ch] = struct{}{}
}
2018-09-26 18:47:58 -07:00
return nil
}
2018-04-28 14:48:51 -04:00
2018-09-26 18:47:58 -07:00
func (services UserServices) Validate() error {
txIndex, err := sanitizeASCII(services.TxIndex)
if err != nil {
return fmt.Errorf("TxIndex %v", err)
}
if _, err := sanitizeASCII(services.RPCAddress); err != nil {
return fmt.Errorf("RPCAddress %v", err)
}
switch cmn.ASCIITrim(txIndex) {
case "on", "off":
// do nothing
default:
return fmt.Errorf("TxIndex should be either 'on' or 'off', got '%v'", txIndex)
}
return nil
}
func sanitizeASCII(input string) (string, error) {
if !cmn.IsASCIIText(input) || cmn.ASCIITrim(input) == "" {
return "", fmt.Errorf("must be valid non-empty ASCII text without tabs, but got %v", input)
}
return cmn.ASCIITrim(input), nil
2015-10-25 18:21:51 -07:00
}
2018-01-23 21:26:19 -05:00
// 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.
2018-01-14 00:10:29 -05:00
func (info NodeInfo) CompatibleWith(other NodeInfo) error {
2015-10-25 18:21:51 -07:00
2018-09-26 18:47:58 -07:00
// if we have no channels, we're just testing
ourChannels := info.Services.Peers.Channels
otherChannels := other.Services.Peers.Channels
if len(ourChannels) == 0 {
return nil
2015-10-25 18:21:51 -07:00
}
2018-09-26 18:47:58 -07:00
// nodes must have the same block version
if info.Version.Protocol.Block != other.Version.Protocol.Block {
return fmt.Errorf(
"Peer is running a different block protocol. Got %v, expected %v",
other.Version.Protocol.Block,
info.Version.Protocol.Block,
)
2015-10-25 18:21:51 -07:00
}
// nodes must be on the same network
2018-09-26 18:47:58 -07:00
if info.Network.Name != other.Network.Name {
return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network.Name, info.Network.Name)
}
// for each of our channels, check if they have it
found := false
2018-01-24 14:27:37 -05:00
OUTER_LOOP:
2018-09-26 18:47:58 -07:00
for _, ch1 := range ourChannels {
for _, ch2 := range otherChannels {
if ch1 == ch2 {
found = true
2018-01-24 14:27:37 -05:00
break OUTER_LOOP // only need one
}
}
}
if !found {
2018-09-26 18:47:58 -07:00
return fmt.Errorf("Peer has no common channels. Our channels: %v ; Peer channels: %v", ourChannels, otherChannels)
}
2015-10-25 18:21:51 -07:00
return nil
}
2018-01-23 21:26:19 -05:00
// NetAddress returns a NetAddress derived from the NodeInfo -
// it includes the authenticated peer ID and the self-reported
// ListenAddr. Note that the ListenAddr is not authenticated and
// may not match that address actually dialed if its an outbound peer.
2018-01-14 00:10:29 -05:00
func (info NodeInfo) NetAddress() *NetAddress {
2018-09-26 18:47:58 -07:00
netAddr, err := NewNetAddressString(IDAddressString(info.Address.ID, info.Address.ListenAddr))
2018-01-13 16:14:28 -05:00
if err != nil {
2018-04-28 14:48:51 -04:00
switch err.(type) {
case ErrNetAddressLookup:
// XXX If the peer provided a host name and the lookup fails here
// we're out of luck.
// TODO: use a NetAddress in NodeInfo
default:
panic(err) // everything should be well formed by now
}
2018-01-13 16:14:28 -05:00
}
return netAddr
}
2017-05-12 23:07:53 +02:00
func (info NodeInfo) String() string {
2018-09-26 18:47:58 -07:00
return "TODO" // fmt.Sprintf("NodeInfo{id: %v, moniker: %v, network: %v [listen %v], version: %v (%v)}",
// info.ID, info.Moniker, info.Network, info.ListenAddr, info.Version, info.Other)
2017-05-12 23:07:53 +02:00
}
2015-10-25 18:21:51 -07:00
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
}