diff --git a/Bootstrap.go b/Bootstrap.go index db79321..5d76400 100644 --- a/Bootstrap.go +++ b/Bootstrap.go @@ -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 } diff --git a/Message Encoding.go b/Message Encoding.go index 085096c..0b7da00 100644 --- a/Message Encoding.go +++ b/Message Encoding.go @@ -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) diff --git a/Message Send.go b/Message Send.go index 925381a..5909170 100644 --- a/Message Send.go +++ b/Message Send.go @@ -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} diff --git a/Network IPv4 Broadcast.go b/Network IPv4 Broadcast.go index 4f5787f..60964d2 100644 --- a/Network IPv4 Broadcast.go +++ b/Network IPv4 Broadcast.go @@ -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 } diff --git a/Network IPv6 Multicast.go b/Network IPv6 Multicast.go index beba9d3..b7d8c49 100644 --- a/Network IPv6 Multicast.go +++ b/Network IPv6 Multicast.go @@ -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 } diff --git a/Network.go b/Network.go index ebc83f5..d58a6aa 100644 --- a/Network.go +++ b/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 +} diff --git a/Test_test.go b/Test_test.go index 63c36f0..feee537 100644 --- a/Test_test.go +++ b/Test_test.go @@ -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<