dvf/blockchain
repository·master·Indexed 27 days ago
https://github.com/dvf/blockchainSource code for the book 'Learn Blockchain by Building One', featuring blockchain implementations in Python and C#. Includes a Docker setup and an HTTP entrypoint for managing blocks, peers, and transactions via routes such as /new_block, /last_block, /get_peers, and /submit_transaction.
What's inside dvf-blockchain
- The C# implementation is designed to be run using Visual Studio. Once running, the console application is controlled via HTTP using the same commands as the Python version.
Run the blockchain using Docker
masterYou can run the blockchain program in a Docker container. To scale the network, run multiple containers by varying the host port mapping (the number before the colon).Install and run the Python implementation
masterTo run the Python version of the blockchain, ensure you have Python 3.6+ installed. Usepipenvto manage dependencies and run the server. You can specify a custom port using the-pflag or the--portflag.Use the Blockchain HTTP entrypoint
masterThe exported module acts as an asynchronous request handler for HTTP requests (compatible with
micro). It routes incoming requests to specificBlockchaininstance methods based on the URL path. It also automatically tracks the peer that contacted the server by adding therequest.headers.hostto the blockchain's peer list.Available Routes:
| Route | Method Called | Description | | :--- | :--- | : | |
/new_block|blockchain.newBlock()| Generates a new block. | |/last_block|blockchain.lastBlock()| Retrieves the most recent block. | |/get_peers|blockchain.getPeers()| Returns the list of known peers. | |/submit_transaction|blockchain.addTransaction(transaction)| Adds a new transaction to the blockchain. | |default|blockchain.lastBlock()| Returns the last block if no route matches. |Note: The
/submit_transactionroute expects atransactionvariable to be available in the scope (though in the provided snippet,transactionis not explicitly defined from the request body, implying it must be handled by the surrounding environment or middleware).