Ethereum Notes

December 30, 2021   

Overview

There is a single canonical computer (called the ethereum VM or EVM)

  • Every Ethereum Node keeps a copy of the state of the EVM
  • Any participant can broadcast a request to this VM
  • Other participants on the network verify, validate and execute the request
  • Requests for computation are called transaction requests
  • The record of all transactions for the EVM state is stored on the block chain

ETH or Ether is the native crypocurrency of Ethereum Ether exists to allow for a market for computation. Which provides economic incentive for participants to verify and execute transaction requests.

Application developers upload programs into EVM storage and users make requests to execute these code snippets with parameters. These programs/snippets are called smart contracts

Terms

  • Blockchain : sequence of all blocks that have been commited to the ethereum network
  • ETH : native crypto of ethereum, users pay ether to other users to have their code execution requests fullfuilled
  • EVM : Ethereum Virtual Machine
  • Nodes : machines which are storing the EVM state. Any user can also request the execution of code by broadcating a code execution request from a node.
  • Accounts : Where eather is stored. Users initialize accounts, deposit ether into the accounts and transfer ether from their accounts to other users. Accounts and their balances are stored in a big table in the EVM, they are part of the overall EVM state
  • Transactions : The formal term for a request for code execution on the EVM, a transaction is a fulfilled transaction request and the associated change in the EVM state. Any user can broadcast a transaction to the network from a node For the transaction request to affect the agreed-upon EVM state it must be validated, executed and “committed to the network” by another node. Example tx’s
    • send X ether from my account to alice’s account
    • Publish some smart contract code inot EVM memory
    • Execute the code of the smart contract at address X in the EVM with args Y
  • Blocks : The volume of transactions is very high so transactions are commited in batches or blocks which generally contain dozens or hundreds of transactions.
  • Smart Contract : A reusable snippet of code (a program) which a developer publishes into EVM memory. Anyone can exectute the smart contrat by making a transaction request. Because developers can write arbitry executable applications into EVM by publishing smart contracts these are often called “dapps” or “Decentralized Apps”

Intro to Ether

Eth is how you pay to have transactions executed on the blockchain. These costs are called Gas Fees

Ether is minted when a miner creates a block on the ethereum blockchain. As an incentive to miners the protocol grants a reward in each block incrementing the balance of an address set by the blocks miner (currently 2 ETH per block)

Ether is burned in every transaction on Ethereum. Users base gas fee is destroyed. Some blocks can burn more ether then they mint!

  • Wei = 10^-18 the smallest amount of ether
  • Gwei - 10^-9 Human readable gas fees

Each transaction on Ethereum contians a value field which specifies the amount of ether to be transferred denominated in wei from the sender to the recipient

When the recipent is a smart contract this transferred ether may be used to pay for gas fees when the smart contract is executed.

users can query the ether balance of any account by inspecting the accounts balance field

Intro to Dapps

A dapp is an application built on a decentralized network that combines a smart contract with a frontend user interface.

A dapp can have its frontend code and UI written in any language. You can host your frontend on IPFS

Benefits of Dapps:

  • Always “online” - unless the blockchain has crashed…
  • Privacy: no real-world identity is required to ineract with a dapp
  • Resistance to censorship: data stored on the blockchain is immutable and indisputable thanks to cryptographic primitives.
  • Complete data integrity - data stored on the blockchain is immutable and indisputable thanks to cryptographic primitives.
  • Trustless computation/verifiable behavior - smart contracts can be analyszed and are guaranteed to execuite in predictable

Drawbacks of Dapps:

  • Maintenance - Hard to modify the once deployed - its immutable!
  • Performance - It takes 1,000,000x the work to do simple tasks
  • Network Congestion - at best 10-15 tx’s second
  • UX - Hard to interact with the blockchain - wallets etc
  • User Friendly and developer friendly solutions may end up centralized

Ethereum Accounts

Accounts come in two types exteranlly owned by private keys and contracts

