@@ -82,43 +82,22 @@ struct BitfieldSubscriptionId(u64);
82
82
/// A pluggable bitfield subscription mechanism
83
83
pub trait BitfieldSubscription : std:: fmt:: Debug + Send + ' static {
84
84
/// Subscribe to a bitfield
85
- fn subscribe (
86
- & mut self ,
87
- peer : BitfieldPeer ,
88
- hash : Hash ,
89
- ) -> BoxStream < ' static , BitfieldSubscriptionEvent > ;
85
+ fn subscribe ( & mut self , peer : BitfieldPeer , hash : Hash ) -> BoxStream < ' static , BitfieldEvent > ;
90
86
}
91
87
92
88
/// A boxed bitfield subscription
93
89
pub type BoxedBitfieldSubscription = Box < dyn BitfieldSubscription > ;
94
90
95
- /// An event from a bitfield subscription
96
- #[ derive( Debug ) ]
97
- pub enum BitfieldSubscriptionEvent {
98
- /// Set the bitfield to the given ranges
99
- Bitfield {
100
- /// The entire bitfield
101
- ranges : ChunkRanges ,
102
- } ,
103
- /// Update the bitfield with the given ranges
104
- BitfieldUpdate {
105
- /// The ranges that were added
106
- added : ChunkRanges ,
107
- /// The ranges that were removed
108
- removed : ChunkRanges ,
109
- } ,
110
- }
111
-
112
91
/// Events from observing a local bitfield
113
92
#[ derive( Debug ) ]
114
- pub enum ObserveEvent {
115
- /// Set the bitfield to the given ranges
116
- Bitfield {
93
+ pub enum BitfieldEvent {
94
+ /// The full state of the bitfield
95
+ State {
117
96
/// The entire bitfield
118
97
ranges : ChunkRanges ,
119
98
} ,
120
- /// Update the bitfield with the given ranges
121
- BitfieldUpdate {
99
+ /// An update to the bitfield
100
+ Update {
122
101
/// The ranges that were added
123
102
added : ChunkRanges ,
124
103
/// The ranges that were removed
@@ -281,7 +260,7 @@ impl Downloader {
281
260
pub async fn observe (
282
261
& self ,
283
262
request : ObserveRequest ,
284
- ) -> anyhow:: Result < tokio:: sync:: mpsc:: Receiver < ObserveEvent > > {
263
+ ) -> anyhow:: Result < tokio:: sync:: mpsc:: Receiver < BitfieldEvent > > {
285
264
let ( send, recv) = tokio:: sync:: mpsc:: channel ( request. buffer ) ;
286
265
self . send
287
266
. send ( UserCommand :: Observe { request, send } )
@@ -332,19 +311,15 @@ impl Downloader {
332
311
struct TestBitfieldSubscription ;
333
312
334
313
impl BitfieldSubscription for TestBitfieldSubscription {
335
- fn subscribe (
336
- & mut self ,
337
- peer : BitfieldPeer ,
338
- _hash : Hash ,
339
- ) -> BoxStream < ' static , BitfieldSubscriptionEvent > {
314
+ fn subscribe ( & mut self , peer : BitfieldPeer , _hash : Hash ) -> BoxStream < ' static , BitfieldEvent > {
340
315
let ranges = match peer {
341
316
BitfieldPeer :: Local => ChunkRanges :: empty ( ) ,
342
317
BitfieldPeer :: Remote ( _) => {
343
318
ChunkRanges :: from ( ChunkNum ( 0 ) ..ChunkNum ( 1024 * 1024 * 1024 * 1024 ) )
344
319
}
345
320
} ;
346
321
Box :: pin (
347
- futures_lite:: stream:: once ( BitfieldSubscriptionEvent :: Bitfield { ranges } )
322
+ futures_lite:: stream:: once ( BitfieldEvent :: State { ranges } )
348
323
. chain ( futures_lite:: stream:: pending ( ) ) ,
349
324
)
350
325
}
@@ -389,11 +364,7 @@ async fn get_valid_ranges_remote(
389
364
}
390
365
391
366
impl < S : Store > BitfieldSubscription for SimpleBitfieldSubscription < S > {
392
- fn subscribe (
393
- & mut self ,
394
- peer : BitfieldPeer ,
395
- hash : Hash ,
396
- ) -> BoxStream < ' static , BitfieldSubscriptionEvent > {
367
+ fn subscribe ( & mut self , peer : BitfieldPeer , hash : Hash ) -> BoxStream < ' static , BitfieldEvent > {
397
368
let ( send, recv) = tokio:: sync:: oneshot:: channel ( ) ;
398
369
match peer {
399
370
BitfieldPeer :: Local => {
@@ -429,7 +400,7 @@ impl<S: Store> BitfieldSubscription for SimpleBitfieldSubscription<S> {
429
400
Ok ( ev) => ev,
430
401
Err ( _) => ChunkRanges :: empty ( ) ,
431
402
} ;
432
- BitfieldSubscriptionEvent :: Bitfield { ranges }
403
+ BitfieldEvent :: State { ranges }
433
404
}
434
405
. into_stream ( ) ,
435
406
)
@@ -549,7 +520,9 @@ mod tests {
549
520
#[ tokio:: test]
550
521
async fn test_valid_ranges ( ) -> TestResult < ( ) > {
551
522
let store = crate :: store:: mem:: Store :: new ( ) ;
552
- let tt = store. import_bytes ( vec ! [ 0u8 ; 1025 ] . into ( ) , crate :: BlobFormat :: Raw ) . await ?;
523
+ let tt = store
524
+ . import_bytes ( vec ! [ 0u8 ; 1025 ] . into ( ) , crate :: BlobFormat :: Raw )
525
+ . await ?;
553
526
let entry = store. get_mut ( tt. hash ( ) ) . await ?. unwrap ( ) ;
554
527
let valid = crate :: get:: db:: valid_ranges :: < crate :: store:: mem:: Store > ( & entry) . await ?;
555
528
assert ! ( valid == ChunkRanges :: from( ChunkNum ( 0 ) ..ChunkNum ( 2 ) ) ) ;
0 commit comments