https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu* H9 n6 T) g6 `# v& E) ` B3 I! M
- sudo apt-get install software-properties-common1 j. t' s' ?) W3 A
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update& F7 n; E5 I- e( n$ |# F& r
- sudo apt-get install ethereum
初始化创世区块
https://github.com/ethereum/go-ethereum$ g( D. l1 u6 W; {1 {( O! L
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value! q' _! |3 s8 U
- cd home
- mkdir blockChain&& cd blockChain; K9 {8 [0 V e1 Z6 ~/ B! s8 M
- vim genesis.json
- //摘自官网
- {
- "config": {
- "chainId": 14,! t( J* k- X. }) r; N+ ?) A; J
- "homesteadBlock": 0,
- "eip155Block": 0,- b4 A* j4 Q4 G! ^0 w
- "eip158Block": 02 f+ ?2 d, O. I! |9 O, h. }
- },$ T* ?0 z7 K& Z& a/ m) h5 Q& @
- "alloc" : {},% a" ~" Y5 I' e9 _, J! k
- "coinbase" : "0x0000000000000000000000000000000000000000",/ D+ F. A3 Y! W! R
- "difficulty" : "0x05000",3 ?3 w* q2 M; v9 t
- "extraData" : "",
- "gasLimit" : "0x2fefd8",
- "nonce" : "0x0000000000000042",# ]0 g9 A3 p8 H
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "timestamp" : "0x00"6 \: Q; v) y& e1 w4 v
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value- ^ P/ n8 w: f# o) Z4 _
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。$ Q0 c4 K( d T/ D* i( }% {
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 console5 L0 R- C' [% b' i
/*, Y f$ M0 y3 O) M0 I! V
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络+ |# M7 r2 f/ Z
--datadir 区块链数据存储目录+ {# B/ B, v3 e$ ?
--networkid 网络标识,私有链取一个大于4的随意的值' ]9 T$ U- L D% s I5 W: l+ \
--rpc 启用ipc服务,默认端口号8545
--rpcapi 表示可以通过ipc调用的对象
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问
console 打开一个可交互的javascript环境0 D/ K- I8 q, k6 [# O% b/ Q
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options- A1 N$ O' F5 z# P/ n
*/: S, H1 J+ r, r8 Y. p V8 H
挖矿以及转账
//创建coinbase账户
personal.newAccount("coinbase")
//查看账户列表! C: A0 l0 g' m3 {( {
eth.accounts: s4 s- L z% U
//开始挖矿
miner.start()3 P$ U- T& ?2 ^( j5 @% T
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务
geth attach ipc:/home/blockChain/data/00/geth.ipc, k2 q$ X# u% G) {
//查看coinbase账户余额
baseAccount = eth.accounts[0]
num = eth.getBalance(baseAccount)
//换算单位为eth
web3.fromWei(num): o" a6 B4 o* _3 ]
//新建一个账户. m2 ~7 I' f9 C! W
personal.newAccount("account")
//查看新建账户的余额
account1 = eth.accounts[1]# }, i& T7 O2 G& ~
eth.getBalance(account1)
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})& D* m, ^9 e6 W" m9 o
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额! M! A% f c; a8 e# y
eth.getBalance(account1)
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。: k4 h2 E' W3 X7 y4 H! E
8 w9 Z; G8 K3 d F& Y3 r8 Q
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。
通过mist客户端访问私链4 c9 C( S3 R( p( Z* O
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。) t+ Z! r; i) o. V t0 S
然后打开cmd,并切换到mist目录,执行" |" s/ o/ _9 E- E& U! n
mist --rpc http://ip_address:port
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。3 r u# r5 D5 x: ^
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.