1 of 40

Blockchain Technology

FROM BITCOIN TO ETHEREUM AND HYPERLEDGER

2 of 40

What’s Bitcoin?

  • Bitcoin is a decentralized electronic payment system designed anonymously by a person known as Satoshi Nakamoto in 2009.
  • Decentralized: there is no central point of control. Anyone can run the program or use the system.

3 of 40

How does it work?

  • Bitcoin is just a computer program that connects to other peers running that program.
  • This program shares a file, commonly called blockchain: basically, it’s a list of transactions.

4 of 40

How does it work? (continued)

  • When a new transaction enters the network, it gets relayed from computer to computer until everyone has a copy of the transaction.
  • At 10 min. intervals a random node updates the blockchain with the latest transactions and shares the changes.

5 of 40

Nothing new, maybe?

  • It seems that all that Bitcoin does is sharing a file across a network of computers. P2P technologies do that and existed way before Bitcoin. So, what’s the trick, uh?
  • Bitcoin adds a new way for managing the blockchain file updates: all the received transactions by a node are NOT immediately written to the file; instead, they are stored in a memory pool. Then, a random node will add the latest transactions from their memory pool on to the blockchain file.
  • This happens every 10 min. for Bitcoin.

6 of 40

Double-spend? No bueno!

  • Let’s say that I create two transactions for the same digital coin: this action is known as double-spend.
  • In P2P networks, all the file updates would be shared as soon as possible to the other peers. We cannot allow it, because peers receive transactions with different orders and conflicting transactions would still be written to the blockchain file!
  • Bitcoin addresses this problem using the trick we talked about in the previous slide.

7 of 40

Read, Wait, Write

  • We read the incoming transactions and store them on to the memory-pool, we wait some time to stabilize the network and then, all the valid transactions are written.
  • The process of writing transactions on to the file is called mining.

8 of 40

Mining: how does it work?

  • Transactions stored in the memory-pool are wrapped in a container called block.
  • Processing power is used to write the block on to the blockchain.
  • Where does this “Processing power” come in?

9 of 40

Proof-of-work: block hash

  • To add a block to the blockchain we first must feed it to something called hash function. In Computer Science, this special function is in the category of trapdoor functions: they are functions that are easy to compute in one direction but very difficult to compute in the opposite direction (finding the inverse).
  • The result of this has function must be below a certain target. So, in order to add a block, we must try many times by adding a different nonce to the block data every iteration and check if the hash result is below the target.

10 of 40

Proof-of-work: difficulty

  • Basically, the lowest the target, the harder is to validate a certain block: the target is strictly related to difficulty.
  • Why do we need a target? It’s an important factor for stabilizing the blockchain network. Bitcoin imposes a 10 min. validation time.

11 of 40

The Reward

  • Every time a miner successfully validates a block, a special type of transaction is added to the block: this represents the miner’s reward. Miners don’t work for free!

12 of 40

How transactions work

  • To create a transaction, I need to unlock what’s called an output associated with my public key, then I need to put my digital signature (calculated from tx data + private key) inside the transaction so that the other nodes can verify that I’m the owner of that public key.
  • Finally, I re-lock to create a new output using the recipient’s public key.

13 of 40

Majority attack

  • Theoretically, blockchain is not 100% foolproof. An attack known as majority attack (or 51% attack) could potentially manipulate the blockchain, making double-spends possible.
  • To execute this attack, an attacker must own more than 50% of the computational power (i.e., hash rate) of the entire network. Then, he would create a private fork with his own double-spend transactions (for example).
  • The original network would merge the private fork because it’s the longest chain (given the > 51% condition).

14 of 40

Blockchain pros and cons

  • Pros:
    • Open: it is accessible to anyone;
    • Verifiable: anyone can verify the correctness of the information;
    • Permanent: blockchain records are permanent;
    • Tighter security: thanks to SHA-256 hashing;
    • Imutability: due to its decentralized structure;
    • Transparency: transactions are visible to anyone;
  • Cons:
    • Scalability: due to the fixed size of blocks for storing transactions;
    • Energy-consuming: due to the block verification phase;
    • Time-consuming: also, due to the block verification phase;
    • Legal constraints: crypto ban, regulations, etc.

15 of 40

Ethereum and Smart Contracts

16 of 40

