93 lines
3.6 KiB
Org Mode
93 lines
3.6 KiB
Org Mode
:PROPERTIES:
|
|
:ID: badea9d7-57cd-4734-98de-343f619ffe70
|
|
:mtime: 20220529123703
|
|
:ctime: 20220529102607
|
|
:END:
|
|
#+title: Blockchain
|
|
|
|
* Introduction
|
|
Blockchain est une *technique de stockage de données* presque impossible à modifier, hacker ou manipuler les données
|
|
stockées :
|
|
* Basée sur la /Distributed Ledger Technology/ (DLT),
|
|
* Principalement utilisée dans les domaines des cryptomonnaies et NFTs.
|
|
|
|
Il s'agit d'un *registre numérique de transactions* généralement partagé entre tous les noeuds du réseau. Celui-ci est
|
|
composé d'une série de blocs incluant plusieurs transactions et à chaque fois qu'une nouvelle transaction est réalisée,
|
|
un enregistrement de celle-ci est ajouté dans le registre de chaque participant.
|
|
|
|
Les données stockées dans la chaine de blocs est sécurisé par des signatures cryptographiques (hash des blocks respectifs).
|
|
|
|
* Les blocs
|
|
Les blocs sont les éléments de base d'une /blockchain/ et sont constitués de :
|
|
1) Transactions,
|
|
2) Le /hash/ du précédent bloc,
|
|
3) Le /hash/ de son propre contenu.
|
|
|
|
Le premier /block/ d'un chaine est appelé /genesis block/.
|
|
|
|
#+BEGIN_SRC python :results output
|
|
from rich import print
|
|
from hashlib import sha256
|
|
from json import dumps
|
|
|
|
def hash_string_256(string):
|
|
"""Create a SHA256 hash for a given input string.
|
|
Arguments:
|
|
:string: The string which should be hashed.
|
|
"""
|
|
return sha256(string).hexdigest()
|
|
|
|
def hash_block(block):
|
|
"""Hashes a block and returns a string representation of it.
|
|
Arguments:
|
|
:block: The block that should be hashed.
|
|
"""
|
|
return hash_string_256(dumps(block, sort_keys=True).encode())
|
|
|
|
genesis_block = {
|
|
"previous_hash": "",
|
|
"transactions": []
|
|
}
|
|
genesis_block_hash = hash_block(genesis_block)
|
|
print(f'{genesis_block_hash = }')
|
|
|
|
block = {
|
|
"previous_hash": genesis_block_hash,
|
|
"index": 1,
|
|
"transactions": [{"sender": "A", "receiver": "B", "amount": 0.5}],
|
|
"proof": 9,
|
|
}
|
|
|
|
block_hash = hash_block(block)
|
|
print(f'{block_hash = }')
|
|
|
|
block['new_hash'] = block_hash
|
|
print(f'{block = }')
|
|
#+END_SRC
|
|
#+RESULTS:
|
|
: genesis_block_hash =
|
|
: '26cfa5c318d1195b72ecc139f11a740c5a71300dc1f3d4656188c684ac8ad01e'
|
|
: block_hash = '25cdb202d88ccc0d1c3eae6d27af4c231e7e9f889885f1feb40c85128aac7c37'
|
|
: block = {'previous_hash':
|
|
: '26cfa5c318d1195b72ecc139f11a740c5a71300dc1f3d4656188c684ac8ad01e', 'index': 1,
|
|
: 'transactions': [{'sender': 'A', 'receiver': 'B', 'amount': 0.5}], 'proof': 9,
|
|
: 'new_hash': '25cdb202d88ccc0d1c3eae6d27af4c231e7e9f889885f1feb40c85128aac7c37'}
|
|
|
|
** /hash/
|
|
Un /hash/ (sha256) est une /str/ de longueur fixe qui apporte une couche de chiffrement autour du /block/, rendant impossible la
|
|
manipulation de la /blockchain/ (modification des transactions ou manipulation de la /blockchain/).
|
|
|
|
** /Proof of work/
|
|
/Proof of work/ est un algorithme propre à une /blockchain/ :
|
|
* C'est le mécanisme qui définit la difficulté à ajouter un /block/ à la /blockchain/,
|
|
* Il s'agit d'un nombre devant satisfaire une condition en utilisant un /hash/, /hash/ différent des /hash/ contenu dans les /block/ existant,
|
|
* Généralement, ce /hash/ est calculé ainsi : sha256((/transactions/ + /hash/ du /block/ précédent) + /proof of work/),
|
|
* /Proof of work/ est ajouté aux /metadata/ du /block/ si le /hash/ calculé satisfait une ou plusieurs condition(s) arbitraire(s) (2
|
|
premiers digits égaux à 0 par exemple), dans le cas échant /proof of work/ est incrémenté et le /hash/ recalculé
|
|
jusqu'à satisfaction des conditions,
|
|
|
|
* Références
|
|
* [[https://medium.com/@seaflux/getting-started-with-blockchain-and-programming-it-in-python-d7663b7cc3ef][Getting started with blockchain and programming it in python - Medium]]
|
|
|
|
|