barryWhiteHat
    • 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Call_context ## Intro Call_context is used to track the correct call context that the memory, storage and stack ops are applied to. ## Data elements ```rust struct CallContext { msg_sender: EthAddress gas_available: u256 gas_used_before_this_call: u256 // tells us if this transaction will throw or revert in the future is_persistant: bool is_static_call: bool // a count of the number of storage updates that need to be undone. revert_todo: u256 // This is the global counter minimum global counter that needs to be reverted. gc_to_revert_to: u256 call_depth: u256 } impl Commitable for CallContext { fn commit(){ return ( msg_sender + gas_available * r + gas_used_before_this_call * r**2 + is_persistant * r**3 + is_static_call * r**4 + revert_todo * r**5 + gc_to_revert_to * r**6 + call_depth * r**7 ) } } ``` The call_context is a random linear combination of 1. msg.sender 2. gas_available 3. gas_used_before_this_call 4. is_persistant 5. is_static_call 6. revert_todo 7. gc_to_revert_to 8. last_call_context r = hash(msg.sender .. last_call_context) $call\_context = msg.sender + gas\_available * r .. last\_call\_context *r ^5$ ## Terms ### is_presistant Is a binary flag that tells us if this transaction will throw or revert in the future ### revert_todo Is a count of the number of storage updates that need to be undone. ### gc_to_revert_to This is the global counter minimum global counter that needs to be reverted. ## Reverts/throws When we reach reverts/throws we need to do two things 1. Find how far back we need to revert 2. revert all storage operations until that point > When we reach op `REVERT`, it is actually a special error case we should not only do above two things, but also refund left gas (other errors consume all left gas). See Q4 for all other errors. > [name=han] ### How far do we revert 1. If gas_passed = gas available the two transactions are in the same revert_context and both will be reverted together. TODO: Confirm this is all we need to check how far to revert ? ### Storage When we are applying storage updates if the is_persitant flag = 1 nothing happens. We apply them as is. If it is_persistant == 0 we need to preapare to revert every storage write we do. This is done by counting every storage update that is done. We excute all sotrage update as they are defined in the smart contract. When we get to the point where the revert happens we undo all these updates. We basically force the prover to show us `todo_revert` storage writes that happened between `gc` and `gc_to_revert_to` . To make this easier storage write bus mapping will contain the value before it was written and the value that was written. For each element in this list we set that storage value to the value_before for `todo_revert` elements with `gc` < = `gc_to_revert_to` ## Questions: 1. Does static call throw on state writes or does it let you make writes as long as they are not persistant ? Assuming it does not let you make writes if you do it throws. Would be good to confirm this > In `STATICCALL`, it **reverts to snapshot** and **consumes all `gas_available`** when: > 1. `op` are able to modify state (`SSTORE`, `LOGX`, `CREATEX`) > 2. `CALL` with value > [Reference](https://github.com/ethereum/go-ethereum/blob/f05419f0fb8c5328dca92ea9fb184d082300344a/core/vm/interpreter.go#L208-L217) 2. How does reverts get stopped does it just revert the current call or can it revert others ? > It reverts others too. See this [example](https://github.com/ChihChengLiang/play-contracts/blob/main/contracts/RevertChild.sol). The parent delegate calls to child, who later revert a call to grandchild. The child's revert reverts the grandchild's state but not the parent's. 3. How does throw get stopped ? Does it just revert everything until there is a tx with more gas left ? > `throw` is just `revert` without keeping gas left, which is due to compiler's behavior (and `throw` is removed in solidity 0.5), they both use `op` `REVERT` in the end. > When any [errors](https://github.com/ethereum/go-ethereum/blob/master/core/vm/errors.go) happen, it always [reverts to the snapshot](https://github.com/ethereum/go-ethereum/blob/master/core/vm/evm.go#L224-L225) before `CALL`, `DELEGATECALL`, `STATICCALL`, and only error due to `REVERT` will keep the `gas_available`, others just consume all given gas. 4. Should storage be seperated by only address instead of call cotext? If we do multiple call to a child contract and change its storage, we need their storage to be continue, for example, contract `A` tries to trigger `B.counter++` multiple times, if we seperated them by different call context, the result won't be correct. > Yes > [name=barryWhiteHat] 5. If call context is only for seperatation (or namespacing) in state circuit, why not just use the `gc` at `CALL`? In the call context now, I can see only `gas_used_before_this_call` and `gc_to_revert_to` are unique, others are easily to collide with other calls. For example, I can have two identical call to same child contract. > Call context is also used as a digest of parent call and last child call, so all the things we need to memorize should be inside call context. 6. When a call is going to end, we have an awkward situation to handle: callee doesn't explicitly halt (opcodes doesn't ended with `STOP`, `RETURN`, `REVERT`), then evm will pad it with an explicit `STOP`. In each `op_lookup(addr, pc, op)` (not sure if it's the function signature in you idea), we need prover to explicitly tell us if we should do the lookup or not. If we do, we lookup for correct op, otherwise we know it's padded `STOP` and switch to parent context.

    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