@@ -59,7 +59,7 @@ use sp_domains_fraud_proof::verification::{
5959 verify_invalid_domain_extrinsics_root_fraud_proof, verify_invalid_state_transition_fraud_proof,
6060 verify_valid_bundle_fraud_proof,
6161} ;
62- use sp_runtime:: traits:: { CheckedSub , Hash , Header , One , Zero } ;
62+ use sp_runtime:: traits:: { Hash , Header , One , Zero } ;
6363use sp_runtime:: { RuntimeAppPublic , SaturatedConversion , Saturating } ;
6464use sp_std:: boxed:: Box ;
6565use sp_std:: collections:: btree_map:: BTreeMap ;
@@ -1860,16 +1860,23 @@ impl<T: Config> Pallet<T> {
18601860 HeadReceiptNumber :: < T > :: get ( domain_id)
18611861 }
18621862
1863- /// Returns the block number of oldest execution receipt.
1864- // FIXME: the `oldest_receipt_number` may not be correct if fraud proof is submitted
1865- // and bad ER were pruned, see https://github.com/subspace/subspace/issues/2354
1866- pub fn oldest_receipt_number ( domain_id : DomainId ) -> DomainBlockNumberFor < T > {
1867- Self :: head_receipt_number ( domain_id) . saturating_sub ( Self :: block_tree_pruning_depth ( ) )
1868- }
1869-
1870- /// Returns the block tree pruning depth.
1871- pub fn block_tree_pruning_depth ( ) -> DomainBlockNumberFor < T > {
1872- T :: BlockTreePruningDepth :: get ( )
1863+ /// Returns the block number of the oldest existing unconfirmed execution receipt, return `None`
1864+ /// means there is no unconfirmed ER exist or submitted yet.
1865+ pub fn oldest_unconfirmed_receipt_number (
1866+ domain_id : DomainId ,
1867+ ) -> Option < DomainBlockNumberFor < T > > {
1868+ let oldest_nonconfirmed_er_number =
1869+ LatestConfirmedDomainBlockNumber :: < T > :: get ( domain_id) . saturating_add ( One :: one ( ) ) ;
1870+
1871+ if BlockTree :: < T > :: get ( domain_id, oldest_nonconfirmed_er_number) . is_some ( ) {
1872+ Some ( oldest_nonconfirmed_er_number)
1873+ } else {
1874+ // The `oldest_nonconfirmed_er_number` ER may not exist if
1875+ // - The domain just started and no ER submitted yet
1876+ // - The oldest ER just pruned by fraud proof and no new ER submitted yet
1877+ // - When using consensus block to derive the challenge period forward (unimplemented yet)
1878+ None
1879+ }
18731880 }
18741881
18751882 /// Returns the domain block limit of the given domain.
@@ -1887,10 +1894,10 @@ impl<T: Config> Pallet<T> {
18871894 return true ;
18881895 }
18891896
1897+ // Start from the oldest non-confirmed ER to the head domain number
1898+ let mut to_check =
1899+ LatestConfirmedDomainBlockNumber :: < T > :: get ( domain_id) . saturating_add ( One :: one ( ) ) ;
18901900 let head_number = HeadDomainNumber :: < T > :: get ( domain_id) ;
1891- let mut to_check = head_number
1892- . checked_sub ( & T :: BlockTreePruningDepth :: get ( ) )
1893- . unwrap_or ( Zero :: zero ( ) ) ;
18941901
18951902 while to_check <= head_number {
18961903 if !ExecutionInbox :: < T > :: iter_prefix_values ( ( domain_id, to_check) ) . all ( |digests| {
0 commit comments