Michał Kucharczyk
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    ## Fork-aware transaction pool ### Recap: current transaction pool architecture This section is for readers who may not be familiar with current design of transaction pool. It briefly presents the state of the art. #### Transaction: provides and requires tags In substratge transactions ordering is implemented through the *required* and *provided* tags (introduced [here](https://github.com/paritytech/substrate/issues/728)). Tag is an opaque vector of bytes. The transaction pool does not interpret them. Tags are computed by the runtime during call to [`validate_transaction`](https://github.com/paritytech/polkadot-sdk/blob/ea4085ab7448bb557a1558a25af164cf364e88d6/substrate/primitives/transaction-pool/src/runtime_api.rs#L49-L53) function and returned in the [`ValidTransaction struct`](https://github.com/paritytech/polkadot-sdk/blob/ea4085ab7448bb557a1558a25af164cf364e88d6/substrate/primitives/runtime/src/transaction_validity.rs#L263-L272). Each transaction may require certain tags, and each transaction provides some tags. The transaction is ready to execute if *required* tags are satisified (by other transaction or chain of transactions that are also ready) or empty. Otherwise the transaction is future. Common example is [account nonce](https://github.com/paritytech/polkadot-sdk/blob/d4c426afd46f43b81115911657ccc0002a361ddb/substrate/frame/system/src/extensions/check_nonce.rs#L116-L121). #### Submit New transactions are submitted to the transaction pool at a specific block. The pool validates the transaction with the runtime against the state for this block, and subsequently places the transaction in either the *future* or *ready* pool. Upon importing a new transaction into the transaction pool, a notification is dispatched to the listeners. #### Single view on ready/future transactions The singular instance of the structure called [`BasicPool`](https://github.com/paritytech/polkadot-sdk/blob/ea4085ab7448bb557a1558a25af164cf364e88d6/substrate/client/transaction-pool/src/graph/base_pool.rs#L211-L221) holds two sets of transactions: *ready* and *future*. This single *view* to transactions within the pool is being continuously updated. The update process is driven by two events: *new best block* and *finalized*. These events activate two internal processes in transaction pool: maintanance and revalidation which are responsible for updating *ready* and *future* sets. #### Maintanance It is a process triggered by *new best block* or *finalized block* notification. This process: - removes (prunes) the transactions included in the notified block from the transaction pool, - checks what tags were provided by the transactions included in the notified block, and updates *ready* and *future* sets within the transaction pool. *New best block* (or *finalized block*) events may be reported for blocks originating from a different fork compared to what the transaction pool is aware of. If that is the case, transactions needs to be resumbitted from blocks on the retracted fork and the transactions on the enacted one should be pruned. #### Revalidation It is a process that periodically validates transactions from the pool against the tip of the chain that the transaction pool is aware of. Obviously, it updates ready and future sets, triggered by every maintenance. #### `ready_at` Provides the future that resolves to an iterator over the ready transactions for the requested block. `ready_at` operates on the block height and, by itself, does not perform any computations. It internally waits for the maintenance process to be finished. #### `import_notification_stream` Every transaction imported to the transaction pool is sent to this public stream, allowing listeners to be notified about newly imported transactions. #### Problems The `ready_at` method operates on block height. It is not synced with maintanance process in terms of forks. This results in two major issues: - when building new blocks on top of block which are not *best* or *finalized*, the `invalid::stale` error will occur. This is because the transaction pruning was not executed on block import, ``` A -- B0[u0] -- C0[..] // If the maintanance was *not* triggered for B0, // ready_at will provide u0 when building C0 , // (which is stale from B0 perspective) ``` - when constructing new blocks on an alternative fork, the `invalid::future` error might arise. This occurs when blocks on the alternative fork lack transactions that serve as prerequisites for transactions present in the ready pool. As the maintained for contains these prerequisite transactions the ready set would comprise transactions that are considered future on the alternative fork. See the figure below: ``` B1[u0,u1]--C1[u2] //u3 is ready, after maintanance was triggered for C1 / A \ B0[t0,t1]--C0 //when building C0 ready_at will provide u3, //which is future from B0 perspective ``` ### Fork awarness proposal #### View All transactions are simply kept in transaction pool internal array as long as they are referenced. The view is a structure that contains two sets of transactions: *ready* and *future* at given block (similar to existing `BasePool` struct). The view keeps references. The view provides *ready* iterator that can be used by the block builder. The *graph* part of existing transaction pool implementaion can be re-used for this. The view is created for imported blocks. This can be done on every block, but can be customized and for consensus which build only on top of the best block, the view may be created for best blocks only (to mimic current behaviour). #### `ready_at` `ready_at` operates on block hash, and simply returns iterator for a view associated with requested block. #### Destroying views Views are destroyed when: - block is finalized (all views for blocks height lower then finalized block are destroyed), - the state for given block is pruned (not sure if we have notification for this), This maybe not be enough for some consensus (e.g. PoW), so LRU cache of views can also be maintained. Maybe it should be a hybrid solution. #### Submit When a new transaction is submitted, we update all blocks starting with height greater than *at block* used for submission. Same applies when transaction is replaced. #### Open questions *How to handle transaction longevity?* Simply check when including to *ready* or *future*. If transaction is stale, we simply do not include it. (And we have valid till field). *Transaction replacement:* Implementation detail: do we need map: `tx-hash -> Vec<block-hash>` ? It could help to find views that needs to be updated. *Is resubmission needed?* No. In this approach resubmission would mean that we take transactions from every imported block and put them into transaction pool if they are not already included. This should not be required - transaction (intended to broadcast) were already gossiped. This would also implicitely put the transactions to the other nodes, even those which are not intended to [propagate](https://github.com/paritytech/polkadot-sdk/blob/ea4085ab7448bb557a1558a25af164cf364e88d6/substrate/primitives/runtime/src/transaction_validity.rs#L283). *Is revalidation needed?* No. All block has a valid up-to-date set of *future* and *ready* transactions. There is no need to do additional revalidation process. *Race between block import and ready_at call?* Are we able to prepare *ready* set quick enough? The solution to this potential problem: `ready_at` could be a channel (or iterator over lockfree queue) which can be fed by view. So validation at given block and block building upon this block can happen partially in parallel. *Do we need tree-route?* Yes. If blocks are not reported one-by-one then we may need pre-computed tree-route to optimize view building. #### Are all scenarios addressed? - new block is imported, - new best block is notified, - finalized block is notified, - block is pruned, - new transaction is submitted, - transaction is replaced - warp sync (cpu usage, don't do useless work) - sync (cpu usage, don't do useless work) - *anything missing?*

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully