以太坊ETH源码分析(0):私有链搭建以及RPC接口使用

本文介绍基于macbook pro环境、 go-ethereum搭建以太坊ETH私有链环境

一、安装编译geth(以太坊ETH客户端工具)

1、基于源代码本地调试编译生成geth

(1)下载源码 :go-ethereum源码下载地址

~/go/src/github.com/ethereum/go-ethereum$ls
AUTHORS         cmd         ethdb           node
COPYING         common          ethstats        p2p
COPYING.LESSER      consensus       event           params
Dockerfile      console         interfaces.go       rlp
Dockerfile.alltools containers      internal        rpc
Makefile        contracts       les         signer
README.md       core            light           swarm
accounts        crypto          log         tests
appveyor.yml        dashboard       metrics         trie
build           eth         miner           vendor
circle.yml      ethclient       mobile          whisper
~/go/src/github.com/ethereum/go-ethereum$m

(2)编译源码:make geth (只编译geth)

~/go/src/github.com/ethereum/go-ethereum$make geth
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/Cellar/go/1.10.3/libexec/bin/go install -ldflags -X main.gitCommit=11d0ff6578c34b724436dbeeede726b31b41c8b8 -s -v ./cmd/geth
Done building.
Run "/Users/wujinquan/go/src/github.com/ethereum/go-ethereum/build/bin/geth" to launch geth.
~/go/src/github.com/ethereum/go-ethereum$   

(3)生成geth

经过(2)后生成geth位于 ethereum/go-ethereum/build/bin/路径下

2、可通过安装形式安装geth (相当安装软件)

此处不做冗述

二、建立私有以太坊网络

1、创建私有目录 private_chain

~/eth_workspace$ls
~/eth_workspace$mkdir private_chain/
~/eth_workspace$ls
private_chain
~/eth_workspace$cd private_chain/
~/eth_workspace/private_chain$

2、准备创世块文件

~/eth_workspace/private_chain$vim genesis.json
  {
    "config": {
          "chainId": 10,
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0
      },
    "alloc"      : {},
    "coinbase"   : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x20000",
    "extraData"  : "",
    "gasLimit"   : "0x2fefd8",
    "nonce"      : "0x0000000000000042",
    "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp"  : "0x00"
  }

每个参数含义:

参数名称 参数描述
chainId 链类型ID,私链:10,testnet: ,mainnet:
alloc 预置账号以及对应以太币数量,因为私有链挖矿容易,故不需要预置有币的账号,需要的时候自己创建即可以
coinbase 矿工的账号,随便填
difficulty 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
extraData 附加信息,随便填,可以填你的个性信息
gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大
nonce nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
mixhash 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
parentHash 上一个区块的hash值(父hash),因为是创世块,所以这个值是0
timestamp 设置创世块的时间戳

3、初始化创世区块

命令:geth init ./genesis.json --datadir ./data/

~/eth_workspace/private_chain$./../../go/src/github.com/ethereum/go-ethereum/build/bin/geth init ./genesis.json  --datadir ./data/
INFO [11-08|17:22:05.173] Maximum peer count                       ETH=25 LES=0 total=25
keydir=/Users/wujinquan/eth_workspace/private_chain/data/keystore
INFO [11-08|17:22:05.185] Allocated cache and file handles         database=/Users/wujinquan/eth_workspace/private_chain/data/geth/chaindata cache=16 handles=16
INFO [11-08|17:22:05.188] Writing custom genesis block
INFO [11-08|17:22:05.189] Persisted trie from memory database      nodes=0 size=0.00B time=13.017µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-08|17:22:05.189] Successfully wrote genesis state         database=chaindata                                                        hash=5e1fc7…d790e0
INFO [11-08|17:22:05.189] Allocated cache and file handles         database=/Users/wujinquan/eth_workspace/private_chain/data/geth/lightchaindata cache=16 handles=16
INFO [11-08|17:22:05.192] Writing custom genesis block
INFO [11-08|17:22:05.192] Persisted trie from memory database      nodes=0 size=0.00B time=4.084µs  gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [11-08|17:22:05.192] Successfully wrote genesis state         database=lightchaindata                                                        hash=5e1fc7…d790e0
~/eth_workspace/private_chain$

初始化之后自动生成data文件夹

~/eth_workspace/private_chain$tree
.
├── data
│   ├── geth
│   │   ├── chaindata
│   │   │   ├── 000001.log
│   │   │   ├── CURRENT
│   │   │   ├── LOCK
│   │   │   ├── LOG
│   │   │   └── MANIFEST-000000
│   │   └── lightchaindata
│   │       ├── 000001.log
│   │       ├── CURRENT
│   │       ├── LOCK
│   │       ├── LOG
│   │       └── MANIFEST-000000
│   └── keystore
└── genesis.json

5 directories, 11 files
~/eth_workspace/private_chain$

4、启动私有链

命令:--rpc 启动rpc服务器,具体参数用 geth -h查看
geth --datadir "/Users/wujinquan/eth_workspace/private_chain/data" --networkid 314590 --rpc console 2>> ./debug.log

