老鱼 avatar

以太坊研究系列【私链合约发布】

chaimyu

Published: 15 Sept 2018 › Updated: 15 Sept 2018

以太坊研究系列【私链合约发布】

最近需要在以太坊上发布个合约,研究这块已经是几个月前的事了,又忘得差不多了,机器上都有哪些环境在哪个目录都不清楚了,现在就发现以前把学习的内容都记录下来的好处了,可以翻翻以前的文章,继续理一遍这块。

复习

以太坊研究系列【基本信息】

以太坊研究系列【私链搭建、挖矿、交易】

以太坊研究系列【Mist】

以太坊研究系列【geth客户端调用mist部署的智能合约】

环境检查

geth

先看看geth还能不能用:

Chaim:~ Chaim$ geth version
Geth
Version: 1.7.3-stable
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.2
Operating System: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.9.2/libexec

brew查看下应该是有新版本了,先把原来那套跑起来再更新吧!

Chaim:~ Chaim$ brew install geth
Error: ethereum 1.7.3 is already installed
To upgrade to 1.8.11, run `brew upgrade ethereum`

找到以前的目录,sh文件还在,执行一下,看起来这块是正常的:

Chaim:workspace Chaim$ cat ethpri.sh 
#!/bin/sh

cd /Users/Chaim/Documents/workspace/ethPri
geth --rpc --datadir "./" --nodiscover console 2>>geth.log

Chaim:workspace Chaim$ ./ethpri.sh 
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable/darwin-amd64/go1.9.2
coinbase: 0x54b865714068f5f03574ace39a1f3279c4e83e2c
at block: 12521 (Fri, 30 Mar 2018 19:19:34 CST)
 datadir: /Users/Chaim/Documents/workspace/ethPri
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

复习下几条简单的geth命令:

> eth.accounts
["0x54b865714068f5f03574ace39a1f3279c4e83e2c", "0x469f07278600b1cb031448fd25f187f24a89ecb6", "0x068a7a0022a0e20d78682f39aa75547a0a260a2a", "0x823e1c4acbfc6527a6210e9bb9b4d5a45dc9c9a4"]
> eth.getBalance(eth.accounts[0])
3.100100377999999606784e+21
> web3.toWei(1)
"1000000000000000000"

MIST

先执行下mistsvr,也还正常:

localhost:workspace Chaim$ cat mistsvr.sh 
#!/bin/sh

cd /Users/Chaim/Documents/workspace/mist/interface
meteor --no-release-check

localhost:workspace Chaim$ ./mistsvr.sh 
[[[[[ ~/Documents/workspace/mist/interface ]]]]]

=> Started proxy.                             
=> Started MongoDB.                           
=> Started your app.                          

=> App running at: http://localhost:3000/

运行mist,不出预料的还是会出现一些不正常的情况,找不到yarn了

localhost:workspace Chaim$ cat mistcli.sh 
#!/bin/sh

cd /Users/Chaim/Documents/workspace/mist
yarn dev:electron . --rpc /Users/Chaim/Documents/workspace/ethPri/geth.ipc 

localhost:workspace Chaim$ ./mistcli.sh 
./mistcli.sh: line 4: yarn: command not found

yarn是npm包,后来切换到nvm了,可能得在nvm下重新安装下,先试下nvm环境:

localhost:workspace Chaim$ nvm use v6
Now using node v6.14.1 (npm v3.10.10)
localhost:workspace Chaim$ node -e 'console.log("Long time no see!")'
Long time no see!

安装yarn:

localhost:workspace Chaim$ sudo npm install -g yarn
/Users/Chaim/.nvm/versions/node/v6.14.1/bin/yarnpkg -> /Users/Chaim/.nvm/versions/node/v6.14.1/lib/node_modules/yarn/bin/yarn.js
/Users/Chaim/.nvm/versions/node/v6.14.1/bin/yarn -> /Users/Chaim/.nvm/versions/node/v6.14.1/lib/node_modules/yarn/bin/yarn.js
/Users/Chaim/.nvm/versions/node/v6.14.1/lib
 yarn@1.9.4 

再次启动mist,这次能进入了:

localhost:workspace Chaim$ ./mistcli.sh 
yarn run v1.9.4
$ electron -r babel-register main.js . --rpc /Users/Chaim/Documents/workspace/ethPri/geth.ipc
[2018-09-15T10:50:20.350] [INFO] Settings - Running in production mode: false
[2018-09-15T10:50:20.520] [INFO] EthereumNode - undefined null 'fast'
[2018-09-15T10:50:20.522] [INFO] EthereumNode - Defaults loaded: geth main fast
[2018-09-15T10:50:20.764] [INFO] main - Starting in Mist mode
...

日志会报一些mist版本升级、找不到geth1.8.13的错误信息,但能正常进入测试,可能正式链上发布合约还是用最新版本比较合适。

此处卡在mist加载界面,记得去geth开启挖矿:miner.start(),然后抱着电脑就像抱着个火炉了...

合约部署

直接去Mist界面中“新建合约”,把合约代码拷到“SOLIDITY合约原始代码”区。

看看把以太链上的合约拿过来编译时出现的一些错误和解决方法:

 "throw" is deprecated in favour of "revert()", "require()" and "assert()".
      throw;
      ^---^

合约代码太旧,throw需要改成require之类的。

 No visibility specified. Defaulting to "public".
    function TEST(
    ^
Spanning multiple lines.

function没指定“public”属性,明确指定是哪种类型。

Function state mutability can be restricted to pure
  function safeMul(uint256 a, uint256 b) internal returns (uint256) {
  ^
Spanning multiple lines.

修改工具函数为“pure”,如果要修改合约存储数据则为“view”。

Expected token LParen got 'eth_compileSolidity'
    function TEST public (

“public”放错地方了,应该放到函数的括号后面,而不是括号前面。

解决所有错误后就可以选择发布合约了,输入密码块确认后就发布成功!

参考

https://blog.csdn.net/tianlongtc/article/details/80261757

Leave 以太坊研究系列【私链合约发布】 to:

Written by

One day sitting in the well looked at the sky, blowing the wind uphole, jumping out of the well, dancing in the wind!

Read more #ethereum posts


Best Posts From 老鱼

We have not curated any of chaimyu's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From 老鱼