mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00

* [NOD-1120] Removed closure in NetAdapter.onConnectedHanlder * [NOD-1120] Implement all connection manager methods * [NOD-1120] Integrated connmanager into kaspad + added call for dnsseeder * [NOD-1120] Allow buffer to not be bytes.Buffer * [NOD-1120] Added timeout to connect * [NOD-1120] Don't enter connections to add loop if none needed * [NOD-1120] Add call for addressManager.Good * [NOD-1120] Minor bug fixes * [NOD-1120] Remove errChan from grpcConnection * [NOD-1120] Add comments to exported methods * [NOD-1120] cancel the context for DialContext in gRPCServer.Connect * [NOD-1120] Don't try to remove from connSet a connection that doesn't exist * [NOD-1120] add ok bool to connectionSet.get * [NOD-1120] Remove overuse of if-else in checkConnectionRequests * [NOD-1120] Made some order in ConnectionManager * [NOD-1120] Moved checkIncomingConnections to it's own file * [NOD-1120] cleanup in checkOutgoingConnections * [NOD-1120] Cleanup in SeedDNS, and move call outside of connection manager * [NOD-1120] Add check that both --connect and --addpeer aren't used * [NOD-1120] Move dial timeout to constant * [NOD-1120] Enhance comment * [NOD-1120] Log connection failure out of initiateConnection * [NOD-1148] Reshuffle checkRequestedConnections to make more sense * [NOD-1120] Move continue to correct place + reshuffle logging code * [NOD-1120] Don't expose server.Connection outside netAdapter - expose a wrapper instead * [NOD-1120] Add comments * [NOD-1120] Don't return the connection from netAdapter.Connect() * [NOD-1120] Use .Address as key for connectionSet * [NOD-1120] Fix minRetryDuration usage * [NOD-1120] Remove the correct number of incoming connections * [NOD-1120] Add comment * [NOD-1120] Rename connSet -> incomingConnectionSet * [NOD-1120] fix grammar
173 lines
4.5 KiB
Go
173 lines
4.5 KiB
Go
package peer
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/netadapter/id"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
mathUtil "github.com/kaspanet/kaspad/util/math"
|
|
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Peer holds data about a peer.
|
|
type Peer struct {
|
|
ready uint32
|
|
|
|
selectedTipHashMtx sync.RWMutex
|
|
selectedTipHash *daghash.Hash
|
|
|
|
id *id.ID
|
|
userAgent string
|
|
services wire.ServiceFlag
|
|
advertisedProtocolVer uint32 // protocol version advertised by remote
|
|
protocolVersion uint32 // negotiated protocol version
|
|
disableRelayTx bool
|
|
subnetworkID *subnetworkid.SubnetworkID
|
|
|
|
pingLock sync.RWMutex
|
|
lastPingNonce uint64 // The nonce of the last ping we sent
|
|
lastPingTime time.Time // Time we sent last ping
|
|
lastPingDuration time.Duration // Time for last ping to return
|
|
}
|
|
|
|
// SelectedTipHash returns the selected tip of the peer.
|
|
func (p *Peer) SelectedTipHash() (*daghash.Hash, error) {
|
|
if atomic.LoadUint32(&p.ready) == 0 {
|
|
return nil, errors.New("peer is not ready yet")
|
|
}
|
|
p.selectedTipHashMtx.RLock()
|
|
defer p.selectedTipHashMtx.RUnlock()
|
|
return p.selectedTipHash, nil
|
|
}
|
|
|
|
// SetSelectedTipHash sets the selected tip of the peer.
|
|
func (p *Peer) SetSelectedTipHash(hash *daghash.Hash) error {
|
|
if atomic.LoadUint32(&p.ready) == 0 {
|
|
return errors.New("peer is not ready yet")
|
|
}
|
|
p.selectedTipHashMtx.Lock()
|
|
defer p.selectedTipHashMtx.Unlock()
|
|
p.selectedTipHash = hash
|
|
return nil
|
|
}
|
|
|
|
// SubnetworkID returns the subnetwork the peer is associated with.
|
|
// It is nil in full nodes.
|
|
func (p *Peer) SubnetworkID() (*subnetworkid.SubnetworkID, error) {
|
|
if atomic.LoadUint32(&p.ready) == 0 {
|
|
return nil, errors.New("peer is not ready yet")
|
|
}
|
|
return p.subnetworkID, nil
|
|
}
|
|
|
|
// ID returns the peer ID.
|
|
func (p *Peer) ID() (*id.ID, error) {
|
|
if atomic.LoadUint32(&p.ready) == 0 {
|
|
return nil, errors.New("peer is not ready yet")
|
|
}
|
|
return p.id, nil
|
|
}
|
|
|
|
// MarkAsReady marks the peer as ready.
|
|
func (p *Peer) MarkAsReady() error {
|
|
if atomic.AddUint32(&p.ready, 1) != 1 {
|
|
return errors.New("peer is already ready")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateFieldsFromMsgVersion updates the peer with the data from the version message.
|
|
func (p *Peer) UpdateFieldsFromMsgVersion(msg *wire.MsgVersion) {
|
|
// Negotiate the protocol version.
|
|
p.advertisedProtocolVer = msg.ProtocolVersion
|
|
p.protocolVersion = mathUtil.MinUint32(p.protocolVersion, p.advertisedProtocolVer)
|
|
log.Debugf("Negotiated protocol version %d for peer %s",
|
|
p.protocolVersion, p.id)
|
|
|
|
// Set the peer's ID.
|
|
p.id = msg.ID
|
|
|
|
// Set the supported services for the peer to what the remote peer
|
|
// advertised.
|
|
p.services = msg.Services
|
|
|
|
// Set the remote peer's user agent.
|
|
p.userAgent = msg.UserAgent
|
|
|
|
p.disableRelayTx = msg.DisableRelayTx
|
|
p.selectedTipHash = msg.SelectedTipHash
|
|
p.subnetworkID = msg.SubnetworkID
|
|
}
|
|
|
|
// SetPingPending sets the ping state of the peer to 'pending'
|
|
func (p *Peer) SetPingPending(nonce uint64) {
|
|
p.pingLock.Lock()
|
|
defer p.pingLock.Unlock()
|
|
|
|
p.lastPingNonce = nonce
|
|
p.lastPingTime = time.Now()
|
|
}
|
|
|
|
// SetPingIdle sets the ping state of the peer to 'idle'
|
|
func (p *Peer) SetPingIdle() {
|
|
p.pingLock.Lock()
|
|
defer p.pingLock.Unlock()
|
|
|
|
p.lastPingNonce = 0
|
|
p.lastPingDuration = time.Since(p.lastPingTime)
|
|
}
|
|
|
|
func (p *Peer) String() string {
|
|
//TODO(libp2p)
|
|
panic("unimplemented")
|
|
}
|
|
|
|
var (
|
|
readyPeers = make(map[*id.ID]*Peer, 0)
|
|
readyPeersMutex sync.RWMutex
|
|
)
|
|
|
|
// ErrPeerWithSameIDExists signifies that a peer with the same ID already exist.
|
|
var ErrPeerWithSameIDExists = errors.New("ready with the same ID already exists")
|
|
|
|
// AddToReadyPeers marks this peer as ready and adds it to the ready peers list.
|
|
func AddToReadyPeers(peer *Peer) error {
|
|
readyPeersMutex.RLock()
|
|
defer readyPeersMutex.RUnlock()
|
|
|
|
if _, ok := readyPeers[peer.id]; ok {
|
|
return errors.Wrapf(ErrPeerWithSameIDExists, "peer with ID %s already exists", peer.id)
|
|
}
|
|
|
|
err := peer.MarkAsReady()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
readyPeers[peer.id] = peer
|
|
return nil
|
|
}
|
|
|
|
// GetReadyPeerIDs returns the peer IDs of all the ready peers.
|
|
func GetReadyPeerIDs() []*id.ID {
|
|
readyPeersMutex.RLock()
|
|
defer readyPeersMutex.RUnlock()
|
|
peerIDs := make([]*id.ID, len(readyPeers))
|
|
i := 0
|
|
for peerID := range readyPeers {
|
|
peerIDs[i] = peerID
|
|
i++
|
|
}
|
|
return peerIDs
|
|
}
|
|
|
|
// IDExists returns whether there's a peer with the given ID.
|
|
func IDExists(peerID *id.ID) bool {
|
|
_, ok := readyPeers[peerID]
|
|
return ok
|
|
}
|