Learn Solidity (0.5) - Merkle Tree

Поделиться
HTML-код
  • Опубликовано: 23 дек 2019
  • Learn about Merkle tree and merkle proof using Solidity.
    Code: solidity-by-example.org/app/m...
    Remix IDE: remix.ethereum.org
    Solidity: solidity.readthedocs.io
    Follow on Twitter: @ProgrammerSmart / programmersmart
    Website: smartcontractprogrammer.com
  • НаукаНаука

Комментарии • 85

  • @startat3098
    @startat3098 2 года назад +30

    just noticed that you are the author of the"solidity by example" which I have been using. Kudos for you.

  • @JohnSpencertheFirst
    @JohnSpencertheFirst 2 месяца назад

    I don't know what I'd have done without these videos man. Very well explained.

  • @akinyemidayotafiq
    @akinyemidayotafiq Год назад +1

    Merkle tree is a complete binary tree, which allows one to calculate all of the information about its parent node simply by been able to differentiate the left and right nodes. I'm glad that MEXC have also adopted it

  • @SnoozeDog
    @SnoozeDog 2 года назад +1

    most underated solidity channel by far

  • @kennycol1234
    @kennycol1234 Год назад +1

    Perfect explanation, thank you!

  • @anastasiasukhovii1755
    @anastasiasukhovii1755 3 года назад

    Really good explanation! Loved it

  • @moeidmandegar1033
    @moeidmandegar1033 6 месяцев назад

    you did a great job explaining it . thank you

  • @highrankin
    @highrankin 2 года назад

    Great explanation dude. Super helpful

  • @user-dp3qk7le8h
    @user-dp3qk7le8h 4 года назад

    Great tutorial! Thanks!

  • @MrCoreyTexas
    @MrCoreyTexas Месяц назад

    The code in TestMerkelProof assumes you have a list of leaves that is a power of 2 in length, making it generic would be a great programming exercise.

  • @eyalovadya2562
    @eyalovadya2562 2 года назад

    Great explanation! Thanks :)

  • @CNOD-lg6mv
    @CNOD-lg6mv 15 дней назад

    Jesus, how can you explain so clearly😂, 🐐 always!!!

  • @salem232
    @salem232 4 года назад +10

    Absolutely awesome visual !! Very helpful !! But probably need to watch a few time to digest. 🎄 + 🎅 is super 😄

    • @smartcontractprogrammer
      @smartcontractprogrammer  4 года назад +2

      I didn't explain it in the clearest way possible either

    • @salem232
      @salem232 4 года назад

      Smart Contract Programmer ???!!! I thought it was very well explained!!! Just with my limited brain power 🤯 need to process it slowly 😯

    • @kiranvysya
      @kiranvysya 4 года назад +3

      @@smartcontractprogrammer she spoke my mind off. probably you need to define what is proof, leaf, index, root then being the detailed part. but not a big deal a quick google would do

    • @smartcontractprogrammer
      @smartcontractprogrammer  4 года назад +2

      @@kiranvysya Thanks for the feedback. I will explain stuff in more details in future videos

    • @kiranvysya
      @kiranvysya 4 года назад

      @@smartcontractprogrammer thanks wish you more success

  • @subhajitdas2379
    @subhajitdas2379 2 года назад

    Nice explanation

  • @j2ab55
    @j2ab55 2 года назад +3

    Great video but slightly confused how we are generating the proofs? We are doing this in the second contract (Test Merkle Proof) right? We are looping through the data/transactions, creating the hashes (aka proofs), and storing them in the "bytes32[] public hashes" array? Also what we are calling "bytes32 public hashes" later becomes bytes32[] public proof which we use in the first contract when we are verifying? In essence "bytes32 public hashes" is our "bytes32 public proof" or do i have something mixed up here?
    And with respect to ....
    while (n > 0) {
    for (uint i = 0; i < n - 1; i += 2) {
    is it "i +=2" in the for loop because we are appending everthing to index 2, aka the 3rd transaction? Is that how we are calculating the rest of the proofs?

  • @litlitty586
    @litlitty586 2 года назад

    How would you generate the 3rd leaf, root, and 2 proofs?

  • @rattle_
    @rattle_ 4 года назад

    Nice!

  • @trndsttr7585
    @trndsttr7585 Год назад

    Pretty poetic.

  • @mohammedghilace5223
    @mohammedghilace5223 4 года назад +1

    Nice vidéo 💜💜💜💜💜💜

  • @aatkaKhan
    @aatkaKhan Год назад

    I didn't understand, that how can anyone get to know the index number of their address? Can anyone explain this please.

  • @dev.regotube
    @dev.regotube 3 года назад

    Merry Chirstmas!

  • @thehard-coder9398
    @thehard-coder9398 Год назад

    Thank you for creating such great video. Would you mind to show how to generate Merkle Proof?

  • @cyberdisco9724
    @cyberdisco9724 2 года назад

    Does calculating the proof array require calculating the hashes of all nodes of the tree? It seems the highest indexed element of the proof array would require computing all hashes that came before it. Or is there an easier way to access the proof array? Like accessing only the needed elements from a saved list of all hashes of the tree.

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад +1

      simplest solution is to log all the leaf hashes and re-compute all the hashes to produce the proof

  • @Ch1no321
    @Ch1no321 2 года назад

    Ive watched this 2 times, I still dont understand how the example turns "i" which is 0 in the first iteration into index 3. proof[0] -> index 3?
    Is the loop condition correct?

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад +2

      proof[0] = hash(arr[3])
      proof[1] = hash(hash(arr[0]), hash(arr[1]))
      proof[2] = hash(hash(hash(arr[4]), hash(arr[5])), hash(hash(arr[6]), hash(arr[7]))
      i = 0 index = 2 hash = keccak256(hash, proof[0])
      i = 1 index = 1 hash = keccak256(proof[1], hash)
      i = 2 index = 0 hash = keccak256(hash, proof[2])

  • @pankajjoshi8292
    @pankajjoshi8292 2 года назад

    Implies that to add transaction we need to wail till transaction are in the form of 2 to pow n ?

    • @inbloom1997
      @inbloom1997 2 года назад

      Nope, the leaf nodes get duplicated so that the number of leaves is a power of 2, then you can compute the tree (1:19)

  • @SK-eu4lh
    @SK-eu4lh Год назад

    Thanks for the video!
    So for the entire blockchain, in which the merkle tree can be gigabytes of data - you will need all that data (basically a list of all txs in order) to be able to get the correct hashes to recompute the root and verify that a tx is indeed in the blockchain?

    • @smartcontractprogrammer
      @smartcontractprogrammer  Год назад +1

      Yes in theory. In practice I think it does other clever cryptographic tricks to save space

  • @amosdominion9502
    @amosdominion9502 Год назад

    Why are those four hashes significant??

  • @Picker22
    @Picker22 11 месяцев назад

    Thanks a lot

  • @shashwat.P
    @shashwat.P 2 года назад +1

    could you elaborate on what does the proof array actually contain?
    does it contain the all the leaf nodes or just the hashes required to build to the root using our leaf?
    If it is the latter, which i think it is, i cannot understand how through the variable 'i' we are accessing the required hashes. How are the actually stored?
    in the example used in the video, is this how hashes are stored proof=[hash(0,1),hash(3),hash(4,5,6,7)] ?

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад +1

      > does it contain the all the leaf nodes or just the hashes required to build to the root using our leaf?
      just the hashes required to build the Merkle root
      > is this how hashes are stored proof=[hash(0,1),hash(3),hash(4,5,6,7)] ?
      [hash(3), hash(0, 1), hash(4,5,6,7)]

    • @shashwat.P
      @shashwat.P 2 года назад

      @@smartcontractprogrammer thank you for the response, it is clear now. Completed this playlist. it was really helpful

  • @timelordm1564
    @timelordm1564 3 года назад +1

    Hi great video thanks. I see some people including openZeppelin do it without index, does this has special requirements!? Like sorted leafs ?

    • @smartcontractprogrammer
      @smartcontractprogrammer  3 года назад +1

      By sorting, you can prove that an element is in or not in the merkle tree.
      Without sorting, you can only prove that an element is in the merkle tree

    • @timelordm1564
      @timelordm1564 3 года назад

      @@smartcontractprogrammer wow thanks 👏🏻

    • @jagadishk5827
      @jagadishk5827 2 года назад +1

      Can you give the link to explain sorting in merkle proof ?

  • @sumerrana6805
    @sumerrana6805 Год назад

    but
    why light theme?

  • @markod3957
    @markod3957 Год назад

    It was my understanding that if a Merkle tree has odd leaves, we assume the current hash of the odd child to be the parent on the next level, not populating the tree with duplicate transactions?
    Guess it depends on what sort of encoding you want to do for your tree, but I assumed that was the standard (e.g. for a transaction trie with say 7 transactions)

  • @g.barios5805
    @g.barios5805 2 года назад

    awesome! excuse me but I don't get it. In 3:00 if you want to check if tr3 is in the block why you just check if the hash is the same that the hash in position 3?

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад

      how do you check that the hash in position 3 is correct?

    • @g.barios5805
      @g.barios5805 2 года назад

      @@smartcontractprogrammer well if you know what your transaction 3 is, you can hash it and compare with the hash. I'm sure I'm missing something here

  • @iliya5741
    @iliya5741 2 года назад

    Nice tutorial. But a question where is the code ? I clicked the link provided for the code but there is no code in that page

  • @user-re3zh7xm9x
    @user-re3zh7xm9x 4 года назад +1

    Sorry, how did You find proof hashes? And can this be done automaticly by leaf?

    • @smartcontractprogrammer
      @smartcontractprogrammer  4 года назад +3

      the proof hashes are computed by hashing the hashes of the left and right branch of the tree.
      Yes you can compute the proof hashes from the leaf, but the point of merkle tree is that you don't need all leaves to compute the root hash.

    • @mrkevinlui
      @mrkevinlui 2 года назад

      @@smartcontractprogrammer I think his question who supplied the proof array, which is basically the list of sibling hash node at each level from the bottom up.

  • @katari8604
    @katari8604 3 года назад

    why would your function require customly cherrypicking the N different hashes instead of all the hashes as generated in the constructor in TestMerkleProof ?

    • @smartcontractprogrammer
      @smartcontractprogrammer  3 года назад

      that's the idea of merkle tree. You don't need all data to compute the merkle root.

    • @katari8604
      @katari8604 3 года назад

      @@smartcontractprogrammer aha so you just need log2 n + 1 items but the ordering has to be very particular for the algorithm to work. from the commented code it wasn't so obvious as to what is going on

  • @ibrahimshehuibrahim918
    @ibrahimshehuibrahim918 2 года назад

    man you are damn Good kudos🤭

  • @bradleywoolf3351
    @bradleywoolf3351 2 года назад

    how did you generate the actual merkle root from the array of strings?

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад +1

      1. hash the array elements
      2. hash each pair of hashes
      3. repeat 2 until there is only one hash

    • @bradleywoolf3351
      @bradleywoolf3351 2 года назад

      @@smartcontractprogrammer are you able to search by the specific data point? like if i were looking for an address, would i pass that in as the bytes32[] proof argument? or would it be the hash of the proof as a leaf?

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад +1

      Merkle tree cannot be used to search for a specific address.
      It's used to prove that some array element is included in the tree by providing the proof in bytes32[] and then checking that the known and computed merkle roots match

  • @kelcheone
    @kelcheone 2 года назад

    Exactly two years later.

  • @dariosanchez1373
    @dariosanchez1373 2 года назад

    Great explanation, you are a crack

  • @ayush4319
    @ayush4319 2 года назад

    why only those four hashes?

    • @eyalovadya2562
      @eyalovadya2562 2 года назад +1

      The leaf nodes of the merkle tree are the transactions. Say you want to verify one transaction, you only need one more transaction (the adjacent) to compute the node in the next level, and then you can take the adjacent node to compute the next level node, and so on until you get to the root. So if you have a merkle tree with n levels you only need to know n nodes to verify if your transaction is in the tree.

  • @goumuk
    @goumuk 2 года назад

    "proof" is an array of only those hashes which are needed along with leaf to construct the hash root.
    "root" is the hash root of the merkel tree, which is supposed to contain my data. Whether this is true, that I need to verify.
    "leaf" is the hash of my data/transaction.
    "index" is the position of hash of my data/transaction in the array of transactions that constitute the block.
    I dont need the other data hashes from the array to calculate the hash root of the merkel tree built from this block. This is the advantage of using merkel tree.
    So, if the block contains 'n' data points, I dont need all the hashes of the 'n' data points, instead around log(n) hashes are sufficient to compute the hash root of the merkel tree.
    These log(n) hashes are present in the 'proof' array. And these log(n) hashes are present in proper sequence, else the algorithm wont work.
    Let's say, there are 8 data in a block.
    I take the hashes of each data, and store them in an array (say arr).
    I am verifying whether the 5th hash [index 4] in the array is my hash or not.
    I already have "leaf" as an input which is supposed to be same as arr[4].
    Along with leaf, I will need hash of arr[5] (since index 4%2 = 0, so arr[4] will be hashed with arr[5] ), hash of arr[6] and arr[7] combined, and hash of (arr[0], arr[1], arr[2], arr[3]), which is nothing but hash(hash(arr[0], arr[1]), hash(arr[2], arr[3])).
    So, 'proof' array will contain arr[5], hash(arr[6], arr[7]), and hash(hash(arr[0], arr[1]), hash(arr[2], arr[3])) as the three elements.
    How will proof array be computed for passing as an input to the verify function, I have no idea about that.
    Inside the verify function, the steps are self-explanatory.
    First the hash variable is initialised with the leaf value.
    The loop will run for three iterations ('proof' array length).
    In each iteration, the current hash's index is checked if it is in even position or odd position.
    Accordingly the hash operation is carried out with current element in proof array.
    The hash value that is calculated in previous step is the parent node of those two elements in merkel tree.
    The hash variable is updated with this computed hash value, and the index is divided by 2 to reflect index position of the parent node.
    This process is repeated till the final hash value is computed, which should be the hash root of the merkel tree.
    If this final hash value is equal to the input hash root, then the block indeed contains the desired data/transaction.

    • @smartcontractprogrammer
      @smartcontractprogrammer  2 года назад

      First paragraph is correct.
      Can you break down the rest of the sentences? Hard to comprehend without line breaks.
      Put in extra effort to make your question easier, I will do my part and answer your questions

  • @fakemonkgin
    @fakemonkgin 3 года назад

    still don't understand merkle tree, will come back later when I learned more

  • @mo_i_nas
    @mo_i_nas 2 года назад

    I think I'm getting old. I got so lost on this video.

  • @startat3098
    @startat3098 2 года назад

    do you live in japan

  • @Dancentralized
    @Dancentralized 2 года назад

    +algorithm

  • @CryptoRootz
    @CryptoRootz 3 года назад

    confused the fuck out out of me bro...

  • @mehdiwadoud8098
    @mehdiwadoud8098 3 года назад +1

    Not well explained !!!