0% found this document useful (0 votes)
34 views22 pages

11-Ethereum and Smart Contract

The document discusses notable cryptocurrencies including Bitcoin, Ethereum, and Zcash. It provides details on Ethereum, describing it as extending blockchain capabilities through smart contracts, which are code running as part of transactions. Ethereum's currency is Ether. Key aspects of Ethereum include accounts, transactions, messages, and its use of a state transition function to process transactions and execute smart contracts at each node.

Uploaded by

Rim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views22 pages

11-Ethereum and Smart Contract

The document discusses notable cryptocurrencies including Bitcoin, Ethereum, and Zcash. It provides details on Ethereum, describing it as extending blockchain capabilities through smart contracts, which are code running as part of transactions. Ethereum's currency is Ether. Key aspects of Ethereum include accounts, transactions, messages, and its use of a state transition function to process transactions and execute smart contracts at each node.

Uploaded by

Rim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Notable Cryptocurrencies

Very different cryptocurrencies each introducing different


and powerful notions

• Bitcoin: the first one, consensus via proof of work, blockchain, miners, etc.

• Ethereum: smart contracts

• : no proof of work, committee selection, no forks, scalable

• Zcash: encrypted ledger, transactions via zero-knowledge proofs


Ethereum and Smart Contract

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

Cf. Shawn Fanning, Napster @19 yr


3
History of Ethereum - Timeline

4
Ethereum
• Ethereum extends blockchain capabilities with
smart contracts

• Smart contract = code running as part of


transactions
• don’t think of it as something that needs to be complied with, but more
generally as code

• Currency is Ether
The Origin of Smart Contract

Nick Szabo
The Origin of Smart Contract

• The word “Smart Contract” was coined


by Nick Szabo in 1996.
• His idea of cryptocurrency Bit gold as a
sort of a precursor for Bitcoin

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

• All participants in Ethereum replay the blockchain as in Bitcoin to


verify transactions, which here also means that they are running
the code of the smart contracts
Ethereum notions

• Accounts
• Transactions
• Messages
Account
Identified by a 20-byte address

Is a tuple consisting of:

• nonce: counter used to identify transactions


• ether balance
• contract code: written in EVM (Ethereum Virtual Machine
Code), a low-level language that is Turing complete. Or you
can use a higher-level language Solidity

• 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

• Contract accounts: controlled by a contract code


o It gets activated when receiving a message, the smart
contract code executes, can read and write from storage and
send messages to other accounts
Transaction
A transaction creates state changes, and consists of:

• recipient of the message Why do we need a max


gas value?

• 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

• start gas value: the max number of steps the transaction is


allowed to execute for Why do we need gas
when we have ether?
• gas price: fee the sender pays for computational step computation vs financial are
different resources
Message
• Same as a transaction, contains all fields a transaction
contains except for the gas price. The message is sent by a
contract account, not by an external account

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

• There is no central place where accounts live, every


Ethereum participant keeps track of the accounts by
playing the entire blockchain

• Each Ethereum participants runs each transaction to verify


it, transfers the messages it generates and runs the
corresponding smart contract codes
Ethereum State Transition Function
Each transaction transitions the state

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)

Process for state transition function:


1. Check that the transaction is valid and well formed.
2. Check that the transaction sender has at least 2000 * 0.001 = 2 ether. If they do, then
subtract 2 ether from the sender's account.
3. Initialize gas = 2000; assuming the transaction is 170 bytes long and the byte-fee is 5, subtract
850 so that there is 1150 gas left.
4. Subtract 10 more ether from the sender's account, and add it to the contract's account.
5. Run the code. It sets the storage to the value CHARLIE. Suppose this takes 187 gas, so the
remaining amount of gas is 1150 - 187 = 963.
6. Add 963 * 0.001 = 0.963 ether back to the sender's account, and return the resulting state.

You might also like