dvf/blockchain

repository·master·Indexed 27 days ago

https://github.com/dvf/blockchain

Source 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.

Tokens
417
Snippets
0
Records
4
Agent score
43%

What's inside dvf-blockchain

  1. Install and run the Python implementation

    master
    To run the Python version of the blockchain, ensure you have Python 3.6+ installed. Use pipenv to manage dependencies and run the server. You can specify a custom port using the -p flag or the --port flag.
  2. Use the Blockchain HTTP entrypoint

    master

    The exported module acts as an asynchronous request handler for HTTP requests (compatible with micro). It routes incoming requests to specific Blockchain instance methods based on the URL path. It also automatically tracks the peer that contacted the server by adding the request.headers.host to 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_transaction route expects a transaction variable to be available in the scope (though in the provided snippet, transaction is not explicitly defined from the request body, implying it must be handled by the surrounding environment or middleware).