Both types accounts can Receive, Hold and send ETH and interact with smart contracts

Key Differences

Externally Owned

  • Creating an account costs nothing
  • Can initiate transactions
  • Transactions between externally owned accounts can only be ETH/Token transferes

Contract

  • Createing a contract will cost Gas as it must be placed on network storage
  • Can only send transactions in response to receiving a transaction
  • transactions from an external account to a contract account can trigger code which can execute may different actions such as transferring tokens ro even creating a new contract

Ethereum accounts have 4 fields

  • nonce - A counter that indicates the number of transactions sent from the account
  • balance - The number of wei owned by this address
  • codeHash - This hash refers to the code of an account on the EVM
  • storageRoot - aka storage hash Pointer to the

Account Creation:

When you create an account you will get a public/private keypair.

The public address of your account is generated by taking the last 20 bytes of the Keccak-256 hash of the public key and adding 0x to the beginning.

It is possible to derive new public keys from your private key but you cannot derive a private key from public keys. This means it’s vital to keep a private key safe and, as the name suggests, PRIVATE.

Account Contracts

Contract accounts have a 42 character hexadecimal address which is usually given when a contract is deployed to the blockchain. The address comes from the creators address and the number of transactions sent from that address (the nonce)

Account == Wallet !!!

A account is the keypair for a user owned ethereum account.
A wallet is an interface or application that lets you interact with your ethereum account!

** https://sandbox.eth.build/ = a good tool for playing with hashing and signatures

Transactions

Transactions are initiated by externally owned accounts and are managed by a human not a contract.

Transaction requests are broadcast out by a node and executed by a miner and propagate the resulting state change to the rest of the network

A transaction request contains:

  • recipient - the recieving address either to transfer value OR to execute the contract code
  • signature - the identifier of the sender
  • value - the amount of WEI from sender to recipient
  • data - optional arbitrary data
  • gasLimit - the maximum amount of gas units that can be consumed by the transaction (units of gas represent computational steps)
  • maxPriorityFeePerGas - the max gas to be included as a tip
  • maxFeePerGas - the maximum amount of gas willing to be paid for the tx includes baseFeePerGas and maxPriorityFeePerGas

When creating a transaction the sending client will include a digitial signature for the request so it can be validated by the reciever

Transaction lifecycle

  • Your transaction and its hash are broadcast to the network
  • A miner picks up your transaction and attempts to include it in a block
  • Your transaction will receive “confirmations” the number of confirmations is the number of blocks created since the block that included your transaction the greater the number the more certian that network processed the transaction

Its possible for recent blocks to get re-organized giving the impressing the transaction failed. However the transaction may still be valid but included in a different block. The probably of “re-organization dcreased with the number of confirmations

Blocks

A block is a group of transactions which are committed to the block chain all at the same time.

Blocks and the transactions in blocks are strictly ordered.

Block Contents

  • timestamp - when it was mined
  • blockNumber - the length of the blockchain in blocks
  • baseFeePerGas - the minimum fee per gas required for a transaction to be included in the block
  • difficulty - the effort required to mine the block
  • mixHash - a unique identifier for that block
  • parentHash - the unique identifier for the block that came before
  • transactions - the transactions included in the block
  • stateRoot - the entire state of the system: account balances, contract storage, contract code and account nonces are inside
  • nonce - a hash that when combinded with the mixHash proves the block has gone through proof-of-work

Block Time - the time it takes to mine a new block.

Block Size - blocks are bounded in size with a target size of 15million gas and a max size fo 30 million gas

Ethereum Virtaul Machine - EVM

Since ethereum supports smart contracts its more then a distributed ledger, its a distributed state machine.

State - the state of the EVM is an modified Merkle Patrica Trie which keeps all accounts linked by hashes and reducible to a single root hash stored ont he blockchain.

Transactions - Instructions from accounts

The EVM is a stack machine with a stack depth of 1024 each with a 256 bit word.

There are numerous EVM implementations and every client implements it.