Ethereum Explained

#1 - Blockchain

Hugo Gonçalves

What is a blockchain?

What is a blockchain?

A storage system, similar to a database, with additional guarantees on security, censorship-resistance, decentralization & trust.

 

 

Security: Data is immutable and cryptographically trustworthy;

Censorship Resistance: Impossible to impose restrictions beyond the protocol rules

Decentralization: No need to rely and trust on a third party;

Trust: The system itself enforces the rules, not its creator/host or any individual entity.

What's so innovative about it?

Bitcoin's scientific breakthrough was not in inventing a new technology, but in combining existing fields of research to build something new:


Distributed Systems: where no central authority exists who can unilaterally manipulate the system

Cryptography: Public/Private key pairs allow establishing identity and mathematical proofs

Economics: Using economical principles to incentivize good behavior while deterring bad behavior (cryptoeconomics)

Ok, but what is it technically?

  • An append-only database

  • Each entry is a block, containing transactions;

  • Each transaction is cryptographically signed;

  • Nodes process transactions in blocks, and publish them to other nodes;

  • Every node validates incoming blocks;

  • If majority of nodes are good actors, the system ensures all the rules, without any central authority

  • Infeasible to hack the system (e.g.: a 51% attack)

Why?

Byzantine General's Problem

It's hard for a decentralized system to reach agreement on a single truth.

A video that explains it much better than I ever could:

https://www.youtube.com/watch?v=dfsRQyYXOsQ

Byzantine Generals Problem

The ability to message and agree on something, without needing to trust the other participants, or the means of communication.

Mining a transaction

When you send a transaction...

  1. Alice sends 1 BTC to Bob, e.g. via node 1
  2. Node 1 broadcasts the new transaction to other nodes
  3. All nodes are working to create block 4 (More on this later)
  4. Node 2 successfully creates block 4, containing Alice's transaction
  5. Node 2 broadcasts new block to the network
  6. Each node receives the new block, validates it, and updates its own copy
  7. All nodes are now up-to-date on block 4, and begin work on block 5

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Node 1

Node 2

Candidate block 3a

