Giuliano Mega
    • 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
    Block Exchange Protocol ======================= $$ \def\textsc#1{\dosc#1\csod} \def\dosc#1#2\csod{\text{#1}\small\style{text-transform: uppercase}{\text{#2}}} $$ $$ \newcommand{\swarmsample}{\textsc{SwarmSample}} \newcommand{\announcepeer}{\textsc{AnnouncePeer}} \newcommand{\have}{\textsc{Have}} $$ The protocol at a given peer $p$ is divided into two distinct components: 1. neighborhood (connection) manager; 2. block exchange manager manager. The neighborhood manager is responsible for maintaining a minimum set of $d$ peers within $p$'s network view. The block exchange manager, on the other hand, will manage block queries and uploads/downloads with surrounding peers, and may request additional or different neighbors to the neighborhood manager should that need arise. Peers can be of any of three distinct types: 1. **download/cache peers (DCPs)**. Downloads blocks from and uploads blocks to other peers in the swarm. May or may not hold a _viable copy_[^1] of the file; 2. **storage peers (SPs).** Only upload blocks to other peers in the swarm. Typically do not hold a viable copy of the file, unless they are also DCPs; 3. **DHT tracker nodes.** Keeps track of a (subset of) the peers that currently make up a download swarm. ## Base Protocol **Neighborhood bootstrap.** A peer $p$ joining a swarm to download a file $F$ starts by: 1. sending an $\announcepeer$ message, containing a peer descriptor with its network address and port, to the DHT tracker; 2. receiving a $\swarmsample$ message in response, which contains up to $d$ randomly chosen peer descriptors (where $d \in \mathbb{Z}^{+}$), and which should be used to bootstrap $p$'s neighborhood manager. ```nim= type PeerDescriptor* = object of RootObj peerId: Hash ip: IpAddress port: Port AnnouncePeer* = object of Message descriptor: PeerDescriptor SwarmSample* = object of Message sample: seq[PeerDescriptor] ``` **Figure 1.** Peer descriptors, $\announcepeer$, and $\swarmsample$. **Block knowledge bootstrap.** Once $p$ has access to a neighborhood, it attempts to map the set of blocks held by each neighbor by sending $\have$ messages to all of them, in sets of size $d_q \leq d$ at a time. Each $\have$ message contains a want list which encodes the full set of blocks that $p$ does not have on $F$. Note that the want list may contain more blocks than what is required for $p$ to obtaion a viable copy of $F$. ```nim= type Have* = object of RootObj wants: IntSet ``` **Figure 2.** A $\have$ message. A neighbor $q$ receiving a $\have$ message will reply with a $\have$ message containing its own wantlist of the blocks it does not have. This exchange allows $q$ to learn about the blocks $p$ currently has (and wants), and $p$ to learn about the blocks $q$ currently has (and wants). $p$ will keep track, for every neighbor $q$, of the blocks that $q$ wants and has. Once $p$ has exchange $\have$ messages with $q$, its knowledge about $q$'s blocks is _bootstrapped_. **Block knowledge maintenance.** Maintenance of block knowledge then happens as peers download blocks, verify their correctness (Merkle proof), and broadcast that knowledge to neighbors that want them. **Block download schedule.** As peer $p$ continuously updates its knowledge about which peers have which blocks, it contructs and maintains its _download schedule_. A download schedule is nothing but an ordered set of $(\text{peer}, \text{block})$ pairs where pairs are ordered by a scoring function $f$. There are several possible ways to build scoring functions. For instance, $p$ could privilege rare blocks as Bittorrent does, and could construct weighted combinations of peer speed/latency and a score for blocks. At first, we will start simple and implement: _i)_ random selection; _ii)_ rarest first. ```nim= proc download(self: Peer, F: File): while not self.store.hasEnoughBlocks(F): let downloadSlot = await self.acquireDownloadSlot() let (neighbor, blockDescriptor) = nextFromSchedule( self.peers, self.store, F) blockDescriptor.set(BlockState.downloading) if await downloadSlot.download(blockDescriptor, self.store, neighbor): blockDescriptor.set(BlockState.downloaded) announceBlock(blockDescriptor, self.peers) else: blockDescriptor.set(BlockState.pending) ``` **Figure 3.** Pseudocode for a file download. **Neighborhood maintenance.** In addition to maintaining block knowledge, $p$ needs to maintain its neighborhood as well. A neighbor $q$ is _elligible for replacement_ if one of the following holds: 1. $q$ is declared dead by $p$'s heartbeat failure detector; 2. $q$ fails to supply blocks to $p$ after $m$ attempts; 3. $q$ is a storage node and $p$ already has all the blocks $q$ has to offer. In any of those cases, $p$ will trigger a _resample_, in which it will ask from the DHT tracker for a new random peer. $p$ may optionally inform the tracker of which peers it already knows so as to avoid having those being returned. If no new peer can be found, $p$ waits for some amount of time, and tries again.[^2] [^1]: a set of blocks that is as large as the minimum number required to reconstruct the file. [^2]: simple exponential backoff could work as a first strategy here.

    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