What’s Ethereum?

  • Ethereum is a decentralized, open-source blockchain with smart contract functionality.
  • Although scripting was possible in Bitcoin, its language was not Turing-complete: not much could be done in Bitcoin.
  • Other reasons: Value-blindness - there is no way for a UTXO (Unspent Transaction Output) script to provide fine-grained control over the amount that can be withdrawn; Lack of state - UTXO can either be spent or unspent; there is no opportunity for multi-stage contracts or scripts which keep any other internal state beyond that; Blockchain-blindness - UTXO are blind to blockchain data such as the nonce, the timestamp and previous block hash

17 of 40

Ethereum smart contracts

  • Ethereum smart contracts solve the limitations of the Bitcoin Blockchain technology (specifically, its scripting functionality).
  • They are computer programs that live on the blockchain. They can execute automatically. You can track their transactions and predict how they act.
  • They are a collection of code (its functions) and data (its state) that resides on a specific Ethereum address. In fact, they are a type of Ethereum account, meaning they have a balance and can send transactions over the network.
  • Smart contracts cannot be deleted by default and interactions with them are irreversible.

18 of 40

GAS

  •  

19 of 40

Solidity programming language

20 of 40

What’s Solidity?

  • Solidity is the main programming language used for the creation of Ethereum smart contracts.
  • It’s statically-typed and object-oriented.
  • It uses ECMAScript-like syntax, so Web Developers should be very comfortable with it; however, unlike ECMAScript it has static typing and variadic return types.
  • It also supports multiple inheritance with C3 linearization (like Python).

Variadic return

Static typing

21 of 40

Ethereum accounts

  • An Ethereum account is an entity with an ether (ETH) balance that can send transactions on Ethereum. Accounts can be user-controlled or deployed as smart contracts.
  • There are 2 types of Ethereum accounts:
    • Externally-owned: controlled by anyone with private keys;
    • Contract: controlled by code
  • Differences:
    • Externally owned:
      • Creating an account costs nothing
      • Can initiate transactions
      • Transactions between externally-owned accounts can only be ETH/token transfers
    • Contract:
      • Creating a contract has a cost because you're using 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 many different actions, such as transferring tokens or even creating a new contract

22 of 40

DEMO

23 of 40

Let’s create an Ethereum wallet

  • First of all, let’s add the MetaMask extension to Chrome and create an account. It serves as an Ethereum wallet.
  • Click on “Show/hide test networks”.
  • Then, switch “on” the test networks!

24 of 40

Create an Alchemy app

  • Alchemy is a web platform to simplify the development of Web3 Decentralized Apps: it provides facilities for monitoring and contract interaction.
  • Next, we want to create an Alchemy Ethereum app on the Goerli network. Let’s do that!
  • Now, let’s add the Alchemy Goerli RPC provider To Metamask by copying the HTTPS URL!

25 of 40

…Connect Metamask to the Alchemy endpoint

26 of 40

Get some ETH on our wallet

27 of 40

Create an ERC-1155 contract

  • Head over to https://docs.openzeppelin.com/contracts/4.x/wizard, and configure a Mintable ERC-1155 smart contract. Basically, it is a Multi-Token Standard that works both for Fungible and Non-Fungible tokens.
  • This way, we can save GAS fees!

28 of 40

Use Remix to create the contract

29 of 40

Deploy the contract

  • At the “Deploy & Run Transactions” tab, be sure to select the Injected Web3 environment and to select the erc-1155.sol file on the Contract dropdown menu.

30 of 40

Mint the ERC-1155 token

31 of 40

Check transactions on Etherscan

32 of 40

Adding NFT metadata

  • Go to https://car.ipfs.io/ and upload all the NFT metadata .json files (they must be named as <TOKEN_ID>.json)

33 of 40

… Adding NFT metadata

34 of 40

… Adding NFT metadata

  • Then, execute that command to upload the .car file to Filebase.

35 of 40

… Adding NFT metadata

  • The new file should be visible on the Filebase Web UI.

36 of 40

… Adding NFT metadata

  • Let’s add the IPFS base URL to our new ERC-1155 contract.
  • Clients must replace the “{id}” substring with the actual token ID.

37 of 40

… Adding NFT metadata

38 of 40

… Adding NFT metadata

39 of 40

EVM

  • All Ethereum smart contracts are run by something called EVM (Ethereum Virtual Machine). Given an input state and a set of transactions, it produces an output state.
  • All smart contracts, once compiled and deployed, are just a sequence of opcodes which perform standard stack operations: xor, and, add, sub, etc.
  • EVM is implemented in various programming languages; the most popular implementation is the Go-Ethereum, implemented in Go (because it’s a very good language for asynchronous and distributed networking).

40 of 40

Example: decompiled sol contract