@@ -22,22 +22,72 @@ describe('BitswapMessage', () => {
22
22
let cids
23
23
24
24
before ( async ( ) => {
25
- blocks = await makeBlock ( 3 )
25
+ blocks = await makeBlock ( 4 )
26
26
cids = blocks . map ( ( b ) => b . cid )
27
27
} )
28
28
29
- it ( '.addEntry - want block' , ( ) => {
30
- const cid = cids [ 1 ]
31
- const msg = new BitswapMessage ( true )
32
- msg . addEntry ( cid , 1 )
33
- const serialized = msg . serializeToBitswap100 ( )
29
+ describe ( '.addEntry' , ( ) => {
30
+ it ( 'want type defaults to want block' , ( ) => {
31
+ const cid = cids [ 1 ]
32
+ const msg = new BitswapMessage ( true )
33
+ msg . addEntry ( cid , 1 )
34
+ const serialized = msg . serializeToBitswap100 ( )
35
+
36
+ expect ( pbm . Message . decode ( serialized ) . wantlist . entries [ 0 ] ) . to . be . eql ( {
37
+ block : cid . buffer ,
38
+ priority : 1 ,
39
+ cancel : false ,
40
+ sendDontHave : false ,
41
+ wantType : pbm . Message . Wantlist . WantType . Block
42
+ } )
43
+ } )
44
+
45
+ it ( 'updates priority only if same want type' , ( ) => {
46
+ const msg = new BitswapMessage ( true )
47
+
48
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , false , false )
49
+
50
+ msg . addEntry ( cids [ 0 ] , 2 , BitswapMessage . WantType . Have , true , false )
51
+ expect ( msg . wantlist . get ( cids [ 0 ] . toString ( 'base58btc' ) ) . priority ) . to . eql ( 1 )
34
52
35
- expect ( pbm . Message . decode ( serialized ) . wantlist . entries [ 0 ] ) . to . be . eql ( {
36
- block : cid . buffer ,
37
- priority : 1 ,
38
- cancel : false ,
39
- sendDontHave : false ,
40
- wantType : pbm . Message . Wantlist . WantType . Block
53
+ msg . addEntry ( cids [ 0 ] , 2 , BitswapMessage . WantType . Block , true , false )
54
+ expect ( msg . wantlist . get ( cids [ 0 ] . toString ( 'base58btc' ) ) . priority ) . to . eql ( 2 )
55
+ } )
56
+
57
+ it ( 'only changes from dont cancel to do cancel' , ( ) => {
58
+ const msg = new BitswapMessage ( true )
59
+
60
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , true , false )
61
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , false , false )
62
+ expect ( msg . wantlist . get ( cids [ 0 ] . toString ( 'base58btc' ) ) . cancel ) . to . eql ( true )
63
+
64
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Block , false , false )
65
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Block , true , false )
66
+ expect ( msg . wantlist . get ( cids [ 1 ] . toString ( 'base58btc' ) ) . cancel ) . to . eql ( true )
67
+ } )
68
+
69
+ it ( 'only changes from dont send to do send DONT_HAVE' , ( ) => {
70
+ const msg = new BitswapMessage ( true )
71
+
72
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , false , false )
73
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , false , true )
74
+ expect ( msg . wantlist . get ( cids [ 0 ] . toString ( 'base58btc' ) ) . sendDontHave ) . to . eql ( true )
75
+
76
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Block , false , true )
77
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Block , false , false )
78
+ expect ( msg . wantlist . get ( cids [ 1 ] . toString ( 'base58btc' ) ) . sendDontHave ) . to . eql ( true )
79
+ } )
80
+
81
+ it ( 'only override want-have with want-block (not vice versa)' , ( ) => {
82
+ const msg = new BitswapMessage ( true )
83
+
84
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Block , false , false )
85
+ msg . addEntry ( cids [ 0 ] , 1 , BitswapMessage . WantType . Have , false , false )
86
+ expect ( msg . wantlist . get ( cids [ 0 ] . toString ( 'base58btc' ) ) . wantType ) . to . eql ( BitswapMessage . WantType . Block )
87
+
88
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Have , false , false )
89
+ msg . addEntry ( cids [ 1 ] , 1 , BitswapMessage . WantType . Block , false , false )
90
+ expect ( msg . wantlist . get ( cids [ 1 ] . toString ( 'base58btc' ) ) . wantType ) . to . eql ( BitswapMessage . WantType . Block )
41
91
} )
42
92
} )
43
93
@@ -54,12 +104,27 @@ describe('BitswapMessage', () => {
54
104
const msg = new BitswapMessage ( true )
55
105
msg . addBlock ( block )
56
106
msg . setPendingBytes ( 10 )
107
+ msg . addEntry ( cids [ 0 ] , 10 , BitswapMessage . WantType . Have , false , true )
108
+ msg . addHave ( cids [ 1 ] )
109
+ msg . addDontHave ( cids [ 2 ] )
57
110
58
111
const serialized = msg . serializeToBitswap110 ( )
59
112
const decoded = pbm . Message . decode ( serialized )
60
-
61
113
expect ( decoded . payload [ 0 ] . data ) . to . eql ( block . data )
62
114
expect ( decoded . pendingBytes ) . to . eql ( 10 )
115
+ expect ( decoded . wantlist . entries . length ) . to . eql ( 1 )
116
+ expect ( decoded . wantlist . entries [ 0 ] . priority ) . to . eql ( 10 )
117
+ expect ( decoded . wantlist . entries [ 0 ] . wantType ) . to . eql ( BitswapMessage . WantType . Have )
118
+ expect ( decoded . wantlist . entries [ 0 ] . cancel ) . to . eql ( false )
119
+ expect ( decoded . wantlist . entries [ 0 ] . sendDontHave ) . to . eql ( true )
120
+ expect ( decoded . blockPresences . length ) . to . eql ( 2 )
121
+ for ( const bp of decoded . blockPresences ) {
122
+ if ( bp . type === BitswapMessage . BlockPresenceType . Have ) {
123
+ expect ( bp . cid . equals ( cids [ 1 ] . buffer ) ) . to . eql ( true )
124
+ } else {
125
+ expect ( bp . cid . equals ( cids [ 2 ] . buffer ) ) . to . eql ( true )
126
+ }
127
+ }
63
128
} )
64
129
65
130
it ( '.deserialize a Bitswap100 Message' , async ( ) => {
@@ -104,6 +169,7 @@ describe('BitswapMessage', () => {
104
169
const cid0 = cids [ 0 ]
105
170
const cid1 = cids [ 1 ]
106
171
const cid2 = cids [ 2 ]
172
+ const cid3 = cids [ 3 ]
107
173
108
174
const b1 = blocks [ 1 ]
109
175
const b2 = blocks [ 2 ]
@@ -112,7 +178,9 @@ describe('BitswapMessage', () => {
112
178
wantlist : {
113
179
entries : [ {
114
180
block : cid0 . buffer ,
115
- cancel : false
181
+ cancel : false ,
182
+ wantType : BitswapMessage . WantType . Block ,
183
+ sendDontHave : true
116
184
} ] ,
117
185
full : true
118
186
} ,
@@ -123,6 +191,10 @@ describe('BitswapMessage', () => {
123
191
data : b2 . data ,
124
192
prefix : cid2 . prefix
125
193
} ] ,
194
+ blockPresences : [ {
195
+ cid : cid3 . buffer ,
196
+ type : BitswapMessage . BlockPresenceType . Have
197
+ } ] ,
126
198
pendingBytes : 10
127
199
} )
128
200
@@ -131,7 +203,7 @@ describe('BitswapMessage', () => {
131
203
expect ( Array . from ( msg . wantlist ) )
132
204
. to . eql ( [ [
133
205
cid0 . toString ( 'base58btc' ) ,
134
- new BitswapMessage . Entry ( cid0 , 0 , BitswapMessage . WantType . Block , false )
206
+ new BitswapMessage . Entry ( cid0 , 0 , BitswapMessage . WantType . Block , false , true )
135
207
] ] )
136
208
137
209
expect (
@@ -140,6 +212,13 @@ describe('BitswapMessage', () => {
140
212
[ cid1 . toString ( 'base58btc' ) , b1 . data ] ,
141
213
[ cid2 . toString ( 'base58btc' ) , b2 . data ]
142
214
] )
215
+
216
+ expect ( Array . from ( msg . blockPresences ) )
217
+ . to . eql ( [ [
218
+ cid3 . toString ( 'base58btc' ) ,
219
+ BitswapMessage . BlockPresenceType . Have
220
+ ] ] )
221
+
143
222
expect ( msg . pendingBytes ) . to . equal ( 10 )
144
223
} )
145
224
0 commit comments