Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)
-
Upload
nivertech -
Category
Technology
-
view
769 -
download
4
Transcript of Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)
![Page 1: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/1.jpg)
Ethereum VM &
DSLs for
Smart Contracts
![Page 3: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/3.jpg)
Agenda
• How Bitcoin works
• Bitcoin Script
• Intro to Smart Contracts
• What is Ethereum
• Ethereum VM
• DSLs for Smart Contracts
![Page 4: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/4.jpg)
What is Bitcoin?
![Page 5: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/5.jpg)
What is Bitcoin?
send X bitcoins
from address A
to address B
![Page 6: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/6.jpg)
What is Bitcoin?
send X bitcoins
from address A
to address B
under condition C
![Page 7: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/7.jpg)
“Under condition C”
• C – is a predicate that Tx is valid
• Q: How Bitcoin represents C?
• A: Using a pair of “Bitcoin Scripts”
– Locking script
– Unlocking (or redeem) script
ZΛDΛTΛ © 2015
![Page 8: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/8.jpg)
What is Bitcoin Script?
• Forth-like, stack-based VM, RPN
• 1 byte opcodes
• All values are variable length byte arrays
• Type interpreted by operations
• Only stack & alt-stack
• No return stack (no calls)
• No heap
• Deterministic - No side effects or I/O
![Page 9: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/9.jpg)
RPN Calculators
![Page 10: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/10.jpg)
RPN Calculators
• Infix:
2 + 3 =
• Postfix (RPN):
2 ↑ 3 +
![Page 11: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/11.jpg)
Subject-Object-Verb
![Page 12: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/12.jpg)
http://neilk.net/blog/2015/02/14/hea
rtforth/
http://neilk.net/bloghttp://neilk.net/blog/2015/02/14/heartforth/
![Page 13: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/13.jpg)
Why Stack-based VM? • memory efficient
• easy to implement VM
– no need for a lexer, parser or AST
• Portable
– run on devices: phones or calculators without consuming too much bandwidth
• compact code
– storage on the Bitcoin Blockchain is very expensive ($600K/GB @ $220/BTC)
![Page 14: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/14.jpg)
Compact Code
Register VM (3 operands)
MOV R1,#2
MOV R2,#3
ADD R3,R1,R2
MOV R1,#2
ADD R1,#3
Stack-based VMs (0 operands)
2
3
ADD
Register VM (2 operands)
Blockchain storage is very expensive ~ $600K/GB ($220/BTC)
≥ 6B
≥ 4B
≥ 3B
![Page 15: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/15.jpg)
http://yosefk.com/blog/my-history-with-forth-stack-machines.html
![Page 16: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/16.jpg)
Bitcoin Script Limitations • deterministic, but not Turing complete - intentionally
• no loops - disallow infinite loops
• no recursive functions
– no functions at all
• no jumps/goto
– but has (OP_IF,OP_ELSE,OP_ENDIF)
• many opcodes disabled (string ops)
• sigop counts – limit # of hashing ops
• scripts are limited in size - max 500B
![Page 17: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/17.jpg)
![Page 18: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/18.jpg)
1 Byte Opcodes
OP_0 OP_FALSE=OP_0 OP_PUSHDATA1 OP_PUSHDATA2 OP_PUSHDATA4 OP_1NEGATE OP_RESERVED OP_1 OP_TRUE=OP_1 ...
OP_VER OP_IF OP_NOTIF OP_VERIF OP_VERNOTIF OP_ELSE OP_ENDIF OP_VERIFY OP_RETURN ...
OP_TOALTSTACK OP_FROMALTSTACK OP_2DROP OP_2DUP OP_3DUP OP_2OVER OP_2ROT OP_2SWAP OP_IFDUP ...
ZΛDΛTΛ © 2015
![Page 19: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/19.jpg)
![Page 20: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/20.jpg)
Blockchain
![Page 21: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/21.jpg)
UTXO – Unspent Transaction Output
![Page 22: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/22.jpg)
Transactions
![Page 23: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/23.jpg)
Tx Fees
• sum(TxInputs) ≥ sum(TxOutputs)
• TxFee = sum(TxInputs) – sum(TxOutputs)
• Min: 0.0001 BTC / 1000 bytes of tx (~ $0.022)
• fee goes to the miner who found a block, which includes this tx
![Page 24: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/24.jpg)
Blockchain – tree of Blocks
• Blocks (BlockHash 32B)
–Txs (TxId - reverse TxHash 32B)
• Inputs (#)
–reference to UTXO (TxId, output#)
– lock script (scriptPubKey)
• Outputs (#)
–value (in satoshis = BTC*10−8)
–unlock script (scriptSig)
![Page 25: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/25.jpg)
![Page 26: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/26.jpg)
![Page 27: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/27.jpg)
• scriptSig / unlocking / input script - key icon (input spending UTXO)
• scriptPubKey / locking / output script - lock icon (output of UTXO)
![Page 28: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/28.jpg)
• output(input) pair as an invocation
• output script - like a function (a hardcoded function - tx type)
• input script - like a parameters to a function
![Page 29: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/29.jpg)
Examples of Conditions
• AlwaysPay(_) = true
• NeverPay(_) = false
• HowMuchIs2by2(Answer) = (Answer == 2*2)
• CheckPwd(Password) = (Password==“secret”)
![Page 30: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/30.jpg)
Examples of Conditions (2)
• P2PK(PubKey')(PubKey, TxSig) = (PubKey' == PubKey) && checksig(PubKey, TxSig)
• P2PKH(PubKeyHash)(PubKey, TxSig) = (PubKeyHash == ripmd160(PubKey)) && checksig(PubKey, TxSig)
![Page 31: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/31.jpg)
Evaluation Logic
1. Start with an empty stack
2. Evaluate the “unlock script” (scriptSig) from UTXO
3. Evaluate the “lock script” (scriptPubKey) from current tx input
4. If result is true (1), tx is valid, otherwise invalid
![Page 32: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/32.jpg)
“Always Pay Anyone” ;)
Stack scriptSig scriptPubKey
… OP_TRUE
…
Concatenate both scripts & start with empty stack
![Page 33: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/33.jpg)
“Always Pay Anyone” ;)
Stack scriptSig scriptPubKey
… OP_TRUE
…
…
Don’t care what’s in scriptSig – unless it invalidate the Tx. May even leave stuff on the stack.
![Page 34: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/34.jpg)
“Always Pay Anyone” ;)
Stack scriptSig scriptPubKey
… OP_TRUE
…
…
…
Don’t care what’s in scriptSig – unless it invalidate the Tx. May even leave stuff on the stack.
![Page 35: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/35.jpg)
“Always Pay Anyone” ;)
Stack scriptSig scriptPubKey
… OP_TRUE
…
1
…
…
The top of the stack is 1 (i.e. true) – the Tx is valid!
![Page 36: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/36.jpg)
“Don’t Pay” – “Burn bitcoins”
Stack scriptSig scriptPubKey
… OP_FALSE
…
Concatenate both scripts & start with empty stack
![Page 37: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/37.jpg)
“Don’t Pay” – “Burn bitcoins”
Stack scriptSig scriptPubKey
… OP_FALSE
…
…
Don’t care what’s in scriptSig – unless it invalidate the Tx. May even leave stuff on the stack.
![Page 38: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/38.jpg)
“Don’t Pay” – “Burn bitcoins”
Stack scriptSig scriptPubKey
… OP_FALSE
…
…
…
Don’t care what’s in scriptSig – unless it invalidate the Tx. May even leave stuff on the stack.
![Page 39: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/39.jpg)
“Don’t Pay” – “Burn bitcoins”
Stack scriptSig scriptPubKey
… OP_FALSE
…
0
…
…
The top of the stack is 0 (i.e. false) – the Tx is invalid!
These bitcoins are burned forever - unspendable!
![Page 40: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/40.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
4 2
2
OP_MUL
OP_EQUALVERIFY
Concatenate both scripts & start with empty stack
![Page 41: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/41.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
_4 2
2
OP_MUL
OP_EQUALVERIFY
4
Push constant to the stack
![Page 42: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/42.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
_4 2
2
OP_MUL
2 OP_EQUALVERIFY
4
Push constant to the stack
![Page 43: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/43.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
_4 2
2
2 OP_MUL
2 OP_EQUALVERIFY
4
Push constant to the stack
![Page 44: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/44.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
_4 2
2
OP_MUL
4 OP_EQUALVERIFY
4
Multiply 2 values on top of the stack
![Page 45: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/45.jpg)
Pay to math genius who knows
how much is 2 * 2 = ? Stack scriptSig scriptPubKey
_4 2
2
OP_MUL
OP_EQUALVERIFY
4 == 4 – the Tx is valid!
Too easy – need a real cryptographic solution!
![Page 46: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/46.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHash>
OP_EQUALVERIFY
OP_CHECKSIG
Concatenate both scripts & start with empty stack.
99% of all Bitcoin Txs use this script.
![Page 47: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/47.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHash>
OP_EQUALVERIFY
<sig> OP_CHECKSIG
push constant onto the stack
![Page 48: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/48.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHash>
<pubKey> OP_EQUALVERIFY
<sig> OP_CHECKSIG
push constant onto the stack
![Page 49: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/49.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKey> <pubKeyHash>
<pubKey> OP_EQUALVERIFY
<sig> OP_CHECKSIG
Duplicate value on the top of the stack
![Page 50: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/50.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHashNew> <pubKeyHash>
<pubKey> OP_EQUALVERIFY
<sig> OP_CHECKSIG
Calculate RIPEMD160 hash:
Bitcoin address = RIPEMD160(pubKey)
![Page 51: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/51.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKeyHash> <pubKey> OP_HASH160
<pubKeyHashNew> <pubKeyHash>
<pubKey> OP_EQUALVERIFY
<sig> OP_CHECKSIG
push constant onto the stack
![Page 52: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/52.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHash>
<pubKey> OP_EQUALVERIFY
<sig> OP_CHECKSIG
verify that 2 values are equal: if equal, continue;
else invalidate tx & stop execution
![Page 53: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/53.jpg)
Pay to PubKeyHash - P2PKH
Stack scriptSig scriptPubKey
<sig> OP_DUP
<pubKey> OP_HASH160
<pubKeyHash>
OP_EQUALVERIFY
1 OP_CHECKSIG
Signature is checked for top two stack items.
1 on top of the stack – Tx is valid!
![Page 54: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/54.jpg)
MultiSig – M out of N Tx Stack scriptSig scriptPubKey
OP_0 2
<SigBuyer> <PubKeyBuyer>
<SigSeller> <PubKeySeller>
<PubKeyMediator>
3
OP_CHECKMULTISIG
Any 2 out of 3 can sign this Tx:
Buyer & Seller, Mediator & Buyer or Mediator & Seller
![Page 55: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/55.jpg)
MultiSig – M out of N Tx Stack scriptSig scriptPubKey
OP_0 2
<SigBuyer> <PubKeyBuyer>
<SigSeller> <PubKeySeller>
<PubKeyMediator>
3
<SigSeller> OP_CHECKMULTISIG
<SigBuyer>
0
Any 2 out of 3 can sign this Tx:
Buyer & Seller, Mediator & Buyer or Mediator & Seller
![Page 56: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/56.jpg)
MultiSig – M out of N Tx Stack scriptSig scriptPubKey
3 OP_0 2
<PubKeyMediator> <SigBuyer> <PubKeyBuyer>
<PubKeySeller> <SigSeller> <PubKeySeller>
<PubKeyBuyer> <PubKeyMediator>
2 3
<SigSeller> OP_CHECKMULTISIG
<SigBuyer>
0
Any 2 out of 3 can sign this Tx:
Buyer & Seller, Mediator & Buyer or Mediator & Seller
![Page 57: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/57.jpg)
MultiSig – M out of N Tx Stack scriptSig scriptPubKey
OP_0 2
<SigBuyer> <PubKeyBuyer>
<SigSeller> <PubKeySeller>
<PubKeyMediator>
3
OP_CHECKMULTISIG
1
If 2 signatures matching any 2 out of 3 public keys
– Tx is Valid!
![Page 58: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/58.jpg)
Standard Tx Script Types
• Pay-to-PubKey (P2PK) – obsolete
• Pay-to-PubKeyHash (P2PKH) – 99% of all Tx
• Pay-to-ScriptHash (P2SH)
• Multisig – obsolete
• Nulldata - OP_RETURN
![Page 59: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/59.jpg)
Non-standard Txs
• DDoS attacks against bitcoin nodes, which send non-standard tx
• an invalid script (and tx) will not be accepted
• a non-standard script (and tx) will not be relayed to the network
• but some miner pools will accept them (Eligius) – need to send directly to them
![Page 60: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/60.jpg)
NullData Script
• OP_RETURN [up to 40 bytes metadata]
- immediately invalidates the tx
- allows embedding metadata into blockchain
- unspendable / non-redeemable (burned)
- Before OP_RETURN was whitelisted metadata was encoded as fake addresses
- provably prunable
![Page 61: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/61.jpg)
NullData Script
Stack scriptSig scriptPubKey
OP_RETURN
<metadata-40B>
![Page 62: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/62.jpg)
NullData Script
Stack scriptSig scriptPubKey
OP_RETURN
<metadata-40B>
Tx immediately invalidated - unspendable
![Page 63: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/63.jpg)
Disadvatages
• Bitcoin Script can be used to implement a weak version of Smart Contracts, but:
– Not Turing-complete
– Designed for Tx Validation – not general purpose
– Lack of state (either valid or invalid Tx, no storage)
– Value-blindness (i.e. just use UTXO value – can’t pay arbitrary amount of BTC)
– Blockchain-blindness (can’t use blockchain data – source of randomness, needed for gambling)
![Page 64: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/64.jpg)
Smart Contracts on Bitcoin
• Smart Contracts on Bitcoin require multiple technologies:
– Pay to Script Hash (P2SH) Multisig
– OP_RETURN to encode Metadata on the Blockchain
– Oracles - network of external servers running Smart Contracts’ deterministic Turing-complete code (decisions by strict majority like Jury)
• Too Hacky, Complex & Error-prone!
![Page 65: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/65.jpg)
SMART CONTRACTS – SCI-FI
![Page 66: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/66.jpg)
![Page 67: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/67.jpg)
if (SelfAware()) {
Suicide();
PowerOff();
}
![Page 68: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/68.jpg)
In Strong AI terms “Sovereign”
is a Smart Contract w/o backdoor
![Page 69: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/69.jpg)
Screw flying cars. I want a car that
own & maintain itself – Mike Hearn
![Page 70: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/70.jpg)
Driverless Mercedes
![Page 71: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/71.jpg)
ZΛDΛTΛ © 2015 http://www.homefreeamerica.us/future-work-turking-uber-wont-like/
![Page 72: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/72.jpg)
![Page 73: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/73.jpg)
![Page 74: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/74.jpg)
![Page 75: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/75.jpg)
SMART CONTRACTS – DOWN TO EARTH
![Page 76: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/76.jpg)
Vending Machines
![Page 77: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/77.jpg)
![Page 78: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/78.jpg)
& “Things”
![Page 79: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/79.jpg)
![Page 80: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/80.jpg)
Ethereum: Bitcoin on Steroids!
![Page 81: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/81.jpg)
![Page 82: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/82.jpg)
Ethereum White Paper
![Page 83: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/83.jpg)
Ethereum “Yellow Paper”
![Page 84: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/84.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 85: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/85.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 86: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/86.jpg)
Secure
• Distributed Systems Consensus:
– Paxos
– Raft
• BGP – Byzantine Generals Problem
• Game Theory / Incentivization
• Trustless Consensus:
– Proof-of-Work (PoW) - Mining
– Proof-of-Stake (PoS)
– Proof-of-X…
![Page 87: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/87.jpg)
Blockchain Forks
![Page 88: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/88.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 89: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/89.jpg)
![Page 90: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/90.jpg)
![Page 91: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/91.jpg)
Decentralization Continuum
Source: The “Unbundling of Trust”: how to identify good cryptocurrency opportunities? by Richard Brown
http://www.gendal.me/2014/11/14/the-unbundling-of-trust-how-to-identify-good-cryptocurrency-opportunities/
![Page 92: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/92.jpg)
Decentralized
Centralized Decentralized
Apple iTunes, Netflix Bittorrent
Facebook Diaspora*
WhatsApp Jabber/XMPP
Cellular operators Firechat – Mesh Networks
AOL Internet
Post Office email
Domain Registrars Namecoin
PayPal Bitcoin
![Page 93: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/93.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 94: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/94.jpg)
Generalized
• Turing-complete, Deterministic code
• Featureless vs. feature based platforms (mostly financial contracts / gambling):
– Mastercoin/Omni
– Counterparty
– NXT
– BitShares
– etc.
![Page 95: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/95.jpg)
Source: Great Chain of Numbers,
Tim Swanson
![Page 96: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/96.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 97: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/97.jpg)
Ledger
![Page 98: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/98.jpg)
What is Ethereum?
A Secure
Decentralized
Generalized
Transaction
Ledger
![Page 99: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/99.jpg)
Bitcoin Tx as a State Transition
Bitcoin State is a set of UTXOs
S’ = apply(S,Tx)
![Page 100: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/100.jpg)
Bitcoin Tx as a State Transition
Bitcoin State is a set of UTXOs
S’ = apply(S,Tx)
![Page 101: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/101.jpg)
Ethereum Tx as a State Transition
Ethereum State is a set of Accounts S’ = apply(S,Tx)
![Page 102: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/102.jpg)
Ethereum Tx as a State Transition
Ethereum State is a set of Accounts S’ = apply(S,Tx)
![Page 103: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/103.jpg)
Block – sequence of Txs
S[n] = foldl(apply, S[0], Txs)
S_FINAL = apply(S[n], PAY_BLOCK_REWARD)
![Page 104: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/104.jpg)
Ethereum Blockchain
• Same concept like in Bitcoin
• Bitcoin block time ~ 10 min
• Ethereum block time 5 block candidates per 1 min ~ 1 block per 12-15 sec
![Page 105: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/105.jpg)
Ethereum Account Types
• EOA (Externally-owned Account)
– controlled by Human or application (DApp)
– only EOA can initiate transactions
• Contract Account
– can receive transactions
– can send messages to itself or other contracts
![Page 106: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/106.jpg)
Ethereum Account
• Address – 160 bit excerpt from public key
• Balance (in ether ~ $0.70/ETH now)
• Nonce
Contract Accounts in addition have:
• Code
• Storage
![Page 107: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/107.jpg)
Contract
• “Contract” is not a good name, better names:
– Autonomous Agent
– Actor
– Object (like in OOP)
![Page 108: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/108.jpg)
Contracts
• Contract are like people:
– can call / send messages to other contracts
– … and return values
– can create new contracts
– can replicate itself
– can “suicide”
– can pay (send ether) other contracts or people
– … can buy things
![Page 109: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/109.jpg)
Create Contract
• Create Contract:
– Endowment (ETH)
– Init code (whatever returned from init code)
– Gas
– Signature
• On creation:
– Places a new account in the system with code
(code in account is whatever returned from init)
![Page 110: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/110.jpg)
Source: Richard Brown
![Page 111: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/111.jpg)
Send a Message Call to Contract
• Send a message call to a contract:
– Recipient account address (160 bit)
– Value (ETH)
– Data (byte array)
– Gas limit
– Gas price (multiplier per ETH, used for tx priority)
– Signature
• On message receipt:
– Value is transferred to recipient’s balance
– Recipients code (if any) runs
– Return result to the caller
![Page 112: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/112.jpg)
Transactions & Messages
• Transaction originates always from EOA
• And can result in multiple message calls to contract accounts
• Transactions are recorded in the blockchain
• Message calls are transient (only exist while transaction executing)
![Page 113: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/113.jpg)
ETHEREUM VM – EVM
![Page 114: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/114.jpg)
Ethereum VM - EVM
• Stack of 32B (256bit) words
• Byte-addressable Memory (2256 bytes addressable)
• Key/Value Storage (2256 words addressable)
![Page 115: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/115.jpg)
EVM - Storage
• Isolated from other accounts
• Storage address space modeled as Associative Array, not a Linear Memory – Key/Value Store
• the only VM which uses Associative Array for Address Space
• Every new (unused) word in memory/storage has 0 value
• Writing 0 to storage word - equivalent to deleting it (freeing it)
![Page 116: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/116.jpg)
EVM State is 8-tuple:
{
block_state, // also references storage
transaction, // current transaction
message, // current message
code, // current contract’s code
memory, // memory byte array
stack, // words on the stack
pc, // program counter → code[pc]
gas // gas left to run tx
}
![Page 117: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/117.jpg)
Why 256 bit?
• Crypto primitives:
– SHA256 (SHA3)
– public key is 256-bit uint (odd/even,x)
– Private key uses sepc256k1/EDCSA is 2 256-bit uints (r,s)
• 160-bit account addresses fit into 256-bit
• 256-bit SIMD ISAs (SSE,AVX) on modern CPUs
![Page 118: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/118.jpg)
WORD – Data Types
• 256 bit big endian unsigned integers - uint256
• 256 bit 2-s complement signed integers - int256
• 256 bit hash (as big endian)
• 160 bit Account Address
– big endian, least significant 20 bytes only
– 12 most significant bytes discarded
• 32 bytes/characters
• 0 – False, 1 - True
![Page 119: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/119.jpg)
Ethereum VM (EVM) ISA
From To Opcode groups
00 0F Stop and Arithmetic Operations
10 1F Comparison & Bitwise Logic Operations
20 2F SHA3 hashing
30 3F Environmental Information
40 4F Block Information
50 5F Stack, Memory, Storage and Flow Operations
60 7F Push Operations
80 8F Duplication Operations
90 9F Exchange Operations
A0 AF Logging Operations
F0 FF Contract Operations
![Page 120: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/120.jpg)
Arithmetic Ops Hex Mnemonic δ α Description
01 ADD 2 1 Addition
02 MUL 2 1 Multiplication
03 SUB 2 1 Subtraction
04 DIV 2 1 Integer division
05 SDIV 2 1 Signed integer division. Where all values are treated as two’s complement signed 256-bit integers
06 MOD 2 1 Modulo remainder
07 SMOD 2 1 Signed modulo remainder. Where all values are treated as two’s complement signed 256-bit integers
08 ADDMOD 3 1 Modulo addition
09 MULMOD 3 1 Modulo multiplication
0A EXP 2 1 Exponential operation
0B SIGNEXTEND 2 1 Extend length of two’s complement signed integer
![Page 121: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/121.jpg)
10s: Comparison & Bitwise Logic Hex Mnemonic δ α Description
10 LT 2 1 Less-than comparison
11 GT 2 1 Greater-than comparison
12 SLT 2 1 Signed less-than comparison
13 SGT 2 1 Signed greater-than comparison
14 EQ 2 1 Equality comparison
15 ISZERO 1 1 Simple not operator
16 AND 2 1 Bitwise AND
17 OR 2 1 Bitwise OR
18 XOR 2 1 Bitwise XOR
19 NOT 1 1 Bitwise NOT
1A BYTE 2 1 Retrieve single byte from word. For Nth byte, we count from the left (i.e. N=0 would be the most significant in big endian)
![Page 122: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/122.jpg)
20s: SHA3 hashing Hex Mnemonic δ α Description
20 SHA3 2 1 Compute Keccak-256 hash for the range in memory [start, start+len-1] μs [0] ≡ Keccak(μm [μs [0] . . . (μs [0] + μs [1] − 1)]) μi ≡ M (μi , μs [0], μs [1])
![Page 123: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/123.jpg)
Message Call Data Ops Hex Mnemonic δ α Description
35 CALLDATALOAD 1 1 Get input data of current environment. This pertains to the input data passed with the message call instruction or transaction
36 CALLDATASIZE 0 1 Get size of input data in current environment. This pertains to the input data passed with the message call instruction or transaction
37 CALLDATACOPY 3 0 Copy input data in current environment to memory. This pertains to the input data passed with the message call instruction or transaction
![Page 124: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/124.jpg)
Contract Code Ops Hex Mnemonic δ α Description
38 CODESIZE 0 1 Get size of code running in current environment
39 CODECOPY 3 0 Copy code running in current environment to memory
3B EXTCODESIZE 1 1 Get size of an account’s code
3C EXTCODECOPY 4 0 Copy an account’s code to memory
![Page 125: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/125.jpg)
30s: Environmental Information Hex Mnemonic δ α Description
30 ADDRESS 0 1 Get address of currently executing account (its like this / self in OOP, self() in Erlang)
31 BALANCE 1 1 Get balance of the given account
32 ORIGIN 0 1 Get execution origination address. This is the sender of original transaction; it is never an account with non-empty associated code
33 CALLER 0 1 Get caller address. This is the address of the account that is directly responsible for this execution
34 CALLVALUE 0 1 Get deposited value by the instruction/transaction responsible for this execution
3A GASPRICE 0 1 Get price of gas in current environment. This is gas price specified by the originating transaction
5A GAS 0 1 Get the amount of available gas
![Page 126: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/126.jpg)
40s: Block Information Hex Mnemonic δ α Description
40 BLOCKHASH 1 1 Get the hash of one of the 256 most recent complete blocks
41 COINBASE 0 1 Get the block’s coinbase address
42 TIMESTAMP 0 1 Get the block’s timestamp
43 NUMBER 0 1 Get the block’s number
44 DIFFICULTY 0 1 Get the block’s difficulty
45 GASLIMIT 0 1 Get the block’s gas limit
![Page 127: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/127.jpg)
Memory Hex Mnemonic δ α Description
51 MLOAD 1 1 Load word from memory
52 MSTORE 2 0 Save word to memory
53 MSTORE8 2 0 Save byte to memory
59 MSIZE 0 1 Get the size of active memory in bytes
![Page 128: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/128.jpg)
Storage Hex Mnemonic δ α Description
54 SLOAD 1 1 Load word from storage
55 SSTORE 2 0 Save word to storage
![Page 129: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/129.jpg)
Control Flow Hex Mnemonic δ α Description
00 STOP 0 0 Halts execution
56 JUMP 1 0 Alter the program counter
57 JUMPI 2 0 Conditionally alter the program counter
58 PC 0 1 Get the program counter
5B JUMPDEST 0 0 Mark a valid destination for jumps. This operation has no effect on machine state during execution
![Page 130: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/130.jpg)
Contract ops Hex Mnemonic δ α Description
F0 CREATE 3 1 Pops a,b,c. Creates a new contract with code from memory[b : b+c] and endowment (initial ether sent) a, and pushes the value of the contract
F1 CALL 7 1 Send message call to contract
F2 RETURN 2 1 Pops a,b. Stops execution, returning memory[a : a+b]
FF SUICIDE 1 0 Sends all remaining ether to specified address, Returns and flags contract for deletion as soon as tx ends Like C++: delete this;
![Page 131: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/131.jpg)
Stack ops Hex Mnemonic δ α Description
50 POP 1 0 Remove item from stack
60 61 … 7F
PUSH1 PUSH2 … PUSH32
0 1 Place 1,2…32 bytes item on stack. The bytes are read in line from the program code’s bytes array. The function c ensures the bytes default to zero if they extend past the limits. The byte is right-aligned (takes the lowest significant place in big endian).
DUP … Operations to duplicate values on the stack
SWAP … Operations to swap values on the stack
![Page 132: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/132.jpg)
GAS ECONOMY
![Page 133: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/133.jpg)
Fee Schedule (Gas)
![Page 134: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/134.jpg)
Fee Schedule (Gas)
![Page 135: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/135.jpg)
Fee Schedule (Gas)
![Page 136: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/136.jpg)
Fee Schedule (Gas)
![Page 137: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/137.jpg)
Name Registry contract
![Page 138: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/138.jpg)
Name Registry contract
![Page 139: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/139.jpg)
Name Registry contract
Compiled to EVM assembly:
PUSH1 0 CALLDATALOAD SLOAD NOT PUSH1 9 JUMPI STOP JUMPDEST PUSH1 32 CALLDATALOAD PUSH1 0 CALLDATALOAD SSTORE
![Page 140: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/140.jpg)
EVM State is 8-tuple:
{
block_state, // also references storage
transaction, // current transaction
message, // current message
code, // current contract’s code
memory, // memory byte array
stack, // words on the stack
pc, // program counter → code[pc]
gas // gas left to run tx
}
![Page 141: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/141.jpg)
EVM State inside Contract:
Invariant per Contract:
block_state, // also references storage
transaction, // current transaction
message, // current message
code // current contract’s code
Contract State:
{
pc, // program counter → code[pc]
gas, // gas left to run tx
stack, // words on the stack
memory, // memory byte array
storage // K/V store of words
}
![Page 142: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/142.jpg)
Example of Tx
Zvi registers a domain “54” with IP “20202020”:
- Send Tx:
- From: “Zvi 160-bit address”
- To: “NameRegistry” contract’s address
- Value: 0 ether
- Data: [54, 20202020]
- GasLimit: 2000 gas
- GasPrice: 1.0 (1 gas == 1 wei)
![Page 143: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/143.jpg)
Example of Tx - Gas
Calldata [54, 20202020] is 2 words of 32 bytes = 64 bytes.
StartGas * GasPrice = 2000 * 1 = 2000 wei
Tx costs:
• 500 + 5*TXDATALEN = 500 – 5*64 bytes = 820 gas
![Page 144: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/144.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD
3 SLOAD
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 145: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/145.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 146: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/146.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 147: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/147.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 148: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/148.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 149: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/149.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 150: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/150.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 151: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/151.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 152: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/152.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD -1 -847 [32] [] {}
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 153: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/153.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD -1 -847 [32] [] {}
13 PUSH1 0 -1 -848 [2020202020] [] {}
15 CALLDATALOAD
16 SSTORE
![Page 154: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/154.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD -1 -847 [32] [] {}
13 PUSH1 0 -1 -848 [2020202020] [] {}
15 CALLDATALOAD -1 -849 [2020202020, 0] [] {}
16 SSTORE
![Page 155: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/155.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD -1 -847 [32] [] {}
13 PUSH1 0 -1 -848 [2020202020] [] {}
15 CALLDATALOAD -1 -849 [2020202020, 0] [] {}
16 SSTORE -300 -850 [2020202020, 54] [] {}
![Page 156: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/156.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {}
2 CALLDATALOAD -1 -821 [0] [] {}
3 SLOAD -20 -822 [54] [] {}
4 NOT -1 -842 [0] [] {}
5 PUSH1 9 -1 -843 [1] [] {}
7 JUMPI -1 -844 [1, 9] [] {}
8 STOP
9 JUMPDEST -1 -845 [] [] {}
10 PUSH1 32 -1 -846 [] [] {}
12 CALLDATALOAD -1 -847 [32] [] {}
13 PUSH1 0 -1 -848 [2020202020] [] {}
15 CALLDATALOAD -1 -849 [2020202020, 0] [] {}
16 SSTORE -300 -850 [2020202020, 54] [] {}
-1150 [] [] {54: 2020202020}
![Page 157: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/157.jpg)
Gas Usage
• 1150 gas consumed by Tx execution
• 2000 gas – 1150 gas = 850 gas refund
• If we were setting GasLimit to less than 1150, the Tx would be failing in the middle and all gas would be consumed (no refund)
![Page 158: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/158.jpg)
Send the same Tx 2nd time
Zvi registers a domain “54” with IP “20202020”:
- Send Tx:
- From: “Zvi 160-bit address”
- To: “NameRegistry” contract’s address
- Value: 0 ether
- Data: [54, 20202020]
- GasLimit: 2000 gas
- GasPrice: 1.0 (1 gas == 1 wei)
![Page 159: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/159.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD
3 SLOAD
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 160: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/160.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 161: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/161.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 162: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/162.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT -1 -842 [2020202020] [] {54: 2020202020}
5 PUSH1 9
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 163: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/163.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT -1 -842 [2020202020] [] {54: 2020202020}
5 PUSH1 9 -1 -843 [0] [] {54: 2020202020}
7 JUMPI
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 164: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/164.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT -1 -842 [2020202020] [] {54: 2020202020}
5 PUSH1 9 -1 -843 [0] [] {54: 2020202020}
7 JUMPI -1 -844 [0, 9] [] {54: 2020202020}
8 STOP
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 165: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/165.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT -1 -842 [2020202020] [] {54: 2020202020}
5 PUSH1 9 -1 -843 [0] [] {54: 2020202020}
7 JUMPI -1 -844 [0, 9] [] {54: 2020202020}
8 STOP -0 -845 [] [] {54: 2020202020}
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
![Page 166: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/166.jpg)
PC OPCODE FEE GAS STACK MEM STORAGE
0 PUSH1 0 -1 -820 [] [] {54: 2020202020}
2 CALLDATALOAD -1 -821 [0] [] {54: 2020202020}
3 SLOAD -20 -822 [54] [] {54: 2020202020}
4 NOT -1 -842 [2020202020] [] {54: 2020202020}
5 PUSH1 9 -1 -843 [0] [] {54: 2020202020}
7 JUMPI -1 -844 [0, 9] [] {54: 2020202020}
8 STOP -0 -845 [] [] {54: 2020202020}
9 JUMPDEST
10 PUSH1 32
12 CALLDATALOAD
13 PUSH1 0
15 CALLDATALOAD
16 SSTORE
-845 {54: 2020202020}
![Page 167: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/167.jpg)
Gas Usage (2nd Tx)
• 845 gas consumed by 2nd Tx execution
• 2000 gas – 845 gas = 1155 gas refund
• If we were setting GasLimit to less than 845, the Tx would be failing in the middle and all gas would be consumed (no refund)
![Page 168: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/168.jpg)
Acceptable uses of the EVM
• Acceptable uses:
– running business logic (“IFTTT - If This Then That")
– verifying signatures & other cryptographic objects
– applications that verify parts of other blockchains (eg. a decentralized ether-to-bitcoin exchange)
• Unacceptable uses:
– using the EVM as a file storage, email or text messaging
– anything to do with GUI, web apps, etc.
– cloud computing, HPC, number crunching, ML, etc.
![Page 169: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/169.jpg)
Ethe
reu
m D
SLs
![Page 170: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/170.jpg)
DSLs for Ethereum Smart Contracts
• Low-level
– EVM Assembly
– LLL (Triple-L) - Lisp-like Low-level Language
• High-level
– Serpent (Python-like) – going to be obsolete?
– EtherScript – Visual DSL
– Mutan (Go-like) – obsolete
– CLL (C-like language) – obsolete
– Solidity - (C/Javascript like with static types)
![Page 171: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/171.jpg)
LLL (triple L)
• Lisp-like Low-level Language
• (*.lll)
• Used mostly for compilers & tools
• LISP-flavored EVM “MacroAssembly”
• S-expressions of opcodes
• Unlike EVM Assembly
– no need to manage stack
– no need to manage jumps & jump dest labels
• Can test & generate LLL from Clojure
– https://github.com/drcode/clll
![Page 172: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/172.jpg)
LLL Basics – Assm as S-expr
(OPCODE OPERAND1 OPERAND2 ...)
0x20 PUSH 0x20
(add 2 3) PUSH 2 PUSH 3 ADD
(mload 0x20) PUSH 0x20 MLOAD
![Page 173: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/173.jpg)
![Page 174: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/174.jpg)
LLL Advanced
Variables: (set 'NAME EXPR)
Macros: (def 'NAME EXPR)
(def 'NAME (ARG1 ARG2 …) EXPR)
Inline Asm: (asm OPCODE OPCODE …)
(asm 23 45 MUL 67 ADD)
![Page 175: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/175.jpg)
LLL Sugar
@EXPR -> (mload EXPR)
[ EXPR1 ] EXPR2 -> (mstore EXPR1 EXPR2)
@@EXPR -> (sload EXPR)
[[ EXPR1 ]] EXPR2 -> (sstore EXPR1 EXPR2)
$N -> (calldataload N)
{ EXPR1 EXPR2 ... } -> (seq EXPR1 EXPR2 ...)
![Page 176: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/176.jpg)
![Page 177: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/177.jpg)
Serpent
“python is serpent,
but Serpent is not Python” -- Ethereum joke
![Page 178: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/178.jpg)
Serpent
• Python-like syntax
• Python control flow (if, while, etc.)
• Infix operators
• EVM semantics
• Special variables to refer to EVM properties
• A little bit higher level than LLL
• Can write unit tests in Python
• (*.se)
![Page 179: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/179.jpg)
![Page 180: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/180.jpg)
![Page 181: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/181.jpg)
Mutan (Go-like syntax) - obsolete
subcurrency.mu
![Page 182: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/182.jpg)
SOLIDITY
Solidity new DSL specifically designed for Ethereum Contracts
![Page 183: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/183.jpg)
Solidity (*.sol)
• DSL designed specifically for Ethereum contracts
• Syntax similar to C/C++
• Statically typed
• ABI – Application Binary Interface – i.e. function from one contract knows how to call and marshal
arguments to function from another contracts
– i.e. common contract code libraries
• Mix IDE for Solidity: – https://github.com/ethereum/wiki/wiki/Mix:-The-DApp-IDE
• Solidity Online Compiler: – http://chriseth.github.io/cpp-ethereum
![Page 184: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/184.jpg)
Contracts
contract Foo {
...
}
![Page 185: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/185.jpg)
“init” code - Constructor
contract Foo {
function Foo {
...
}
}
![Page 186: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/186.jpg)
“member” variable – in storage
public by default contract Foo {
function Foo {
x = 69;
}
uint x;
}
![Page 187: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/187.jpg)
Private “member” variable
contract Foo {
function Foo {
x = 69;
}
private uint x;
}
![Page 188: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/188.jpg)
Functions can access
private members contract Foo {
function Foo {
x = 69;
}
function getx() returns (uint) {
return x;
}
private uint x;
}
![Page 189: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/189.jpg)
Types
• bool
• intN - N in [8:8:256] bit, int is int256
• uintN - N in [8:8:256] bit, uint is uint256
• hashN - N in [8:8:256] bit, hash is hash256
• address - 160 bit
• stringN - N in [0:32] bytes
– string0 - empty string
– string1 – character
– string32 – 32 char fixed-length string
![Page 190: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/190.jpg)
Type Inference
hash x = 0x123;
var y = x; // y will be of type “hash”
![Page 191: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/191.jpg)
Struct
struct Account {
string32 name;
address accountNo;
uint256 balance;
}
![Page 192: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/192.jpg)
Mappings (assoc arrays)
mapping (KEYTYPE => VALUETYPE) M;
• Regular finite-size member variables take continuous storage slots starting from position 0
• The mapping variable (M) itself takes unfilled slot in some position p (i.e. p = addr(M) )
• Mappings layout in storage:
addr(M[k]) = sha3(k . p)
![Page 193: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/193.jpg)
Nested Mappings
mapping (K1 => mapping (K2 => V) ) M;
addr(M[K1][K2]) = sha3(K2 . sha3(K1 . addr(M)))
![Page 194: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/194.jpg)
Data Structure Nesting
• No Arrays (yet)
• Structs can be nested
• Mappings can be nested
• Structs can include Mappings
• Mappings can include Structs
![Page 195: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/195.jpg)
“Paid” Function Calls contract InfoFeed {
function info() returns (uint ret) {
return 42;
}
}
contract Consumer {
InfoFeed feed;
function setFeed(address addr) {
feed = InfoFeed(addr);
}
function callFeed() {
feed.info.value(10).gas(800)(); }
}
![Page 196: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/196.jpg)
Subcurrency example contract Coin {
function Coin {
balances[msg.sender] = 1000000000;
}
function send(address to, uint value) {
if(balances[msg.sender] >= value) {
balances[msg.sender] -= value;
balances[to] += value;
}
}
private mapping(address => uint) balances;
}
![Page 197: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/197.jpg)
Events vs Polling
Source: Richard Brown
![Page 198: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/198.jpg)
Events
contract Counter {
event Incremented();
function Counter {
//total = 0;
}
function Inc() {
total++;
Incremented();
}
uint total;
}
![Page 199: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/199.jpg)
subcurrency.sol with event contract Coin {
event BalanceChanged(address indexed from,
address indexed to, uint value);
function Coin {
balances[msg.sender] = 1000000000;
}
function send(address to, uint value) {
if(balances[msg.sender] >= value) {
balances[msg.sender] -= value;
balances[to] += value;
BalanceChanged(msg.sender, to, value);
}
}
private mapping(address => uint) balances;
}
![Page 200: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/200.jpg)
MULTIPLE INHERITANCE & MODIFIERS
![Page 201: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/201.jpg)
contract owned {
modifier onlyowner {
if (msg.sender == owner) _
address owner = msg.sender;
}
![Page 202: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/202.jpg)
contract owned {
modifier onlyowner {
if (msg.sender == owner) _
address owner = msg.sender;
}
contract mortal is owned {
function kill() onlyowner { suicide(owner);
}
}
![Page 203: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/203.jpg)
contract owned {
modifier onlyowner {
if (msg.sender == owner) _
address owner = msg.sender;
}
contract mortal is owned {
function kill() onlyowner { suicide(owner);
}
}
contract Foo is owned, mortal {...}
![Page 204: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/204.jpg)
Thank You! Now Q&A
All images are taken from Google Image search and various other places on the Internet
© Copyright of corresponding owners
![Page 205: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/205.jpg)
BACKUP SLIDES
![Page 206: Ethereum VM and DSLs for Smart Contracts (updated on May 12th 2015)](https://reader034.fdocuments.in/reader034/viewer/2022042717/55d58c5ebb61eb1f218b45ba/html5/thumbnails/206.jpg)
Ethereum VM became
Industry Standard • IBM Adept (+ Samsung)
– IoT + Blockchain + Smart Contracts
• Eris Industries
– Middleware for Private Blockchain + Decentralized Apps
• Clearmatics
– Middleware for OTC financial contracts clearing / settlement
• Counterparty/Ethereum
– Financial Contracts + bets