@@ -48,10 +48,12 @@ pub struct Hypercore {
4848 pub ( crate ) bitfield : Bitfield ,
4949 skip_flush_count : u8 , // autoFlush in Javascript
5050 header : Header ,
51+ #[ cfg( feature = "replication" ) ]
52+ events : crate :: replication:: events:: Events ,
5153}
5254
5355/// Response from append, matches that of the Javascript result
54- #[ derive( Debug ) ]
56+ #[ derive( Debug , PartialEq ) ]
5557pub struct AppendOutcome {
5658 /// Length of the hypercore after append
5759 pub length : u64 ,
@@ -60,7 +62,7 @@ pub struct AppendOutcome {
6062}
6163
6264/// Info about the hypercore
63- #[ derive( Debug ) ]
65+ #[ derive( Debug , PartialEq ) ]
6466pub struct Info {
6567 /// Length of the hypercore
6668 pub length : u64 ,
@@ -247,6 +249,8 @@ impl Hypercore {
247249 bitfield,
248250 header,
249251 skip_flush_count : 0 ,
252+ #[ cfg( feature = "replication" ) ]
253+ events : crate :: replication:: events:: Events :: new ( ) ,
250254 } )
251255 }
252256
@@ -321,6 +325,14 @@ impl Hypercore {
321325 if self . should_flush_bitfield_and_tree_and_oplog ( ) {
322326 self . flush_bitfield_and_tree_and_oplog ( false ) . await ?;
323327 }
328+
329+ #[ cfg( feature = "replication" ) ]
330+ {
331+ let _ = self . events . send ( crate :: replication:: events:: DataUpgrade { } ) ;
332+ let _ = self
333+ . events
334+ . send ( crate :: replication:: events:: Have :: from ( & bitfield_update) ) ;
335+ }
324336 }
325337
326338 // Return the new value
@@ -330,10 +342,27 @@ impl Hypercore {
330342 } )
331343 }
332344
345+ #[ cfg( feature = "replication" ) ]
346+ /// Subscribe to core events relevant to replication
347+ pub fn event_subscribe ( & self ) -> async_broadcast:: Receiver < crate :: replication:: events:: Event > {
348+ self . events . channel . new_receiver ( )
349+ }
350+
351+ /// Check if core has the block at the given `index` locally
352+ #[ instrument( ret, skip( self ) ) ]
353+ pub fn has ( & self , index : u64 ) -> bool {
354+ self . bitfield . get ( index)
355+ }
356+
333357 /// Read value at given index, if any.
334358 #[ instrument( err, skip( self ) ) ]
335359 pub async fn get ( & mut self , index : u64 ) -> Result < Option < Vec < u8 > > , HypercoreError > {
336360 if !self . bitfield . get ( index) {
361+ #[ cfg( feature = "replication" ) ]
362+ // if not in this core, emit Event::Get(index)
363+ {
364+ self . events . send_on_get ( index) ;
365+ }
337366 return Ok ( None ) ;
338367 }
339368
@@ -522,12 +551,12 @@ impl Hypercore {
522551 self . storage . flush_infos ( & outcome. infos_to_flush ) . await ?;
523552 self . header = outcome. header ;
524553
525- if let Some ( bitfield_update) = bitfield_update {
554+ if let Some ( bitfield_update) = & bitfield_update {
526555 // Write to bitfield
527- self . bitfield . update ( & bitfield_update) ;
556+ self . bitfield . update ( bitfield_update) ;
528557
529558 // Contiguous length is known only now
530- update_contiguous_length ( & mut self . header , & self . bitfield , & bitfield_update) ;
559+ update_contiguous_length ( & mut self . header , & self . bitfield , bitfield_update) ;
531560 }
532561
533562 // Commit changeset to in-memory tree
@@ -537,6 +566,21 @@ impl Hypercore {
537566 if self . should_flush_bitfield_and_tree_and_oplog ( ) {
538567 self . flush_bitfield_and_tree_and_oplog ( false ) . await ?;
539568 }
569+
570+ #[ cfg( feature = "replication" ) ]
571+ {
572+ if proof. upgrade . is_some ( ) {
573+ // Notify replicator if we receieved an upgrade
574+ let _ = self . events . send ( crate :: replication:: events:: DataUpgrade { } ) ;
575+ }
576+
577+ // Notify replicator if we receieved a bitfield update
578+ if let Some ( ref bitfield) = bitfield_update {
579+ let _ = self
580+ . events
581+ . send ( crate :: replication:: events:: Have :: from ( bitfield) ) ;
582+ }
583+ }
540584 Ok ( true )
541585 }
542586
@@ -725,7 +769,7 @@ fn update_contiguous_length(
725769}
726770
727771#[ cfg( test) ]
728- mod tests {
772+ pub ( crate ) mod tests {
729773 use super :: * ;
730774
731775 #[ async_std:: test]
@@ -1091,7 +1135,9 @@ mod tests {
10911135 Ok ( ( ) )
10921136 }
10931137
1094- async fn create_hypercore_with_data ( length : u64 ) -> Result < Hypercore , HypercoreError > {
1138+ pub ( crate ) async fn create_hypercore_with_data (
1139+ length : u64 ,
1140+ ) -> Result < Hypercore , HypercoreError > {
10951141 let signing_key = generate_signing_key ( ) ;
10961142 create_hypercore_with_data_and_key_pair (
10971143 length,
@@ -1103,7 +1149,7 @@ mod tests {
11031149 . await
11041150 }
11051151
1106- async fn create_hypercore_with_data_and_key_pair (
1152+ pub ( crate ) async fn create_hypercore_with_data_and_key_pair (
11071153 length : u64 ,
11081154 key_pair : PartialKeypair ,
11091155 ) -> Result < Hypercore , HypercoreError > {
0 commit comments