@@ -1700,26 +1700,21 @@ impl Worker {
17001700
17011701 // Notify once for the latest block even if multiple blocks have been enacted.
17021702 fn send_snapshot_notify ( & mut self , c : & dyn ConsensusClient , enacted : & [ BlockHash ] ) {
1703- let mut last_term_end = None ;
1704- for block_hash in enacted {
1705- let header = c. block_header ( & BlockId :: Hash ( * block_hash) ) . expect ( "Block is enacted" ) . decode ( ) ;
1706- let parent_header = match c. block_header ( & BlockId :: Hash ( * header. parent_hash ( ) ) ) {
1707- Some ( h) => h. decode ( ) ,
1708- // NOTE: Only the genesis block and the snapshot target don't have the parent in the blockchain
1709- None => continue ,
1710- } ;
1711- let term_seconds = if let Some ( p) = c. term_common_params ( parent_header. hash ( ) . into ( ) ) {
1712- p. term_seconds ( )
1713- } else {
1714- continue
1715- } ;
1716- if super :: engine:: is_term_changed ( & header, & parent_header, term_seconds) {
1717- last_term_end = Some ( * block_hash) ;
1703+ let mut last_snapshot_point = None ;
1704+ for block_hash in enacted. iter ( ) . rev ( ) {
1705+ let block_id = BlockId :: Hash ( * block_hash) ;
1706+ let last_term_finished_block_num = c. last_term_finished_block_num ( block_id) . expect ( "Block is enacted" ) ;
1707+ let block_number = c. block_number ( & block_id) . expect ( "Block number should exist for enacted block" ) ;
1708+
1709+ if let Some ( params) = c. term_common_params ( block_id) {
1710+ if params. era ( ) == 1 && ( last_term_finished_block_num + 1 == block_number) {
1711+ last_snapshot_point = Some ( block_hash) ;
1712+ }
17181713 }
17191714 }
1720- if let Some ( last_term_end ) = last_term_end {
1715+ if let Some ( last_snapshot_point ) = last_snapshot_point {
17211716 // TODO: Reduce the snapshot frequency.
1722- self . snapshot_notify_sender . notify ( last_term_end ) ;
1717+ self . snapshot_notify_sender . notify ( * last_snapshot_point ) ;
17231718 }
17241719 }
17251720
0 commit comments