diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..f30d405 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://81.68.78.108:3306/commodity + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 27277f3..5e5bdbb 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,16 @@ + + - + + + \ No newline at end of file diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml new file mode 100644 index 0000000..3dfd21f --- /dev/null +++ b/.idea/qaplug_profiles.xml @@ -0,0 +1,465 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.assets/image-20220316140843880.png b/README.assets/image-20220316140843880.png new file mode 100644 index 0000000..490c163 Binary files /dev/null and b/README.assets/image-20220316140843880.png differ diff --git a/README.assets/image-20220316141030829.png b/README.assets/image-20220316141030829.png new file mode 100644 index 0000000..466b02e Binary files /dev/null and b/README.assets/image-20220316141030829.png differ diff --git a/README.assets/image-20220316141210492.png b/README.assets/image-20220316141210492.png new file mode 100644 index 0000000..d816245 Binary files /dev/null and b/README.assets/image-20220316141210492.png differ diff --git a/README.assets/image-20220316142419701.png b/README.assets/image-20220316142419701.png new file mode 100644 index 0000000..ddf3cfc Binary files /dev/null and b/README.assets/image-20220316142419701.png differ diff --git a/README.assets/image-20220316143159849.png b/README.assets/image-20220316143159849.png new file mode 100644 index 0000000..a64889e Binary files /dev/null and b/README.assets/image-20220316143159849.png differ diff --git a/README.assets/image-20220316143452706.png b/README.assets/image-20220316143452706.png new file mode 100644 index 0000000..35b3f4e Binary files /dev/null and b/README.assets/image-20220316143452706.png differ diff --git a/README.assets/image-20220316143655244.png b/README.assets/image-20220316143655244.png new file mode 100644 index 0000000..eff91f2 Binary files /dev/null and b/README.assets/image-20220316143655244.png differ diff --git a/README.md b/README.md index 36c9cbe..48be780 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,176 @@ # commodity_backingtrack_system 基于区块链的商品溯源系统 + + + +### 运行方式 + + + +#### 1. 环境配置 + + + +##### 1.1 Python获取 + +需从python官网下载python环境,下载地址: + +[ Python Releases for Windows | Python.org](https://www.python.org/downloads/windows/) + + + +进入页面后找到3.7.7版本下载即可 + +这里给出windows环境的二进制安装包下载链接: + +[python-3.7.7-amd64.exe](https://www.python.org/ftp/python/3.7.7/python-3.7.7-amd64.exe) + + + +##### 1.2 Python安装 + +运行下载好的安装包, + +![image-20220316140843880](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316140843880.png) + +> 选择下面的`Customize installation`进行自定义安装,可以勾选下方的`Add Python 3.7 to PATH`选项,将python可执行文件添加至系统变量 + +![image-20220316141030829](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316141030829.png) + +> 本页选择默认即可 + + + +![image-20220316141210492](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316141210492.png) + + + +> 本页面选项可默认,下面的安装地址如果需要可以进行修改,但是一定要记住这个地址,后续配置需要用到。设置完成后点击Install按钮进行安装。 + + + + + +#### 2. 在IDEA中运行 + + + +使用IDEA打开项目,点击File>Setting>Plugins打开插件安装界面,搜索Python插件并安装 + +![image-20220316142419701](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316142419701.png) + + + +打开`commodity_backingtrack_system\venv`目录下的`pyvenv.cfg`文件,修改其第一行内容为上面python安装的地址 + + + +![image-20220316143159849](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316143159849.png) + + + +随后依次点击 File > Project Structure > Project ,打开项目设置界面,并下拉SDK一栏,找到 Add SDK,并选择 PythonSDK + +![image-20220316143452706](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316143452706.png) + + + + + +在打开的界面中,保持左侧选项卡为Virtualenv Environment不变,右侧选择Existing environment + +![image-20220316143655244](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316143655244.png) + +点击下拉框右侧的三个点,选择当前项目下的venv文件夹中的python.exe文件,之后全程点击OK即可,等待IDEA完成加载。 + + + +#### 3. 项目启动前的配置 + +- 数据库配置:修改项目的`app/config/local_setting.py`文件中的`SQLALCHEMY_DATABASE_URI`字段,可参照原有数据格式进行修改,即: + +``` +SQLALCHEMY_DATABASE_URI = "mysql://用户名:密码@数据库地址/数据库名" # 数据库连接配置 +``` + +- 监听端口配置:继续修改`local_setting.py`文件中的DOMAN字段,修改`www`字段为运行程序的机器所在的局域网IP,如果本地运行则修改为`http://127.0.0.1:5000`即可。 +- 数据库建表:打开`app/manager.py`,找到IDEA左侧显示的运行按钮,点击运行,此时会发现运行失败,但是右上角出现了运行配置,此时点击下拉框,选择Edit Configuration选项,编辑运行配置 + +![image-20220316145921723](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316145921723.png) + + + +编辑Parameters框,填入参数“create_all”, 并在下面的`Environment variables`中追加:`;ops_config=local`,表示添加环境变量键值对,应用启动时会根据这个配置加载`xxx_setting.py`配置文件。 + +![image-20220316150431537](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316150431537.png) + + + +再次运行项目,如果数据库连接正常的话,会成功创建三个数据表: + +![image-20220316150846421](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316150846421.png) + +如果建表失败,也可以使用如下建表语句手动创建数据表: + +```sql +create table commodity +( + id int auto_increment + primary key, + name varchar(32) default '' not null, + origin varchar(32) default '' not null, + seller varchar(32) default '' not null, + trans varchar(32) default '' not null, + warehouse varchar(32) default '' not null, + buyer varchar(32) default '' not null, + status varchar(32) default '' not null +); + +create table operation +( + id int auto_increment + primary key, + user_id int not null, + type varchar(255) not null, + commodity_id int not null, + date datetime not null +); + +create table user +( + id int auto_increment + primary key, + name varchar(30) not null, + type varchar(10) not null, + password varchar(32) not null, + regist_time datetime not null, + constraint name + unique (name) +); +``` + +#### 4. 正式运行项目 + +##### 4.1 区块链服务端及客户端启动 + +打开`/app/blockchain`目录,其中`server`为服务端,`clients`文件夹为客户端集群,`client`文件夹为客户端源码,运行时不需要此文件夹中的内容,其他客户端均由此文件夹复制生成,内部为三个完全一致地客户端节点,打开`zmq_server.py`或`zmq_client.py`文件后,下方运行入口左边会显示运行小三角,点击即可直接运行。 + +![image-20220316151641995](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316151641995.png) + + + +此时下方已经运行了1个服务节点和3个客户端节点。 + + + +##### 4.2 启动管理系统 + +修改之前的manager运行配置,将`create_all`参数改为`runserver`启动参数后,点击运行即可 + +![image-20220316151926589](D:\workspace\project\python\commodity_backingtrack_system\README.assets\image-20220316151926589.png) + +此时应用已经启动,可在浏览器中访问`http://127.0.0.1:5000`打开系统,开始前需要进行账号注册操作,共有4种账户类型,分别代表商品流转过程中经历的不同节点,商品的状态以及可以操作的权限可参考代码。商品的状态发生改变时,会被区块链服务端接收到,并由客户端进行工作量计算,得到结果后服务端会选择占大多数的证明为最终结果写入区块链,区块链保存在json中,每个节点均保存一个备份。 + + + +点击溯源按钮后,服务端会从区块链上查询商品的操作记录并进行展示。 diff --git a/app/blockchain/clients/client1/blockchain.json b/app/blockchain/clients/client1/blockchain.json index bb8300b..8acc1d5 100644 --- a/app/blockchain/clients/client1/blockchain.json +++ b/app/blockchain/clients/client1/blockchain.json @@ -18,7 +18,46 @@ ], "proof": 130809, "previous_hash": "2c54cf5a99bddf446dca8b50d1536f6e3818225b1b9d6821a3c2f8a2971c903b" + }, + { + "index": 3, + "content": [ + { + "commodity": 1, + "operator": 1, + "option": "\u4e0a\u67b6", + "time": "2022-03-16 15:25:13" + } + ], + "proof": 56571, + "previous_hash": "000080ae1f49b23b88423eb1f29ec5c0d31fc68cfd9bdc4191015be161cb8f1e" + }, + { + "index": 4, + "content": [ + { + "commodity": "1", + "operator": 4, + "option": "\u4e0b\u5355", + "time": "2022-03-16 15:26:25" + } + ], + "proof": 9270, + "previous_hash": "000049a5e7d4323a9210f497adf4a7f488f029ecd282b8ba9f5e8382ccc64e21" + }, + { + "index": 5, + "content": [ + { + "commodity": "1", + "operator": 1, + "option": "\u53d1\u8d27", + "time": "2022-03-16 15:28:51" + } + ], + "proof": 130028, + "previous_hash": "0000a3fc602b30f28ef148fcb3e2568a5b544893eb06f11d71f697b5d18230d7" } ], - "length": 2 + "length": 5 } \ No newline at end of file diff --git a/app/blockchain/clients/client2/blockchain.json b/app/blockchain/clients/client2/blockchain.json index bb8300b..8acc1d5 100644 --- a/app/blockchain/clients/client2/blockchain.json +++ b/app/blockchain/clients/client2/blockchain.json @@ -18,7 +18,46 @@ ], "proof": 130809, "previous_hash": "2c54cf5a99bddf446dca8b50d1536f6e3818225b1b9d6821a3c2f8a2971c903b" + }, + { + "index": 3, + "content": [ + { + "commodity": 1, + "operator": 1, + "option": "\u4e0a\u67b6", + "time": "2022-03-16 15:25:13" + } + ], + "proof": 56571, + "previous_hash": "000080ae1f49b23b88423eb1f29ec5c0d31fc68cfd9bdc4191015be161cb8f1e" + }, + { + "index": 4, + "content": [ + { + "commodity": "1", + "operator": 4, + "option": "\u4e0b\u5355", + "time": "2022-03-16 15:26:25" + } + ], + "proof": 9270, + "previous_hash": "000049a5e7d4323a9210f497adf4a7f488f029ecd282b8ba9f5e8382ccc64e21" + }, + { + "index": 5, + "content": [ + { + "commodity": "1", + "operator": 1, + "option": "\u53d1\u8d27", + "time": "2022-03-16 15:28:51" + } + ], + "proof": 130028, + "previous_hash": "0000a3fc602b30f28ef148fcb3e2568a5b544893eb06f11d71f697b5d18230d7" } ], - "length": 2 + "length": 5 } \ No newline at end of file diff --git a/app/blockchain/clients/client3/blockchain.json b/app/blockchain/clients/client3/blockchain.json index bb8300b..8acc1d5 100644 --- a/app/blockchain/clients/client3/blockchain.json +++ b/app/blockchain/clients/client3/blockchain.json @@ -18,7 +18,46 @@ ], "proof": 130809, "previous_hash": "2c54cf5a99bddf446dca8b50d1536f6e3818225b1b9d6821a3c2f8a2971c903b" + }, + { + "index": 3, + "content": [ + { + "commodity": 1, + "operator": 1, + "option": "\u4e0a\u67b6", + "time": "2022-03-16 15:25:13" + } + ], + "proof": 56571, + "previous_hash": "000080ae1f49b23b88423eb1f29ec5c0d31fc68cfd9bdc4191015be161cb8f1e" + }, + { + "index": 4, + "content": [ + { + "commodity": "1", + "operator": 4, + "option": "\u4e0b\u5355", + "time": "2022-03-16 15:26:25" + } + ], + "proof": 9270, + "previous_hash": "000049a5e7d4323a9210f497adf4a7f488f029ecd282b8ba9f5e8382ccc64e21" + }, + { + "index": 5, + "content": [ + { + "commodity": "1", + "operator": 1, + "option": "\u53d1\u8d27", + "time": "2022-03-16 15:28:51" + } + ], + "proof": 130028, + "previous_hash": "0000a3fc602b30f28ef148fcb3e2568a5b544893eb06f11d71f697b5d18230d7" } ], - "length": 2 + "length": 5 } \ No newline at end of file diff --git a/app/blockchain/server/blockchain.json b/app/blockchain/server/blockchain.json index eb8c70d..c17b5c6 100644 --- a/app/blockchain/server/blockchain.json +++ b/app/blockchain/server/blockchain.json @@ -18,7 +18,46 @@ ], "proof": 130809, "previous_hash": "2c54cf5a99bddf446dca8b50d1536f6e3818225b1b9d6821a3c2f8a2971c903b" + }, + { + "index": 3, + "content": [ + { + "operator": 1, + "option": "\u4e0a\u67b6", + "commodity": 1, + "time": "2022-03-16 15:25:13" + } + ], + "proof": 56571, + "previous_hash": "000080ae1f49b23b88423eb1f29ec5c0d31fc68cfd9bdc4191015be161cb8f1e" + }, + { + "index": 4, + "content": [ + { + "operator": 4, + "option": "\u4e0b\u5355", + "commodity": "1", + "time": "2022-03-16 15:26:25" + } + ], + "proof": 9270, + "previous_hash": "000049a5e7d4323a9210f497adf4a7f488f029ecd282b8ba9f5e8382ccc64e21" + }, + { + "index": 5, + "content": [ + { + "operator": 1, + "option": "\u53d1\u8d27", + "commodity": "1", + "time": "2022-03-16 15:28:51" + } + ], + "proof": 130028, + "previous_hash": "0000a3fc602b30f28ef148fcb3e2568a5b544893eb06f11d71f697b5d18230d7" } ], - "length": 2 + "length": 5 } \ No newline at end of file diff --git a/app/common/models/commodity.py b/app/common/models/commodity.py index f6b1247..e002058 100644 --- a/app/common/models/commodity.py +++ b/app/common/models/commodity.py @@ -7,10 +7,10 @@ class Commodity(db.Model): __tablename__ = 'commodity' id = db.Column(db.Integer, primary_key=True, info='主键-商品id') - name = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='商品名称') - origin = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='商品产地') - seller = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='卖家') - trans = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='运输') - warehouse = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='仓库') - buyer = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='买家') - status = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='商品状态') + name = db.Column(db.String(32), nullable=False, server_default='', info='商品名称') + origin = db.Column(db.String(32), nullable=False, server_default='', info='商品产地') + seller = db.Column(db.String(32), nullable=False, server_default='', info='卖家') + trans = db.Column(db.String(32), nullable=False, server_default='', info='运输') + warehouse = db.Column(db.String(32), nullable=False, server_default='', info='仓库') + buyer = db.Column(db.String(32), nullable=False, server_default='', info='买家') + status = db.Column(db.String(32), nullable=False, server_default='', info='商品状态') diff --git a/app/config/local_setting.py b/app/config/local_setting.py index 323dfd4..f6bfa35 100644 --- a/app/config/local_setting.py +++ b/app/config/local_setting.py @@ -9,5 +9,5 @@ SQLALCHEMY_DATABASE_URI = "mysql://root:123456@127.0.0.1/commodity" # 数据库 SECRET_KEY = "123456" # debug_toolbar 配置 DOMAIN = { - "www": "http://192.168.137.1:5000" + "www": "http://127.0.0.1:5000" } \ No newline at end of file diff --git a/venv/pyvenv.cfg b/venv/pyvenv.cfg index 32d61f6..1dfd732 100644 --- a/venv/pyvenv.cfg +++ b/venv/pyvenv.cfg @@ -1,3 +1,3 @@ -home = D:\workshop\path\python3.7 +home = D:\workspace\environment\python include-system-site-packages = false version = 3.7.7