github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/peer.go (about) 1 package network 2 3 import ( 4 "context" 5 "net" 6 7 "github.com/nspcc-dev/neo-go/pkg/network/payload" 8 ) 9 10 type AddressablePeer interface { 11 // ConnectionAddr returns an address-like identifier of this connection 12 // before we have a proper one (after the handshake). It's either the 13 // address from discoverer (if initiated from node) or one from socket 14 // (if connected to node from outside). 15 ConnectionAddr() string 16 // PeerAddr returns the remote address that should be used to establish 17 // a new connection to the node. It can differ from the RemoteAddr 18 // address in case the remote node is a client and its current 19 // connection port is different from the one the other node should use 20 // to connect to it. It's only valid after the handshake is completed. 21 // Before that, it returns the same address as RemoteAddr. 22 PeerAddr() net.Addr 23 // Version returns peer's version message if the peer has handshaked 24 // already. 25 Version() *payload.Version 26 } 27 28 // Peer represents a network node neo-go is connected to. 29 type Peer interface { 30 AddressablePeer 31 // RemoteAddr returns the remote address that we're connected to now. 32 RemoteAddr() net.Addr 33 Disconnect(error) 34 35 // BroadcastPacket is a context-bound packet enqueuer, it either puts the 36 // given packet into the queue or exits with errors if the context expires 37 // or peer disconnects. It accepts a slice of bytes that 38 // can be shared with other queues (so that message marshalling can be 39 // done once for all peers). It returns an error if the peer has not yet 40 // completed handshaking. 41 BroadcastPacket(context.Context, []byte) error 42 43 // BroadcastHPPacket is the same as BroadcastPacket, but uses a high-priority 44 // queue. 45 BroadcastHPPacket(context.Context, []byte) error 46 47 // EnqueueP2PMessage is a blocking packet enqueuer, it doesn't return until 48 // it puts the given message into the queue. It returns an error if the peer 49 // has not yet completed handshaking. This queue is intended to be used for 50 // unicast peer to peer communication that is more important than broadcasts 51 // (handled by BroadcastPacket) but less important than high-priority 52 // messages (handled by EnqueueHPMessage). 53 EnqueueP2PMessage(*Message) error 54 // EnqueueP2PPacket is similar to EnqueueP2PMessage, but accepts a slice of 55 // message(s) bytes. 56 EnqueueP2PPacket([]byte) error 57 58 // EnqueueHPMessage is similar to EnqueueP2PMessage, but uses a high-priority 59 // queue. 60 EnqueueHPMessage(*Message) error 61 // EnqueueHPPacket is similar to EnqueueHPMessage, but accepts a slice of 62 // message(s) bytes. 63 EnqueueHPPacket([]byte) error 64 LastBlockIndex() uint32 65 Handshaked() bool 66 IsFullNode() bool 67 68 // SetPingTimer adds an outgoing ping to the counter and sets a PingTimeout 69 // timer that will shut the connection down in case of no response. 70 SetPingTimer() 71 // SendVersion checks handshake status and sends a version message to 72 // the peer. 73 SendVersion() error 74 SendVersionAck(*Message) error 75 // StartProtocol is a goroutine to be run after the handshake. It 76 // implements basic peer-related protocol handling. 77 StartProtocol() 78 HandleVersion(*payload.Version) error 79 HandleVersionAck() error 80 81 // HandlePing checks ping contents against Peer's state and updates it. 82 HandlePing(ping *payload.Ping) error 83 84 // HandlePong checks pong contents against Peer's state and updates it. 85 HandlePong(pong *payload.Ping) error 86 87 // AddGetAddrSent is to inform local peer context that a getaddr command 88 // is sent. The decision to send getaddr is server-wide, but it needs to be 89 // accounted for in peer's context, thus this method. 90 AddGetAddrSent() 91 92 // CanProcessAddr checks whether an addr command is expected to come from 93 // this peer and can be processed. 94 CanProcessAddr() bool 95 }