mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Moving FeatureSupport() and UserBlockchain.Header() calls out of Message Encoding.
This paves the way for refactoring the message code into the protocol package.
This commit is contained in:
@@ -207,7 +207,8 @@ func autoMulticastBroadcast() {
|
||||
// contactArbitraryPeer contacts a new arbitrary peer for the first time.
|
||||
func contactArbitraryPeer(publicKey *btcec.PublicKey, address *net.UDPAddr, receiverPortInternal uint16) (contacted bool) {
|
||||
findSelf := ShouldSendFindSelf()
|
||||
packets := msgEncodeAnnouncement(true, findSelf, nil, nil, nil)
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
packets := msgEncodeAnnouncement(true, findSelf, nil, nil, nil, FeatureSupport(), blockchainHeight, blockchainVersion)
|
||||
if len(packets) == 0 || packets[0].err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -478,16 +478,6 @@ func isPacketSizeExceed(currentSize int, testSize int) bool {
|
||||
return currentSize+testSize > udpMaxPacketSize-protocol.PacketLengthMin
|
||||
}
|
||||
|
||||
func FeatureSupport() (feature byte) {
|
||||
if countListen4 > 0 {
|
||||
feature |= 1 << FeatureIPv4Listen
|
||||
}
|
||||
if countListen6 > 0 {
|
||||
feature |= 1 << FeatureIPv6Listen
|
||||
}
|
||||
return feature
|
||||
}
|
||||
|
||||
// announcementPacket contains information about a single announcement message
|
||||
type announcementPacket struct {
|
||||
raw []byte // The raw packet
|
||||
@@ -500,7 +490,7 @@ type announcementPacket struct {
|
||||
// findPeer is a list of node IDs (blake3 hash of peer ID compressed form)
|
||||
// findValue is a list of hashes
|
||||
// files is a list of files stored to inform about
|
||||
func msgEncodeAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore) (packets []*announcementPacket) {
|
||||
func msgEncodeAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore, features byte, blockchainHeight, blockchainVersion uint64) (packets []*announcementPacket) {
|
||||
createPacketLoop:
|
||||
for {
|
||||
packet := &announcementPacket{}
|
||||
@@ -510,10 +500,9 @@ createPacketLoop:
|
||||
packetSize := announcementPayloadHeaderSize
|
||||
|
||||
raw[0] = byte(ProtocolVersion) // Protocol
|
||||
raw[1] = FeatureSupport() // Feature support
|
||||
raw[1] = features // Feature support
|
||||
//raw[2] = Actions // Action bit array
|
||||
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
binary.LittleEndian.PutUint32(raw[3:7], uint32(blockchainHeight))
|
||||
binary.LittleEndian.PutUint64(raw[7:15], blockchainVersion)
|
||||
|
||||
@@ -641,7 +630,7 @@ const EmbeddedFileSizeMax = udpMaxPacketSize - protocol.PacketLengthMin - announ
|
||||
|
||||
// msgEncodeResponse encodes a response message
|
||||
// hash2Peers will be modified.
|
||||
func msgEncodeResponse(sendUA bool, hash2Peers []Hash2Peer, filesEmbed []EmbeddedFileData, hashesNotFound [][]byte) (packetsRaw [][]byte, err error) {
|
||||
func msgEncodeResponse(sendUA bool, hash2Peers []Hash2Peer, filesEmbed []EmbeddedFileData, hashesNotFound [][]byte, features byte, blockchainHeight, blockchainVersion uint64) (packetsRaw [][]byte, err error) {
|
||||
for n := range filesEmbed {
|
||||
if len(filesEmbed[n].Data) > EmbeddedFileSizeMax {
|
||||
return nil, errors.New("embedded file too big")
|
||||
@@ -654,10 +643,9 @@ createPacketLoop:
|
||||
packetSize := announcementPayloadHeaderSize
|
||||
|
||||
raw[0] = byte(ProtocolVersion) // Protocol
|
||||
raw[1] = FeatureSupport() // Feature support
|
||||
raw[1] = features // Feature support
|
||||
//raw[2] = Actions // Action bit array
|
||||
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
binary.LittleEndian.PutUint32(raw[3:7], uint32(blockchainHeight))
|
||||
binary.LittleEndian.PutUint64(raw[7:15], blockchainVersion)
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@ func (peer *PeerInfo) Chat(text string) {
|
||||
|
||||
// sendAnnouncement sends the announcement message. It acquires a new sequence for each message.
|
||||
func (peer *PeerInfo) sendAnnouncement(sendUA, findSelf bool, findPeer []KeyHash, findValue []KeyHash, files []InfoStore, sequenceData interface{}) (packets []*announcementPacket) {
|
||||
packets = msgEncodeAnnouncement(sendUA, findSelf, findPeer, findValue, files)
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
packets = msgEncodeAnnouncement(sendUA, findSelf, findPeer, findValue, files, FeatureSupport(), blockchainHeight, blockchainVersion)
|
||||
|
||||
for _, packet := range packets {
|
||||
packet.sequence = peer.msgNewSequence(sequenceData)
|
||||
@@ -47,7 +48,8 @@ func (peer *PeerInfo) sendAnnouncement(sendUA, findSelf bool, findPeer []KeyHash
|
||||
|
||||
// sendResponse sends the response message
|
||||
func (peer *PeerInfo) sendResponse(sequence uint32, sendUA bool, hash2Peers []Hash2Peer, filesEmbed []EmbeddedFileData, hashesNotFound [][]byte) (err error) {
|
||||
packets, err := msgEncodeResponse(sendUA, hash2Peers, filesEmbed, hashesNotFound)
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
packets, err := msgEncodeResponse(sendUA, hash2Peers, filesEmbed, hashesNotFound, FeatureSupport(), blockchainHeight, blockchainVersion)
|
||||
|
||||
for _, packet := range packets {
|
||||
raw := &protocol.PacketRaw{Command: protocol.CommandResponse, Payload: packet, Sequence: sequence}
|
||||
|
||||
@@ -90,7 +90,8 @@ func (network *Network) BroadcastIPv4Listen() {
|
||||
|
||||
// BroadcastIPv4Send sends out a single broadcast messages to discover peers
|
||||
func (network *Network) BroadcastIPv4Send() (err error) {
|
||||
packets := msgEncodeAnnouncement(true, true, nil, nil, nil)
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
packets := msgEncodeAnnouncement(true, true, nil, nil, nil, FeatureSupport(), blockchainHeight, blockchainVersion)
|
||||
if len(packets) == 0 || packets[0].err != nil {
|
||||
return packets[0].err
|
||||
}
|
||||
|
||||
@@ -136,7 +136,8 @@ func (network *Network) MulticastIPv6Listen() {
|
||||
|
||||
// MulticastIPv6Send sends out a single multicast messages to discover peers at the same site
|
||||
func (network *Network) MulticastIPv6Send() (err error) {
|
||||
packets := msgEncodeAnnouncement(true, true, nil, nil, nil)
|
||||
_, blockchainHeight, blockchainVersion := UserBlockchain.Header()
|
||||
packets := msgEncodeAnnouncement(true, true, nil, nil, nil, FeatureSupport(), blockchainHeight, blockchainVersion)
|
||||
if len(packets) == 0 || packets[0].err != nil {
|
||||
return packets[0].err
|
||||
}
|
||||
|
||||
11
Network.go
11
Network.go
@@ -341,3 +341,14 @@ func (network *Network) SelfReportedPorts() (portI, portE uint16) {
|
||||
|
||||
return portI, portE
|
||||
}
|
||||
|
||||
// FeatureSupport returns supported features by this peer
|
||||
func FeatureSupport() (feature byte) {
|
||||
if countListen4 > 0 {
|
||||
feature |= 1 << FeatureIPv4Listen
|
||||
}
|
||||
if countListen6 > 0 {
|
||||
feature |= 1 << FeatureIPv6Listen
|
||||
}
|
||||
return feature
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestMessageEncodingAnnouncement(t *testing.T) {
|
||||
findPeer = append(findPeer, KeyHash{Hash: hash1})
|
||||
findValue = append(findValue, KeyHash{Hash: hash2})
|
||||
|
||||
packets := msgEncodeAnnouncement(true, true, findPeer, findValue, files)
|
||||
packets := msgEncodeAnnouncement(true, true, findPeer, findValue, files, 1<<FeatureIPv4Listen|1<<FeatureIPv6Listen, 0, 0)
|
||||
|
||||
msg := &MessageRaw{PacketRaw: packetR, SenderPublicKey: publicKey}
|
||||
msg.Payload = packets[0].raw
|
||||
@@ -64,7 +64,7 @@ func TestMessageEncodingResponse(t *testing.T) {
|
||||
|
||||
hashesNotFound = append(hashesNotFound, protocol.HashData([]byte("NA")))
|
||||
|
||||
packetsRaw, err := msgEncodeResponse(true, hash2Peers, filesEmbed, hashesNotFound)
|
||||
packetsRaw, err := msgEncodeResponse(true, hash2Peers, filesEmbed, hashesNotFound, 1<<FeatureIPv4Listen|1<<FeatureIPv6Listen, 0, 0)
|
||||
if err != nil {
|
||||
fmt.Printf("Error msgEncodeAnnouncement: %s\n", err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user