https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu
- sudo apt-get install software-properties-common
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update: J; r: m3 J; J' @1 B2 N# t
- sudo apt-get install ethereum
初始化创世区块
https://github.com/ethereum/go-ethereum
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value
- cd home
- mkdir blockChain&& cd blockChain
- vim genesis.json
- //摘自官网5 c0 g% G& J3 M; A- j4 A2 m
- {& B5 g U# h' u6 e! G! S) p
- "config": {# j; w5 t+ B& W: A
- "chainId": 14,
- "homesteadBlock": 0,# D" e# @2 v, K# b
- "eip155Block": 0,2 m: G% d, T9 c0 d4 w& ?. L
- "eip158Block": 0
- }, D5 k: K4 \, j2 T4 {
- "alloc" : {},- F# h1 g6 j7 P
- "coinbase" : "0x0000000000000000000000000000000000000000",
- "difficulty" : "0x05000",9 k1 C" j/ _; }2 _! B: q0 f
- "extraData" : "",3 m7 V& n# n4 Y
- "gasLimit" : "0x2fefd8",# Z% f" ^( H/ a. u0 V" Z5 D
- "nonce" : "0x0000000000000042",/ j0 y- k, e3 g9 I
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", K) I) o, A ^. I
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "timestamp" : "0x00"7 A% W& w3 N c' v. D/ ]) C1 q g
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value. p1 v4 S( A) ?" K9 u. T$ T
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。, F. o9 S1 l8 N: |1 z
geth --datadir /home/blockChain/data/00 init genesis.json
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console
/*
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络! t3 ]1 S$ N- s& l" [3 g8 A
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值/ e7 Y3 |" c9 i5 g1 D, C( n
--rpc 启用ipc服务,默认端口号8545
--rpcapi 表示可以通过ipc调用的对象. ?) ~2 d2 [ h4 D% x( ^
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问- S' j* v, i9 ~
console 打开一个可交互的javascript环境7 I* D4 h. j/ S. {( k4 Z
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options( _- O: D( m: V% L0 F
*/
挖矿以及转账 T2 E8 @# i8 J2 }6 K
//创建coinbase账户 A5 t7 n5 M' ?! p
personal.newAccount("coinbase")3 |9 O$ x6 d% T" s( h& ^5 b' J
//查看账户列表
eth.accounts
//开始挖矿
miner.start()
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务
geth attach ipc:/home/blockChain/data/00/geth.ipc4 c) r+ t0 ~. a) d& e
//查看coinbase账户余额
baseAccount = eth.accounts[0]0 c( n) C) r0 }$ i( C
num = eth.getBalance(baseAccount)
//换算单位为eth% G1 ]4 f, k2 \3 {1 z8 H
web3.fromWei(num)
//新建一个账户
personal.newAccount("account")$ b! k* T1 Q# D( K5 }2 C
//查看新建账户的余额
account1 = eth.accounts[1]' Y1 i9 q! ]" S2 v/ W
eth.getBalance(account1)# S# A" N% L- b+ V. G" Y- P
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})9 c7 D$ |- |1 _* P0 `8 o
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额
eth.getBalance(account1)
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。4 [' n$ n* ^# |1 ^1 R* G3 y
5 n/ E* z" |4 j5 }4 M! {2 ?3 _% J
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。5 _' _- i) Y2 h( M: q |: K; W
通过mist客户端访问私链$ t1 ~) J' {( I6 V* w
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。
然后打开cmd,并切换到mist目录,执行* G. k8 I5 Y) ?$ C
mist --rpc http://ip_address:port: W4 D. ~; B. O9 v
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.