@@ -1369,6 +1369,8 @@ impl<Block: BlockT> Backend<Block> {
1369
1369
Ok ( ( ) )
1370
1370
}
1371
1371
1372
+ /// `remove_displaced` can be set to `false` if this is not the last of many subsequent calls
1373
+ /// for performance reasons.
1372
1374
fn finalize_block_with_transaction (
1373
1375
& self ,
1374
1376
transaction : & mut Transaction < DbHash > ,
@@ -1377,6 +1379,7 @@ impl<Block: BlockT> Backend<Block> {
1377
1379
last_finalized : Option < Block :: Hash > ,
1378
1380
justification : Option < Justification > ,
1379
1381
current_transaction_justifications : & mut HashMap < Block :: Hash , Justification > ,
1382
+ remove_displaced : bool ,
1380
1383
) -> ClientResult < MetaUpdate < Block > > {
1381
1384
// TODO: ensure best chain contains this block.
1382
1385
let number = * header. number ( ) ;
@@ -1389,6 +1392,7 @@ impl<Block: BlockT> Backend<Block> {
1389
1392
hash,
1390
1393
with_state,
1391
1394
current_transaction_justifications,
1395
+ remove_displaced,
1392
1396
) ?;
1393
1397
1394
1398
if let Some ( justification) = justification {
@@ -1466,7 +1470,8 @@ impl<Block: BlockT> Backend<Block> {
1466
1470
1467
1471
let mut current_transaction_justifications: HashMap < Block :: Hash , Justification > =
1468
1472
HashMap :: new ( ) ;
1469
- for ( block_hash, justification) in operation. finalized_blocks {
1473
+ let mut finalized_blocks = operation. finalized_blocks . into_iter ( ) . peekable ( ) ;
1474
+ while let Some ( ( block_hash, justification) ) = finalized_blocks. next ( ) {
1470
1475
let block_header = self . blockchain . expect_header ( block_hash) ?;
1471
1476
meta_updates. push ( self . finalize_block_with_transaction (
1472
1477
& mut transaction,
@@ -1475,6 +1480,7 @@ impl<Block: BlockT> Backend<Block> {
1475
1480
Some ( last_finalized_hash) ,
1476
1481
justification,
1477
1482
& mut current_transaction_justifications,
1483
+ finalized_blocks. peek ( ) . is_none ( ) ,
1478
1484
) ?) ;
1479
1485
last_finalized_hash = block_hash;
1480
1486
last_finalized_num = * block_header. number ( ) ;
@@ -1654,6 +1660,7 @@ impl<Block: BlockT> Backend<Block> {
1654
1660
hash,
1655
1661
operation. commit_state ,
1656
1662
& mut current_transaction_justifications,
1663
+ true ,
1657
1664
) ?;
1658
1665
} else {
1659
1666
// canonicalize blocks which are old enough, regardless of finality.
@@ -1779,16 +1786,18 @@ impl<Block: BlockT> Backend<Block> {
1779
1786
Ok ( ( ) )
1780
1787
}
1781
1788
1782
- // write stuff to a transaction after a new block is finalized.
1783
- // this canonicalizes finalized blocks. Fails if called with a block which
1784
- // was not a child of the last finalized block.
1789
+ // Write stuff to a transaction after a new block is finalized. This canonicalizes finalized
1790
+ // blocks. Fails if called with a block which was not a child of the last finalized block.
1791
+ /// `remove_displaced` can be set to `false` if this is not the last of many subsequent calls
1792
+ /// for performance reasons.
1785
1793
fn note_finalized (
1786
1794
& self ,
1787
1795
transaction : & mut Transaction < DbHash > ,
1788
1796
f_header : & Block :: Header ,
1789
1797
f_hash : Block :: Hash ,
1790
1798
with_state : bool ,
1791
1799
current_transaction_justifications : & mut HashMap < Block :: Hash , Justification > ,
1800
+ remove_displaced : bool ,
1792
1801
) -> ClientResult < ( ) > {
1793
1802
let f_num = * f_header. number ( ) ;
1794
1803
@@ -1813,13 +1822,17 @@ impl<Block: BlockT> Backend<Block> {
1813
1822
apply_state_commit ( transaction, commit) ;
1814
1823
}
1815
1824
1816
- let new_displaced = self . blockchain . displaced_leaves_after_finalizing ( f_hash, f_num) ?;
1825
+ if remove_displaced && !matches ! ( self . blocks_pruning, BlocksPruning :: KeepAll ) {
1826
+ let new_displaced = self . blockchain . displaced_leaves_after_finalizing ( f_hash, f_num) ?;
1817
1827
1818
- self . blockchain . leaves . write ( ) . remove_displaced_leaves ( FinalizationOutcome :: new (
1819
- new_displaced. displaced_leaves . iter ( ) . copied ( ) ,
1820
- ) ) ;
1828
+ self . blockchain . leaves . write ( ) . remove_displaced_leaves ( FinalizationOutcome :: new (
1829
+ new_displaced. displaced_leaves . iter ( ) . copied ( ) ,
1830
+ ) ) ;
1831
+
1832
+ self . prune_displaced_branches ( transaction, & new_displaced) ?;
1833
+ }
1821
1834
1822
- self . prune_blocks ( transaction, f_num, & new_displaced , current_transaction_justifications) ?;
1835
+ self . prune_blocks ( transaction, f_num, current_transaction_justifications) ?;
1823
1836
1824
1837
Ok ( ( ) )
1825
1838
}
@@ -1828,39 +1841,29 @@ impl<Block: BlockT> Backend<Block> {
1828
1841
& self ,
1829
1842
transaction : & mut Transaction < DbHash > ,
1830
1843
finalized_number : NumberFor < Block > ,
1831
- displaced : & DisplacedLeavesAfterFinalization < Block > ,
1832
1844
current_transaction_justifications : & mut HashMap < Block :: Hash , Justification > ,
1833
1845
) -> ClientResult < ( ) > {
1834
- match self . blocks_pruning {
1835
- BlocksPruning :: KeepAll => { } ,
1836
- BlocksPruning :: Some ( blocks_pruning) => {
1837
- // Always keep the last finalized block
1838
- let keep = std:: cmp:: max ( blocks_pruning, 1 ) ;
1839
- if finalized_number >= keep. into ( ) {
1840
- let number = finalized_number. saturating_sub ( keep. into ( ) ) ;
1841
-
1842
- // Before we prune a block, check if it is pinned
1843
- if let Some ( hash) = self . blockchain . hash ( number) ? {
1844
- self . blockchain . insert_persisted_body_if_pinned ( hash) ?;
1845
-
1846
- // If the block was finalized in this transaction, it will not be in the db
1847
- // yet.
1848
- if let Some ( justification) =
1849
- current_transaction_justifications. remove ( & hash)
1850
- {
1851
- self . blockchain . insert_justifications_if_pinned ( hash, justification) ;
1852
- } else {
1853
- self . blockchain . insert_persisted_justifications_if_pinned ( hash) ?;
1854
- }
1855
- } ;
1846
+ if let BlocksPruning :: Some ( blocks_pruning) = self . blocks_pruning {
1847
+ // Always keep the last finalized block
1848
+ let keep = std:: cmp:: max ( blocks_pruning, 1 ) ;
1849
+ if finalized_number >= keep. into ( ) {
1850
+ let number = finalized_number. saturating_sub ( keep. into ( ) ) ;
1851
+
1852
+ // Before we prune a block, check if it is pinned
1853
+ if let Some ( hash) = self . blockchain . hash ( number) ? {
1854
+ self . blockchain . insert_persisted_body_if_pinned ( hash) ?;
1855
+
1856
+ // If the block was finalized in this transaction, it will not be in the db
1857
+ // yet.
1858
+ if let Some ( justification) = current_transaction_justifications. remove ( & hash) {
1859
+ self . blockchain . insert_justifications_if_pinned ( hash, justification) ;
1860
+ } else {
1861
+ self . blockchain . insert_persisted_justifications_if_pinned ( hash) ?;
1862
+ }
1863
+ } ;
1856
1864
1857
- self . prune_block ( transaction, BlockId :: < Block > :: number ( number) ) ?;
1858
- }
1859
- self . prune_displaced_branches ( transaction, displaced) ?;
1860
- } ,
1861
- BlocksPruning :: KeepFinalized => {
1862
- self . prune_displaced_branches ( transaction, displaced) ?;
1863
- } ,
1865
+ self . prune_block ( transaction, BlockId :: < Block > :: number ( number) ) ?;
1866
+ }
1864
1867
}
1865
1868
Ok ( ( ) )
1866
1869
}
@@ -2121,6 +2124,7 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
2121
2124
None ,
2122
2125
justification,
2123
2126
& mut current_transaction_justifications,
2127
+ true ,
2124
2128
) ?;
2125
2129
2126
2130
self . storage . db . commit ( transaction) ?;
0 commit comments