Skip to content
Snippets Groups Projects
  1. Feb 19, 2025
  2. Jul 18, 2024
  3. Jul 05, 2024
    • Nazar Mokrynskyi's avatar
      Optimize finalization performance (#4922) · 221eddc9
      Nazar Mokrynskyi authored
      This PR largely fixes
      https://github.com/paritytech/polkadot-sdk/issues/4903 by addressing it
      from a few different directions.
      
      The high-level observation is that complexity of finalization was
      unfortunately roughly `O(n^3)`. Not only
      `displaced_leaves_after_finalizing` was extremely inefficient on its
      own, especially when large ranges of blocks were involved, it was called
      once upfront and then on every single block that was finalized over and
      over again.
      
      The first commit refactores code adjacent to
      `displaced_leaves_after_finalizing` to optimize memory allocations. For
      example things like `BTreeMap<_, Vec<_>>` were very bad in terms of
      number of allocations and after analyzing code paths was completely
      unnecessary and replaced with `Vec<(_, _)>`. In other places allocations
      of known size were not done upfront and some APIs required unnecessary
      cloning of vectors.
      
      I checked invariants and didn't find anything that was violated after
      refactoring.
      
      Second commit completely replaces `displaced_leaves_after_finalizing`
      implementation with a much more efficient one. In my case with ~82k
      blocks and ~13k leaves it takes ~5.4s to finish
      `client.apply_finality()` now.
      
      The idea is to avoid querying the same blocks over and over again as
      well as introducing temporary local cache for blocks related to leaves
      above block that is being finalized as well as local cache of the
      finalized branch of the chain. I left some comments in the code and
      wrote tests that I belive should check all code invariants for
      correctness. `lowest_common_ancestor_multiblock` was removed as
      unnecessary and not great in terms of performance API, domain-specific
      code should be written instead like done in
      `displaced_leaves_after_finalizing`.
      
      After these changes I noticed finalization is still horribly slow,
      turned out that even though `displaced_leaves_after_finalizing` was way
      faster that before (probably order of magnitude), it was called for
      every single of those 82k blocks :face_palm:
      
      
      
      The quick hack I came up with in the third commit to handle this edge
      case was to not call it when finalizing multiple blocks at once until
      the very last moment. It works and allows to finish the whole
      finalization in just 14 seconds (5.4+5.4 of which are two calls to
      `displaced_leaves_after_finalizing`). I'm really not happy with the fact
      that `displaced_leaves_after_finalizing` is called twice, but much
      heavier refactoring would be necessary to get rid of second call.
      
      ---
      
      Next steps:
      * assuming the changes are acceptable I'll write prdoc
      * https://github.com/paritytech/polkadot-sdk/pull/4920 or something
      similar in spirit should be implemented to unleash efficient parallelsm
      with rayon in `displaced_leaves_after_finalizing`, which will allow to
      further (and significant!) scale its performance rather that being
      CPU-bound on a single core, also reading database sequentially should
      ideally be avoided
      * someone should look into removal of the second
      `displaced_leaves_after_finalizing` call
      * further cleanups are possible if `undo_finalization` can be removed
      
      ---
      
      Polkadot Address: 1vSxzbyz2cJREAuVWjhXUT1ds8vBzoxn2w4asNpusQKwjJd
      
      ---------
      
      Co-authored-by: default avatarSebastian Kunert <skunert49@gmail.com>
  4. Jun 11, 2024
    • Sebastian Kunert's avatar
      finalization: Skip tree route calculation if no forks present (#4721) · 96ab6869
      Sebastian Kunert authored
      ## Issue
      
      Currently, syncing parachains from scratch can lead to a very long
      finalization time once they reach the tip of the chain. The problem is
      that we try to finalize everything from 0 to the tip, which can be
      thousands or even millions of blocks.
      
      We finalize sequentially and try to compute displaced branches during
      finalization. So for every block on the way, we compute an expensive
      tree route.
      
      ## Proposed Improvements
      
      In this PR, I propose improvements that solve this situation:
      
      - **Skip tree route calculation if `leaves().len() == 1`:** This should
      be enough for 90% of cases where there is only one leaf after sync.
      - **Optimize finalization for long distances:** It can happen that the
      parachain has imported some leaf and then receives a relay chain
      notification with the finalized block. In that case, the previous
      optimization will not trigger. A second mechanism should ensure that we
      do not need to compute the full tree route. If the finalization distance
      is long, we check the lowest common ancestor of all the leaves. If it is
      above the to-be-finalized block, we know that there are no displaced
      leaves. This is fast because forks are short and close to the tip, so we
      can leverage the header cache.
      
      ## Alternative Approach
      
      - The problem was introduced in #3962. Reverting that PR is another
      possible strategy.
      - We could store for every fork where it begins, however sounds a bit
      more involved to me.
      
      
      fixes #4614
  5. May 15, 2024
    • shamil-gadelshin's avatar
      Change forks pruning algorithm. (#3962) · 9c69bb98
      shamil-gadelshin authored
      
      This PR changes the fork calculation and pruning algorithm to enable
      future block header pruning. It's required because the previous
      algorithm relied on the block header persistence. It follows the
      [related
      discussion](https://github.com/paritytech/polkadot-sdk/issues/1570)
      
      The previous code contained this comment describing the situation:
      ```
      	/// Note a block height finalized, displacing all leaves with number less than the finalized
      	/// block's.
      	///
      	/// Although it would be more technically correct to also prune out leaves at the
      	/// same number as the finalized block, but with different hashes, the current behavior
      	/// is simpler and our assumptions about how finalization works means that those leaves
      	/// will be pruned soon afterwards anyway.
      	pub fn finalize_height(&mut self, number: N) -> FinalizationOutcome<H, N> {
      ```
      
      The previous algorithm relied on the existing block headers to prune
      forks later and to enable block header pruning we need to clear all
      obsolete forks right after the block finalization to not depend on the
      related block headers in the future.
      
      ---------
      
      Co-authored-by: default avatarBastian Köcher <git@kchr.de>
  6. Mar 26, 2024
    • Dcompoze's avatar
      Fix spelling mistakes across the whole repository (#3808) · 002d9260
      Dcompoze authored
      **Update:** Pushed additional changes based on the review comments.
      
      **This pull request fixes various spelling mistakes in this
      repository.**
      
      Most of the changes are contained in the first **3** commits:
      
      - `Fix spelling mistakes in comments and docs`
      
      - `Fix spelling mistakes in test names`
      
      - `Fix spelling mistakes in error messages, panic messages, logs and
      tracing`
      
      Other source code spelling mistakes are separated into individual
      commits for easier reviewing:
      
      - `Fix the spelling of 'authority'`
      
      - `Fix the spelling of 'REASONABLE_HEADERS_IN_JUSTIFICATION_ANCESTRY'`
      
      - `Fix the spelling of 'prev_enqueud_messages'`
      
      - `Fix the spelling of 'endpoint'`
      
      - `Fix the spelling of 'children'`
      
      - `Fix the spelling of 'PenpalSiblingSovereignAccount'`
      
      - `Fix the spelling of 'PenpalSudoAccount'`
      
      - `Fix the spelling of 'insufficient'`
      
      - `Fix the spelling of 'PalletXcmExtrinsicsBenchmark'`
      
      - `Fix the spelling of 'subtracted'`
      
      - `Fix the spelling of 'CandidatePendingAvailability'`
      
      - `Fix the spelling of 'exclusive'`
      
      - `Fix the spelling of 'until'`
      
      - `Fix the spelling of 'discriminator'`
      
      - `Fix the spelling of 'nonexistent'`
      
      - `Fix the spelling of 'subsystem'`
      
      - `Fix the spelling of 'indices'`
      
      - `Fix the spelling of 'committed'`
      
      - `Fix the spelling of 'topology'`
      
      - `Fix the spelling of 'response'`
      
      - `Fix the spelling of 'beneficiary'`
      
      - `Fix the spelling of 'formatted'`
      
      - `Fix the spelling of 'UNKNOWN_PROOF_REQUEST'`
      
      - `Fix the spelling of 'succeeded'`
      
      - `Fix the spelling of 'reopened'`
      
      - `Fix the spelling of 'proposer'`
      
      - `Fix the spelling of 'InstantiationNonce'`
      
      - `Fix the spelling of 'depositor'`
      
      - `Fix the spelling of 'expiration'`
      
      - `Fix the spelling of 'phantom'`
      
      - `Fix the spelling of 'AggregatedKeyValue'`
      
      - `Fix the spelling of 'randomness'`
      
      - `Fix the spelling of 'defendant'`
      
      - `Fix the spelling of 'AquaticMammal'`
      
      - `Fix the spelling of 'transactions'`
      
      - `Fix the spelling of 'PassingTracingSubscriber'`
      
      - `Fix the spelling of 'TxSignaturePayload'`
      
      - `Fix the spelling of 'versioning'`
      
      - `Fix the spelling of 'descendant'`
      
      - `Fix the spelling of 'overridden'`
      
      - `Fix the spelling of 'network'`
      
      Let me know if this structure is adequate.
      
      **Note:** The usage of the words `Merkle`, `Merkelize`, `Merklization`,
      `Merkelization`, `Merkleization`, is somewhat inconsistent but I left it
      as it is.
      
      ~~**Note:** In some places the term `Receival` is used to refer to
      message reception, IMO `Reception` is the correct word here, but I left
      it as it is.~~
      
      ~~**Note:** In some places the term `Overlayed` is used instead of the
      more acceptable version `Overlaid` but I also left it as it is.~~
      
      ~~**Note:** In some places the term `Applyable` is used instead of the
      correct version `Applicable` but I also left it as it is.~~
      
      **Note:** Some usage of British vs American english e.g. `judgement` vs
      `judgment`, `initialise` vs `initialize`, `optimise` vs `optimize` etc.
      are both present in different places, but I suppose that's
      understandable given the number of contributors.
      
      ~~**Note:** There is a spelling mistake in `.github/CODEOWNERS` but it
      triggers errors in CI when I make changes to it, so I left it as it
      is.~~
  7. Jul 09, 2023
  8. Feb 21, 2023
    • Vivek Pandya's avatar
      Remove years from copyright notes. (#13415) · bc53b9a0
      Vivek Pandya authored
      * Change copyright year to 2023 from 2022
      
      * Fix incorrect update of copyright year
      
      * Remove years from copy right header
      
      * Fix remaining files
      
      * Fix typo in a header and remove update-copyright.sh
      bc53b9a0
  9. Nov 11, 2022
  10. Nov 09, 2022
  11. Oct 11, 2022
  12. Apr 30, 2022
  13. Apr 12, 2022
    • Bastian Köcher's avatar
      Finality notification: Optimize calculation of stale heads (#11200) · cc4b5c48
      Bastian Köcher authored
      
      * Finality notification: Optimize calculation of stale heads
      
      While looking into some problem on Versi where a collator seemed to be stuck. I found out that it
      was not stuck but there was a huge gap between last finalized and best block. This lead to a lot
      leaves and it was basically trapped inside some loop of reading block headers from the db to find
      the stale heads. While looking into this I found out that `leaves` already supports the feature to
      give us the stale heads relative easily. However, the semantics change a little bit. Instead of
      returning all stale heads of blocks that are not reachable anymore after finalizing a block, we
      currently only return heads with a number lower than the finalized block. This should be no problem,
      because these other leaves that are stale will be returned later when a block gets finalized which
      number is bigger than the block number of these leaves.
      
      While doing that, I also changed `tree_route` of the `FinalityNotification` to include the
      `old_finalized`. Based on the comment I assumed that this was already part of it. However, if
      wanted, I can revert this change.
      
      * FMT
      
      * Update client/service/src/client/client.rs
      
      Co-authored-by: default avatarAndré Silva <123550+andresilva@users.noreply.github.com>
      
      * Do not include the last finalized block
      
      * Rename function
      
      * FMT
      
      * Fix tests
      
      * Update figure
      
      Co-authored-by: default avatarAndré Silva <123550+andresilva@users.noreply.github.com>
      cc4b5c48
  14. Feb 23, 2022
    • Davide Galassi's avatar
      Clean obsolete BABE's weight data (#10748) · 26ec5d71
      Davide Galassi authored
      
      * Clean obsolete BABE weight data
      * Take out test assertion from check closure
      * Optimize metadata access using `HeaderMetadata` trait
      * Apply suggestions from code review
      * Introduce finalize and import pre-commit synchronous actions
      * Do not hold locks between internal methods calls
      * Remove unused generic bound
      * Apply suggestions from code review
      * Register BABE's pre-commit actions on `block_import` instead of `start_babe`
      * PreCommit actions should be `Fn` instead of `FnMut`
      * More robust safenet in case of malformed finality notifications
      
      Co-authored-by: default avatarBastian Köcher <bkchr@users.noreply.github.com>
      Co-authored-by: default avatarAndré Silva <123550+andresilva@users.noreply.github.com>
      26ec5d71
  15. Jan 13, 2022
  16. Jan 03, 2022
  17. Jul 21, 2021
  18. Jan 04, 2021
    • Bastian Köcher's avatar
      Happy new year (#7814) · e3e651f7
      Bastian Köcher authored
      * Happy new year
      
      Updates the copyright years and fixes wrong license headers.
      
      * Fix the template
      
      * Split HEADER into HEADER-APACHE & HEADER-GPL
      e3e651f7
  19. Jun 05, 2020
  20. May 15, 2020
  21. May 07, 2020
  22. May 04, 2020
  23. Mar 12, 2020
  24. Feb 14, 2020
  25. Jan 05, 2020
  26. Dec 02, 2019
    • Benjamin Kampmann's avatar
      The crate rename (#4223) · 927e13c1
      Benjamin Kampmann authored
      * Adding script for rename, could be applicable for nodes on top of it, too
      
      * add stderr and gitlab ci features
      
      * apply script
      
      * fix now minor details in expected stderr
      
      * Update the Cargo.lock
      
      * fix name: sc-transaction -> sc-tracing
      
      * fix rename in script, too
      927e13c1
  27. Nov 26, 2019
    • Benjamin Kampmann's avatar
      Remove all (non-dev) `client` references from `frame`, activate dependency enforcer (#4184) · bd652793
      Benjamin Kampmann authored
      * Move transaction pool to primitives
      
      * move backend, errors into primitives
      
      * remove unused client depencies
      
      * Move rpc-api into primitives
      
      * Move peerset back to client
      
      * Move rpc/api back to client, move palette/support/rpc into utils
      
      * move support-rpc into subfolder
      
      * move system-rpc into utils
      
      * move transaction-pool  and -graph back into client
      
      * fix broken imports
      
      * Clean up test primitives
      
      * Make support test utils independent of frame
      
      * remove unnecessary node dependencies from service
      
      * Reactivate dependency script:
       - only enforce the now achieved status quo will remain
       - allow for primitives to depend on /client for now without failing
       - more discriptive error message so people understand, what it wants
       - minor fix to differentiative between ../client and /client (which may be a subfolder)
       - don't allow this to fail anylonger.
      
      * fix doc comment
      
      * 'Should not' rather than 'must not'.
      
      * Revert unwanted dependency changes
      
      * fix faulty import
      
      * fixup derive_more version
      
      * fix wrong import path
      bd652793
  28. Nov 19, 2019
  29. Nov 14, 2019
    • Benjamin Kampmann's avatar
      Reorganising the repository - external renames and moves (#4074) · 60e5011c
      Benjamin Kampmann authored
      * Adding first rough ouline of the repository structure
      
      * Remove old CI stuff
      
      * add title
      
      * formatting fixes
      
      * move node-exits job's script to scripts dir
      
      * Move docs into subdir
      
      * move to bin
      
      * move maintainence scripts, configs and helpers into its own dir
      
      * add .local to ignore
      
      * move core->client
      
      * start up 'test' area
      
      * move test client
      
      * move test runtime
      
      * make test move compile
      
      * Add dependencies rule enforcement.
      
      * Fix indexing.
      
      * Update docs to reflect latest changes
      
      * Moving /srml->/paint
      
      * update docs
      
      * move client/sr-* -> primitives/
      
      * clean old readme
      
      * remove old broken code in rhd
      
      * update lock
      
      * Step 1.
      
      * starting to untangle client
      
      * Fix after merge.
      
      * start splitting out client interfaces
      
      * move children and blockchain interfaces
      
      * Move trie and state-machine to primitives.
      
      * Fix WASM builds.
      
      * fixing broken imports
      
      * more interface moves
      
      * move backend and light to interfaces
      
      * move CallExecutor
      
      * move cli off client
      
      * moving around more interfaces
      
      * re-add consensus crates into the mix
      
      * fix subkey path
      
      * relieve client from executor
      
      * starting to pull out client from grandpa
      
      * move is_decendent_of out of client
      
      * grandpa still depends on client directly
      
      * lemme tests pass
      
      * rename srml->paint
      
      * Make it compile.
      
      * rename interfaces->client-api
      
      * Move keyring to primitives.
      
      * fixup libp2p dep
      
      * fix broken use
      
      * allow dependency enforcement to fail
      
      * move fork-tree
      
      * Moving wasm-builder
      
      * make env
      
      * move build-script-utils
      
      * fixup broken crate depdencies and names
      
      * fix imports for authority discovery
      
      * fix typo
      
      * update cargo.lock
      
      * fixing imports
      
      * Fix paths and add missing crates
      
      * re-add missing crates
      60e5011c
  30. Oct 07, 2019
  31. Oct 02, 2019