@@ -100,7 +100,8 @@ use sc_consensus::{
100100 import_queue:: { BasicQueue , BoxJustificationImport , DefaultImportQueue , Verifier } ,
101101} ;
102102use sc_consensus_epochs:: {
103- descendent_query, Epoch as EpochT , EpochChangesFor , SharedEpochChanges , ViableEpochDescriptor ,
103+ descendent_query, Epoch as EpochT , EpochChangesFor , SharedEpochChanges , ViableEpoch ,
104+ ViableEpochDescriptor ,
104105} ;
105106use sc_consensus_slots:: {
106107 check_equivocation, BackoffAuthoringBlocksStrategy , CheckedHeader , InherentDataProviderExt ,
@@ -1039,26 +1040,16 @@ where
10391040
10401041 let slot_now = Slot :: from_timestamp ( Timestamp :: current ( ) , self . slot_duration ) ;
10411042
1042- let parent_header_metadata = self
1043- . client
1044- . header_metadata ( parent_hash)
1045- . map_err ( Error :: < Block > :: FetchParentHeader ) ?;
1046-
10471043 let pre_digest = find_pre_digest :: < Block > ( & block. header ) ?;
10481044 let ( check_header, epoch_descriptor) = {
1049- let epoch_changes = self . epoch_changes . shared_data ( ) ;
1050- let epoch_descriptor = epoch_changes
1051- . epoch_descriptor_for_child_of (
1052- descendent_query ( & * self . client ) ,
1053- & parent_hash,
1054- parent_header_metadata. number ,
1055- pre_digest. slot ( ) ,
1056- )
1057- . map_err ( |e| Error :: < Block > :: ForkTree ( Box :: new ( e) ) ) ?
1058- . ok_or ( Error :: < Block > :: FetchEpoch ( parent_hash) ) ?;
1059- let viable_epoch = epoch_changes
1060- . viable_epoch ( & epoch_descriptor, |slot| Epoch :: genesis ( & self . config , slot) )
1061- . ok_or ( Error :: < Block > :: FetchEpoch ( parent_hash) ) ?;
1045+ let ( epoch_descriptor, viable_epoch) = query_epoch_changes (
1046+ & self . epoch_changes ,
1047+ self . client . as_ref ( ) ,
1048+ & self . config ,
1049+ number,
1050+ pre_digest. slot ( ) ,
1051+ parent_hash,
1052+ ) ?;
10621053
10631054 // We add one to the current slot to allow for some small drift.
10641055 // FIXME #1019 in the future, alter this queue to allow deferring of headers
@@ -1419,30 +1410,15 @@ where
14191410
14201411 // Check for equivocation and report it to the runtime if needed.
14211412 let author = {
1422- let epoch_changes = self . epoch_changes . shared_data ( ) ;
1423- let number: U256 = number. into ( ) ;
1424- let epoch_descriptor = epoch_changes
1425- . epoch_descriptor_for_child_of (
1426- descendent_query ( & * self . client ) ,
1427- & parent_hash,
1428- NumberFor :: < Block > :: try_from ( number - 1 )
1429- . map_err ( |_| "parent block exists, hence must be able to decrement it" )
1430- . unwrap ( ) ,
1431- babe_pre_digest. slot ( ) ,
1432- )
1433- . map_err ( |e| ConsensusError :: Other ( Box :: new ( e) ) ) ?
1434- . ok_or_else ( || {
1435- ConsensusError :: Other (
1436- Error :: < Block > :: EpochUnavailable ( parent_hash. to_string ( ) ) . into ( ) ,
1437- )
1438- } ) ?;
1439- let viable_epoch = epoch_changes
1440- . viable_epoch ( & epoch_descriptor, |slot| Epoch :: genesis ( & self . config , slot) )
1441- . ok_or_else ( || {
1442- ConsensusError :: Other (
1443- Error :: < Block > :: EpochUnavailable ( parent_hash. to_string ( ) ) . into ( ) ,
1444- )
1445- } ) ?;
1413+ let ( _epoch_descriptor, viable_epoch) = query_epoch_changes (
1414+ & self . epoch_changes ,
1415+ self . client . as_ref ( ) ,
1416+ & self . config ,
1417+ number,
1418+ slot,
1419+ parent_hash,
1420+ )
1421+ . map_err ( |e| ConsensusError :: Other ( babe_err ( e) . into ( ) ) ) ?;
14461422 let epoch = viable_epoch. as_ref ( ) ;
14471423 match epoch. authorities . get ( babe_pre_digest. authority_index ( ) as usize ) {
14481424 Some ( author) => author. 0 . clone ( ) ,
@@ -1975,3 +1951,37 @@ where
19751951 client. insert_aux ( values, weight_keys. iter ( ) )
19761952 } )
19771953}
1954+
1955+ fn query_epoch_changes < Block , Client > (
1956+ epoch_changes : & SharedEpochChanges < Block , Epoch > ,
1957+ client : & Client ,
1958+ config : & BabeConfiguration ,
1959+ block_number : NumberFor < Block > ,
1960+ slot : Slot ,
1961+ parent_hash : Block :: Hash ,
1962+ ) -> Result <
1963+ ( ViableEpochDescriptor < Block :: Hash , NumberFor < Block > , Epoch > , ViableEpoch < Epoch > ) ,
1964+ Error < Block > ,
1965+ >
1966+ where
1967+ Block : BlockT ,
1968+ Client : HeaderBackend < Block > + HeaderMetadata < Block , Error = sp_blockchain:: Error > ,
1969+ {
1970+ let block_number: U256 = block_number. into ( ) ;
1971+ let epoch_changes = epoch_changes. shared_data ( ) ;
1972+ let epoch_descriptor = epoch_changes
1973+ . epoch_descriptor_for_child_of (
1974+ descendent_query ( & * client) ,
1975+ & parent_hash,
1976+ NumberFor :: < Block > :: try_from ( block_number - 1 )
1977+ . map_err ( |_| "parent block exists, hence must be able to decrement it" )
1978+ . unwrap ( ) ,
1979+ slot,
1980+ )
1981+ . map_err ( |e| Error :: < Block > :: ForkTree ( Box :: new ( e) ) ) ?
1982+ . ok_or ( Error :: < Block > :: FetchEpoch ( parent_hash) ) ?;
1983+ let viable_epoch = epoch_changes
1984+ . viable_epoch ( & epoch_descriptor, |slot| Epoch :: genesis ( & config, slot) )
1985+ . ok_or ( Error :: < Block > :: FetchEpoch ( parent_hash) ) ?;
1986+ Ok ( ( epoch_descriptor, viable_epoch. into_cloned ( ) ) )
1987+ }
0 commit comments