11-Ethereum and Smart Contract
11-Ethereum and Smart Contract
• Bitcoin: the first one, consensus via proof of work, blockchain, miners, etc.
Refs:
• Ethereum white paper
• https://medium.com/@preethikasireddy/how-does-ethereum-
work-anyway-22d1df506369
• C. Harvey, R. Popa, etc..
History of Ethereum
Vitalik Buterin
• Russian-Canadian programmer
• Co-founded Ethereum when he
was 19 years old
• Now 27 (2021), a co-founder of Bitcoin Magazine
4
Ethereum
• Ethereum extends blockchain capabilities with
smart contracts
• Currency is Ether
The Origin of Smart Contract
Nick Szabo
The Origin of Smart Contract
Nick Szabo
Blockchain Platform Examples for
Smart Contracts
• Linux Foundation's Hyperledger blockchain has a smart
contract feature called Chaincode
• Ethereum
• NEM
• Waves
• NEO
• …
Smart Contract Analogy
• Vending machine
Pros and Cons of Smart Contracts
• Self Executing
• Immutable
• Public
• Letter Strict
https://medium.com/482-labs/smart-
contracts-7f76a27ebe8b
Smart Contract
Code Example
The Ethereum Ledger
• The ledger is the same as in Bitcoin:
• same proof of work
• same idea of mining and competition on extending the
blockchain
• same consensus criteria that the longest chain wins
• The main innovations are in how the ledger is used, e.g., smart
contracts
• Accounts
• Transactions
• Messages
Account
Identified by a 20-byte address
• storage
Two types of accounts
• Externally owned account: controlled by private keys
o It can create and send a message to another account by
signing a transaction
• signature of sender
For countering a potential denial-
of-service attack, so a contract
• amount of ether to transfer from sender to recipient cannot stall all nodes by making
them run an infinite loop
• optional data
transaction message
A B C
external account contract account external or
contract
account
Spending Gas
•Gas allowance assigned by a transaction or contract applies to the total
gas consumed by that transaction and all sub-executions.
•For example:
•an external actor A sends a transaction to B with 1,000 gas;
•B’s contract code consumes 600 gas before sending a message to C;
•C’s contract code consumes 300 gas before returning;
transaction message
A B C
1000 gas 600 gas 300 gas
Then B can spend another 100 gas before running out of gas.
All execution happens at all the participants
account
Ethereum state transition function, APPLY(S,TX) -> S’
(running at every participant)
1. Check if the transaction is well-formed, the signature is valid, and the nonce matches the nonce in
the sender's account. If not, return an error.
2. Calculate the transaction fee as STARTGAS x GASPRICE, and determine the sending address
from the signature. Subtract the fee from the sender's account balance and increment the
sender's nonce. If there is not enough balance to spend, return an error.
3. Initialize GAS = STARTGAS, and take off a certain quantity of gas per byte to pay for the bytes in
the transaction. The number of bytes is given by the lines of code and data info.
4. Transfer the transaction value from the sender's account to the receiving account. If the receiving
account does not yet exist, create it. If the receiving account is a contract, run the contract's code
either to completion or until the execution runs out of gas.
5. If the value transfer failed because the sender did not have enough money, or the code execution
ran out of gas, revert all state changes except the payment of the fees, and add the fees to the
miner's account.
6. Otherwise, refund the fees for all remaining gas to the sender, and send the fees paid for gas
consumed to the miner.
Example
Transaction:
• 10 ether value, 2000 gas, 0.001 ether gasprice
• data 64bytes: 0-31 represents number 2, and 32-63 represents string CHARLIE
Contract code:
if !self.storage[calldataload(0)]:
self.storage[calldataload(0)] = calldataload(32)