イーサリアムのデータ構造

 イーサリアムのデータ構造

イーサリアムのデータ構造を「状態」の観点から掘り下げます
- Merkle Patricia Trie
- RLP Encoding
- State Trie / Storage Trieを実際に見てみる

Transcript

  1. ΠʔαϦΞϜͷσʔλߏ଄ !NPPOUZTBM CMPDLDIBJOUPLZP

  2. 1SPpMF!NPPOUZTBM 4PGUXBSF&OHJOFFS!(VOPTZ -PWF(P5ZQF4DSJQU4XJGU$SZTUBM,PUMJO )PCCZόοΫΧϯτϦʔܢྲྀ௼Γࢁ

  3. ϞνϕʔγϣϯΠʔαϦΞϜͷʮঢ়ଶʯͷ΋ͪํΛ۷ΓԼ͛ͯσʔλߏ଄Λཧղ͢Δ  &UIFSFVNঢ়ଶػցͳͷ͔ͩΒঢ়ଶͷ·ΘΓΛ۷͍͚ͬͯ͹ཧղ͕ਂ·ΔͷͰ͸  Ͳͷঢ়ଶΛʮͲ͜ʹʯͲͷΑ͏ͳϑΥʔϚοτͰ͍࣋ͬͯΔ͔  ͲͷΑ͏ͳΞϧΰϦζϜ͕ར༻͞Ε͍ͯΔ͔  ͋ΘΑ͘͹.FSLMF1SPPGΛࣗ෼Ͱܭࢉ͍ͯ͘͠ͱཧղͰ͖ΔͷͰ͸ʁ 

    CJUDPJOΛཧղ͢Δͱ͖ʹ৭ʑखܭࢉͨ͠Β͸͔ͲͬͨͷͰ ݪମݧʣ
  4. ঢ়ଶػցͱͯ͠ͷΠʔαϦΞϜ  ͋Δ࣌఺Uͷঢ়ଶМ U ͔Βɺ࣍ͷঢ়ଶМ U  ΁ɺτϥϯβΫγϣϯʹΑͬͯભҠ͢Δ Ethereum, taken

    as a whole, can be viewed as a transaction-based state machine [Ethereum Yellow Paper]
  5. ঢ়ଶػցͱͯ͠ͷΠʔαϦΞϜϒϩοΫੜ੒ʹΑΔঢ়ଶભҠͷੵΈॏͶ  τϥϯβΫγϣϯ͸ରԠ͢ΔݸʑͷΞΧ΢ϯτͷঢ়ଶΛมԽͤ͞Δ  ωοτϫʔΫ͸τϥϯβΫγϣϯΛूΊɺঝೝͯ͠ϒϩοΫΛੜ੒͢Δ Ethereum, taken as a whole,

    can be viewed as a transaction-based state machine [Ethereum Yellow Paper] ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ ʜʜʜ
  6. The world state (state), is a mapping between addresses (160-bit

    identifiers) and account states (a data structure serialised as RLP,..) [Ethereum Yellow Paper Chapter 4] XPSMETUBUF͸ΞυϨεͱঢ়ଶͷϚοϐϯάͰද͞ΕΔ  ΞυϨε͕Ωʔ  ঢ়ଶ͕஋  3-1Τϯίʔυ͞Ε͍ͯΔ  ࢒ߴ  /PODF  $PEFIBTI  4UBUF3PPU Y%%"&#G&#$$#'EBD% YCEDCCDEECD YFDEFDBEFGBDFG     )"4) 4UPSBHF5SJF3PPU  )"4) lz < )"4) ʜ  )"4) lz > YGEʜʜCCGBEEB
  7. Though not stored on the blockchain, it is assumed that

    the implementation will maintain this mapping in a modified Merkle Patricia tree [Ethereum Yellow Paper Chapter 4] Ϛοϐϯά͸ϚʔΫϧύτϦγΞ໦ͱͯ͠อଘ͞ΕΔ  LFZWBMVFͰঢ়ଶΛอଘ͢ΔͨΊͷσʔλߏ଄  ޮ཰తͳΩʔ୳ࡧ  CJUDPJOͰ΋͓ͳ͡ΈͷϚʔΫϧπϦʔ  .FSLMF5SFF.FSLMF1SPPG %& %&" '$    YCB Y'' Y$$ <          " # $ % & '> %& <          " # $ % & '> YCB Y'' Y$$ $ 3PPU
  8. Though not stored on the blockchain, it is assumed that

    the implementation will maintain this mapping in a modified Merkle Patricia tree [Ethereum Yellow Paper Chapter 4] Ϛοϐϯά͸ϚʔΫϧύτϦγΞ໦ͱͯ͠อଘ͞ΕΔ  LFZWBMVFͰঢ়ଶΛอଘ͢ΔͨΊͷσʔλߏ଄  ޮ཰తͳΩʔ୳ࡧ  CJUDPJOͰ΋͓ͳ͡ΈͷϚʔΫϧπϦʔ  .FSLMF5SFF.FSLMF1SPPG W W W W I W I W I W I W I I W  I W I I W  I W I ʜ / W W W I W I W I W I O I I O  I W I I W  I W I ʜ @OFX ϦʔϑϊʔυͷϋογϡΛ఻೻ͤࠜ͞ϊʔυͷϋογϡΛ࡞Δ ༿ͷ஋Λมߋ͢Δͱࠜϊʔυͷϋογϡ͕มΘΔ ໦શମΛ஌Βͳͯ͘΋༿ͷ஋ͷଘࡏΛ͔֬ΊΒΕΔ
  9. Though not stored on the blockchain, it is assumed that

    the implementation will maintain this mapping in a modified Merkle Patricia tree [Ethereum Yellow Paper Chapter 4] Ϛοϐϯά͸ϚʔΫϧύτϦγΞ໦ͱͯ͠อଘ͞ΕΔ  LFZWBMVFͰঢ়ଶΛอଘ͢ΔͨΊͷσʔλߏ଄  ޮ཰తͳΩʔ୳ࡧ  CJUDPJOͰ΋͓ͳ͡ΈͷϚʔΫϧπϦʔ  .FSLMF5SFF.FSLMF1SPPG W W W W I W I W I W I W I I W  I W I I W  I W I ʜ / W W W I W I W I W I O I I O  I W I I W  I W I ʜ @OFX ϦʔϑϊʔυͷϋογϡΛ఻೻ͤࠜ͞ϊʔυͷϋογϡΛ࡞Δ ༿ͷ஋Λมߋ͢Δͱࠜϊʔυͷϋογϡ͕มΘΔ ໦શମΛ஌Βͳͯ͘΋༿ͷ஋ͷଘࡏΛ͔֬ΊΒΕΔ
  10. ΠʔαϦΞϜͷXPSMETUBUFͷભҠશମ૾ϒϩοΫϔομ !)FBEFS 4UBUF5SJF 5SBOTBDUJPO5SJF 3FDFJQUT5SJF 4USPSBHF5SJF

  11. ΠʔαϦΞϜͷXPSMETUBUFͷભҠશମ૾4UBUFͱ4UPSBHF 4UBUF5SJF "DDPVOU4UPSBHF PVUPG&7. ,FZ LFZ CJU WBMVF CJU ,FZ

    ,FZ    7BMVF 7BMVF 7BMVF ,FZ LFZ CJU WBMVF CJU ,FZ ,FZ    7BMVF 7BMVF 7BMVF ,FZ LFZ CJU WBMVF CJU ,FZ ,FZ    7BMVF 7BMVF 7BMVF )BTI ʜ
  12. ΠʔαϦΞϜͷσʔλߏ଄ͷཁॴ  .FSLMF1BUSJDJB5SFF  )1&ODPEJOH  3-1&ODPEJOH  σʔλछผ͝ͱʹ໦͕͋Δ 

    ֊૚ߏ଄͕͋Δ Y%%"&#G&#$$#'EBD% YCEDCCDEECD    )"4) 4UPSBHF5SJF3PPU  )"4) lz < )"4) ʜ  )"4) lz > *NQMFNFOUFEJO"1BUSJDJB5SJF
  13. 1BUSJDJB5SJF5SJF τϥΠ໦ʣ  3FUSJFWBM ૝ىʣ͕ޠݯ  lEPHz  lEPHFz 

    lEPOF  lDBUz  lDBQz  จࣈྻݕࡧʹద͍ͯ͠Δ  จࣈྻ௕NɺσʔλαΠζ/ʹର͠  0 N  WTೋ෼୳ࡧ໦0 MPH/ จࣈྻू߹ τϥΠ໦Ͱͷදݱ  ͭͷϊʔυ͸֤จࣈΛද͢  ϊʔυ͸֤จࣈͷ෼͚ͩࢠϊʔυ΁ͷϙΠϯλΛ࣋ͭ  ࢠϊʔυ͕ͳ͍৔߹͸/6--΁ͷϙΠϯλΛ࣋ͭ  ಛ௃ ϝϞϦ࢖༻ྔ͕ଟ͍Lจࣈ࢖༻͢Δͱͯ͠ ʙ Ωʔͷ௕͕͞ҰఆͳΒ͹0   E D P H F O F B U Q Lจࣈ࢖༻͢Δͱ͖NMPH@L/  ୯७ൺֱͰ͖ͳ͍
  14. 5SJF͔Β1BUSJDJB5SJF΁  5SJFͷϝϞϦফඅྔͷ໰୊ʹରॲ  ࢠϊʔυ͕͔ͭ͠ͳ͍ϊʔυΛࢠͱϚʔδ  1SBDUJDBM"MHPSJUINUP3FUSJFWF*OGPSNBUJPO$PEFEJO"MQIBOVNFSJDΒ͍͠  σʔλͷૢ࡞ίετͳͲ͸τϥΠ໦ͱಉ͡Φʔμ E

    D P H F O F B U Q EP DB H F O F U Q EP DB HF OF U Q τϥΠ໦ ύτϦγΞ໦
  15. &UIFSFVNʹ͓͚ΔA.PEJpFEA.FSLMF1BUSJDJB5SJF  छྨͷϊʔυ EP DB HF OF U Q ύτϦγΞ໦

    EP DB HF OF U Q ʹ CSBODI FYUFOTJPO MFBG  /6--  -FBG  &YUFOTJPO  #SBODI
  16. &UIFSFVNʹ͓͚ΔA.PEJpFEA.FSLMF1BUSJDJB5SJF  छྨͷϊʔυ  /6--  -FBG  &YUFOTJPO 

    #SBODI
  17. &UIFSFVNʹ͓͚ΔA.PEJpFEA.FSLMF1BUSJDJB5SJF  छྨͷϊʔυ  /6--  -FBG  ຤୺ͷϊʔυ஋Λ࣋ͭ 

    &YUFOTJPO  #SBODI
  18. &UIFSFVNʹ͓͚ΔA.PEJpFEA.FSLMF1BUSJDJB5SJF  छྨͷϊʔυ  /6--  -FBG  &YUFOTJPO 

    ࢠΛ͔ͭ࣋ͨ͠ͳ͍ϊʔυͷ·ͱ·Γ  #SBODI
  19. &UIFSFVNʹ͓͚ΔA.PEJpFEA.FSLMF1BUSJDJB5SJF  छྨͷϊʔυ  /6--  -FBG  &YUFOTJPO 

    #SBODI  ࢬ෼͔Ε
  20. 4USPSBHFʹอଘ͞Ε͍ͯΔঢ়ଶΛΛ֞ؒݟΔ  MFWFM%#͔ΒҾͬ͜ൈ͍ͯ΋͍͍͕݁ߏେมͦ͏ʢͩͬͨʣ  ΋ͪΖΜXFCͷHFU4UPSBHF"U YYY Λ࢖ͬͯ΋͍͍ͷ͕ͩʜʢνϣοτνΨ΢ʣ  (FUIͷEVNQίϚϯυΛ࢖͏ 

    EBUBEJSΛࢦఆͯ͠ୟ͘
  21. LFZʹର͢ΔWBMVFͷγϦΞϥΠζํࣜ3-1&ODPEJOH The purpose of RLP is to encode arbitrarily nested

    arrays of binary data, and RLP is the main encoding method used to serialize objects in Ethereum. [Ethereum Wiki]  ܕΛѻΘͳ͍γϯϓϧͳγϦΞϥΠζख๏  Ϧετߏ଄ΛऔΓѻ͑Δ  LFZWBMVFͷϚοϓ͸αϙʔτ͍ͯ͠ͳ͍  ఆٛ  ΤϯίʔυͰ͖Δ΋ͷΞΠςϜ  จࣈྻ͸ΞΠςϜ  ΞΠςϜͷϦετ͸ΞΠςϜ όΠτྻΛѻ͏ lEPHz <lEPHz lXPMGz> < > <lEPHz lXPMGz> < > lXPMGz lz <lEPHz lXPMGz lz> < > lz <<lEPHz lXPMGz> lDBUz> < > lDBUz < > < <>
  22. 3-1ͷσίʔυ͔Β3-1ΤϯίʔσΟϯάΛݟΔจࣈ  ѻ͏ͷ͸όΠτྻ YY⒎   σίʔμ͸౸ணͨ͠όΠτྻͷ࠷ॳͷཁૉΛݟͯͰσίʔυର৅͕Կ͔ͳͲΛ஌Δ͜ͱ͕Ͱ͖Δ Y YG จࣈʹରԠ

    CJU Y Y⒎ ࢒ΓͷCJU YYGͷൣғʹೖ͍ͬͯΔ৔߹ɺͦͷ··ͷόΠτͱͯ͠ղऍ͢Δ
  23. σίʔυ๏จࣈྻ Y YD Y⒎ ࢒ΓͷCJU ಡΈࠐΜͩઌ಄ͷόΠτ͕ͷ࢒ΓͷCJUͷ͏ͪઌ಄CJUͩͬͨ৔߹͸ɺจࣈྻ͕Τϯίʔυ͞Ε͍ͯΔ จࣈྻΛද͢ YC YC จࣈҎԼͷจࣈྻ

    จࣈҎ্ YCG Y͔ΒYCޙଓΛYYόΠτ෼ɺจࣈͱͯ͠σίʔυ ઌ಄ͷόΠτ͕ ྫ G ઌ಄YYόΠτ෼ͷจࣈྻY YG Y Y UP4USJOHGPVS
  24. σίʔυ๏௕͍จࣈྻ Y YD Y⒎ ಡΈࠐΜͩ࠷ॳͷόΠτ͕ͷ࢒ΓͷCJUͷ͏ͪઌ಄CJUͩͬͨ৔߹͸ɺจࣈྻ͕Τϯίʔυ͞Ε͍ͯΔ  จࣈྻΛද͢ YC YC จࣈҎԼͷจࣈྻ

    จࣈҎ্ YCG ࠷ॳͷόΠτ͕ YC͔ΒYCGޙଓ͕จࣈྻ௕ͷ YYC ͷ௕͞ͷόΠτදݱ จࣈྻͱͯ͠σίʔυ ྫͱͯ͠ڥ໨ΛݟΔ
  25. σίʔυ๏Ϧετ YD Y⒎ ϦετΛද͢ QBZMPBE༰ྔόΠτҎԼͷϦετ 0UIFSXJTF YD͔ΒYGޙଓΛ༰ྔʢYYDʣόΠτͷϦετͱͯ͠σίʔυ YG YG ࠷ॳͷόΠτ͕

    ಡΈࠐΜͩ࠷ॳͷόΠτ͕࢒ΓͷCJUͩͬͨ৔߹͸ɺϦετ͕Τϯίʔυ͞Ε͍ͯΔ
  26. σίʔυ๏̑௕͍Ϧετ YD Y⒎ ϦετΛද͢ QBZMPBE༰ྔόΠτҎԼͷϦετ 0UIFSXJTF YD͔ΒYGޙଓ͸ YG YG ࠷ॳͷόΠτ͕

    ಡΈࠐΜͩ࠷ॳͷόΠτ͕࢒ΓͷCJUͩͬͨ৔߹͸ɺϦετ͕Τϯίʔυ͞Ε͍ͯΔ  Ϧετͷ༰ྔͷόΠτදݱͷ௕͞ Ϧετ಺༰ͱͯ͠σίʔυ ྫYGYY Y ʜ YGYGYGYϦετ༰ྔ͸όΠτͰදͤΔ YϦετ༰ྔ͸όΠτ
  27. σίʔυ৭ʑ3-1 Լه͸ಉ͡ʹͳͬͯ͠·͏͕όΠτྻΛ/VNCFSͱͯ͠ղऍ͢Δ͔4USJOHͱͯ͠ղऍ͢Δ͔͸ղऍଆʹҕͶΒΕΔͬΆ͍ ΤϯίʔυͰ͖Δ࠷΋௕͍จࣈྻ͸ʁɹYCGYCόΠτ͕จࣈྻ௕ͷදݱʹ࢖͑Δ YCG ઌ಄ ⒎⒎⒎⒎⒎⒎⒎⒎ จࣈྻ௕ จࣈྻ  

    จࣈ ?จࣈ ?ϖλ
  28. 3-1Τϯίʔυ͞Εͨঢ়ଶΛ೷͖ݟΔ  LFZWBMVFσʔλϕʔεͷࡏΓॲ  -FWFM%#  HFUIͷ৔߹EBUBEJSͰࢦఆͨ͠σΟϨΫτϦʹ֨ೲ͞Ε͍ͯΔ  (6*ϥΠϒϥϦ୭͔஌Γ·ͤΜ͔ʜ 

    (6*ϥΠϒϥϦ͕͋Ε͹΋ͬͱָʹ৭ʑʮ೷͚Δʯ͸ͣ  AOQNTUBSUͱ͸A  ֤छϥΠϒϥϦ  MFWFM%#  +BWB4DSJQUMFWFMVQMFWFMEPXO  1ZUIPOQZMWFM  3-1  FUIFSFVNKTSFQ  FUIFSFVNQZSMQ ඍົ͔΋ʁʣ ɹύτϦγΞ໦ +BWB4DSJQUFUIFSFVNKTNFSLMFQBUSJDJBUSFF
  29. αϯϓϧͱઃఆ  MFWFM%#͔Βঢ়ଶΛಡΈ͜ΜͰશEVNQͯ͠ΈΔ

  30. αϯϓϧͱઃఆ ݟͨ͜ͱͷ͋ΔϑΥʔϚοτYGʜ YG Y Y Yʜ  ϦετͷϖΠϩʔυͷόΠτ௕ͷόΠτදݱ͸όΠτ  Y

     όΠτ  Ylz  Yʜlz  YBʜ
  31. αϯϓϧEFDPEF  ͍͔ͭ͘σίʔυͯ͠ΈΔ  OPODF CBMBODFTUPSBHF3PPU DPEF)BTIͷॱʹ֨ೲ͞Ε͍ͯΔ  bDEGDFECEDDDFCDBCCGBEEB`
 ͸ۭจࣈྻʹର͢Δϋογϡ஋ͬΆ͍

    ͔Ϳ͍ͬͯΔͷͰʣ
  32. ·ͱΊ  3-1ΤϯίʔσΟϯάͱ.FSLMF1BUSJDJB5SJF͕෼͔Δσʔλͷ΋ͪํ͕େ·͔ʹ෼͔Δ  4UPSBHF5SJF·ͰݟΕͨͷͰɺؤுΕ͹4UBUF3PPUܭࢉͰ͖Δ  τϥϯβΫγϣϯͷϋογϡ஋औಘͯ͠MFWFMECಡΈࠐΜͰΞΧ΢ϯτͷঢ়ଶ֬ೝͯ͠ʜͱಈ͔͢ͱཧղ͕ਐΉ  3-1Τϯίʔυ͸ίʔυಡΜͩ΄͏͕ͳʹ΍ͬͯΔ͔ཧղ͠΍͍͢ 

    3-1ʹؔͯ͠͸׳ΕΔͱ໨EFDPEFEͰ͖ͦ͏ &7.ͷ͓ؾ࣋ͪʁʣ