77 "math/bits"
88
99 partialmessages "github.com/libp2p/go-libp2p-pubsub/partialmessages"
10- "github.com/libp2p/go-libp2p/core/peer"
1110)
1211
1312const partLen = 1024
@@ -17,6 +16,17 @@ type PartialMessage struct {
1716 parts [8 ][]byte // each part is partLen sized or nil if empty
1817}
1918
19+ // PartsMetadata implements partialmessages.PartialMessage.
20+ func (p * PartialMessage ) PartsMetadata () []byte {
21+ out := []byte {0 }
22+ for i := range p .parts {
23+ if len (p .parts [i ]) > 0 {
24+ out [0 ] |= 1 << i
25+ }
26+ }
27+ return out
28+ }
29+
2030// FillParts is used to initialize this PartialMessage for testing by filling in
2131// the parts it should have. The algorithm is simple:
2232// - treat the groupID as our starting uint64 number BigEndian
@@ -38,32 +48,11 @@ func (p *PartialMessage) FillParts(bitmap byte) error {
3848 return nil
3949}
4050
41- // AvailableParts implements partialmessages.PartialMessage.
42- func (p * PartialMessage ) AvailableParts () ([]byte , error ) {
43- out := []byte {0 }
44- for i := range p .parts {
45- if len (p .parts [i ]) > 0 {
46- out [0 ] |= 1 << i
47- }
48- }
49- return out , nil
50- }
51-
5251// GroupID implements partialmessages.PartialMessage.
5352func (p * PartialMessage ) GroupID () []byte {
5453 return p .groupID [:]
5554}
5655
57- // MissingParts implements partialmessages.PartialMessage.
58- func (p * PartialMessage ) MissingParts () ([]byte , error ) {
59- b , _ := p .AvailableParts ()
60- b [0 ] = ^ b [0 ]
61- if b [0 ] == 0 {
62- return nil , nil
63- }
64- return b , nil
65- }
66-
6756func (p * PartialMessage ) Extend (data []byte ) error {
6857 if len (data ) < 1 + len (p .groupID ) {
6958 return errors .New ("invalid data length" )
@@ -98,12 +87,8 @@ func (p *PartialMessage) Extend(data []byte) error {
9887 return nil
9988}
10089
101- // PartialMessageBytesFromMetadata implements partialmessages.PartialMessage.
102- func (p * PartialMessage ) PartialMessageBytesFromMetadata (metadata []byte ) ([]byte , []byte , error ) {
103- if len (metadata ) == 0 {
104- // Treat this as the same as a request for all parts
105- metadata = []byte {0xff }
106- }
90+ // PartialMessageBytes implements partialmessages.PartialMessage.
91+ func (p * PartialMessage ) PartialMessageBytes (metadata []byte ) ([]byte , []byte , error ) {
10792 if len (metadata ) != 1 {
10893 return nil , nil , errors .New ("invalid metadata length" )
10994 }
@@ -112,7 +97,8 @@ func (p *PartialMessage) PartialMessageBytesFromMetadata(metadata []byte) ([]byt
11297 out = append (out , 0 ) // This byte will contain the parts we are including in the message
11398 remaining := []byte {metadata [0 ]}
11499 for i := range p .parts {
115- if metadata [0 ]& (1 << i ) == 0 {
100+ if metadata [0 ]& (1 << i ) != 0 {
101+ // They already have this part
116102 continue
117103 }
118104 if len (p .parts [i ]) == 0 {
@@ -133,13 +119,4 @@ func (p *PartialMessage) PartialMessageBytesFromMetadata(metadata []byte) ([]byt
133119 return out , remaining , nil
134120}
135121
136- // ShouldRequest implements partialmessages.PartialMessage.
137- func (p * PartialMessage ) ShouldRequest (_ peer.ID , peerHasMetadata []byte ) bool {
138- wants , _ := p .MissingParts ()
139- if len (wants ) == 0 || len (peerHasMetadata ) == 0 {
140- return false
141- }
142- return wants [0 ]& peerHasMetadata [0 ] != 0
143- }
144-
145122var _ partialmessages.PartialMessage = (* PartialMessage )(nil )
0 commit comments