~/eth_workspace/private_chain$./../../go/src/github.com/ethereum/go-ethereum/build/bin/geth --datadir "/Users/wujinquan/eth_workspace/private_chain/data" --networkid 314590  --rpc console 2>> ./debug.log
keydir=/Users/wujinquan/eth_workspace/private_chain/data/keystore
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.17-unstable-11d0ff65/darwin-amd64/go1.10.3
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

查看log日志

~/eth_workspace/private_chain$rm debug.log
~/eth_workspace/private_chain$cat debug.log
INFO [11-08|17:40:20.112] Maximum peer count                       ETH=25 LES=0 total=25
INFO [11-08|17:40:20.138] Starting peer-to-peer node               instance=Geth/v1.8.17-unstable-11d0ff65/darwin-amd64/go1.10.3
INFO [11-08|17:40:20.138] Allocated cache and file handles         database=/Users/wujinquan/eth_workspace/private_chain/data/geth/chaindata cache=768 handles=1024
INFO [11-08|17:40:20.155] Initialised chain configuration          config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [11-08|17:40:20.155] Disk storage enabled for ethash caches   dir=/Users/wujinquan/eth_workspace/private_chain/data/geth/ethash count=3
INFO [11-08|17:40:20.156] Disk storage enabled for ethash DAGs     dir=/Users/wujinquan/.ethash                                      count=2
INFO [11-08|17:40:20.156] Initialising Ethereum protocol           versions="[63 62]" network=314590
INFO [11-08|17:40:20.156] Loaded most recent local header          number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.156] Loaded most recent local full block      number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.156] Loaded most recent local fast block      number=0 hash=5e1fc7…d790e0 td=131072 age=49y6mo3w
INFO [11-08|17:40:20.157] Loaded local transaction journal         transactions=0 dropped=0
INFO [11-08|17:40:20.157] Regenerated local transaction journal    transactions=0 accounts=0
INFO [11-08|17:40:20.157] Starting P2P networking
INFO [11-08|17:40:22.281] UDP listener up                          self=enode://d73c11c71cc49ec1f435df321fe2616c35203154ad37debb1287c0a3657e233312d6dafe59669cb75ff7b83ed5d587a2b0e2124607fe02d1a27025b0844d64c2@[::]:30303
INFO [11-08|17:40:22.281] RLPx listener up                         self=enode://d73c11c71cc49ec1f435df321fe2616c35203154ad37debb1287c0a3657e233312d6dafe59669cb75ff7b83ed5d587a2b0e2124607fe02d1a27025b0844d64c2@[::]:30303
INFO [11-08|17:40:22.283] IPC endpoint opened                      url=/Users/wujinquan/eth_workspace/private_chain/data/geth.ipc
INFO [11-08|17:40:22.283] HTTP endpoint opened                     url=http://127.0.0.1:8545                                      cors= vhosts=localhost
~/eth_workspace/private_chain$

三、启动挖矿

现在私有网络就搭建成功,下面就可以在这个刚刚搭建出来的私有以太坊网络中执行挖矿操作了,

挖矿首先必须有一个账户,输入下面的命令,查看当前节点中的所有账户:

> eth.accounts
[]
#输出了一个"[]",说明毛都么有一根,更别说账户了.既然没有,那就来创建一个,继续输入命令:
> personal.newAccount("12345678")
"0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6"   //此处成功创建一个地址,默认第一个地址为coinbase账户
> eth.accounts
["0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6"]
> miner.start()
null
> eth.blockNumber
109
> miner.stop()
null
> eth.getBalance("0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6")  //查询账户余额
640000000000000000000
> eth.coinbase  //查询coinbase账户,
"0xa1430e46d5da67f60f5b0c2bb817a6ac5a2f31d6"

查看log,可看到区块hash,区块高度等信息

