son
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Part 3 : Why share ? Understanding the BitTorrent economics . All the peer connections are symmetric i.e data flow is not one way and each peer can download and upload data simultaneously . But why should I contribute in the swarm and increase my data usage ? Why should't I just maintain a faster download speed ? *Womp, Womp* . If you won't share data chunks , you won't receive any. BitTorrent uses a **tit for tat exchange scheme** . Peers chose those peers to share data with who reciprocate the same . As a BitTorrent peer you will only have limited number of upload slots to allocate to other peers . So if you are uploading data to a peer who in return is not uploading any data to network then the BitTorrent will choke connection with this peer and try to alocate this upload slot to a more coorperative peer . ## 3.1 How can I participate in swarm when I have no pieces to trade ? When a new peer joins the network it doesn't have pieces to participate in trade and hence this **tic to tac exchange scheme** will prevent it from participating. To avoid and counter such effects a mechanism called "optimistic unchoking" is used. Periodically , the upload slot will be randomly allocated to an uncooperative peer . This gives chance to new peers to join in , and second chance to previously non-coorperating peers. --------------------------------- ## 3.2 But how do I discover and connect to a peer ? Although the BitTorrent creates a decentralised file-sharing system there still exists a crucial centralised point(i.e. _tracker_) for maintaining the list of peers. The tracker acts as an intermediary that helps all the peers discover each other and also maintains the number of seeders and leechers available for a particular torrent. But the need for a tracker is only limited to the discovery of peers and once connection between peers is established , the need for trackers is not significant and P2P communication can go on without its need. ### 3.2.1 Promblems with trackers : If the trackers goes down for maintainance or due to technical issues then the updated lists of peers couldn't be fetched making it difficult to share files. Trackers handle a large number of requests from peers creating significant bandwidth demand on the tracker's server .Hence if the publishers is seeding some popular content they need to invest more in the infrastructure which might be very costly. Seems like bit of a problem to me. #### So, Any Solution ? **_YUP_** . A _trackerless torrent_ solves the problem. A _trackerless_ refers to a BitTorrent download that operates without relying on a central tracker server for peer discovery. One common method for such decentralised peer discovery is Distributed Hash Table(DHT). ## 3.3 Distributed Hash Tables To eliminate the need of a central tracker , each peer must themselves hold a list similar to that of the tracker. But the list of tracker is too large to be stored individually with each peer. Hecne each peer maintains a partial list of other peers and the pieces of files they possess. The DHT works with _info-hash(i.e. hash of metadata)_ as the key and peer-information(IP addresses and ports) as the value. Hence the data of the peer swarm is stored in the DHT . The BitTorrent uses Kademlia as it's DHT. Now let's try to understand what Kademlia is ? _~~I have tried my best to simpify it as much as possible T-T.~~_ ### Kademlia DHT #### A Neighbourhood Analogy To understand how the Kademlia works , I have a small analogy to present . Imagine there are 128($2^7$) houses in a neighbourhood ,not neccessarily all occupied . Each house with a family in it is assigned with a pencil , eraser and a copy with 7 pages (chosen as 128= $2^7$) and space for 2 entries per page . These pages are used to write the phone number of the houses(occupied) in the neighbourhood with some rules. The rules are stated below : * Each page say page number `i`(`i` from `0` to `6`) can only have the phone number of houses at a distance from $2^i$ and $2^{i+1}$ - 1 * Each page can atmost have 2 phone numbers, no limit on minimum. Now suppose I want to know details about a household at a distance of 78 units from my house . Hence I must try to call the house which is as close as to the target house. So now i will dial a number from page no.6 as it contains phone number of houses with distance of 64 to 127. Now I dial up one of the numbers out of the two written on the list. It appears that the house I called is at a distance of 14 from the target house . So it will in turn again follow a similar procedure , turn up to page 3 of it's own list (contains phone number of houses with distance between 8 and 15) and call a house . And this process continues untill I get closest to/reach the target . But this is just not it yet . There are some more aspects missing in this analogy. This distance of ours is assumed to be special. Special in what sense might be the question in your mind . First , The distance of the two houses whose phone number are written on page `i` are always at distance less than $2^i$ . How is this possible might be a question in your head . So consider 7 circles with their diameters as 1,2,4,8,16,32 and 64 . ![image](https://hackmd.io/_uploads/rJrrS7fNkl.png) You can see if 2 points lie in a circle the maximum distance they can be at is the diameter of that circle . Hence you can imagine the neighbourhood to look like this from each house . Second , no 2 houses can be equidistant to the same house , i.e. if house A and B are at a distance of 2 units then House A and C or B and C can not be at a distance of 2 units.Don't worry too much about thinking about implementing this condition geometrically. What purpose this special distacne serve . It ensures that it takes at max 7 calls to reach the desired node (distance reduces geometrically). Another advantage of it is that our target can uniquely identified by distance from current house as no other house is at same distance from the current one. Now there is one last aspect of the algorithm analogy is to make use of our limited copy space to full extent . If initially when I called the house of page no `6` , as this house is at distance of greater than 64 from my house hence my house is also at distance greater than 64 from this house(obvious actually) , so if this house were to store my number it would store it in the page `6` of it's notebook. Now considering you can only store 2 phone numbers on each page so it is in best interest to store the phone numbers of houses that are active(actively pick up your call). So now if this house which I initially called had less than 2 numbers written on it's page `6` , it will note my number with the time of contact . If it already has 2 contacts it will now contact the phone which was least recently called . Now there are two possibilities : 1) This least recently contacted house doesn't pick up the call, hence proving to have become inactive . In this case this house will save my number . 2) This least recently contacted house picks up the call proving that it is still active . In this case my number will not be saved and the time of contact of this least recently contacted house will be updated to most recently contacted . Now in the actual Kademlia DHT these houses are basically the hash-value and instead of 128($2^7$) possibilities we have $2^160$ possibilites as we use 160-bit space. The distance between the hashes is calculated by xoring them and it follows the properties said above . The 7 pages of the above the above notebook in actual are 160 different lists called K-buckets where K corresponds to the maximum number of entries in each list(in above analogy it was 2). Now let's formally define the Kademlia DHT . #### Kademlia DHT The Kademlia DHT uses a 160-bit space i.e. hashes of metadata will have 160-bits in it ( _e.g.,SHA-1_ ).Let us call this 160-bits space as **key space**. Each participating computer is called a _node_ and is assigned an ID(called _node ID_) in the **key space**. The <key,value> pairs are stored on nodes that are close to the key values. This closeness is defined by the XOR Distance. | Term | Definition | |--------------|----------------------------------------------------------------------------| | Xor Distance | The distance between node and the key is defined as their **bitwise exclusive or (XOR)**. | --------------------- Why XOR distance though? Well,consider a 4 bit-binary number 1001.If this is the target is to be reached what is the most natural way to select from all 4-bit binary numbers ($2^4$ numbers) . Start from the left-most bit and drop the numbers from consideration if their bit doesn't match. So all numbers with 0xxx will be dropped and 1xxx will only be considered, so in one go we dropped 8 numbers. Now again we repeat this process for second left-most bit and drop the numbers with bits as 01xx, hence again droppin 4 numbers from consideration. Now repeating the same process again ,2 more times,we will reach the target. So it can be said that for a n-bit number in n steps we can reach the target by comparing if the left-most bit at each step matches our target or not . Now how does XOR distance facilitate us in this comparision. Here is the XOR table before I exlain further : | A | B | A ⊕ B | |---|---|-------| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 0 | Now as we can say for same bit in two numbers A & B the XOR operator results in 0. So we can clearly state that the more the bits(from the left-most end) of node match with the target the lesser the value of XOR distance it will assume . -------------------------------- Kademlia stores data about nodes in the lists called _$k$-Buckets_. Each bucket ,indexed with i : 0 $\leq i <$ 160 , stores triples ==<IP Address , Port Number and Node ID>== for nodes of distance between $2^i$ and $2^{i+1}$ . For closer buckets , i.e. smaller $i$ , the bucket can sometimes be empty. And for large values of $i$ the bucket might contain upto $k$ number of triples. ![image](https://hackmd.io/_uploads/BJPbVOt8R.png) What $k$ signifies is the maximum numbers of numbers triples stored in a bucket. The number $k$ is chosen such that any given $k$ nodes are very unlikely to fail (stop replying to requests) within an hour . Generally the $k$ is taken to be 20 . ##### Now we have information stored on the nodes, let's understand how does the lookup takes place . The lookup starts from the the very first _$k$-bucket_ for the node closest to the key(called target.) . If the target node is not found in the closest $k$-bucket, the initiating node queries nodes in the next closest $k$-bucket. As the distance used is the ==XOR Distance== each step increases the common prefix between the node and the key by atleast one . Hence this process continues untill the initiating node reaches closest node to the target . Each node has a <key,value> pair list for the closest keys and it is stored in it's **local storage** (different from DHT). The value for the key from the closest node to the target is hence fetched from this list . This is how the trackerless torrent is able to share data chunks without a central system .

    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