Skip to content

Commit

Permalink
Set NegotiatedVersion on Peer Object (#1334)
Browse files Browse the repository at this point in the history
* Set NegotiatedVersion on Peer Object

* Fix naming
  • Loading branch information
tholonious committed May 17, 2024
1 parent 604777d commit a66a597
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type DeSoMessageMeta struct {
// Any communication with other nodes happens via this object, which maintains a
// queue of messages to send to the other node.
type Peer struct {
// TODO: Remove this and merge it with the RemoteNode.HandshakeMetaData type
NegotiatedProtocolVersion ProtocolVersionType

// These stats should be accessed atomically.
bytesReceived uint64
bytesSent uint64
Expand Down Expand Up @@ -401,7 +404,7 @@ func (pp *Peer) HandleGetBlocks(msg *MsgDeSoGetBlocks) {
// Before Version2 we would send each block in a single message, which was quite
// slow. Now when we receive a GetBlocks message we will send the blocks in large
// batches, which is much faster.
if pp.Params.ProtocolVersion == ProtocolVersion2 {
if pp.NegotiatedProtocolVersion == ProtocolVersion2 {
allBlocks := MsgDeSoBlockBundle{}
for _, hashToSend := range msg.HashList {
blockToSend := pp.srv.blockchain.GetBlock(hashToSend)
Expand Down Expand Up @@ -931,6 +934,13 @@ func (pp *Peer) SetServiceFlag(sf ServiceFlag) {
pp.serviceFlags = sf
}

func (pp *Peer) SetNegotiatedProtocolVersion(negotiatedProtocolVersion ProtocolVersionType) {
pp.PeerInfoMtx.Lock()
defer pp.PeerInfoMtx.Unlock()

pp.NegotiatedProtocolVersion = negotiatedProtocolVersion
}

func (pp *Peer) outHandler() {
pp.startGroup.Done()
glog.V(1).Infof("Peer.outHandler: Starting outHandler for Peer %v", pp)
Expand Down
5 changes: 5 additions & 0 deletions lib/remote_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,12 @@ func (rn *RemoteNode) HandleVersionMessage(verMsg *MsgDeSoVersion, responseNonce
negotiatedVersion = NewProtocolVersionType(verMsg.Version)
}

// TODO: everywhere we update the negotiated protocol version on the handshake metadata, we also update
// the value in the peer object. The two should be merged to reduce duplication of data in the future.
vMeta.negotiatedProtocolVersion = negotiatedVersion
if rn.peer != nil {
rn.peer.SetNegotiatedProtocolVersion(negotiatedVersion)
}

// Record the services the peer is advertising.
vMeta.serviceFlag = verMsg.Services
Expand Down

0 comments on commit a66a597

Please sign in to comment.