Smart Contracts and Ethereum
Winter School on Cryptocurrency and Blockchain Technologies Shanghai, Jan. 15-17 2017
Some slides are courtesy of Vitalik Buterin
Loi Luu National University of Singapore
1
Smart Contracts and Ethereum Winter School on Cryptocurrency Loi - - PowerPoint PPT Presentation
Smart Contracts and Ethereum Winter School on Cryptocurrency Loi Luu and Blockchain Technologies National University of Singapore Shanghai, Jan. 15-17 2017 Some slides are courtesy of Vitalik Buterin 1 Agenda Smart contracts and
Some slides are courtesy of Vitalik Buterin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
– Complex access policies depending on amount, withdrawal limits, etc – Dead man’s switch, “digital will”
– Prediction markets – Insurance – Micro-payments for computational services (file storage, bandwidth, computation, etc)
21
22
25
26
**********
27
28
value storage)
– Send ETH to other accounts – Read/write storage – Call (ie. start execution in) other contracts
29
Private Storage Can be invoked by
30
Ethereum VM Bytecode Stack Language Lower-Level Language Serpent Solidity
Functional, macros, looks like scheme
Types, invariants, looks like Javascript Looks like python Looks like Forth. Defined in Yellowpaper
Slide is courtesy of Andrew Miller
31
32
60606040526040516102503 80380610250833981016040 528........ PUSH 60 PUSH 40 MSTORE PUSH 0 CALLDATALOAD ..... What you write What other see on the blockchain What people get from the disassembler
33
34
– nonce: previous nonce + 1 – to: empty – value: value sent to the new contract – data: contains the code of the contract – gasprice (amount of ether per unit gas) – startgas (maximum gas consumable) – v, r, s (ECDSA signature values)
35
– nonce: previous nonce + 1 – to: contract address – value: value sent to the new contract – data: data supposed to be read by the contract – gasprice (amount of ether per unit gas) – startgas (maximum gas consumable) – v, r, s (ECDSA signature values)
36
Address Balance (BTC) 0x123456… 10 0x1a2b3f… 1 0xab123d… 1.1
Address Object 0x123456… X 0x1a2b3f… Y 0xab123d… Z
37
38
Tx-n Tx-1
Miners
Tx-2
Block
A set of TXs Previous block New State Root Receipt Root Nonce
SHA3(Block) < D Broadcast Block
39
Verify transactions & execute all code to update the state
This is a new block! I’m a leader This is a new block! This is a new block! This is a new block! This is a new block! This is a new block!
40
uint i = 1; while (i++ > 0) { donothing(); }
41
42
– If startgas is less than needed
happened
– Total gas spent by all transactions in a block < Gas Limit
43
44
Bitcoin Network Ethereum Network BTCRelay
45
Bitcoin Ethereum The verified Bitcoin transaction is relayed to the smart contract A Bitcoin transaction is submitted, BTCRelay verifies TX based on the block header Relayers constantly submit Bitcoin block headers
46
ETH-BTC Swap contract 50 ETH for anyone who sends 1 BTC to my address BTCRelay I sent 1 Bitcoin to Alice address, here is the proof P Check proof P Bitcoin Network Send 1 BTC to Alice address Send 50 ETH
47
48
49
Question: can we build a decentralized exchange between cryptocurrencies using all the relays?
– Miners go to mining pools for stable and frequent rewards – Decentralized platforms are secured by centralized entities
50
Bitcoin Network Block Block Shares Block
51
Pool operator
– Maintain a share-chain within the pool (just like the blockchain) – Pay miners in proportional to their contributions
– Broadcast to all miners – Check if the coinbase tx is correct and extend the share-chain
52
Bitcoin Network Shares Block P2Pool
Bitcoin Network Shares Block
53
P2Pool
– Reduce number of messages (txs) to the contract significantly
– Randomly verify only one share per submission – Probability of cheating being detected is proportional to the amount of cheating
SmartPool Submit Sample &Chec k
54
Get 1.5 Reward with 2/3 probability Get 0 Reward with 1/3 probability Probabilistic verification passed detected Expected reward = 1
55
Reward = 1
56
57
58
59
60
E E E E E E
61
62
63
Shard 1 Shard 2 Shard 3
– Scaling by using off-chain transactions – Can update the state multiple times – Only settlement transactions are on- chain
– Cannot create state channel for all applications – Still early research, more work needed
Blockchain TX1 TX2 X’s Initial State X’s Final State TX3 TX4 Many states i Alice Bob
Contract X
64
65
66
PuzzleSolver() SetPuzzle reward=100 PuzzleSolver Contract SubmitSolution(solution) if isCorrect(solution): Send(reward) UpdateReward(newReward) reward=newReward Owner can update the reward anytime Anyone can submit a solution to claim the reward
Balance: 100
67
Random TXs
PuzzleSolver() SetDifficulty reward=100 PuzzleSolver Contract SubmitSolution(solution) if isCorrect(solution): Send(reward) UpdateReward(newReward) reward=newReward Miners
Other TXs Solution for Puzzle
Random TXs SubmitSolution Other TXs
+100 Balance: 100 Balance: 0
68
PuzzleSolver() SetDifficulty reward=100 PuzzleSolver Contract SubmitSolution(solution) if isCorrect(solution): Send(reward) UpdateReward(newReward) reward=newReward Miners
Other TXs Solution for Puzzle Update Reward to $0!
Block
UpdateReward = 0 SubmitSolution Other TXs
+0 Balance:100 Balance: 0
69
Other TXs Solution for Puzzle Update Reward to $0! 70
Other TXs Solution for Puzzle Update Reward to $0! 71
– Most expensive vulnerability to date
... // Burn DAO Tokens if (balances[msg.sender] == 0) throw; withdrawRewardFor(msg.sender); totalSupply -= balances[msg.sender]; balances[msg.sender] = 0; paidOut[msg.sender] = 0; return true;
72
withdrawRewardFor(msg.sender) splitDAO(proposal, address)
function() {}
rewardAccount.payOut(_account, reward) balances[msg.sender] = 0;
73
withdrawRewardFor(msg.sender) splitDAO(proposal, address)
splitDAO()
rewardAccount.payOut(_account, reward)
74
75
76
77
EXPLORER
CORE ANALYSIS
Z3 Bit-Vector Solver
VALIDATOR
ByteCode Ethereum State CFG BUILDER Visualizer 6060604052123 123123528.....
78
F T F T T F F T T T T F
3 2 1
1
2
3
Is there any value of x?
79
5411 3056 340 83 1385 135 186 52 1000 2000 3000 4000 5000 6000
Callstack TOD Reentrancy Timestamp
Flagged Buggy Contracts
Total Unique F T F T T F F T 80
81