一秒通1MT.CN一秒通1MT.CN

理财、金融、保险、贷款、虚拟货币
等知识简单易懂一秒就通!

以太坊(Ethereum)Geth (go-ethereum) 教学

以太坊(Ethereum)Geth (go-ethereum) 教学

简介

https://geth.ethereum.org/ethereum的Go语言实现

安装

https://geth.ethereum.org/downloads/

git clone https://github.com/ethereum/go-ethereumsudo apt-get install -y build-essential golangcd go-ethereummake geth

Mac

brew tap ethereum/ethereumbrew install ethereum

Arm embeded system

http://ethembedded.com/?page_id=171

使用

https://github.com/ethereum/go-ethereum/wiki/geth

如果是从source安装(或可加入环境变数)

~/go-ethereum/build/bin/geth

如果是从安装档可直接输入

geth help

可用命令https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

1.创建创世区块(genesis block)

custom_genesis.json

{ "config": { "chainId": 123, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "difficulty": "0x40", "gasLimit": "4712388", "alloc": { }}

然后输入

geth --datadir "./privatechain" --networkid 123 init custom_genesis.json

上面将会创建一个privatechain资料夹,里面存放我们private chain的资讯,genesis blcok会放在/privatechain资料夹/geth/chaindata/...ldb,而account会放在keystore里面

(如果是1.6之前版本要改为如下)

如果出现Fatal: invalid genesis file: json: cannot unmarshal hex string of odd length into Go struct field Genesis.extraData of type hexutil.Bytes 为版本>1.6 请参考上面

https://ethereum.stackexchange.com/questions/15283/getting-error-on-max-invalid-genesis-file-hex-string-has-odd-length

{ "nonce": "0x0000000000000042", "timestamp": "0x0", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x0", "gasLimit": "0x8000000", "difficulty": "0x400", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x3333333333333333333333333333333333333333", "alloc": { }}

然后输入以下,之后开启Mist wallet就会进入private network

geth --datadir "./privatechain" init ./custom_genesis.json geth --ipcpath ~/Library/Ethereum/geth.ipc --datadir "./privatechain" --networkid 123

上面要写上--ipcpath原因是我们指定了datadir

以下为各os路径后面记得加geth.ipc

Mac

~/Library/Ethereum/

Linux

~/.ethereum/

Windows

~/AppData/Roaming/Ethereum

方法2

也可使用下面产生private network

geth init custom_genesis.json geth networkid=5

这样打开钱包也可进入private network

如果出现错误:Fatal: Failed to write genesis block: database already contains an incompatible genesis block

把路径下的chaindata资料夹删除即可~/Library/Ethereum/geth/chaindata

2.预先分配ether给account

先把刚才的节点启动

geth --ipcpath ~/Library/Ethereum/geth.ipc --datadir "./privatechain" --networkid 123

然后打开Mist钱包后先创建帐户,然后把帐户号码复制,之后贴到刚才genesis的alloc下面的一串hash,然后重新init 即可

"alloc": { "0x66A2e289b35147188876c2007f9a810Dd20e480d": { "balance": "1337000000000000000000"} }geth --datadir "./privatechain" --networkid 123 init custom_genesis.json

每次init genesis chaindata资料夹下都会多一个ldb但他会去读取最新的

有时上面讯息有Success 但下面又有Fatal可以不用理会

之后再次启动

geth --ipcpath ~/Library/Ethereum/geth.ipc --datadir "./privatechain" --networkid 123

点选Mist图案打开钱包即可看到帐户余额增加

试着挖矿

打开另一个termianl输入以下,进入console

geth attach

或是可在刚才启动ethereum后面加上console字样,而geth attach预设只会找ipc不会找rpc

可在mist新增地址或是在console输入personal.newAccount("密碼")加入挖矿要放钱的地址

web3.miner.setEtherbase('0xB4eb9148CD0Aa801215f0e62A354F55A7E1AA67A')// 上面為剛才新增的地址// 也可以直接寫如下 會去讀取剛才創建得地址web3.miner.setEtherbase(personal.listAccounts[0])

开始挖矿(需稍等)

(目前新版1.6有可能卡在dag产生完后)

miner.start(1)

停止

miner.stop()

3.加入其他节点

https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster

https://github.com/ethereum/go-ethereum/wiki/Connecting-to-the-network#connecting-to-the-network

1.用RPC,记得要使用--rpc

2.每个节点要使用同一个创世区块,才能找到彼此,我们先输入如下指令,分别创建两个节点的资料夹,并且用同一个genesis.json个别于两资料夹产生相同创世区块

先把刚privatechain资料夹删除,重新创建一个./privatechain/src 然后里面放入刚才的custom_genesis.json

sudo rm -rf ./privatechain ; mkdir ./privatechain ; mkdir ./privatechain/src ; mv ./custom_genesis.json ./privatechain/src/custom_genesis.jsongeth --datadir "./privatechain/01" init ./privatechain/src/custom_genesis.json geth --datadir "./privatechain/02" init ./privatechain/src/custom_genesis.json

3.再来分别启动两个节点(networkid要相同,而--rpcport与--port要不同)

geth --ipcdisable --rpc --rpcport 8104 --datadir "./privatechain/01" --networkid 123 --rpcapi="db,eth,net,web3,personal" --nodiscover --port=30310 consolegeth --ipcdisable --rpc --rpcport 8103 --datadir "./privatechain/02" --networkid 123 --rpcapi="db,eth,net,web3,personal" --nodiscover --port=30308 console

4.之后于01节点的console输入

admin.nodeInfo

把enode部分复制

5.于02节点输入

admin.addPeer("貼上剛才複製的enode")

6.之后于两个节点分别输入admin.peers即可找到彼此

注意

我们没有设预设bootnode所以要手动加入如果有三个节点ABC,A加入B之后,A再加入C这时B仍然找不到C要B主动加入C才可一开始没有加入peers时会发现有时输入admin.peers会出现有时没有,原因是在连线到任何peers时他会试着去连接其他网路上的节点节点互相找不到的原因通常为你没有输入init genesis.json,所以两个节点间的genesis.json不相同以及没有输入--nodiscover 所以他会加上网路上其他相同genesis和networkid的陌生人重新启动某节点该节点的peer会断掉,要重新加入

7.测试RPC-JSON server

之后钱包连线会使用

我們可以先輸入 curl http://localhost:8104 8104是該RPC的port,來看一下有沒有啟動

成功会出现下图

8.这时开启Mist要设定钱包要连线到哪个位置,所以这次我们不能直接点选图案开启,要使用命令列开启,并加上--rpc <PORT>

(你可能直接从图案开启应用程式,也看到他有开启时有显示private net,但其实他还是连到MainNet)

OSX

/Applications/Ethereum\ Wallet.app/Contents/MacOS/Ethereum\ Wallet --rpc http://localhost:8104

Windows

start <加上程式路徑> --rpc <IP+PORT>

# 1.6新增之puppeth CLI可用来产生genesis_block

1.记得先产生account

geth --datadir ./privatechain account new

2.直接在terminal输入`puppeth` 之后会出现如下图

然后造着步骤走即可,最后会产生一个创世.json

但要记得如果共识方法选的是Poa,之后其他节点要挖矿会产生

Block sealing failed err=unauthorized

记得要

clique.propose("帳號", true)

clique指令只有POA才有

注意事项

1.

注意:每次从terminal启动其他ethereum程序后开启钱包,如果你没有指定RPC 或IPC port,钱包都只会去读取最新开启的terminal的程序

2.

如果进入到钱包,但是testnet与main都无法点选,表示你尝试进入到private network但是参数没有下成功

3.

每个--参数都要和前面空一格,否则无效

4

windows作业系统的terminal如果不好复制,可以使用git bash

5.

节点互相找不到的原因通常为你没有输入init genesis.json,所以两个节点间的genesis.json不相同以及没有输入--nodiscover 所以他会加上网路上其他相同genesis和networkid的陌生人

6.

32位元版本的geth无法挖矿,需下载64位元

https://github.com/ethereum/go-ethereum/issues/14633

两节点加入后新加入节点不用挖矿就会自动同步,但如果有产生合约或是广播交易网路上的其中一个节点必须挖矿

使用DEV Chain

不用自己init genesis block 和指定datadir 以及自动帮你配好一些参数可直接启动私有链

geth --dev --rpc --rpcport 8104 --datadir ./Ethtest --rpccorsdomain="*" console

开放Remote 连线

https://ethereum.stackexchange.com/questions/3163/how-can-i-expose-geths-rpc-server-to-external-connections

设定Bootnode

因為homebrew的安裝沒有bootnode executable所以要build from source

以为步骤

git clone https://github.com/ethereum/go-ethereum.gitmake allcd ./build/bin./bootnode -genkey bootnode.key

之后资料夹下会产一个bootnode.key档案

然后输入

./bootnode -nodekey bootnode.key

会产生如下

之后我们开启terminal输入以下启动节点

> 记得--nodiscover要拿掉然后--bootnodes后面填上刚才出现的enode:

并且把enode[::]换成ip地址

bootnode本身不算是节点只启动下面一个时在admin.peers还是空的

geth --ipcdisable --rpc --rpcport 8106 --datadir "./privatechain/01" --networkid 123 --rpcapi="db,eth,net,web3,personal" --bootnodes "enode://92d7f28e8ca3bd1e1fa43959bb1cf37b9d284a81d17bcaee9cf233a773f3da0ce60b2351c3b29b07eb78229e6a84731d36065506f6ad972ed3203c5d18bef313@[127.0.0.1]:30301" --port=30310 consolegeth --ipcdisable --rpc --rpcport 8107 --datadir "./privatechain/02" --networkid 123 --rpcapi="db,eth,net,web3,personal" --bootnodes "enode://92d7f28e8ca3bd1e1fa43959bb1cf37b9d284a81d17bcaee9cf233a773f3da0ce60b2351c3b29b07eb78229e6a84731d36065506f6ad972ed3203c5d18bef313@[127.0.0.1]:30301" --port=30311 console

之后在其中一个terminal输入`admin.peers`

即可看到如下

Geth版本升级

where geth

之后会显示geth安装路径

然后下载https://geth.ethereum.org/downloads/最新版本后在安装时覆盖原本路径即可。

相关推荐
本文地址: http://www.1mt.cn/542557
文章来源: 焕焕
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 931614094@qq.com 举报,一经查实,本站将立刻删除。 未经允许不得转载:
分享到: 更多
以太坊(Ethereum)Geth (go-ethereum) 教学文档下载: PDF DOC TXT

一秒通1MT.CN | 理财、金融、保险、贷款、虚拟货币 等知识简单易懂一秒就通!

联系站长网站公告