@@ -109,7 +109,7 @@ pub trait Poll {
109
109
110
110
/// Returns the block associated with the given header.
111
111
fn fetch_block < ' a > ( & ' a mut self , header : & ' a ValidatedBlockHeader ) ->
112
- AsyncBlockSourceResult < ' a , Block > ;
112
+ AsyncBlockSourceResult < ' a , ValidatedBlock > ;
113
113
}
114
114
115
115
/// A chain tip relative to another chain tip in terms of block hash and chainwork.
@@ -126,8 +126,20 @@ pub enum ChainTip {
126
126
Worse ( ValidatedBlockHeader ) ,
127
127
}
128
128
129
- impl BlockHeaderData {
130
- fn validate ( self , block_hash : BlockHash ) -> BlockSourceResult < ValidatedBlockHeader > {
129
+ /// The `Validate` trait defines behavior for validating chain data.
130
+ trait Validate {
131
+ /// The validated data wrapper which can be dereferenced to obtain the validated data.
132
+ type T : std:: ops:: Deref < Target = Self > ;
133
+
134
+ /// Validates the chain data against the given block hash and any criteria needed to ensure that
135
+ /// it is internally consistent.
136
+ fn validate ( self , block_hash : BlockHash ) -> BlockSourceResult < Self :: T > ;
137
+ }
138
+
139
+ impl Validate for BlockHeaderData {
140
+ type T = ValidatedBlockHeader ;
141
+
142
+ fn validate ( self , block_hash : BlockHash ) -> BlockSourceResult < Self :: T > {
131
143
self . header
132
144
. validate_pow ( & self . header . target ( ) )
133
145
. or ( Err ( BlockSourceError :: Persistent ) ) ?;
@@ -140,6 +152,22 @@ impl BlockHeaderData {
140
152
}
141
153
}
142
154
155
+ impl Validate for Block {
156
+ type T = ValidatedBlock ;
157
+
158
+ fn validate ( self , block_hash : BlockHash ) -> BlockSourceResult < Self :: T > {
159
+ if self . block_hash ( ) != block_hash {
160
+ return Err ( BlockSourceError :: Persistent ) ;
161
+ }
162
+
163
+ if !self . check_merkle_root ( ) || !self . check_witness_commitment ( ) {
164
+ return Err ( BlockSourceError :: Persistent ) ;
165
+ }
166
+
167
+ Ok ( ValidatedBlock { block_hash, inner : self } )
168
+ }
169
+ }
170
+
143
171
/// A block header with validated proof of work and corresponding block hash.
144
172
#[ derive( Clone , Copy , Debug , PartialEq ) ]
145
173
pub struct ValidatedBlockHeader {
@@ -187,6 +215,20 @@ impl ValidatedBlockHeader {
187
215
}
188
216
}
189
217
218
+ /// A block with validated data against its transaction list and corresponding block hash.
219
+ pub struct ValidatedBlock {
220
+ block_hash : BlockHash ,
221
+ inner : Block ,
222
+ }
223
+
224
+ impl std:: ops:: Deref for ValidatedBlock {
225
+ type Target = Block ;
226
+
227
+ fn deref ( & self ) -> & Self :: Target {
228
+ & self . inner
229
+ }
230
+ }
231
+
190
232
async fn look_up_prev_header < P : Poll > ( chain_poller : & mut P , header : & ValidatedBlockHeader , cache : & HeaderCache ) -> BlockSourceResult < ValidatedBlockHeader > {
191
233
match cache. get ( & header. header . prev_blockhash ) {
192
234
Some ( prev_header) => Ok ( * prev_header) ,
@@ -325,9 +367,7 @@ async fn sync_chain_monitor<CL: ChainListener + Sized, P: Poll>(new_header: Vali
325
367
Err ( e) => return Err ( ( e, new_tip) ) ,
326
368
Ok ( b) => b,
327
369
} ;
328
- if block. header != header. header || !block. check_merkle_root ( ) || !block. check_witness_commitment ( ) {
329
- return Err ( ( BlockSourceError :: Persistent , new_tip) ) ;
330
- }
370
+ debug_assert_eq ! ( block. block_hash, header. block_hash) ;
331
371
println ! ( "Connecting block {}" , header. block_hash. to_hex( ) ) ;
332
372
chain_notifier. block_connected ( & block, header. height ) ;
333
373
header_cache. insert ( header. block_hash , header) ;
0 commit comments