2020use batch:: Batch ;
2121use block:: Block ;
2222use execution:: execution_platform:: { ExecutionPlatform , NULL_STATE_HASH } ;
23- use gossip:: permission_verifier:: PermissionVerifier ;
23+ use std:: sync:: {
24+ atomic:: { AtomicBool , AtomicUsize , Ordering } ,
25+ mpsc:: { channel, Receiver , RecvTimeoutError , Sender } ,
26+ Arc , Mutex ,
27+ } ;
28+ use std:: thread;
29+ use std:: time:: Duration ;
30+
31+ use uluru;
32+
2433use journal:: block_scheduler:: BlockScheduler ;
2534use journal:: chain_commit_state:: {
2635 validate_no_duplicate_batches, validate_no_duplicate_transactions,
2736 validate_transaction_dependencies, ChainCommitStateError ,
2837} ;
2938use journal:: validation_rule_enforcer:: enforce_validation_rules;
3039use journal:: { block_manager:: BlockManager , block_wrapper:: BlockStatus } ;
40+ use permissions:: verifier:: PermissionVerifier ;
3141use scheduler:: TxnExecutionResult ;
32- use state:: { settings_view:: SettingsView , state_view_factory:: StateViewFactory } ;
33- use std:: sync:: {
34- atomic:: { AtomicBool , AtomicUsize , Ordering } ,
35- mpsc:: { channel, Receiver , RecvTimeoutError , Sender } ,
36- Arc , Mutex ,
42+ use state:: {
43+ identity_view:: IdentityView , settings_view:: SettingsView , state_view_factory:: StateViewFactory ,
3744} ;
38- use std:: thread;
39- use std:: time:: Duration ;
40- use uluru;
4145
4246const BLOCKVALIDATION_QUEUE_RECV_TIMEOUT : u64 = 100 ;
4347
@@ -162,7 +166,7 @@ impl BlockValidationResult {
162166type InternalSender = Sender < ( Block , Sender < BlockValidationResult > ) > ;
163167type InternalReceiver = Receiver < ( Block , Sender < BlockValidationResult > ) > ;
164168
165- pub struct BlockValidator < TEP : ExecutionPlatform , PV : PermissionVerifier > {
169+ pub struct BlockValidator < TEP : ExecutionPlatform > {
166170 channels : Vec < ( InternalSender , Option < InternalReceiver > ) > ,
167171 index : Arc < AtomicUsize > ,
168172 validation_thread_exit : Arc < AtomicBool > ,
@@ -171,20 +175,17 @@ pub struct BlockValidator<TEP: ExecutionPlatform, PV: PermissionVerifier> {
171175 block_manager : BlockManager ,
172176 transaction_executor : TEP ,
173177 view_factory : StateViewFactory ,
174- permission_verifier : PV ,
175178}
176179
177- impl < TEP : ExecutionPlatform + ' static , PV : PermissionVerifier + ' static > BlockValidator < TEP , PV >
180+ impl < TEP : ExecutionPlatform + ' static > BlockValidator < TEP >
178181where
179182 TEP : Clone ,
180- PV : Clone ,
181183{
182184 #[ allow( too_many_arguments) ]
183185 pub fn new (
184186 block_manager : BlockManager ,
185187 transaction_executor : TEP ,
186188 block_status_store : BlockValidationResultStore ,
187- permission_verifier : PV ,
188189 view_factory : StateViewFactory ,
189190 ) -> Self {
190191 let mut channels = vec ! [ ] ;
@@ -201,7 +202,6 @@ where
201202 block_status_store,
202203 block_manager,
203204 view_factory,
204- permission_verifier,
205205 }
206206 }
207207
@@ -223,8 +223,8 @@ where
223223 let validation2: Box < BlockValidation < ReturnValue = ( ) > > =
224224 Box :: new ( OnChainRulesValidation :: new ( self . view_factory . clone ( ) ) ) ;
225225
226- let validation3: Box < BlockValidation < ReturnValue = ( ) > > =
227- Box :: new ( PermissionValidation :: new ( self . permission_verifier . clone ( ) ) ) ;
226+ let validation3: Box < dyn BlockValidation < ReturnValue = ( ) > > =
227+ Box :: new ( PermissionValidation :: new ( self . view_factory . clone ( ) ) ) ;
228228
229229 let validations = vec ! [ validation1, validation2, validation3] ;
230230
@@ -355,8 +355,8 @@ where
355355 let validation2: Box < BlockValidation < ReturnValue = ( ) > > =
356356 Box :: new ( OnChainRulesValidation :: new ( self . view_factory . clone ( ) ) ) ;
357357
358- let validation3: Box < BlockValidation < ReturnValue = ( ) > > =
359- Box :: new ( PermissionValidation :: new ( self . permission_verifier . clone ( ) ) ) ;
358+ let validation3: Box < dyn BlockValidation < ReturnValue = ( ) > > =
359+ Box :: new ( PermissionValidation :: new ( self . view_factory . clone ( ) ) ) ;
360360
361361 let validations = vec ! [ validation1, validation2, validation3] ;
362362
@@ -375,9 +375,7 @@ where
375375 }
376376}
377377
378- impl < TEP : ExecutionPlatform + Clone , PV : PermissionVerifier + Clone > Clone
379- for BlockValidator < TEP , PV >
380- {
378+ impl < TEP : ExecutionPlatform + Clone > Clone for BlockValidator < TEP > {
381379 fn clone ( & self ) -> Self {
382380 let transaction_executor = self . transaction_executor . clone ( ) ;
383381 let validation_thread_exit = Arc :: clone ( & self . validation_thread_exit ) ;
@@ -398,7 +396,6 @@ impl<TEP: ExecutionPlatform + Clone, PV: PermissionVerifier + Clone> Clone
398396 block_scheduler : self . block_scheduler . clone ( ) ,
399397 block_status_store : self . block_status_store . clone ( ) ,
400398 block_manager : self . block_manager . clone ( ) ,
401- permission_verifier : self . permission_verifier . clone ( ) ,
402399 view_factory : self . view_factory . clone ( ) ,
403400 }
404401 }
@@ -638,19 +635,17 @@ impl BlockValidation for DuplicatesAndDependenciesValidation {
638635 }
639636}
640637
641- struct PermissionValidation < PV : PermissionVerifier > {
642- permission_verifier : PV ,
638+ struct PermissionValidation {
639+ state_view_factory : StateViewFactory ,
643640}
644641
645- impl < PV : PermissionVerifier > PermissionValidation < PV > {
646- fn new ( permission_verifier : PV ) -> Self {
647- PermissionValidation {
648- permission_verifier,
649- }
642+ impl PermissionValidation {
643+ fn new ( state_view_factory : StateViewFactory ) -> Self {
644+ Self { state_view_factory }
650645 }
651646}
652647
653- impl < PV : PermissionVerifier > BlockValidation for PermissionValidation < PV > {
648+ impl BlockValidation for PermissionValidation {
654649 type ReturnValue = ( ) ;
655650
656651 fn validate_block (
@@ -664,13 +659,23 @@ impl<PV: PermissionVerifier> BlockValidation for PermissionValidation<PV> {
664659 format ! ( "During permission check of block {} block_num is {} but missing a previous state root" ,
665660 & block. header_signature, block. block_num) )
666661 } ) ?;
662+
663+ let identity_view: IdentityView = self . state_view_factory . create_view ( state_root)
664+ . map_err ( |err| {
665+ ValidationError :: BlockValidationError (
666+ format ! ( "During permission check of block ({}, {}) state root was not found in state: {}" ,
667+ & block. header_signature, block. block_num, err) )
668+ } ) ?;
669+ let permission_verifier = PermissionVerifier :: new ( Box :: new ( identity_view) ) ;
667670 for batch in & block. batches {
668671 let batch_id = & batch. header_signature ;
669- if !self
670- . permission_verifier
671- . is_batch_signer_authorized ( batch, state_root)
672- {
673- return Err ( ValidationError :: BlockValidationError (
672+ if !permission_verifier. is_batch_signer_authorized ( batch)
673+ . map_err ( |err| {
674+ ValidationError :: BlockValidationError (
675+ format ! ( "During permission check of block ({}, {}), unable to read permissions: {}" ,
676+ & block. header_signature, block. block_num, err) )
677+ } ) ? {
678+ return Err ( ValidationError :: BlockValidationFailure (
674679 format ! ( "Block {} failed permission verification: batch {} signer is not authorized" ,
675680 & block. header_signature,
676681 batch_id) ) ) ;
0 commit comments