HackMD
    • Sharing Link copied
    • /edit
    • View mode
      • Edit mode
      • View mode
      • Book mode
      • Slide mode
      Edit mode View mode Book mode Slide mode
    • 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
    • More (Comment, Invitee)
    • Publishing
    • Commenting Enable
      Disabled Forbidden Owners Signed-in users Everyone
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Invitee
    • No invitee
    • Options
    • Versions and GitHub Sync
    • Transfer ownership
    • Delete this note
    • Template
    • Save as template
    • Insert from template
    • Export
    • Google Drive Export to Google Drive
    • Gist
    • Import
    • Google Drive Import from Google Drive
    • Gist
    • Clipboard
    • Download
    • Markdown
    • HTML
    • Raw HTML
Menu Sharing Help
Menu
Options
Versions and GitHub Sync Transfer ownership Delete this note
Export
Google Drive Export to Google Drive Gist
Import
Google Drive Import from Google Drive Gist Clipboard
Download
Markdown HTML Raw HTML
Back
Sharing
Sharing Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
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
More (Comment, Invitee)
Publishing
More (Comment, Invitee)
Commenting Enable
Disabled Forbidden Owners Signed-in users Everyone
Permission
Owners
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Invitee
No invitee
   owned this note    owned this note      
Published Linked with GitHub
Like BookmarkBookmarked
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# FC finality/justificaion atomicity problem Paul pointed out that `store` can at times update finality non-atomically with justification. This is because of the (complex) logic in [Code-0](#Code-0) In such a case, this can short-circuit `get_filtered_block_tree` because there are no leaf states that know about the finalized/justified combo in the state. That is `correct_justified and correct_finalized` in [Code-A](#Code-A) never returns true for any leaf block, thus `get_filtered_block_tree` will return an empty `blocks` which causes `get_head` to simply return whatever is `store.justified_checkpoint.root` (see [Code-B](#Code-B)). ### More on the problem If we look again at [Code-0](#Code-0), we see that when newly finalized there are two times we might want to atomically update the justification: 1. When the newly justified is strictly better (greater than) what is in the store. Thus the `state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch` condition. 2. When `store.justified_checkpoint` is *higher* than what is in state (thus bypassing (1)), but it is on a chain that doesn't know about the finalized root. It turns out that (2) is misguided. It can lead to the `store` having a combination of finalized and justified checkpoints that *does not exist* in any leaf state. Thus `get-filtered_block_tree` will return an empty dict, allowing `get_head` to return non-sensical values. ### The solution You could imagine putting an even stricter clause for (2) -- That is, only keep the previous higher justified if there exists a leaf in the block-tree that has a state that has both the newly finalized and the previous higher justified (from the `store`) in that state (as `state.finalized_epoch` and `state.current_justified_epoch`). Although such a condition would be *correct*, it would never be hit because if such a leaf block with such a state existed, then `store.finalized_epoch` would have already been updated with the previous higher justified we are concerned about when such a leaf block was imported. This is not the case because we are only now updating `store.finalized` on this other branch, thus such a leaf block *does not exist*. Due to the above, the entire (1) and (2) are misguided conditionals and can be entirely removed. Instead `on_block` "Update finalized checkpoint" section can be written as follows, always atomically updated finalized and justified: ```python # Update finalized checkpoint if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch: store.finalized_checkpoint = state.finalized_checkpoint store.justified_checkpoint = state.current_justified_checkpoint ``` ### Code-0 Code snippet from phase0 [`on_block`](https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#on_block) *The main idea is that there are two cases we want to update justified when we update finalized*: 1. When the newly justified is strictly better (greater than) what is in the store. Thus the `state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch` condition. 2. When `store.justified_checkpoint` is *higher* than what is in state, but it is on a chain that doesn't know about the finalized root. It turns out that (2) is misguided. It can lead to the `store` having a combination of finalized and justified checkpoints that do not exist in any leaf state. Thus `get-filtered_block_tree` will return an empty dict. ```python # Update finalized checkpoint if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch: store.finalized_checkpoint = state.finalized_checkpoint # Potentially update justified if different from store if store.justified_checkpoint != state.current_justified_checkpoint: # Update justified if new justified is later than store justified if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch: store.justified_checkpoint = state.current_justified_checkpoint return # Update justified if store justified is not in chain with finalized checkpoint finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch) ancestor_at_finalized_slot = get_ancestor(store, store.justified_checkpoint.root, finalized_slot) if ancestor_at_finalized_slot != store.finalized_checkpoint.root: store.justified_checkpoint = state.current_justified_checkpoint ``` ### Code-A Code snippet from phase0 [`filter_block_tree`](https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#filter_block_tree) *If `correct_justified` and `correct_finalized` are never seen as a combo in any leaf state, then `filter_block_tree` will return an empty tree.* ```python # If leaf block, check finalized/justified checkpoints as matching latest. head_state = store.block_states[block_root] correct_justified = ( store.justified_checkpoint.epoch == GENESIS_EPOCH or head_state.current_justified_checkpoint == store.justified_checkpoint ) correct_finalized = ( store.finalized_checkpoint.epoch == GENESIS_EPOCH or head_state.finalized_checkpoint == store.finalized_checkpoint ) # If expected finalized/justified, add to viable block-tree and signal viability to parent. if correct_justified and correct_finalized: blocks[block_root] = block return True ``` ### Code-B Code snippet from phase0 [`get_head`](https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/fork-choice.md#get_head) *If get_filtered_block_tree returns and empty `dict` then the first iteration of `while` loop will have empty children which will return `head` which is the value it was initialized at -- `store.justified_checkpoint.root`* ```python blocks = get_filtered_block_tree(store) # Execute the LMD-GHOST fork choice head = store.justified_checkpoint.root while True: children = [ root for root in blocks.keys() if blocks[root].parent_root == head ] if len(children) == 0: return head ```

Import from clipboard

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 lost 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?

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

Sign in via SAML

or

Sign in via GitHub

Help

  • English
  • 中文
  • 日本語

Documents

Tutorials

Book Mode Tutorial

Slide Example

YAML Metadata

Resources

Releases

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

Versions and GitHub Sync

Sign in to link this note to GitHub Learn more
This note is not linked with GitHub Learn more
 
Add badge Pull Push GitHub Link Settings
Upgrade now

Version named by    

More Less
  • Edit
  • Delete

Note content is identical to the latest version.
Compare with
    Choose a version
    No search result
    Version not found

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. Learn more

       Sign in to GitHub

      HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

      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
      Available push count

      Upgrade

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Upgrade

      Danger Zone

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

      Syncing

      Push failed

      Push successfully