GithubHelp home page GithubHelp logo

blockchain_simple_implementation's Introduction

blockchain simple implementation in Python

A simple implementation for developing a blockchain application with mining in Python.

项目是一个区块链的简单实现,是支持数据上传,挖矿和共识。

为了简化,没有加入签名认证,并且实验时一次只能有一个节点挖矿。


必要环境

  • python3

基于Flask框架和requests请求

pip3 install -r requirements.txt

功能实现和测试

  1. 交易上传

    python flask_server.py -p 8080
    python client.py -p 8080 -a hdk -d hhhhhh
    

    访问:

    localhost:8080/pending_tx
    

    输出:

    [{"author": "hdk", "content": "hhhhhh", "timestamp": 1674557970.5027435}]
  2. 挖块

    访问:
    localhost:8080/generate_block
    localhost:8080/chain
    

    输出链结构:

    {"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 1674557968.4578717, "previous_hash": "0", "nonce": 0, "hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173"}, {"index": 1, "transactions": [{"author": "hdk", "content": "hhhhhh", "timestamp": 1674557970.5027435}], "timestamp": 1674558249.506999, "previous_hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173", "nonce": 761, "hash": "00006de85025cab6ee8bc33dc34c7dad21489c8c1363836113766e6eb1226d07"}]}
  3. 节点加入

    python .\flask_server.py -p 8081

    端口8081加入8080的网络

    localhost:8081/join/localhost/8080

    查看8080端口加入的节点:

    浏览器访问:localhost:8080/peers
    [{'ip': 'localhost', 'port': 8081}]

    查看8081端口加入的节点

    浏览器访问:localhost:8080/peers
    返回:[{'ip': 'localhost', 'port': 8081}]

    查看8080端口块数据

    浏览器访问:localhost:8080/chain
    返回:{"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 1674557968.4578717, "previous_hash": "0", "nonce": 0, "hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173"}, {"index": 1, "transactions": [{"author": "hdk", "content": "hhhhhh", "timestamp": 1674557970.5027435}], "timestamp": 1674558249.506999, "previous_hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173", "nonce": 761, "hash": "00006de85025cab6ee8bc33dc34c7dad21489c8c1363836113766e6eb1226d07"}]}

    查看8081端口块数据

    浏览器访问:localhost:8081/chain
    返回:{"length": 2, "chain": [{"index": 0, "transactions": [], "timestamp": 1674557968.4578717, "previous_hash": "0", "nonce": 0, "hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173"}, {"index": 1, "transactions": [{"author": "hdk", "content": "hhhhhh", "timestamp": 1674557970.5027435}], "timestamp": 1674558249.506999, "previous_hash": "1857515eb50c52193140126226825e0a8203488afa70b2b21a407cb45c0e4173", "nonce": 761, "hash": "00006de85025cab6ee8bc33dc34c7dad21489c8c1363836113766e6eb1226d07"}]}

​ 块数据一致



Ref:

blockchain_simple_implementation's People

Stargazers

 avatar  avatar

Watchers

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