@@ -6,7 +6,7 @@ use crate::{
6
6
metrics:: OverlayMetrics ,
7
7
storage:: PortalStorage ,
8
8
types:: {
9
- content_key:: OverlayContentKey ,
9
+ content_key:: { HistoryContentKey , OverlayContentKey } ,
10
10
messages:: {
11
11
Accept , ByteList , Content , CustomPayload , FindContent , FindNodes , Message , Nodes ,
12
12
Offer , Ping , Pong , ProtocolId , Request , Response , SszEnr ,
@@ -81,15 +81,15 @@ pub enum OverlayRequestError {
81
81
Discv5Error ( discv5:: RequestError ) ,
82
82
83
83
/// Error types resulting from building ACCEPT message
84
- #[ error( "Error while building accept message" ) ]
85
- AcceptError ( ssz_types :: Error ) ,
84
+ #[ error( "Error while building accept message: {0} " ) ]
85
+ AcceptError ( String ) ,
86
86
87
87
/// Error types resulting from building ACCEPT message
88
- #[ error( "Error while sending offer message" ) ]
88
+ #[ error( "Error while sending offer message: {0} " ) ]
89
89
OfferError ( String ) ,
90
90
91
91
/// uTP request error
92
- #[ error( "uTP request error" ) ]
92
+ #[ error( "uTP request error: {0} " ) ]
93
93
UtpError ( String ) ,
94
94
}
95
95
@@ -643,27 +643,42 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
643
643
}
644
644
}
645
645
646
- /// Attempts to build a `Accept` response for a `Offer` request.
646
+ /// Attempts to build a `Accept` response for an `Offer` request.
647
647
fn handle_offer ( & self , request : Offer ) -> Result < Accept , OverlayRequestError > {
648
648
self . metrics
649
649
. as_ref ( )
650
650
. and_then ( |m| Some ( m. report_inbound_offer ( ) ) ) ;
651
- let mut requested_keys = BitList :: with_capacity ( request. content_keys . len ( ) )
652
- . map_err ( |e| OverlayRequestError :: AcceptError ( e) ) ?;
653
- let conn_id: u16 = crate :: utp:: stream:: rand ( ) ;
654
651
655
- // TODO: Pipe this with overlay DB and request only not available keys.
652
+ let mut requested_keys =
653
+ BitList :: with_capacity ( request. content_keys . len ( ) ) . map_err ( |_| {
654
+ OverlayRequestError :: AcceptError (
655
+ "Unable to initialize bitlist for requested keys." . to_owned ( ) ,
656
+ )
657
+ } ) ?;
658
+
656
659
let accept_keys = request. content_keys . clone ( ) ;
657
660
658
- for ( i, key) in request. content_keys . iter ( ) . enumerate ( ) {
659
- // should_store is currently a dummy function
660
- // the actual function will take ContentKey type, so we'll have to decode keys here
661
+ for ( i, key) in request. content_keys . into_iter ( ) . enumerate ( ) {
662
+ // TODO: This will not work when we receive state Offer message with state content keys
663
+ // we need to refactor it in a way to get access to the protocol id in this method
664
+ let key = HistoryContentKey :: try_from ( key)
665
+ . map_err ( |err| OverlayRequestError :: AcceptError ( err. to_string ( ) ) ) ?;
661
666
requested_keys
662
- . set ( i, should_store ( key) )
663
- . map_err ( |e| OverlayRequestError :: AcceptError ( e) ) ?;
667
+ . set (
668
+ i,
669
+ self . storage . read ( ) . should_store ( & key) . map_err ( |_| {
670
+ OverlayRequestError :: AcceptError (
671
+ "Portal storage error: unable to check content availability" . to_owned ( ) ,
672
+ )
673
+ } ) ?,
674
+ )
675
+ . map_err ( |_| {
676
+ OverlayRequestError :: AcceptError ( "Unable to set requested keys bits" . to_owned ( ) )
677
+ } ) ?;
664
678
}
665
679
666
680
// listen for incoming connection request on conn_id, as part of utp handshake
681
+ let conn_id: u16 = crate :: utp:: stream:: rand ( ) ;
667
682
let utp_request = UtpListenerRequest :: OfferStream ( conn_id) ;
668
683
if let Err ( err) = self . utp_listener_tx . send ( utp_request) {
669
684
return Err ( OverlayRequestError :: UtpError ( format ! (
@@ -1809,7 +1824,3 @@ mod tests {
1809
1824
assert_eq ! ( target_bucket_idx, expected_index as u8 ) ;
1810
1825
}
1811
1826
}
1812
-
1813
- fn should_store ( _key : & Vec < u8 > ) -> bool {
1814
- return true ;
1815
- }
0 commit comments