搭建一个区块链程序的实践

博客参考
https://blog.csdn.net/blockchain_lemon/article/details/80837336(程序员们,只需三步,教你搭建一个区块链程序)
https://github.com/dvf/blockchain
https://zhuanlan.zhihu.com/p/127820010
https://blog.csdn.net/ninitiange2/article/details/108768633
https://www.cnblogs.com/cocomoly/p/11725219.html
https://www.freesion.com/article/750562621/
配置postmen,这个postmen很好用,大家可以配置一下,文末有写具体配置过程。

部署流程

1、首先要确保安装了Python 3.6以上的环境和Flask,此外还需要安装一个碉堡的Requests库。版本信息如下:

 pip install Flask==0.12.2 requests==2.18.4 

2、程序源代码

https://github.com/dvf/blockchain

3、安装PyCharm
4、 启动程序

$ python blockchain.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

4、交易格式

{
 "sender": "my address",
 "recipient": "someone else's address",
 "amount": 5
}

5、创建交易、挖矿、查看区块链

http://localhost:5000/transactions/new(post方法)
为一个区块创建一个新的交易
http://localhost:5000/mine (get方法)
告诉我们的服务器开采一个新的区块;
http://localhost:5000/chain (get方法)
返回完整的 Blockchain 类。

6、共识
http://localhost:5000/mine (创世块)
http://localhost:5000/chain
http://localhost:5000/transactions/new

{
 "sender": "my address",
 "recipient": "someone else's address",
 "amount": 5
}

http://localhost:5000/mine (加一个块)
http://localhost:5000/chain
这个时候5000这个端口的节点的区块链上已经有两个块。
http://localhost:5001/mine (创世块)
http://localhost:5001/chain
这个时候5001这个端口的节点的区块链上已经有一个块。
链短的同步链长的
http://localhost:5001/nodes/register(加入5000这个节点)

{
    "nodes":["http://localhost:5000"]
}

http://localhost:5001/nodes/resolve(同步最长链)

遇到问题:

1、遇到127.0.0.1 - - [04/Nov/2020 13:46:10] “GET / HTTP/1.1” 404
将主函数改成

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

即可。

2、特别看清关于Postman的各种图片,我因为没有设置相应的json格式,出了很多错,研究了很久。

配置postmen:

  1. 下载

Postman工具下载地址
https://www.getpostman.com/downloads/
在这里插入图片描述选择Linux版本
2. 安装
将安装包复制到Ubuntu指定目录下,打开Ubuntu终端,输入命令将安装包解压

sudo tar -xzf Postman-linux-x64-7.2.2.tar.gz

3、启动

进入解压后的Postman文件夹,启动Postman

./Postman/Postman

当桌面出现Postman的图标时,右键将其固定在状态栏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值