以太坊数据上链

---
title: 以太坊数据上链
created: '2020-10-09T01:43:56.000Z'
modified: '2020-10-27T06:20:59.000Z'
---

# 以太坊数据上链

- why

- 部署数据上链的业务,熟悉区块链(以太坊)的一般流程,针对新手快速入门区块链(以太坊)

- 本人做的所有教程都有两个目的 备忘、希望有人指正

- how

- 搭建 流行的以太坊 私链服务器,实现数据上链,并将一系列流程 记录

- what

- 初次 实现数据上链 笔记

查阅的资料

- <span style="color: unset;">链社区</span>

- [https://learnblockchain.cn/](https://learnblockchain.cn/)

- solidity 教程

- [https://www.tryblockchain.org/Solidity-AccessorFunction-%E8%AE%BF%E9%97%AE%E5%87%BD%E6%95%B0.html](https://www.tryblockchain.org/Solidity-AccessorFunction-%E8%AE%BF%E9%97%AE%E5%87%BD%E6%95%B0.html)

- [https://dev.fo/zh-cn/guide/smart-contract-database.html](https://dev.fo/zh-cn/guide/smart-contract-database.html)

- [https://learnblockchain.cn/docs/solidity/layout-of-source-files.html#import](https://learnblockchain.cn/docs/solidity/layout-of-source-files.html#import)

- eth 控制台命令

- [https://blog.51cto.com/12880687/2083802](https://blog.51cto.com/12880687/2083802)

- [https://www.jianshu.com/p/9fa31e4cdf4d](https://www.jianshu.com/p/9fa31e4cdf4d)

- 保存数据

- [https://www.cnblogs.com/tinyxiong/p/8084477.html](https://www.cnblogs.com/tinyxiong/p/8084477.html)

- 智能合约 编译部署

- [https://blog.csdn.net/fpcc/article/details/82746442](https://blog.csdn.net/fpcc/article/details/82746442)

- python调接口

- [http://blog.hubwi z.com/2018/08/21/ethereum-python-json-rpc/](http://blog.hubw iz.com/2018/08/21/ethereum-python-json-rpc/)

- [https://web3py.readthedocs.io/en/latest/web3.main.html](https://web3py.readthedocs.io/en/latest/web3.main.html)

- [http://cw.hubw iz.com/card/c/ethereum-json-rpc-api/1/3/26/](http://cw.h ubwiz.com/card/c/ethereum-json-rpc-api/1/3/26/)

- 不足:

- 此文难以阅读,描述和实例文件 跨度大,需要C-f 查找关键字

源码链接

- [https://github.com/abrance/MedicalLeagueChain](https://github.com/abrance/MedicalLeagueChain)

涉及语言

- python

- solidity

数据上链基本流程

1. 部署环境

2. 部署geth 服务

3. 建立连接,启动矿工

4. 编写 solidity 智能合约,并编译,得到abi和bytecode

5. 部署合约

6. 调用合约

环境

- 一台香港服务器 debian系统

- - nodejs安装

- npm 包

- git安装

- python3 (3.7)

- geth 安装

- go 安装

genesis.json

<span style="color: unset;">存储基本配置信息和额外配置信息,在写脚本 时可以直接导入文件内容(实例下面)</span>

geth 启动服务参数

geth 连接服务

- geth attach [http://localhost:8545](http://localhost:8545/)    # 一般是这个端口 rpc连接

- geth attach ipc:/data/gethdata/geth.ipc    # ipc 连接

- ...    # websocket 连接 没试过

geth 命令行操作

<p align="start">- <span style="color: rgb(0, 0, 0);">eth.accounts</span>

- <span style="color: rgb(0, 0, 0);">查看所有账户</span>

- <span style="color: rgb(0, 0, 0);">eth.getBalance() 查看某一账户财产</span>

- <span style="color: rgb(0, 0, 0);">eth.coinbase</span>

- - <span style="color: rgb(0, 0, 0);">miner.start()</span>

- <span style="color: rgb(0, 0, 0);">miner.stop()</span>

- <span style="color: rgb(0, 0, 0);">...</span>

- <span style="color: rgb(0, 0, 0);">exit    退出</span></p>

solc 编译sol文件(不智能)

- 安装

- npm install -g solc

- 编译

- solcjs storage.sol --bin --abi --optimize -o ./    # 编译storage.sol 文件,文件输出到同级目录出现 abi 和 bin文件 bin使用时需要加'0x'

truffle 进行sol文件的智能编译

- 参考

- [https://truffle.tryblockchain.org/Truffle-CommandRefrence-%E5%91%BD%E4%BB%A4%E6%8C%87%E5%8D%97.html](https://truffle.tryblockchain.org/Truffle-CommandRefrence-%E5%91%BD%E4%BB%A4%E6%8C%87%E5%8D%97.html)

- 安装truffle

- npm install -g truffle

- 基本使用

- mkdir contracts

- cd contracts

- truffle init

- .... 自动建立项目 目录树

- build contracts migrations test truffle-config.js

- 这里只用到 contracts build两个目录

- 写好一个 sol 文件,放入contracts目录

- truffle compile

- 设置版本在 <span style="background-color: rgba(27, 31, 35, 0.05);color: rgb(36, 41, 46);">truffle-config.js 中设置</span>

- [https://github.com/trufflesuite/truffle/issues/1827](https://github.com/trufflesuite/truffle/issues/1827)

- 这时 build/contracts 出现 同名.json 文件

- 完成,json文件备用,写脚本时、部署合约时使用

- 第n次 进行编译

- truffle compile 默认只编译修改过的文件,要想编译新增的文件,需要加参数

- truffle compile --compile-all

- 部署

- 第一种

- 取build/contracts/\*.json中 abi值和bytecode值进行合约的部署

- 第二种

- truffle 自动部署

- 第三种

- 脚本部署

metamask 使用

- 功能

- 连接私链账户

- 连接remix部署智能合约

- 账户内容

- 交易内容

- 修改交易细节

- 导出私钥

- 三点-账户详情-导出私钥

- 合约信息

- 读《精通以太坊》 第二章:以太坊基本概念-MetaMask入门 跟书走一遍流程可以学会 几乎所有内容(除了导出私钥)

remix 使用

- 连接私链 并 部署合约

- deploy & run transaction-environment-injected web3 弹窗输入连接地址

- 选择有钱的账户

- 写sol文件

- deploy

- 可以看到右下 控制台出现

- creation of Hello pending...    # 说明正在部署

- \[block:918 txIndex:0\]

- from: 0xCcD...965Ecto: Hello.(constructor)value: 1 weidata: 0x608...00000logs: 0hash: 0xf09...66a0b    # 部署成功

- 服务器日志中也可以看到

python web3.py 来写脚本调用geth

- 安装 web3.py

- pip3 install web3 报错 lru\_dict cytoolz 有问题

- - git clone https://github.com/ethereum/web3.py.git

- apt-get install python3-dev    # 不进行这一步可能导致 报错

- python3.7 setup.py install     # 避免连接错了python3版本

- web3 使用

- 与pypi 里不同 ?

- 使用web3.utils 报错,无utils,试了一下可以直接使用utils里内容

- 实例

- 部署已编译合约 deploy.py

tip

- geth 编译问题,这在国外服务器应该不存在

- 如果在类Unix 系统发生连接超时类问题,依次执行

- go env -w GOPROXY=https://goproxy.cn,direct

- go env -w GOPRIVATE=.gitlab.com,.gitee.com

- go env -w GOSUMDB=off

- go env -w GOSUMDB="sum.golang.google.cn"

- 再继续即可

- 交易

- miner

- metamask 进行交易时,一直pending; 我以为是gasprice问题,加高没用,后来执行 miner.start() 一瞬间完成;不知原因

- gas limit问题

- 在交易时 报错exceeds block gas limit, 需要调高price ,或调低 gas 参数; 不确定 21000\*gasPrice左右,不要太离谱

- 函数调用问题

- transact(

- {'from': user, 'gas': 900000, 'gasPrice': 10}

- )

- call()

- [https://web3py.readthedocs.io/en/latest/contracts.html](https://web3py.readthedocs.io/en/latest/contracts.html) 好好参考文档,看看是不是需要调用这个阻塞函数 <span style="color: rgb(64, 64, 64);"><small>w3</small></span><span style="color: rgb(102, 102, 102);"><small>.</small></span><span style="color: rgb(64, 64, 64);"><small>eth</small></span><span style="color: rgb(102, 102, 102);"><small>.</small></span><span style="color: rgb(64, 64, 64);"><small>waitForTransactionReceipt</small></span><span style="color: rgb(64, 64, 64);"><small>(</small></span><span style="color: rgb(64, 64, 64);"><small>tx\_hash</small></span><span style="color: rgb(64, 64, 64);"><small>)</small></span>

总结

- 很多方式可以 交易,数据上链,各种信息查询,但是做完觉得,脚本方式最简单

posted @ 2020-11-02 09:47  DaguguJ  阅读(876)  评论(0)    收藏  举报