tx 7 (alice's)

tx 8

tx 9

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Candidate block 3b

tx 7 (alice's)

tx 10

one proof-of-work later...

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Node 1 wins the race, creates block 3

Node 2 is still unaware of it

Candidate block 4

 

...

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Candidate block 3b

 

tx 7 (alice's)

tx 10

Block 3

tx 7

tx 8

tx 9

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Candidate block 4a

 

...

Candidate block 4b

 

...

Block 3

tx 7

tx 8

tx 9

After a small broadcast delay, both nodes get in sync again

Block 1

tx 1

tx 2

tx 3

Block 2

tx 4

tx 5

tx 6

Block 3

tx 7

tx 8

tx 9

Double Spending

(or why Proof-of-Work is important)

Alice has 1 BTC in her account

 

What if she submits a transaction sending "1 BTC to Bob", but also another sending "1 BTC to Charlie"?

 

A  centralized authority (bank) would catch that, and refuse to process the 2nd transaction.

But without centralization, who is the police?

Double Spending

Miners want transaction fees.

They're economically incentivized to accept as many transactions as possible

 

Proof-of-work ensures they need work, which will be peer-reviewed by everyone else.

 

It ensures everyone polices everyone else.

Double Spending

Explaining Proof-of-Work

Proof of Work

It's a consensus mechanism (one of many)

Blockchain capacity is finite and time sensitive

  • Users want to send fast and cheap transactions
  • Miners want the highest fees, and want other miners to agree on their proposed blocks

 

A supply-demand problem is created

Proof of Work

Proof-of-work creates both security and fairness in the system:

  • All miners are forced to play the same cryptographic game
  • Whoever "wins" (mines a block), gets the rewards for it, and others need to agree (it's in their best interest to do so)

{demo}
Proof-of-Work

Energy waste?

A real problem in Bitcoin and others, but not an inherent problem with blockchain, or proof-of-work.

 

Consensus protocol require a form of value to be staked by miners. That value is turned into protocol security.

 

In PoW, the value is given by forcing miners to spend computational power:

  • incentivizes good behaviour, since rewards provide profit
  • deters bad behaviour, since energy spent is costly to the miner

Energy waste?

sha256 is compute-bound

more computational power ➔more mining power ➔ more profits

 

This causes overall network power (Hashrate) to escalate.

 

But PoW can also be memory-bound, latency-bound, etc, which does not scale as well.

Other consensus methods

  • Proof-of-stake
    • Instead of computing power, you lock some of your funds as insurance
       
  • Delegated proof-of-stake
     
  • Proof-of-useful-work (mostly theoretical right now)
    • Replaces sha256 with useful computation

Finality

What if two nodes broadcast a block at the same time?

For every new block, a lot of work is put into the chain.

The chain with the most work wins

Tutorial:
Attacking Ethereum

To revert Block #3, we need to:

- Create a new Block #3b

- Send it to everyone, before someone else creates Block #4

 

Not easy, but statistically doable

(happens all the time, by accident)

To revert Block #2, we need to:

- Create a new Block #2b

- Create a new Block #3b as well

- Send them to everyone, before someone else creates Block #4 is created

 

Twice as hard, but might still be doable

To revert Block #1, we need to:

- Create a new Block #1b

- Create a new Block #2b

- Create a new Block #3b

- Send them to everyone, before someone else creates Block #4
 

Probably 99% unlikely, or something

Not real math

You need more power than the rest of network.

As in: You need at least 51%

Each new block helps confirm all previous blocks

By adding work on top of it

Finality:
~6 confirmations in BTC
~20-30 confirmations in ETH

Ethereum Explained

#2 - Ethereum

Hugo Gonçalves

Ethereum

What is Ethereum?

Ethereum is a decentralized, open-source blockchain with smart contract functionality.

 

Ethereum is also the most actively used blockchain.

 

The native cryptocurrency of the Ethereum network is Ether (ETH or Ξ). ETH is the second-largest cryptocurrency by market capitalization.
 

Ethereum is developed based on EIPs - Ethereum Improvement Proposals.

 

Ethereum is currently undergoing a fundamental re-design of the ecosystem, including a change to a Proof of Stake algorithm, commonly known as Ethereum 2.0.

Why was Ethereum a game-changer?

Bitcoin's primary use cases are store of value and serving as decentralized digital currency.

 

While Ether can also be used for the same purposes, the Ethereum network is serves as a decentralized computing platform: Anyone can  create smart contracts and distribute them to the network.

 

While Bitcoin is a distributed ledger, Ethereum is a distributed state-machine, where anyone can create programs that change the state.

 

Applications that use the blockchain, using smart contracts to connect to it, are called distributed apps or DApps.

Transaction Models

Transaction Models

For blockchains, a transaction model is how a transaction (tx) is represented in the system.

 

Across the different blockchains you'll commonly find two:

 

          1. Account Based - used by Ethereum

          2. Unspent Transaction Outputs - used by Bitcoin

Ethereum's Account Tx Model

There are two types of accounts: user and contract accounts.

Each account has a balance and can send transactions using that balance.

 

Accounts are identified by a single address, a 40 hexadecimal digit string.

 i.e. 0x588b38CAF871AbE020a49E6617A10ed2B71E61cB

 

A transaction is comprised of the sender's address, the receiver's address and the amount being transferred. Fee information and others are also included.

 

Sending txs decreases the account's balance. Inversely, Receiving txs increases the account's balance.

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

We are ignoring fees and other ETH functionalities. Keeping it simple.

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

From: 0x123...

To: 0xabc...

Amount: 1 ETH

We are ignoring fees and other ETH functionalities. Keeping it simple.

Send Transaction

Alice's Account

Balance: 5 ETH

Bob's Account

Balance: 0 ETH

Address: 0x123...

Address: 0xabc...

From: 0x123...

To: 0xabc...

Amount: 1 ETH

Alice's Account

Balance: 4 ETH

Bob's Account

Balance: 1 ETH

Address: 0x123...

Address: 0xabc...

We are ignoring fees and other ETH functionalities. Keeping it simple.

-1 ETH

+1 ETH

Send Transaction

Bitcoin's Unspent Transaction Ouputs Tx Model

A Bitcoin transaction is comprised of inputs and outputs.


Outputs are the result of transactions.


Each output has a destination address and an amount. They also store the transaction hash on which they were created.


Until they are used as inputs on a new transaction, they are considered unspent outputs.


The balance of a given Bitcoin address is the sum of all unspent outputs the address has.

Transaction Fees

How are miners rewarded for running nodes?

For each transaction, a certain amount of fees needs to be paid, for it to be included in a block.

 

When miners mine a block, they get newly-minted coins as a reward, plus fees from each of the transactions included in said block.

 

Transactions with higher fees will be included faster in blocks, as miners will obviously prefer transactions with higher fees.

 

The fee system can be different between blockchains.

 

Keep in mind that third-party services may require you to pay other fees to send transactions, besides the transactions fees.

Fees in Ethereum - A methaphor

Imagine you are driving from Braga to Lisbon. Roughly 364 km.

 

You know you need at most 25L of gas to do the trip ➔ the is the gas limit.

 

However, to fill up your tank with 25L you pay 1.45€/L ➔ this is the gas price.

 

You might end up spending the whole 25L or not.

Ethereum's Gas System

In Ethereum, fees are called gas.

 

Gas is the unit that measures the use of computational power in the Ethereum network.

 

For now, miners get all of the gas paid by the transactions included in the blocks they mine.

 

When you send an Ethereum transaction, you are actually paying miners to execute some code on the Ethereum network.

Ethereum's Gas System

There are two factors set by the sender on each transaction that determine the gas paid for a transaction: gasPrice & gasLimit

 

gasLimit defines the maximum amount of gas the transaction will use.

More complex operations ➔ More gas needs to be paid.

For example, sending Ether between user addresses costs a standard of 21000 gas.

 

gasPrice is how much Ether you want to pay per each unit of gas. It's the equivalent of the miner fee in Bitcoin.

 

tx cost = gasSpent × gasPrice

Smart Contracts

Smart Contracts Explained

Smart contracts consist of a set of operations that are executed once the predefined conditions are met.

 

In the blockchain, smart contracts are programs stored on the blockchain itself. This means the (byte)code is public.

 

These programs can be interacted with by sending messages to the smart contract (in the form of transactions) through the network.

 

Smart contracts can also interact with other smart contracts on the network.

Ethereum Smart Contracts

Ethereum smart contracts are written in Solidity, a scripting programming language created for this specific purpose.

 

Smart contracts can be deployed by sending an Ethereum transaction without recipient and the bytecode of the contract attached.

 

Once deployed, the smart contract will have a dedicated address on the blockchain.

 

To run a function on a smart contract, you need to send a message to the contract's address, specifying the function you want to run and the relevant inputs.

Ethereum & Smart Contracts

By Hugo Gonçalves

Ethereum & Smart Contracts

  • 119