The github repository contains a basic implementation of a blockchain.
By creating Blockchain class that has the main params/data_variables and the main functionality related to the blockchain it's self.
- chain list:: which is the list will be updated every time you mine a new block.
- block:: is the current block which we are trying to mine using the blockchain class functions. Every block has it's own set of key value pair which are 'index', 'timestamp', 'proof', 'previous_hash'.
- new_proof:: is the value we are trying to find to be able to mine a new block.
- fun
__init__
is the constructor of the Blockchain class, it initialize the chain list and create the very first block with proof of 1 and previous_hash equals to 0. - fun
create_block
it creates a new block as a new instance of the Blockchain class. - fun
get_previous_block
is for getting the previous block from the chain list and using it's previous_hash for th mining process. - fun
proof_of_work
to proof that the current proof is thr right value using hashlib and sha256 operation for hashing and make sure that the end value starts with '0000'. - fun
hash
using hashlib and sha256 operation for hashing values. - fun
is_chain_valid
to be able to check if the chain values and no fake values.
using Flask.
creating a new instance from the Blockchain class.
using that new blockchain use this web based function mine_block to mine a new block.
using that new blockchain use this web based function get_chain to get the full chain.
using that new blockchain use this web based function is_valid to check is_chain_valid.
$ FLASK_APP=blockchain.py flask run
* Running on http://localhost:5000/
Or run our app using the ide to be deployed and start testing it.
GET http://127.0.0.1:5000/get_chain
which will initiate the chain and create the first block.GET http://127.0.0.1:5000/mine_block
which will mine a new block.GET http://127.0.0.1:5000/get_chain
which will get the new chain after mining that new block form the previous step.- Repeat mining step 2 for 5 times for example.
GET http://127.0.0.1:5000/get_chain
again.GET http://127.0.0.1:5000/is_valid
to check if your chain is valid.
# Install httpie
$ pip install httpie
$ http http://127.0.0.1:5000/get_chain
$ http http://127.0.0.1:5000/mine_block
$ http http://127.0.0.1:5000/get_chain
# Repeat http http://127.0.0.1:5000/mine_block 5 times.
$ http http://127.0.0.1:5000/get_chain
$ http http://127.0.0.1:5000/is_valid
$ curl http://127.0.0.1:5000/get_chain
$ curl http://127.0.0.1:5000/mine_block
$ curl http://127.0.0.1:5000/get_chain
# Repeat curl http://127.0.0.1:5000/mine_block 5 times.
$ curl http://127.0.0.1:5000/get_chain
$ curl http://127.0.0.1:5000/is_valid
Flask is the only required dependency.
$ pip install -r requirements.txt