NFO [11-08|17:48:09.409] Updated mining threads                   threads=8
INFO [11-08|17:48:09.409] Transaction pool price threshold updated price=1000000000
INFO [11-08|17:48:09.409] Etherbase automatically configured       address=0xa1430e46D5da67F60f5B0c2bb817A6aC5A2F31d6
INFO [11-08|17:48:09.409] Commit new mining work                   number=1 sealhash=f3fa96…83a0f8 uncles=0 txs=0 gas=0 fees=0 elapsed=157.722µs
INFO [11-08|17:48:10.752] Generating DAG in progress               epoch=0 percentage=0 elapsed=664.794ms
INFO [11-08|17:48:11.636] Generating DAG in progress               epoch=0 percentage=1 elapsed=1.548s
INFO [11-08|17:48:12.357] Generating DAG in progress               epoch=0 percentage=2 elapsed=2.269s
...................................这个percenage=100,也就是100%以后.,就不断的出现下面的提示.............
INFO [11-08|17:49:27.143] Generating DAG in progress               epoch=0 percentage=96 elapsed=1m17.055s
INFO [11-08|17:49:27.900] Generating DAG in progress               epoch=0 percentage=97 elapsed=1m17.812s
INFO [11-08|17:49:28.660] Generating DAG in progress               epoch=0 percentage=98 elapsed=1m18.572s
INFO [11-08|17:49:29.643] Generating DAG in progress               epoch=0 percentage=99 elapsed=1m19.555s
INFO [11-08|17:49:29.646] Generated ethash verification cache      epoch=0 elapsed=1m19.558s
INFO [11-08|17:49:30.592] Successfully sealed new block            number=1 sealhash=f3fa96…83a0f8 hash=105a28…f84f4a elapsed=1m21.182s
INFO [11-08|17:49:30.592] 🔨 mined potential block                  number=1 hash=105a28…f84f4a
INFO [11-08|17:49:30.592] Commit new mining work                   number=2 sealhash=aa8877…008b35 uncles=0 txs=0 gas=0 fees=0 elapsed=120.273µs
INFO [11-08|17:49:30.946] Successfully sealed new block            number=2 sealhash=aa8877…008b35 hash=86d9ab…7204d1 elapsed=354.259ms
INFO [11-08|17:49:30.946] 🔨 mined potential block                  number=2 hash=86d9ab…7204d1
INFO [11-08|17:49:30.947] Commit new mining work                   number=3 sealhash=03689c…72e9ad uncles=0 txs=0 gas=0 fees=0 elapsed=126.99µs
INFO [11-08|17:49:30.969] Successfully sealed new block            number=3 sealhash=03689c…72e9ad hash=1a23c4…2caa7e elapsed=22.541ms
INFO [11-08|17:49:30.969] 🔨 mined potential block                  number=3 hash=1a23c4…2caa7e
INFO [11-08|17:49:30.969] Mining too far in the future             wait=2s
INFO [11-08|17:49:32.073] Generating DAG in progress               epoch=1 percentage=0  elapsed=763.140ms
INFO [11-08|17:49:32.962] Generating DAG in progress               epoch=1 percentage=1  elapsed=1.652s
INFO [11-08|17:49:32.977] Commit new mining work                   number=4 sealhash=3c207e…34ffe9 uncles=0 txs=0 gas=0 fees=0 elapsed=2.007s
INFO [11-08|17:49:33.833] Successfully sealed new block            number=4 sealhash=3c207e…34ffe9 hash=453e88…0ad8c5 elapsed=856.617ms
INFO [11-08|17:49:33.833] 🔨 mined potential block                  number=4 hash=453e88…0ad8c5
INFO [11-08|17:49:33.845] Commit new mining work                   number=5 sealhash=d9c064…4c004c uncles=0 txs=0 gas=0 fees=0 elapsed=194.536µs
INFO [11-08|17:49:34.581] Generating DAG in progress               epoch=1 percentage=2  elapsed=3.271s
.................................好多这个提示,挖到好多矿................................
INFO [11-08|17:51:53.036] Successfully sealed new block            number=127 sealhash=928a99…83b9d5 hash=32b091…91aa9f elapsed=608.969ms
INFO [11-08|17:51:53.037] 🔗 block reached canonical chain          number=120 hash=28e4b8…8fdff1
INFO [11-08|17:51:53.037] 🔨 mined potential block                  number=127 hash=32b091…91aa9f
INFO [11-08|17:51:53.037] Commit new mining work                   number=128 sealhash=6ad711…aa321b uncles=0 txs=0 gas=0 fees=0 elapsed=126.24µs
INFO [11-08|17:51:53.634] Successfully sealed new block            number=128 sealhash=6ad711…aa321b hash=503f6e…8db68c elapsed=597.437ms
INFO [11-08|17:51:53.634] 🔗 block reached canonical chain          number=121 hash=c1f25b…f11d46
INFO [11-08|17:51:53.634] 🔨 mined potential block                  number=128 hash=503f6e…8db68c

四、至此以太坊的私有节点搭建完成,继续讲解用Postman于ETH的RPC接口进行通信

(1) 以太坊rpc接口文档扩展接口

(2) 用postman工具查询rpc常用的方法

在启动私有节点时我们已经知道rpc端口为默认的8545

五、总结

以太坊私链搭建难度总体不大,需要有一定的软件相关经验。截止目前区块链研究人员已经不少,过程中遇到问题稍加搜索即可解决

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • 本文是对以太坊文档 Ethereum Frontier Guide 和 Ethereum Homestead 的整...
    趁风卷阅读 9,475评论 0 16
  • 本文的安装环境是在ubuntu 16.04 位上。假定你已经搭建好了ubuntu环境。 第一部分:下载Ethere...
    大贤笃志阅读 1,655评论 1 2
  • 5月21日,FCOIN交易所上线了beta交易,此后第三天,上线了FCION对ETH的交易对。依靠分红机制吸引投资...
    阿拉伯爵阅读 743评论 2 0
  • 收拾家务,可以带给人很愉快的心情。看着家里一点一点变得干净起来,内心总会有莫名的成就感和满足感。也许女人天生...
    水晶丫头阅读 247评论 0 0
  • 季羡林老先生我一直很佩服,从看他谈写作到看他的日记,真是一片天真毫无谎言。喜欢的很,那种俗气不做作,才当真配的上大...
    杰米酱阅读 358评论 0 0