https://github.com/vndee/blockchain-demo
A very simple implementation of blockchain and its applications in Python.
https://github.com/vndee/blockchain-demo
blockchain
Last synced: 7 months ago
JSON representation
A very simple implementation of blockchain and its applications in Python.
- Host: GitHub
- URL: https://github.com/vndee/blockchain-demo
- Owner: vndee
- License: mit
- Created: 2023-01-29T06:26:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-22T15:53:41.000Z (over 2 years ago)
- Last Synced: 2025-01-19T07:26:50.113Z (9 months ago)
- Topics: blockchain
- Language: HTML
- Homepage:
- Size: 616 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A very simple implementation of blockchain and its applications in Python.
- [Blockchain](#blockchain)
- [Cryptocurrency](#cryptocurrency)
- [Smart Contracts](#smart-contracts)### Blockchain
To use the base functionality of the blockchain, you can use the `Blockchain` class. It has the following methods:
- `new_block`: Adds a block to the blockchain.
- `hash`: Hashes a block.
- `proof_of_work`: Simple Proof of Work Algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p', p is the previous nonce, and p' is the new nonce.
- The number of leading zeroes can be changed by changing the `difficulty` variable.
- The `proof_of_work` method returns the nonce that satisfies the above condition.
- The `new_block` method calls the `proof_of_work` method to get the correct nonce for the new block.
- `check_valid_nonce`: Checks if the nonce satisfies the proof of work condition.
- `check_valid_chain`: Determines if a given blockchain is valid.I also add a REST API server to serving these above functions. The API has the following endpoints:
- `/mine-block`: Mines a new block.
- `/get-chain`: Returns the full blockchain.
- `/is-valid`: Checks if the blockchain is valid.In order to try out the demo, you can run the following commands:
```bash
# Install FastAPI and uvicorn
$ pip install fastapi uvicorn# Run the server
$ uvicorn blockchain:app
```
Now, navigate to `http://localhost:8000/` to have a look at the demo page and `http://localhost:8000/docs` for the detail
documentations. The app behaviors should be like the following:
Figure 1: Mine a block
Figure 2: Get the blockchain

Figure 3: Check if the blockchain is valid

Developed by [Duy Huynh](https://duy-huynh.com/), 2023.