Implementation of a blockchain in Python
Core features including block creation, transaction processing, and a consensus algorithm.
Blockchain in Python — implementation of a blockchain which includes core features such as block creation, transaction processing, and a consensus algorithm. It consists of two modules: one implements the blockchain using Flask to create an API, while the other represents a simpler implementation of the blockchain using classes.
⚠️ Warning! This is a fairly old version that I moved to my historical repository and may continue to develop. This version is still in development and research phase.
This module creates a blockchain using Flask, allowing you to interact with it via HTTP requests.
/mine runs the proof-of-work algorithm and creates a new block/transactions/new adds new transactions to the blockchain/chain returns the current chain of blocks and its length/nodes/register adds new nodes to the network/nodes/resolve checks and resolves conflicts in the chainThis module implements the basic structure of a blockchain with the Block and Blockchain classes.
create_genesis_block creates the first block in the chainadd_block adds a new block to the chain, setting the correct hash of the previous blockInstallation:
pip install -r requirements.txt
Run Flask API:
python blockchain.py
Run Simple Blockchain:
python blockchain_base.py
Mine a new block:
curl http://localhost:5000/mine
Response:
{
"index": 2,
"message": "New Block Forged",
"previous_hash": "5d9d222c768d6c501ae28670a7333e0ae39c14d7884650bf12cf32d3961e2682",
"proof": 35293,
"transactions": [
{
"amount": 1,
"recipient": "902a4bd96aa44abcb0d9a097bd78c462",
"sender": "0"
}
]
}
Get full chain:
curl http://localhost:5000/chain
Create a new transaction:
curl -X POST -H "Content-Type: application/json" -d '{
"sender": "d4ee26eee15148ee92c6cd394edd974e",
"recipient": "someone-other-address",
"amount": 5
}' "http://localhost:5000/transactions/new"
Response:
{
"message": "Transaction will be added to Block 4"
}
Index: 0 Timestamp: 2024-10-30 00:01:20.180414 Data: Genesis Block Previous Hash: 0 Hash: 02a478085aca11b94a6ea0b5c2cfe9df56dffbe5153df269f3609faa19fc5641 Index: 1 Timestamp: 2024-10-30 00:01:20.180433 Data: Transaction Data Previous Hash: 02a478085aca11b94a6ea0b5c2cfe9df56dffbe5153df269f3609faa19fc5641 Hash: 3a7bc11cc8e213468fc25b6708b39e41780d5673ffffb995bbfdaa909d37b73e Index: 2 Timestamp: 2024-10-30 00:01:20.180441 Data: Another Transaction Previous Hash: 3a7bc11cc8e213468fc25b6708b39e41780d5673ffffb995bbfdaa909d37b73e Hash: 477333623f8b9819e23412dfda01aee127d63a2744b32515cc2d0678fcf7fac1
By using this software, you agree to the full disclaimer terms.
Software provided "AS IS" without warranty. You assume all risks.
Full legal disclaimer: See DISCLAIMER.md
License: BSD 3-Clause License