{"id":21814042,"url":"https://github.com/hoanhan101/blockchain-db","last_synced_at":"2025-04-13T23:45:53.508Z","repository":{"id":112869837,"uuid":"112439788","full_name":"hoanhan101/blockchain-db","owner":"hoanhan101","description":"Blockchain implementation using MongoDB","archived":false,"fork":false,"pushed_at":"2018-05-26T05:33:40.000Z","size":350,"stargazers_count":9,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-27T13:51:20.840Z","etag":null,"topics":["bitcoin","blockchain","database"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hoanhan101.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-29T07:07:25.000Z","updated_at":"2024-01-19T10:25:01.000Z","dependencies_parsed_at":"2023-04-10T18:31:15.011Z","dependency_job_id":null,"html_url":"https://github.com/hoanhan101/blockchain-db","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoanhan101%2Fblockchain-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoanhan101%2Fblockchain-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoanhan101%2Fblockchain-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoanhan101%2Fblockchain-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoanhan101","download_url":"https://codeload.github.com/hoanhan101/blockchain-db/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799693,"owners_count":21163398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitcoin","blockchain","database"],"created_at":"2024-11-27T14:35:04.346Z","updated_at":"2025-04-13T23:45:53.499Z","avatar_url":"https://github.com/hoanhan101.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# blockchain-db\n\n![build-success](https://img.shields.io/badge/build-success-brightgreen.svg)\n![test-passing](https://img.shields.io/badge/test-passing-brightgreen.svg)\n![status-stable](https://img.shields.io/badge/status-stable-green.svg)\n\n\u003e Blockchain + MongoDB = BlockchainDB, aka blockchain-db\n\n**blockchain-db** is Blockchain's implementation. It uses MongoDB as a database\nmanagement system to store blocks' metadata. Other interesting technical\ndetails such as merkle tree, nonce, difficulty, and transactions are also\nimplemented and documented in the source code.\n\n![Demo](demo.png)\n\n## Project Status\n\nIt is done for the most part. Below are ideas for future implementations.\n\n### Tasks\n\n- [x] Model Bitcoin's blockchain structure \n- [x] Implement Proof of Work, nonce, difficulty\n- [x] Enable transactions\n- [x] Implement Merkle Tree\n- [x] Introduce mining mechanics \n- [x] Introduce block reward\n- [x] Remember elapsed time and hash power metrics for later analytics\n- [x] Expose RESTful APIs, with simple UI\n- [x] Store the full chain in mongodb\n\n### Ideas\n\n- [ ] Dockerize the project\n- [ ] Introduce networking with multiple nodes. For now, it only works with one node, which is the local host\n- [ ] Introduce Wallet\n- [ ] Introduce Attack\n\n## Table of Contents\n\n- [Files and structure](#files-and-structure)\n  - [blockchain_db.py](#blockchain_dbpy)\n    - [Block structure](#block-structure)\n    - [Methods](#methods)\n    - [GET methods](#get-methods)\n  - [blockchain_db_server.py](#blockchain_db_serverpy)\n    - [APIs](#apis)\n- [Usage](#usage)\n  - [With networking](#with-networking)\n  - [Without networking](#without-networking)\n\n## Files and structure\n\n### blockchain_db.py\n\nThis file contains the main BlockchainDB logic. I am using `pymongo` to connect with mongodb\ndatabase named `blockchain` and the `block` collection. Whenever a new bock is mined, it will\nwrite to the database.  \n\n#### Block structure\n\n```python3\nblock = {\n    \"previous_block\": \u003cstr\u003e,\n    'height': \u003cint\u003e,\n    'timestamp': \u003cunix time\u003e,\n    'transactions': \u003clist\u003e,\n    \"merkle_root\": \u003cstr\u003e,\n    'number_of_transaction': \u003cint\u003e,\n    'nonce': \u003cint\u003e,\n    'previous_hash': \u003cstr\u003e,\n    'block_reward': \u003cint\u003e,\n    'difficulty_bits': \u003cint\u003e,\n    'difficulty': \u003cint\u003e,\n    'elapsed_time': \u003cint\u003e,\n    'hash_power': \u003cint\u003e\n}\n```\n\n#### Methods\n\nHere are the main methods. Details are well documented in doctrings.\n\n- `generate_genesis_block()`\n- `generate_next_block(nonce, previous_hash=None)`\n- `add_transaction(sender, recipient, amount)`\n- `find_merkle_root(transaction_ids)`\n- `mine_for_next_block()`\n- `calculate_nonce(last_block, number_of_bits)`\n- `calculate_block_reward()`\n- `calculate_difficulty_bits()`\n- `calculate_difficulty()`\n- `hash_json_object(json_object)`\n- `hash_string_pair(string_1, string_2)`\n\n#### GET methods\n\n- `get_length()`\n- `get_last_n_blocks(number)`\n- `get_top_blocks(state, number)`\n- `get_last_block()`\n- `get_genesis_block()`\n- `get_block(height)`\n- `get_all_blocks()`\n- `get_transaction_ids()`\n\n### blockchain_db_server.py\n\nThis file uses *Flask* to serve as a web page. \n\n#### APIs\n\nEndpoint | Description\n--- | ---\n`/reset` | Drop the database and create a genesis block\n`/mine/\u003cint:number\u003e` | Mine a number of blocks over network\n`/view/chain` | View the whole blockchain\n`/view/last_blocks/\u003cint:number\u003e` | View some number of last mined blocks\n`/view/last_block` | View the last mined block \n`/view/genesis_block` | View the genesis block\n`/view/block/\u003cint:number\u003e` | View a specific block\n`/view/top/\u003cint:number\u003e/\u003cstring:state\u003e` | View top numbers of blocks for a given state\n\n## Usage\n\n#### With networking\n\n- In `src`, start `blockchain_db_server.py` and visit `localhost:5000`\n- Hit `/reset` endpoint to create a genesis block. This endpoints can also be used to drop the database\nand start over whenever you want to.\n- Mine some number of blocks at `/mine/\u003cint:number\u003e`\n- Use available `/view` endpoints as mentioned above for more details.\n\n#### Without networking\n\n- Use `blockchain_db_test.py` to create an instance of BlockchainDB to mine some blocks.\n- Execute `reset()` only once when you start to drop the old database and create a genesis block.\n- Comment it out after the second run and try to mine some blocks with the provided testing script.\n- Similar to the first option, start `blockchain_db_server.py` in `src` to serve as a web page and view the result on the web\nor just print it using the console.  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoanhan101%2Fblockchain-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoanhan101%2Fblockchain-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoanhan101%2Fblockchain-db/lists"}