Cayman
    • 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
    # Lodestar SSZ Design Doc ### Types SSZ defines a set of types that can be composed to build application-level datatypes. ##### Uint - uints have a fixed bytelength of 1, 2, 4, 8, 16, or 32 ##### Boolean - true / false ##### List - elements must be of a single type - lists have a limit, a max number of elements ##### Vector - elements must be of a single type - vectors have a fixed length ##### Container - collection of key-value pairs - fixed list of keys (their order is specified) - each value has a specific type ##### BitList - list of bits, can be treated as a list of booleans ##### BitVector - vector of bits, can be treated as a vector of booleans ```typescript type SSZType = Uint | Boolean | List<SSZType> | Vector<SSZType> | RecordLike<string, SSZType> | BitList | BitVector; ``` We can imagine a `Type` interface/class that gives us a nice place to store type specifics and methods. ```typescript interface Type {...} class UintType implements Type {...} const Uint32 = new UintType({byteLength: 4}); ``` ### Operations SSZ-typed datastructures can be operated on in several different ways. ##### Serialization / Deserialization - data can be unambiguously serialized into a bytestring ##### Hash Tree Root / Root Expansion - heirarchical data can be mapped into a merkle tree with stable indices for each data field - data fields corresponding to merkle tree roots can be 'expanded' into their full or partial typed data ##### Get / Set field/index values - heirarchical data can be fetched by field or index ##### Clone / Equals - data can be relied on to be well-typed - data can be copied, new values can be created from defaults, equality between values of the same type can be established A type can provide methods for these operations. ```typescript interface Type<T> { hashTreeRoot(value: T): Uint8Array; serialize(value: T): Uint8Array; deserialize(data: Uint8Array, options: BackingOptions): T; defaultValue(options: BackingOptions): T; createValue(value: any, options: BackingOptions): T; clone(value: T, options: BackingOptions): T; equals(a: T, b: T): boolean; } ``` Note the type must be given "backing options" for operations that create a new value. In some cases, these operations may be methods on the value itself. ```typescript interface SSZValue<T> { hashTreeRoot(): Uint8Array; serialize(): Uint8Array; clone(): SSZValue<T>; equals(other: SSZValue<T>): boolean; // get and set [fieldOrIndex: string | number]: SSZValue<T[keyof T]>; } ``` ### Backings The ssz value's "backing" is the data structure that is to be interpreted as a certain type via type information. In Eth2, ssz data is used in many different places, for many different purposes. The form/backing the data takes depends on the usecase at hand. Some examples: - the _structural_ form is currently used for application-level logic - eg: the beacon state transition requires lookups for eg: `block.body.deposits[i]` - the _merkle tree_ form is used to generate/consume proofs - eg: server X uses a merkle tree to generate a multiproof for client X - eg: client Y receives a proof, corresponding to a partial merkle tree form of a BeaconBlock and must interpret the proof as an ssz object - the _serialized_ form is sent/received off the wire - eg: client Z received a byte array, a serialized Attestation that must be validated These three representations of ssz data have differing tradeoffs for the known ssz operations: (this is very simplified and there are more intricacies in practice) | operation | structural | merkle tree | serialized |---|---|---|--- |hashTreeRoot | N log N | 1 | N log N |serialize | N | N | 1 |deserialize | N | N | 1 |size | N | N | 1 |clone | N | 1 | N |equals | N | 1 | N |create | N | 1 | 1 |get | 1 | log N | 1 |set | 1 | log N | 1 Edit from Proto: - structural/merkle tree: - size: O(1) for static, and commonly less than log(N) for simple non-nested dynamic structures, although O(N) for lists of dynamic elements (avoided in eth2 spec). - merkle tree: - (de)serialize: O(N), but with significant overhead over structural. I.e. no range copies. (but may matter more for Go/Rust). Note that certain backings and certain datatypes may allow for an extended set of operations and that these are simply the shared operations core to all ssz objects. **If we can provide the same interface for these different formats, this will be a huge usability and code-reuse win.** #### Structural Backing In this form, data is backed by native language types corresponding best to ssz data types of the type definition. Practically speaking, this means using `Object`s, `Array`s, `boolean`s, `number`s, etc. to compose the data. eg: - A uint is a `number` or `BigInt` object - A boolean is a `boolean` - A list or vector is an `Array` - A list of vector of bytes is a `Uint8Array` - A bit list is a `BitList` object - A bit vector is a `BitVector` object - An container is an `Object` This backing is _convenient_ to create and manipulate because of the close relationship between the backing and interpretation. It is also trivial to get/set properties, since this corresponds to simple object property lookup. ```typescript const block: BeaconBlock = {...}; const slot = block.slot; const root = BeaconBlock.hashTreeRoot(block); ``` #### Merkle Tree Backing In this form, data is backed by a linked datastructure that corresponds to nodes in a merkle tree. The tree may be fully formed or stubbed out partially. Any interpretation of tree nodes is external to the tree backing itself. This backing is great for generating and consuming proofs. It can additionally be used to share data between objects if 'immutable, persistent' tree node management is used. An ES6 `Proxy` handler is used to provide methods and getter/setter capabilities that handle type conversion to/from the relevant types. ```typescript import { Tree } from "@chainsafe/persistent-merkle-tree" const backing: Tree = ...; const block: TreeBacked<BeaconBlock> = BeaconBlock.tree.asTreeBacked(backing); const slot = block.slot; const root = block.hashTreeRoot(); ``` #### Serialized Backing In this form, data is backed by a byte array that is the serialized form of the data. Lookups and operations are done from this serialized form, rather than fully deserializing the data first. If data is received in a serialized format, or space is a concern, it may be appropriate to use the serialized backing to operate on the data. An ES6 `Proxy` handler is used to provide methods and getter/setter capabilities that handle type conversion to/from the relevant types. ```typescript const backing: Uint8Array = ...; const block: SSZValue<BeaconBlock> = BeaconBlock.serialized.asByteArrayBacked(backing); const slot = block.slot; const root = block.hashTreeRoot(); ```

    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