https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu8 c! t, e! s& K" k) g
- sudo apt-get install software-properties-common ?& j( d0 [$ @9 f5 ?% N# U2 C
- sudo add-apt-repository -y ppa:ethereum/ethereum/ E1 K+ n# q f* d) ~
- sudo apt-get update
- sudo apt-get install ethereum
初始化创世区块. ]1 f4 Y9 A0 j/ W5 Q* h
https://github.com/ethereum/go-ethereum* L$ k# F4 c# ^" s6 b5 U" Y& l' C
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value( ]; d) \1 V) S' w7 }
- cd home; S2 f0 ^3 k9 A. _2 v+ S/ n# j
- mkdir blockChain&& cd blockChain( _7 K4 i8 E/ D" }7 j7 ?
- vim genesis.json
- //摘自官网0 j2 Q0 v% [. B$ J* X( z
- {0 `2 ]- ^. z: I! a0 ^3 Y
- "config": {
- "chainId": 14,
- "homesteadBlock": 0,
- "eip155Block": 0,9 x S; ~) h& c4 z" R4 G# A
- "eip158Block": 0
- },
- "alloc" : {},+ m0 k, o' d- V9 a
- "coinbase" : "0x0000000000000000000000000000000000000000",& h! k8 T; {' t! C0 V8 l2 j1 l
- "difficulty" : "0x05000",1 p) m5 F% Z8 |. f& m
- "extraData" : "",
- "gasLimit" : "0x2fefd8",( m7 }& a9 r; X
- "nonce" : "0x0000000000000042",
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",5 [# z4 M/ z" ~0 @# j j4 \/ J
- "timestamp" : "0x00"- _6 V7 G- u( A# o9 f# [% B$ e _
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。+ w2 E1 F( }! \1 o7 x5 s6 S
geth --datadir /home/blockChain/data/00 init genesis.json( x+ o2 h7 q, u! y
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console+ n4 c: a8 `+ Q- {: h; s1 _
/*2 ^ s. W: ?; m) w! S
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值( U) ]; T( N+ a- t- S
--rpc 启用ipc服务,默认端口号8545
--rpcapi 表示可以通过ipc调用的对象4 [$ N' |# y t- R. i" J3 C" z
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问/ e2 ~# R4 i& u
console 打开一个可交互的javascript环境3 w% L) h. v+ r+ C4 R
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options
*/
挖矿以及转账" A5 L7 G" l) E V) a4 X' o/ ?6 g8 u
//创建coinbase账户
personal.newAccount("coinbase"), I: M) |' G4 Y6 G0 P
//查看账户列表
eth.accounts2 I* v' t0 U* U( q$ o+ d
//开始挖矿/ n8 U' b" O! H' w. A# ]5 `
miner.start()# c: R; T- \+ S& ]
//继续挖矿,不要停~ E$ E: B ~) g0 @/ {
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务, l; P3 O! o: {1 E* o
geth attach ipc:/home/blockChain/data/00/geth.ipc8 j# W, P& H8 W8 v" k
//查看coinbase账户余额
baseAccount = eth.accounts[0]
num = eth.getBalance(baseAccount)
//换算单位为eth" H3 f- r4 Q" U& m2 `% U3 l
web3.fromWei(num)2 y3 X/ |: S. u5 ^9 F: C8 u7 |
//新建一个账户
personal.newAccount("account")
//查看新建账户的余额4 ?( G G' o2 L( g
account1 = eth.accounts[1] P: d3 W1 n5 p" g7 q
eth.getBalance(account1)
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})! h, f0 }" j2 ?- M- D% |
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额9 W: o% c- i9 r5 z. i
eth.getBalance(account1)
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
x3 ?* z. O. [ i# \: {9 V5 O
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。5 j' r+ H# x* V6 _0 B
通过mist客户端访问私链9 B4 ~% E( ^) V: F, h& b0 C
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror+ r- t. f I4 r/ V7 S
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。
然后打开cmd,并切换到mist目录,执行
mist --rpc http://ip_address:port
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。$ l+ R6 o4 p4 p, {; O
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.



