Giter Club home page Giter Club logo

blockchain-db's Introduction

blockchain-db

build-success test-passing status-stable

Blockchain + MongoDB = BlockchainDB, aka blockchain-db

blockchain-db is Blockchain's implementation. It uses MongoDB as a database management system to store blocks' metadata. Other interesting technical details such as merkle tree, nonce, difficulty, and transactions are also implemented and documented in the source code.

Demo

Project Status

It is done for the most part. Below are ideas for future implementations.

Tasks

  • Model Bitcoin's blockchain structure
  • Implement Proof of Work, nonce, difficulty
  • Enable transactions
  • Implement Merkle Tree
  • Introduce mining mechanics
  • Introduce block reward
  • Remember elapsed time and hash power metrics for later analytics
  • Expose RESTful APIs, with simple UI
  • Store the full chain in mongodb

Ideas

  • Dockerize the project
  • Introduce networking with multiple nodes. For now, it only works with one node, which is the local host
  • Introduce Wallet
  • Introduce Attack

Table of Contents

Files and structure

blockchain_db.py

This file contains the main BlockchainDB logic. I am using pymongo to connect with mongodb database named blockchain and the block collection. Whenever a new bock is mined, it will write to the database.

Block structure

block = {
    "previous_block": <str>,
    'height': <int>,
    'timestamp': <unix time>,
    'transactions': <list>,
    "merkle_root": <str>,
    'number_of_transaction': <int>,
    'nonce': <int>,
    'previous_hash': <str>,
    'block_reward': <int>,
    'difficulty_bits': <int>,
    'difficulty': <int>,
    'elapsed_time': <int>,
    'hash_power': <int>
}

Methods

Here are the main methods. Details are well documented in doctrings.

  • generate_genesis_block()
  • generate_next_block(nonce, previous_hash=None)
  • add_transaction(sender, recipient, amount)
  • find_merkle_root(transaction_ids)
  • mine_for_next_block()
  • calculate_nonce(last_block, number_of_bits)
  • calculate_block_reward()
  • calculate_difficulty_bits()
  • calculate_difficulty()
  • hash_json_object(json_object)
  • hash_string_pair(string_1, string_2)

GET methods

  • get_length()
  • get_last_n_blocks(number)
  • get_top_blocks(state, number)
  • get_last_block()
  • get_genesis_block()
  • get_block(height)
  • get_all_blocks()
  • get_transaction_ids()

blockchain_db_server.py

This file uses Flask to serve as a web page.

APIs

Endpoint Description
/reset Drop the database and create a genesis block
/mine/<int:number> Mine a number of blocks over network
/view/chain View the whole blockchain
/view/last_blocks/<int:number> View some number of last mined blocks
/view/last_block View the last mined block
/view/genesis_block View the genesis block
/view/block/<int:number> View a specific block
/view/top/<int:number>/<string:state> View top numbers of blocks for a given state

Usage

With networking

  • In src, start blockchain_db_server.py and visit localhost:5000
  • Hit /reset endpoint to create a genesis block. This endpoints can also be used to drop the database and start over whenever you want to.
  • Mine some number of blocks at /mine/<int:number>
  • Use available /view endpoints as mentioned above for more details.

Without networking

  • Use blockchain_db_test.py to create an instance of BlockchainDB to mine some blocks.
  • Execute reset() only once when you start to drop the old database and create a genesis block.
  • Comment it out after the second run and try to mine some blocks with the provided testing script.
  • 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 or just print it using the console.

blockchain-db's People

Contributors

hoanhan101 avatar omers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.