Mod 2_2
Mod 2_2
History of Ethereum
• Vitalik Buterin
• Russian-Canadian programmer
• Co-founded Ethereum when he
was 19 years old
2
History of Ethereum -
Timeline
3
Introduction
• The intent of Ethereum is to create an alternative protocol for building
decentralized applications, providing a different set of tradeoffs that we believe
will be very useful for a large class of decentralized applications, with particular
emphasis on situations where rapid development time, security for small and
rarely used applications, and the ability of different applications to very
efficiently interact, are important.
• Ethereum does this by building what is essentially the ultimate abstract
foundational layer: a blockchain with a built-in Turing-complete programming
language, allowing anyone to write smart contracts and decentralized
applications where they can create their own arbitrary rules for ownership,
transaction formats and state transition functions.
Introduction
• The Ethereum blockchain is essentially a transaction-based state machine. In
computer science, a state machine refers to something that will read a series of
inputs and, based on those inputs, will transition to a new state.
Introduction
• With Ethereum’s state machine, we begin with a “genesis state.” This is
analogous to a blank slate, before any transactions have happened on the
network. When transactions are executed, this genesis state transitions into
some final state. At any point in time, this final state represents the current
state of Ethereum.
• Remember that the gas limit represents the maximum gas the sender is willing to spend
money on. If they have enough Ether in their account balance to cover this maximum, they’re
good to go. The sender is refunded for any unused gas at the end of the transaction,
exchanged at the original rate.
Gas and payment
• In the case that the sender does not provide the necessary gas to execute the transaction,
the transaction runs “out of gas” and is considered invalid. In this case, the transaction
processing aborts and any state changes that occurred are reversed, such that we end up
back at the state of Ethereum prior to the transaction. Additionally, a record of the
transaction failing gets recorded, showing what transaction was attempted and where it
failed. And since the machine already expended effort to run the calculations before running
out of gas, logically, none of the gas is refunded to the sender.
Gas and payment
• Where exactly does this gas money go? All the money spent on gas by the sender is sent to
the “beneficiary” address, which is typically the miner’s address. Since miners are
expending the effort to run computations and validate transactions, miners receive the gas
fee as a reward.
Typically, the higher the gas price the sender is willing to pay, the greater the value the miner
derives from the transaction. Thus, the more likely miners will be to select it. In this way, miners are
free to choose which transactions they want to validate or ignore. In order to guide senders
on what gas price to set, miners have the option of advertising the minimum gas price for
Transaction and messages
• We noted earlier that Ethereum is a transaction-based state machine. In other words,
transactions occurring between different accounts are what move the global state of
Ethereum from one state to the next.
• In the most basic sense, a transaction is a cryptographically signed piece of instruction that
is generated by an externally owned account, serialized, and then submitted to the
blockchain.
• There are two types of transactions: message calls and contract creations (i.e. transactions
that create new Ethereum contracts).
Transaction and messages
All transactions contain the following components, regardless of their type:
• nonce: a count of the number of transactions sent by the sender.
• gasPrice: the number of Wei that the sender is willing to pay per unit of gas required to execute the
transaction.
• gasLimit: the maximum amount of gas that the sender is willing to pay for executing this transaction.
This amount is set and paid upfront, before any computation is done.
• to: the address of the recipient. In a contract-creating transaction, the contract account address does
not yet exist, and so an empty value is used.
• value: the amount of Wei to be transferred from the sender to the recipient. In a contract-creating
transaction, this value serves as the starting balance within the newly created contract account.
• v, r, s: used to generate the signature that identifies the sender of the transaction.
• init (only exists for contract-creating transactions): An EVM code fragment that is used to initialize
the new contract account. init is run only once, and then is discarded. When init is first run, it returns
the body of the account code, which is the piece of code that is permanently associated with the
contract account.
• data (optional field that only exists for message calls): the input data (i.e. parameters) of the message
call. For example, if a smart contract serves as a domain registration service, a call to that contract
Transaction and messages
Block header
A block header is a portion of the block consisting of:
• parentHash: a hash of the parent block’s header (this is what makes the block set a “chain”)
• ommersHash: a hash of the current block’s list of ommers
• beneficiary: the account address that receives the fees for mining this block
• stateRoot: the hash of the root node of the state trie (recall how we learned that the state trie is stored in the
header and makes it easy for light clients to verify anything about the state)
• transactionsRoot: the hash of the root node of the trie that contains all transactions listed in this block
• receiptsRoot: the hash of the root node of the trie that contains the receipts of all transactions listed in this block
• logsBloom: a Bloom filter (data structure) that consists of log information
• difficulty: the difficulty level of this block
• number: the count of current block (the genesis block has a block number of zero; the block number increases by 1
for each each subsequent block)
• gasLimit: the current gas limit per block
• gasUsed: the sum of the total gas used by transactions in this block
• timestamp: the unix timestamp of this block’s inception
• extraData: extra data related to this block
• mixHash: a hash that, when combined with the nonce, proves that this block has carried out enough computation
Block header
Transaction Execution
We’ve come to one of the most complex parts of the Ethereum protocol: the execution of a transaction. Say you send
a transaction off into the Ethereum network to be processed. What happens to transition the state of Ethereum to
include your transaction?
• First, all transactions must meet an initial set of requirements in order to be executed. These
include:
• The transaction must be a properly formatted RLP. “RLP” stands for “Recursive Length Prefix”
and is a data format used to encode nested arrays of binary data. RLP is the format Ethereum
uses to serialize objects.
• Valid transaction signature.
• Valid transaction nonce. Recall that the nonce of an account is the count of transactions sent
from that account. To be valid, a transaction nonce must be equal to the sender account’s nonce.
• The transaction’s gas limit must be equal to or greater than the intrinsic gas used by the
transaction. The intrinsic gas includes:
• 1) a predefined cost of 21,000 gas for executing the transaction
• 2) a gas fee for data sent with the transaction (4 gas for every byte of data or code that equals zero, and 68 gas
for every non-zero byte of data or code)
• 3) if the transaction is a contract-creating transaction, an additional 32,000 gas
• 4) gas cost of each operation performed by transaction
Transaction Execution
•The sender’s account balance must have enough Ether to cover the “upfront” gas costs that the
sender must pay. The calculation for the upfront gas cost is simple: First, the transaction’s gas limit is
multiplied by the transaction’s gas price to determine the maximum gas cost. Then, this maximum
cost is added to the total value being transferred from the sender to the recipient.
If the transaction meets all of the above requirements for validity, then we move onto the next step.
First, we deduct the upfront cost of execution from the sender’s balance, and increase the nonce of
the sender’s account by 1 to account for the current transaction. At this point, we can calculate the
gas remaining as the total gas limit for the transaction minus the intrinsic gas used.
Transaction Execution
• Next, the transaction starts executing. Throughout the execution of a transaction, Ethereum keeps track
of the “substate.” This substate is a way to record information accrued during the transaction that will
be needed immediately after the transaction completes. Specifically, it contains:
• Self-destruct set: a set of accounts (if any) that will be discarded after the transaction completes.
• Log series: archived and indexable checkpoints of the virtual machine’s code execution.
• Refund balance: the amount to be refunded to the sender account after the transaction. Remember how we
mentioned that storage in Ethereum costs money, and that a sender is refunded for clearing up storage? Ethereum
keeps track of this using a refund counter. The refund counter starts at zero and increments every time the contract
deletes something in storage.
• Next, the various computations required by the transaction are processed.
• Once all the steps required by the transaction have been processed, and assuming there is no invalid
state, the state is finalized by determining the amount of unused gas to be refunded to the sender. In
addition to the unused gas, the sender is also refunded some allowance from the “refund balance” that
we described above.
• Once the sender is refunded:
• the Ether for the gas is given to the miner
• the gas used by the transaction is added to the block gas counter (which keeps track of the total gas used by all
transactions in the block, and is useful when validating a block)
• all accounts in the self-destruct set (if any) are deleted
Ethereum State Transition
Function
Ethereum State Transition
Function
The Ethereum state transition function, APPLY(S,TX) -> S' can be defined as
follows:
• Check if the transaction is well-formed (ie. has the right number of values), the signature
is valid, and the nonce matches the nonce in the sender's account. If not, return an error.
• Calculate the transaction fee as STARTGAS * 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.
• Initialize GAS = STARTGAS, and take off a certain quantity of gas per byte to pay for the
bytes in the transaction.
• 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.
• 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.
• Otherwise, refund the fees for all remaining gas to the sender, and send the fees paid for
gas consumed to the miner.
Execution model
Before executing a particular computation, the processor (EVM) makes sure that
the following information is available and valid:
• System state
• Remaining gas for computation
• Address of the account that owns the code that is executing
• Address of the sender of the transaction that originated this execution
• Address of the account that caused the code to execute (could be different from the
original sender)
• Gas price of the transaction that originated this execution
• Input data for this execution
• Value (in Wei) passed to this account as part of the current execution
• Machine code to be executed
• Block header of the current block
• Depth of the present message call or contract creation stack