SlideShare a Scribd company logo
1 of 75
Download to read offline
Inside Casper
2018 5/24 Blockchain.tokyo #8
zigen
Zigen
● 酒飲みながらコード書きます
● Github @zigen
● Twitter @kei_0811
● Web Frontend
○ JS, React , ReactNative
● Blockchain初心者
○ もうすぐ3ヶ月目!!
What’s Casper?
1. Proof of Stake-based Finality
2. Overlays on Proof of Work block proposal mechanism
New Fork Choice Rule
用語
● Source
● Target
● Supermajority Link
● Justified
● Finalized
● Validators
● Vote
● Dynasty
● Epoch
block1
block3
block2
block4
block5
block6
時間 t
上にいくほど新しい
block n は block n-1 を参照している
1
3
2
4
5
6
時間 t
4b
5b
6b 6c
3b
4c
5
6d
どのブロックからでも分岐できる
0
1
48
時間 t
Checkpoint49
50
Epoch n
Length: 50
Epoch n + 1
1
3
2
4
99
100
時間 t
4b
99b
100b 100d
3b
4c
99c
100c
4つのcheckpointがある
1
100
時間 t
100b 100d100c
普通のblockを省略
このcheckpointだけの表示を
subtreeと呼ぶ
r
100
時間 t
100b 100d
Source
Targe
t
Root
100 100b 100d
Validators
Root
100 100b 100d
Vote
Root
100 100b 100d
Vote
Supermajority Link
Root
100 100b 100d
Supermajority Link
Justified
Root
100 100b 100dJustified
100 100b 100d
Vote
Root
100 100b 100dJustified
100 100b 100d
Vote
Supermajority Link
Root
100 100b 100d
Justified
100 100b 100d
Supermajority Link
Root
100 100b 100d
Justified
100 100b 100d
Supermajority Link
Finalized
Root
100 100b 100d
Justified
100 100b 100d
Cannot Revert!!!
Finalized
Implementations
1. Casper Contract ethereum/casper.git 6a171e1
2. Pythapp karlfloersch/pyethapp#dev_envd1a5d1f
3. Pyethereum ethereum/pythereum.git 3d5ec14
4. Parity paritytech/parity.git#casper 9c51885
Roles
● Casper Contract simple_casper.v.py
● Miner
● Validator validator_service.py
Implementations
1. Casper Contract ethereum/casper.git 6a171e1
2. Pythapp karlfloersch/pyethapp#dev_envd1a5d1f
3. Pyethereum ethereum/pythereum.git 3d5ec14
4. Parity paritytech/parity.git#casper 9c51885
Step
1. Initial: Deploy simple_casper.v.py
2. Miner: Initialize casper metadata of node
3. Validator: Deposit
4. Validator: Vote/Slash
5. Miner: Accept Block
Implementations
1. Casper Contract
● Written in Vyper
● As Smart Contract
Casper Contract validators
Decimal????
Casper Contract parameters
Casper Contractを叩くまでの壁
1. Vyper
2. Serpent
3. Decimal
Vyper (the former Viper) ethereum/vyper.git
● New Experimental Language
● Simple, Secure, Readable
● Alternative to Solidity
● *.v.py
Casper Contractを叩くまでの壁
1. Vyper
$pip install vyper すると
casperのコンパイルがこける
$pip3 install -r requirements.txt しよう
結構な人がハマってた
Casper Contractを叩くまでの壁
2. Serpent
MSG_HASHERと
PURITY_CHECKERは
Serpent製
Casper Contractをinitするときにこの二つの Contract Addressを渡す
Casper Contractを叩くまでの壁
3. Decimal
なお, ABIのspecにはdecimalは存在しない
Casper Contractを叩くまでの壁
3. Decimal
なお, ABIのspecにはdecimalは存在しない
Casper Contractを叩くまでの壁
3. Decimal
でもなぜethereum/casperのテストでは
vyperコンパイルが通るのか?
Casper Contractを叩くまでの壁
3. Decimal
Casper Contractを叩くまでの壁
3. Decimal
abi.pyをみてみると
https://github.com/ethereum/pyethereum/blob/develop/ethereum/abi.py#L315-L318
Casper Contractを叩くまでの壁
3. Decimal
1年前からある...
https://github.com/ethereum/pyethereum/commit/b4dcb30fc96ce0b462b6ddfcaa3db4432e2ce200
Casper Contractを叩くまでの壁
3. Decimal
pythereumでしか動かないので
ethereum/vyperにissueが上がってた
https://github.com/ethereum/vyper/issues/566
Casper Contractを叩くまでの壁
3. Decimal
https://github.com/ethereum/vyper/issues/566#issuecomment-389851829
Casper Contract validators
Casper Contract checkpoints
Init L126~
● デプロイ後1回だけ
● パラメータ与えて
初期化していく
● とくに難しいことはない
initialize_epoch L468~
● デプロイ後1回だけ
● パラメータ与えて
初期化していく
● とくに難しいことはない
initialize_epoch L468~
initialize_epoch L425~
● 各epochが始まるたびに誰かが叩いて更新する
● Penalty factorなどの調整をするキモの部分
deposit L465~
● Validatorとして登録される
● Depositが十分かどうか?
● validator_indexをincrement
deposit validation_addrのチェック
● 'xa1x90x3exab' はPURITY_CHECKERのmethod
● keccak256(“submit(address)”) => a1903eab65b303e5
https://ethresear.ch/t/verifying-the-purity-of-the-function-stored-at-the-validation-code-address/430
deposit validation_addrのチェック
● こんな感じで他のコントラクトを結構呼び出している
● 頑張れば読める
Vote L550~
● Validatorがcheckpointに対してvoteする
● 80行くらいある
Vote L550~
● MSG_HASHERを呼び出してhashを取得
● RLPをdecode
Casper Vote Format
● Validator_index
● Target_hash
● Target_epoch
● Source_epoch
● signature
https://github.com/ethereum/casper/blob/master/VALIDATOR_GUIDE.md#casper-vote-format
b0
b1 b2 b3
r
Epoch: n
Epoch: n+1
Validators
Address
index
Casper Vote Format
● Validator_index
● Target_hash
● Target_epoch
● Source_epoch
● signature
https://github.com/ethereum/casper/blob/master/VALIDATOR_GUIDE.md#casper-vote-format
b0
b1 b2 b3
r
source
Target epoch, blockhash
Validators
Address
index
Vote L550~
● RLPでdecodeした値をチェックしていく
● すでにこのepochでvoteしていないかどうか
Vote L550~
● Target_hashがrecommended_target_hashかどうか?
● target_epochがcurrent_epochかどうか?
● sourceがjustifyされているかどうか?
Vote L550~
● Voteしたtarget_hashがrecommended_target_hashでなければ,
Voteそのものが失敗してしまう???
Recommended_target_hash L365~
● Target_epochにおけるcheckpointのblockのhashを返す
● どうやら本当にrecommended_target_hashにvoteしないといけない
● そもそもrecommendedとは...
そもそも誰がvoteするのか?
● もちろんvalidatorがdepositしてvoteするのである
● karlfloersch/pyethappにvalidator_service.pyという実装がある
○ ethereum/pythappにもあるけど, これは古いcasperのvalidatorの実装なので注意
● とりあえず読んでみる
https://github.com/ethereum/casper/blob/master/VALIDATOR_GUIDE.md
https://github.com/karlfloersch/pyethapp/blob/dev_env/pyethapp/validator_service.py
Pythapp validator_service.py L127 vote
Pythapp validator_service.py L127 vote
● 似たようなmethodが呼び出されている
validator_service.py L264~ recommended_vote_contents
● Target_hash は current_epoch * epoch_length -1 らしい
● 結局のところcasperのrecommended_target_hashと同じ
Validatorの挙動
● Vote_msgのtarget_hashは,
casper contractのrecommended_target_hashと同じ
● つまり, casper contractが指定するtarget_hashにvoteするしかない
● ValidatorはVote先を選ぶことはできない!
● New Fork Choice Ruleなのに選べない!
ところで
もし, Validatorが自由にTargetを選べたら
ContractはどうやってそのTarget Checkpointが
存在することを確認するのか?
そもそもContractを実行するEVMはどこ?
EVMはどこ?
b0
b1 b2 b3
r
ClientClient
EVMはどこ?
b0
b1 b2 b3
r
b0
r
b0
r
Client
● 各Clientにある
● そのEVMの上でrecommended_target_hashを行うと?
ClientClient
EVMはどこ?
b0
b1 b2 b3
r
b0
r
b0
r
Client
● 各Clientにある
● そのEVMの上でrecommended_target_hashを行うと?
EVMはどこ?
● blockhashはblockNumberからそのblockのhashを取得する
Casper ContractのVote methodで
fork先のtargetを選ぶのではない
どのnodeに投げるかで選ぶ
Root
100 100b 100d
Vote(100b)
Casper Contract
finalize
間違った理解
ClientClient
実際
b0
b1 b2 b3
r
b0
r
b0
r
Client
Contract Contract Contract
Vote
Casper Contract再び
つまりassertは, 本当にこのNodeに対して行われたvoteかどうかを
確認している
Forkが前提のContractを読み書きするとき
● Nodeが別々であることを意識する
● EVMが何を知っているかを考える
まとめ
● コードを読もう
● 読めばわかる!!!楽しい!!!!
● Minerがどうやってブロックを選ぶか喋りたかった

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Inside casper