博客参考
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:
- 下载
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的图标时,右键将其固定在状态栏