Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ethereum Protocol

Ethereum Protocol

Miya Chen

August 29, 2019
Tweet

More Decks by Miya Chen

Other Decks in Technology

Transcript

  1. Hello! I am Miya Chen, backend Enginner at AMIS Golang

    Developer Istanbul BFT consensus Wallet service Node management 2
  2. Ethereum ▷ Inspired by Bitcoin ▷ Provide a decentralized platform

    that runs customized code (smart contract) ▷ Launched on 30 July 2015 5
  3. Design (Eth 1.x) ▷ Account based ▷ Account nonce ▷

    Proof-of-Work ▷ Merkle Patricia tree/trie (MPT) ▷ Recursive Length Prefix (RLP) ▷ Gas / Gas Price ▷ EVM 6
  4. Account ▷ There are two types of accounts ◦ Externally

    owned account (EOAs), controlled by private key ◦ Contract account, related to smart contract code 7
  5. Transaction ▷ Standard transaction (EOA to EOA) ▷ Contract transaction

    (EOA to contract account) ◦ Contract creation ◦ Contract invocation 16
  6. Address of the sender (EOA) Construct a transaction Address of

    the recipient (EOA or contract account) Amount of ether transferred Number of gas provided to execute this transaction Price used for each paid gas Compiled code of contract or the hash of the invoked method signature and encoded parameter https://etherscan.io/txsPending 22
  7. What’s nonce ▷ Begin with zero ▷ Avoid replay attack

    ▷ Allow to overwrite your own pending transaction the use the same nonce 23
  8. What’s gas ▷ Minimun gas for standard transaction: 21000 ▷

    Block gas limit: ~8,000,000 ◦ Can pack maximum 380 txs in a block ▷ Tx fee = gas * gas price Tx1 gasLimit = 21000 Tx1 gasLimit = 21000 Tx3 gasLimit = 21000 ... Block gasLimit = 8,000,000 24
  9. Broadcast transaction Node Miner A memory allocation called transaction pool,

    used to store TXs. Not each node has the same view. 31
  10. Transaction pool ▷ Local vs Remote transactions ◦ Local: directly

    send to this node, will be kept always ◦ Remote: received from other nodes, might be dropped due to full tx pool 32
  11. Transaction pool ▷ Pending vs Queued transactions ◦ Pending: ready

    to be processed and included in the block ◦ Queued: the transaction nonce is not in sequence 33
  12. Transaction pool ▷ Can replace the transaction? Y ◦ Same

    sender ◦ Same nonce ◦ Higher gas price (10% by default) ◦ Transaction hash will be changed From: 0xABC.. To: 0xdef… Nonce: 100 Data: 0xffff… gas: 50000 gasPrice: 1 GWei From: 0xABC.. To: 0xdef… Nonce: 100 Data: 0xffff… gas: 50000 gasPrice: 2 GWei 34
  13. Pack transactions ▷ Consider the pending transactions ◦ Valid transaction

    ◦ Valid signature (v, r, s) ◦ Valid nonce ▷ How does a miner decide to pack TXs? ◦ Limited block size, gasUsed <= gasLimit ◦ Order by price and nonce ◦ Empty block is valid 36
  14. Transaction execution Sender Recipient Start End operattion gas used 50

    operattion gas used 30 gas -50 gas -30 gas 200 Tx value: 300 wei gas limit: 200 gas price: 1wei Sender balance -500 Need to pay: 300 (value) + 200 * 1 (pre-paid tx fee) = 500 Recipient balance + 300 Miner Miner balance + 80 gas refund Sender balance + 120 39
  15. Need to pay: 300 (value) + 200 * 1 (pre-paid

    tx fee) = 500 Transaction execution (execution failed) Sender Start End operattion gas used 50 operattion gas used 30 gas -50 gas -30 gas 200 Tx value: 300 wei gas limit: 200 gas price: 1wei Sender balance -500 Miner Miner balance + 80 ether transfer revert / gas refund Sender balance +300 + 120 revert state 40
  16. Need to pay: 300 (value) + 200 * 1 (pre-paid

    tx fee) = 500 Transaction execution (Out of Gas) Sender Start End operattion gas used 150 operattion gas used 80 gas -150 gas -50 gas 200 Tx value: 300 wei gas limit: 200 gas price: 1wei Sender balance -500 Miner Miner balance + 200 Sender balance +300 revert state 42 ether transfer revert / no gas refund
  17. Estimate gas limit ▷ Gas limit is to avoid the

    code runs into an infinite loop. ▷ Give large gas => enough balance ◦ Ether transfer: 21000 ◦ Token transfer depend on contract: 90000~200000 ▷ web3.eth.estimateGas ◦ Dry run transaction in EVM with current state 44
  18. Why need consensus? ▷ Consensus means that the nodes on

    the network agree on the same state of a blockchain ◦ Prevent double spend 97 98 99 100 100 100 46
  19. How to reach a consensus? ▷ Pick block proposer ◦

    Randomize => prevent single entity can control ◦ Block reward 47
  20. PoW design principle ▷ Everyone can participate to solve problem

    (Decentralization) ▷ No one can control the right to write ledger (Randomize) ▷ Same probability The first one to get the number less than 3 can write blockchain ledger. 48
  21. ▷ Follow the most difficult chain ▷ Reorg might be

    occurred ◦ Need the confirmation to ensure the transaction is finalized Fork choice rule Difficulty 2,992,833,186,612,935 Total Difficulty 7,381,509,111,578,673,834,542 Difficulty 3,081,843,657,645,896 Total Difficulty 7,394,502,708,643,356,684,070 51
  22. Fast block time problem ▷ High number of stale blocks

    ▷ Frequently forks ▷ Centralization Bias ◦ Miner with higher hashpower alway wins the block reward ◦ Miner with lower hashpower continues to work on side chain 52
  23. GHOST ▷ Greedy Heaviest Observed Subtree ◦ Includes uncle blocks

    ◦ Uncles get uncle reward ◦ Miner gets uncle inclusion reward ▷ Limited ◦ Only 2 uncle blocks in a block ◦ Uncles includable only up to 7 generations 1 2 3 4A 4 4B 5 block reward -> 5 7/8 block reward -> 4A 1/32 block reward -> 5 6 block reward -> 6 6/8 block reward -> 4B 1/32 block reward -> 6 7 53 https://github.com/ethereum/wiki/wiki/White-Paper#modified-ghost-implementation