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

Ethereum Transaction

Ethereum Transaction

Tran B. V. Son

February 13, 2019
Tweet

More Decks by Tran B. V. Son

Other Decks in Technology

Transcript

  1. Schedule 1. Ethereum accounts 2. Transaction Structure 3. Sign Transaction

    4. Transaction Execution 5. Broadcast Transaction 6. Demo
  2. Externally Owned Account Contract Account Controlled by private key Controlled

    by contract code Can create transaction Can create transaction in reponses incoming transaction Has a balance Has a balance and its own persistent state. (can call other contracts) Note: Contract (smart contract) is written by programing language like Vyper or Solidity
  3. Transaction Structure Nonce A sequence number, issued by the originating

    EOA, used to prevent message replay Gas price The price of gas (in wei) the originator is willing to pay Gas limit The maximum amount of gas the originator is willing to buy Recipient The destination Ethereum address (EOA or Contract Address) Value The amount of ether to send to the destination Data The variable-length binary data payload v, r, s The three components of an ECDSA digital signature of the originating EOA (we can know who send transaction from v, r, s)
  4. Transaction Structure: Nonce What ? - A count of the

    number of transactions sent by the sender - The nonce isn’t stored as apart of an account’s state on the blockchain (it’s calculated dynamically) Why ? - Prevents double-spending How ? - Transaction must be in Order: transaction with nonce 2 can’t be mined before nonce of 1 - No Skipping
  5. What’s minimum of Gas Price ? Zero. Your wallet can

    generate free transactions (but these transactions may never be confirmed)
  6. What if sender doesn’t have enough gas ? Even transaction

    failed, none of the gas will be refund to sender.
  7. What’s purpose of Gas, actually ? 1. Pay for miners.

    2. Ethereum is a Turning machine - a machine that can simulate any computer algorithm. If there were no fee (gas), a malicious actor could easily try to disrupt the network by executing an infinite loop within a transaction
  8. Transaction Structure: Value If recipient is 1. EOA: Ethereum will

    record a state change, adding the value you sent to the balance of the address. 2. Contract Account: EVM (Ethereum Virtual Machine) will execute the contract and will attempt to call the function named in the data payload of your transaction. Transaction with Value = Payment
  9. Transaction Structure: Data If recipient is 1. EOA: Most wallets

    ignore any data received in a transaction to an EOA they control. 2. Contract Account: The data will be interpreted by the EVM as a contract invocation : a hex-serialized encoding of : - A function selector : First 4 bytes of the Keccak-256 hash of the function’s prototype - The function arguments: Hex of arguments Transaction with data, will almost come to Contract Account
  10. Transaction Structure: Data (Example) function withdraw(uint withdraw_amount) public {} *

    Our data payload: 2e1a7d4d000000000000000000000000000000000000000000000000002386f26fc10000 A Function selector The function arguments > web3.sha3("withdraw(uint256)") > withdraw_amount = web3.toWei(0.01, "ether"); > withdraw_amount_hex = web3.toHex(withdraw_amount); '0x2e1a7d4d13322e7b96f9a57413e1525c250fb7a9021cf 91d1540d5b69f16a49f' '0x2386f26fc10000' 0x2e1a7d4d (first 4 bytes) 0x2386f26fc10000 *Solidity function
  11. Transaction Structure: v, r, s components In transaction, there is

    no “from” data, but public key is derived from v, r, s. v, r, s EOA’s public key ECDSA Signature ECDSA = Elliptic Curve Digital Signature Algorithm
  12. Transaction Structure: v, r, s components - V : the

    chain ID and the recovery identifier to help the ECDSA recover function check the signature. - Sig = F sig ( Fkeccak256 ( m ) , k ) • k is the signing private key. • m is the RLP-encoded transaction. • Fkeccak256 is the Keccak-256 hash function. • Fsig is the signing algorithm. • Sig is the resulting signature. - Sig = (r, s)
  13. Sign Transaction 1. Produce an RLP-encoded serialized message of the

    transaction structure 2. Compute the Keccak-256 hash of serialized message 3. Compute the ECDSA signature: signing the hash with EOA private key 4. Append the ECDSA signature’s computed v, r, s to the transaction Transaction (nonce, gas price, gas limit, data, value, v, r=0, s=0) Serialized message (hex string) Transaction hash Signed raw transaction (1) (2) (3, 4)
  14. Transaction Execution - Requirements 1. The transaction must be formatted

    RLP (Recursive Length Prefix) 2. Valid transaction signature 3. Valid transaction nonce (transaction.nonce == account.nonce) 4. Sender’s balance >= Upfront cost
  15. Transaction Execution - Requirements 5. Transaction’s gas limit >= Intrinsic

    gas • Predefined gas fee: Executing transaction (default) • Storage fee: Gas fee for data sent with the transaction • + 32k gas if transaction is a contract creation transaction
  16. Broadcast Transaction 1. The signed transaction will be broadcast to

    transaction pool 2. Miner node accepts transaction (transaction is included in block ) 3. Miner node find a valid block and broadcasts to the network 4. When block is added in blockchain, your tx will be locked into blockchain
  17. Reference 1. Life cycle of Ethereum transaction 2. Mastering Ethereum

    3. How does Ethereum works, anyway ? 4. What is gas ? 5. What is gas - Ethereum StackExchange 6. Normal transaction - Internal Transaction 7. What does v, r, s mean ? 8. What is Nonce ? 9. Transactions in Ethereum