1
- use beacon_chain:: get_block_root;
2
- use lighthouse_network:: {
3
- rpc:: { methods:: BlobsByRootRequest , BlocksByRootRequest } ,
4
- PeerId ,
5
- } ;
6
- use std:: sync:: Arc ;
7
1
use strum:: IntoStaticStr ;
8
- use types:: {
9
- blob_sidecar:: BlobIdentifier , BlobSidecar , ChainSpec , EthSpec , Hash256 , SignedBeaconBlock ,
10
- } ;
2
+ use types:: Hash256 ;
11
3
4
+ pub use blobs_by_root:: { ActiveBlobsByRootRequest , BlobsByRootSingleBlockRequest } ;
5
+ pub use blocks_by_root:: { ActiveBlocksByRootRequest , BlocksByRootSingleRequest } ;
12
6
pub use data_columns_by_root:: {
13
7
ActiveDataColumnsByRootRequest , DataColumnsByRootSingleBlockRequest ,
14
8
} ;
15
9
10
+ mod blobs_by_root;
11
+ mod blocks_by_root;
16
12
mod data_columns_by_root;
17
13
18
14
#[ derive( Debug , PartialEq , Eq , IntoStaticStr ) ]
@@ -25,148 +21,3 @@ pub enum LookupVerifyError {
25
21
InvalidInclusionProof ,
26
22
DuplicateData ,
27
23
}
28
-
29
- pub struct ActiveBlocksByRootRequest {
30
- request : BlocksByRootSingleRequest ,
31
- resolved : bool ,
32
- pub ( crate ) peer_id : PeerId ,
33
- }
34
-
35
- impl ActiveBlocksByRootRequest {
36
- pub fn new ( request : BlocksByRootSingleRequest , peer_id : PeerId ) -> Self {
37
- Self {
38
- request,
39
- resolved : false ,
40
- peer_id,
41
- }
42
- }
43
-
44
- /// Append a response to the single chunk request. If the chunk is valid, the request is
45
- /// resolved immediately.
46
- /// The active request SHOULD be dropped after `add_response` returns an error
47
- pub fn add_response < E : EthSpec > (
48
- & mut self ,
49
- block : Arc < SignedBeaconBlock < E > > ,
50
- ) -> Result < Arc < SignedBeaconBlock < E > > , LookupVerifyError > {
51
- if self . resolved {
52
- return Err ( LookupVerifyError :: TooManyResponses ) ;
53
- }
54
-
55
- let block_root = get_block_root ( & block) ;
56
- if self . request . 0 != block_root {
57
- return Err ( LookupVerifyError :: UnrequestedBlockRoot ( block_root) ) ;
58
- }
59
-
60
- // Valid data, blocks by root expects a single response
61
- self . resolved = true ;
62
- Ok ( block)
63
- }
64
-
65
- pub fn terminate ( self ) -> Result < ( ) , LookupVerifyError > {
66
- if self . resolved {
67
- Ok ( ( ) )
68
- } else {
69
- Err ( LookupVerifyError :: NoResponseReturned )
70
- }
71
- }
72
- }
73
-
74
- #[ derive( Debug , Copy , Clone ) ]
75
- pub struct BlocksByRootSingleRequest ( pub Hash256 ) ;
76
-
77
- impl BlocksByRootSingleRequest {
78
- pub fn into_request ( self , spec : & ChainSpec ) -> BlocksByRootRequest {
79
- BlocksByRootRequest :: new ( vec ! [ self . 0 ] , spec)
80
- }
81
- }
82
-
83
- #[ derive( Debug , Clone ) ]
84
- pub struct BlobsByRootSingleBlockRequest {
85
- pub block_root : Hash256 ,
86
- pub indices : Vec < u64 > ,
87
- }
88
-
89
- impl BlobsByRootSingleBlockRequest {
90
- pub fn into_request ( self , spec : & ChainSpec ) -> BlobsByRootRequest {
91
- BlobsByRootRequest :: new (
92
- self . indices
93
- . into_iter ( )
94
- . map ( |index| BlobIdentifier {
95
- block_root : self . block_root ,
96
- index,
97
- } )
98
- . collect ( ) ,
99
- spec,
100
- )
101
- }
102
- }
103
-
104
- pub struct ActiveBlobsByRootRequest < E : EthSpec > {
105
- request : BlobsByRootSingleBlockRequest ,
106
- blobs : Vec < Arc < BlobSidecar < E > > > ,
107
- resolved : bool ,
108
- pub ( crate ) peer_id : PeerId ,
109
- }
110
-
111
- impl < E : EthSpec > ActiveBlobsByRootRequest < E > {
112
- pub fn new ( request : BlobsByRootSingleBlockRequest , peer_id : PeerId ) -> Self {
113
- Self {
114
- request,
115
- blobs : vec ! [ ] ,
116
- resolved : false ,
117
- peer_id,
118
- }
119
- }
120
-
121
- /// Appends a chunk to this multi-item request. If all expected chunks are received, this
122
- /// method returns `Some`, resolving the request before the stream terminator.
123
- /// The active request SHOULD be dropped after `add_response` returns an error
124
- pub fn add_response (
125
- & mut self ,
126
- blob : Arc < BlobSidecar < E > > ,
127
- ) -> Result < Option < Vec < Arc < BlobSidecar < E > > > > , LookupVerifyError > {
128
- if self . resolved {
129
- return Err ( LookupVerifyError :: TooManyResponses ) ;
130
- }
131
-
132
- let block_root = blob. block_root ( ) ;
133
- if self . request . block_root != block_root {
134
- return Err ( LookupVerifyError :: UnrequestedBlockRoot ( block_root) ) ;
135
- }
136
- if !blob. verify_blob_sidecar_inclusion_proof ( ) {
137
- return Err ( LookupVerifyError :: InvalidInclusionProof ) ;
138
- }
139
- if !self . request . indices . contains ( & blob. index ) {
140
- return Err ( LookupVerifyError :: UnrequestedIndex ( blob. index ) ) ;
141
- }
142
- if self . blobs . iter ( ) . any ( |b| b. index == blob. index ) {
143
- return Err ( LookupVerifyError :: DuplicateData ) ;
144
- }
145
-
146
- self . blobs . push ( blob) ;
147
- if self . blobs . len ( ) >= self . request . indices . len ( ) {
148
- // All expected chunks received, return result early
149
- self . resolved = true ;
150
- Ok ( Some ( std:: mem:: take ( & mut self . blobs ) ) )
151
- } else {
152
- Ok ( None )
153
- }
154
- }
155
-
156
- pub fn terminate ( self ) -> Result < ( ) , LookupVerifyError > {
157
- if self . resolved {
158
- Ok ( ( ) )
159
- } else {
160
- Err ( LookupVerifyError :: NotEnoughResponsesReturned {
161
- expected : self . request . indices . len ( ) ,
162
- actual : self . blobs . len ( ) ,
163
- } )
164
- }
165
- }
166
-
167
- /// Mark request as resolved (= has returned something downstream) while marking this status as
168
- /// true for future calls.
169
- pub fn resolve ( & mut self ) -> bool {
170
- std:: mem:: replace ( & mut self . resolved , true )
171
- }
172
- }
0 commit comments