@@ -1645,6 +1645,8 @@ impl Worker {
16451645 }
16461646 } ;
16471647
1648+ self . send_snapshot_notify ( c. as_ref ( ) , enacted. as_slice ( ) ) ;
1649+
16481650 if self . step . is_commit ( ) && ( imported. len ( ) + enacted. len ( ) == 1 ) {
16491651 let ( _, committed_block_hash) = self . step . committed ( ) . expect ( "Commit state always has block_hash" ) ;
16501652 if imported. first ( ) == Some ( & committed_block_hash) {
@@ -1662,28 +1664,6 @@ impl Worker {
16621664 }
16631665 }
16641666
1665- let mut last_term_end = None ;
1666- for block_hash in & enacted {
1667- let header = c. block_header ( & BlockId :: Hash ( * block_hash) ) . expect ( "Block is enacted" ) . decode ( ) ;
1668- let parent_header = match c. block_header ( & BlockId :: Hash ( * header. parent_hash ( ) ) ) {
1669- Some ( h) => h. decode ( ) ,
1670- // NOTE: Only the genesis block and the snapshot target don't have the parent in the blockchain
1671- None => continue ,
1672- } ;
1673- let term_seconds = if let Some ( p) = c. term_common_params ( parent_header. hash ( ) . into ( ) ) {
1674- p. term_seconds ( )
1675- } else {
1676- continue
1677- } ;
1678- if super :: engine:: is_term_changed ( & header, & parent_header, term_seconds) {
1679- last_term_end = Some ( * block_hash) ;
1680- }
1681- }
1682- if let Some ( last_term_end) = last_term_end {
1683- // TODO: Reduce the snapshot frequency.
1684- self . snapshot_notify_sender . notify ( last_term_end) ;
1685- }
1686-
16871667 if let Some ( ( last, rest) ) = imported. split_last ( ) {
16881668 let ( imported, last_proposal_header) = {
16891669 let header =
@@ -1718,6 +1698,26 @@ impl Worker {
17181698 }
17191699 }
17201700
1701+ // Notify once for the latest block even if multiple blocks have been enacted.
1702+ fn send_snapshot_notify ( & mut self , c : & dyn ConsensusClient , enacted : & [ BlockHash ] ) {
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+ }
1713+ }
1714+ }
1715+ if let Some ( last_snapshot_point) = last_snapshot_point {
1716+ // TODO: Reduce the snapshot frequency.
1717+ self . snapshot_notify_sender . notify ( * last_snapshot_point) ;
1718+ }
1719+ }
1720+
17211721 fn send_proposal_block (
17221722 & self ,
17231723 signature : SchnorrSignature ,
0 commit comments