github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/peermeta.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2pcommon 7 8 import ( 9 "github.com/aergoio/aergo/types" 10 ) 11 12 const ( 13 UnknownVersion = "" 14 ) 15 // PeerMeta contains non changeable information of peer node during connected state 16 type PeerMeta struct { 17 ID types.PeerID 18 // IPAddress is human readable form of ip address such as "192.168.0.1" or "2001:0db8:0a0b:12f0:33:1" 19 IPAddress string 20 Port uint32 21 Designated bool // Designated means this peer is designated in config file and connect to in startup phase 22 23 Version string 24 Hidden bool // Hidden means that meta info of this peer will not be sent to other peers when getting peer list 25 Outbound bool 26 } 27 28 func (m *PeerMeta) GetVersion() string { 29 if m.Version == "" { 30 return "(old)" 31 } else { 32 return m.Version 33 } 34 } 35 36 // FromStatusToMeta create peerMeta from Status message 37 func NewMetaFromStatus(status *types.Status, outbound bool) PeerMeta { 38 meta := FromPeerAddress(status.Sender) 39 meta.Hidden = status.NoExpose 40 meta.Outbound = outbound 41 meta.Version = status.Version 42 return meta 43 } 44 45 // FromPeerAddress convert PeerAddress to PeerMeta 46 func FromPeerAddress(addr *types.PeerAddress) PeerMeta { 47 meta := PeerMeta{IPAddress: addr.Address, 48 Port: addr.Port, ID: types.PeerID(addr.PeerID)} 49 return meta 50 } 51 52 // ToPeerAddress convert PeerMeta to PeerAddress 53 func (m PeerMeta) ToPeerAddress() types.PeerAddress { 54 addr := types.PeerAddress{Address: m.IPAddress, Port: m.Port, 55 PeerID: []byte(m.ID)} 56 return addr 57 }