Lecture 1: Introduction to blockchains
======================================

Goals
-----
In this section you will learn the basics of what is a blockchain.

Overview
--------
    Blockchains are tamper evident and tamper resistant **digital ledgers** implemented in a **distributed
    fashion** (i.e., without a central repository) and usually **without a central authority** (i.e., a bank,
    company, or government).
    
    -- *Blockchain technology overview*, National institute of Standards and Technology (NIST)

You may have already heard the term *blockchain*, often related to the terms
*cryptocurrency*, *NFT* or *Bitcoin*. In fact, blockchains are the infrastructure
on which cryptocurrencies (like Bitcoin) and NFT are built upon.

The blockchain definition from the NIST may be somewhat complicated for now.
In simpler words, a blockchain can be seen as a *community database that let
you save informations without validation from a trusted entity*. For instance,
with cryptocurrencies, blockchain can process, validate and store financial
transactions without the need of a central bank: everything happens on a 
community network.

Overview of a transaction life-cycle
------------------------------------
Let's say Alice want to send 3 coins to Bob. She will emit a *transaction* on
the blockchain network. This works as follows:

1.  Alice will write the transaction containing the informations about the 
    transfer
2.  She will *sign* it with her *private key*. (Cryptographic) signatures can 
    be seen as real-world document signature. When you sign a document, you use
    your own signature to show that you agree with the document. Having a document
    with your signature on it shows that you've agreed and that this document applies
    to you.
3.  Alice can now send the transaction in the blockchain network. She will send it
    to one node that will forward it to the others. Soon, every node will receive the 
    transaction. At this time, the transaction has not been validated yet!
4.  Transactions are validated through batches called blocks. The exact mechanism
    behind block creation depends on the blockchain implementation. It often involve
    some kind of competition between nodes. Once a node have created a valid block
    (a block containing valid transactions), it can broadcast it on the network
5.  All the other nodes will receive the block, validate it (by checking that 
    every transaction is valid) and add it to their blockchain.
6.  Alice's transaction is now in a block that have been accepted into everyone's 
    blockchain. It can now be considered valid!  

.. note::

    The term *transaction* does not only refer to *financial transactions*. 
    It can really be any kind of "facts". If you would create a blockchain
    that would keep track of diplomas in a university, *Alice got her engineering 
    diploma in 2023* could be a transaction on the blockchain. In a supply chain,
    *one ton of frozen potatoes at -20°C got transfered from big corp C to big store S at this 
    date* would be another one. 

Data on a blockchain
--------------------
We've talk about the concept of blocks. Let's dig a bit deeper!

In a blockchain, transactions are not validated one by one, but through batches
called a block. Each block contains a list of validated transactions and 
several metadata (like its creation date). Having those list of transactions
makes the validation process easier. As the agreement process in a blockchain
network can be costly (in term of time and computing resources), validating 
several transactions at once makes thing smoother. 

The particularity of a blockchain is that each block is linked to the previous
one, making a *chain of block* (hence the name *blockchain*!). How does this 
link work in practice? Through the usage of *hash functions*. You may already 
know what a *function* in programming is. A hash function is a function that 
takes data of arbitrary size and maps it to a fixed-size value (the *hash
value*). This process has several properties that is interesting for us: 

-   It is deterministic: it returns the same result for the 
    same input. 
-   Even a tiny little change to the input will change the output
-   The probability of a collision is quite low. A collision happens when two 
    different input return the same output. That would be problematic when we use 
    the hash value as an identifier for the input data.

Hence, a hash value can be used as an identifier for our blocks! As every block 
has the hash value of the previous one, we have a nice property: we can't modify
an existing block without having to modify all the following blocks! How so? Let's 
say we have a blockchain container five blocks. We modify the first one. Then its 
hash value change. To keep a correct blockchain, we need to modify the hash value 
of block 1 stored in block 2. As a result, the hash value of block 2 change. Then we
now need to modify the hash value of block 2 stored in block 3. As a result, the 
hash value of block 3 change! We now need to modify block 4, then block 5... 