CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf ·...

21
Ethereum & Smart Contracts CSCE 678

Transcript of CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf ·...

Page 1: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Ethereum & Smart Contracts

CSCE 678

Page 2: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Limitations of Cryptocurrencies

• Cryptocurrencies like Bitcoins (or Zcash/Monero)

only support coin transactions

• <Alice, 50 BTC, Bob>

• Very specific transaction structures

• To extend transactions, you need to fork the network

• What if you want to do other things?

• Examples?

2

Page 3: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Ethereum

• Blockchain + Programmable transactions

• An expressive language for writing Smart Contracts

• Decentralized Apps (DApps):web services without centralized providers (like AWS)

• All DApps can run on a public blockchain

3

Page 4: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Smart Contracts

• Turing-complete programs to process digital assets

• Transferring money

• Storing records

• Selling digital items (in-game sales)

• Online voting

• Think about a Java program:

• Runs in a VM (like a JVM)

• Bytecode instructions

• Some memory and storage

4

Page 5: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Example

contract Notary {

bytes32 public proof;

function notarize(string doc) public {

proof = sha256(doc);

}

}

5

Solidity language(close to Javascript)

Stored on blockchain

Invoke on private account

Page 6: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Example

contract Notary {

bytes32 public proof;

function notarize(string doc) public {

proof = sha256(doc);

}

}

6

Bytecode6060604052341561000f57600080fd5b5b61023b8061001f6000396000f30060606040526000357c0100000000000000000...

PUSH1 0x60PUSH1 0x40MSTORE

CALLVALUEISZERO

PUSH2 0xF...

Disassemble

Solidity language(close to Javascript)

Page 7: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Tx Structure

• Tx consists of:

• Nonce: a random number

• To: destination

• Value: ETHs to transfer

• Data: data for the contract

• Gasprice: number of ETHs per gas unit

• Maxgas: maximum gas units

• Signature: signature of the Tx

7

Return: Tx address

Depends ontransaction types

Page 8: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Deploying a Contract

• Tx consists of:

• Nonce: previous Nonce + 1

• To: N/A

• Value: 0 ETH

• Data: contract bytecode

• Gasprice: number of ETHs per gas unit

• Maxgas: maximum gas units

• Signature: signature of the Tx

8

Return: Contract address

Page 9: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Invoking a Contract

• Tx consists of:

• Nonce: previous Nonce + 1

• To: contract address

• Value: 0 ETH

• Data: {function, input}

• Gasprice: number of ETHs per gas unit

• Maxgas: maximum gas units

• Signature: signature of the Tx

9

Return: Address to retrieve return value

Page 10: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Submitting a Tx to Blockchain

• Very similar to Bitcoin

10

Alice

TxTx

Tx

Tx

Tx

Page 11: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Submitting a Tx to Blockchain

• Very similar to Bitcoin

11

Alice

TxTx

Tx

Tx

Tx

Miner

Tx

TxTx

PrevHash

ThisHash

newblock

Miner

New state

(1) Proof-of-Work(2) Execute all Txs(3) Update new state

Page 12: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Submitting a Tx to Blockchain

• Very similar to Bitcoin

12

Alice

TxTx

Tx

Tx

Tx

Miner

Tx

TxTx

PrevHash

ThisHash

newblock

Miner

New state

Verify

Page 13: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

States in Blocks

• Each block stores a Merkle root of the current state

13

Tx

PrevHash

ThisHash

New state

Tx

PrevHash

ThisHash

New state

Previous State Current State

Tx

Tx

Replicateto miners& users

Page 14: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Bitcoin vs Ethereum

• Bitcoin: miners (& other users) only has to verify if

money is double-spent

• Ethereum: all miners & users who want to verify

need to run every transaction

14

Tx

Tx

…Tx

PrevHash

ThisHash

New state

Verify

Tx

Tx

Tx

Tx

Page 15: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Executing Contract Code

15

PUSH1 0x60PUSH1 0x40MSTORE

CALLVALUEISZERO

PUSH2 0xF...

PC: 0PC: 1PC: 2PC: 3PC: 4PC: 5PC: 6

EthereumVirtual

Machine(EVM)

New state

StackLocal variables

Page 16: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Safe to Execute Contract?

• No memory attack: Type-safe and isolated

• DoS (Denial-of-service) attack:

What if a contract has a infinite loop?

16

Page 17: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Max Gas

• Senders set a max gas to each Tx

• Each instruction has a gas cost

• Tx is abandoned when there is no more gas left

• Each block also has a gas limit

(max total gas spent)

17

Instr Gas

ADD 3

MUL 5

SUB 3

DIV 5

JUMP 8

SLOAD 50

Page 18: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Max Gas and Gas Price

• Set max gas too high:

Expensive Tx fee

• Set max gas too low:

Out of gas

18

Tx fee = gas price (i.e., ETH/gas) * max gas

• Set gas price too high:

Expensive Tx fee

• Set gas price too low:

Miners refuse to run the Tx

https://ethgasstation.info/Gas price information:

Page 19: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Scalability Problem

• Ethereum is NOT scalable

• Every miner and user needs to run every transaction

• Same data is replicated to every node

• Gas limit on each block:

• Too small: cannot run a large contract (like an OS)

• Too big: can’t stop DoS attack

19

Page 20: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Scalability Problem

20

By 2017, Cryptokitties accounts for 10%of transactions on Ethereum.

Page 21: CSCE678 - Ethereum & Smart Contractscourses.cse.tamu.edu/chiache/csce678/s19/slides/ethereum.pdf · 2019-04-29 · Bitcoin vs Ethereum •Bitcoin: miners (& other users) only has

Scalability Solution: Sharding

• Split the network into partitions

• Each shard has its own blockchain and global state

• User only run part of transactions

• Cross-shard transaction is future work

• Security problem:

• With 100 shards, 51% attack ➔ 0.51% attack

• Solution: randomize sharding, so that users can’t predict which shards they are in

21