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* Q# y; [' i, t: }4 F8 L
- sudo apt-get update% S7 a9 y; V" K# S
- 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
- //摘自官网
- {
- "config": {0 @3 d. g; j* J. c
- "chainId": 14,
- "homesteadBlock": 0,# j+ I ]; A# ^ G
- "eip155Block": 0,
- "eip158Block": 03 y2 n' w( x8 }: q, ~& A8 M5 U8 c
- },
- "alloc" : {},
- "coinbase" : "0x0000000000000000000000000000000000000000",- P8 G2 w, d- N' I# [4 s/ i( B- |
- "difficulty" : "0x05000",
- "extraData" : "",
- "gasLimit" : "0x2fefd8",
- "nonce" : "0x0000000000000042",, W9 D* Q! S; c# S# I; {
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",* z, C6 B/ w$ l9 Q/ ?# y) _& i5 F; S
- "timestamp" : "0x00"8 C- J9 Q8 u5 K: l2 ~- |
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。! h0 L. x: D1 q; x% R3 C
geth --datadir /home/blockChain/data/00 init genesis.json0 Q3 \) A% p4 _3 e3 o+ Y! y7 }
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console6 F" M6 B. y- r Q/ k1 E
/*4 Z- M7 k) ^# n E
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络7 e+ l/ W' w1 w/ X' M( `& }
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值
--rpc 启用ipc服务,默认端口号85456 R; D2 n! [8 ~8 \0 Y+ A5 Z& V
--rpcapi 表示可以通过ipc调用的对象- N/ |( E4 ~+ S) J. T3 l+ T
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问1 V& A; M0 r6 Z
console 打开一个可交互的javascript环境
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options/ z5 A+ C3 k) ?, n K9 ` R o' }
*/0 w) A }% [& g& v
挖矿以及转账
//创建coinbase账户" }! h! @; w, V
personal.newAccount("coinbase"), l/ r4 T; P' W2 h
//查看账户列表 b. \6 [# Q: f) K
eth.accounts
//开始挖矿
miner.start()3 L/ |9 @; t' }3 c. N) S2 e+ I( ?
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务+ w. e$ q+ J+ L
geth attach ipc:/home/blockChain/data/00/geth.ipc8 ?# z# w) ?9 O+ J" l4 n
//查看coinbase账户余额
baseAccount = eth.accounts[0]6 {1 U& T8 ~4 G, Z" v7 E
num = eth.getBalance(baseAccount)8 u; X& G, H$ | {( {) I$ }" P/ a/ L0 h
//换算单位为eth! s9 w# X7 L6 N& ?7 |# u
web3.fromWei(num)
//新建一个账户4 p d4 x2 w! D3 j" R) `
personal.newAccount("account")& W, t" r) J& ]3 G7 w3 D" ]0 q0 P
//查看新建账户的余额/ a4 a: e7 [/ Q) V7 C6 @3 S
account1 = eth.accounts[1]
eth.getBalance(account1), P; X$ C( i* C! n8 x0 o
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")3 j; S$ M( X* l* E" J# b- M6 N
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})3 A* Z; [* M5 [ `" U4 x, E+ m5 j
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额( X# n0 m& r8 a$ q% Z2 |. ?
eth.getBalance(account1)7 k- ~: u R0 X: k) I$ j
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。
通过mist客户端访问私链( M O4 F, J( p9 \! h9 }+ @* ^# A' f
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror9 M: n; R, e9 ]1 Y" e2 d
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。8 o0 e3 E I! Z
然后打开cmd,并切换到mist目录,执行
mist --rpc http://ip_address:port
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.