https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu( t) e7 Z! Z3 A/ U
- sudo apt-get install software-properties-common$ x8 x- y+ Z2 a& n3 T5 G5 I
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update
- sudo apt-get install ethereum
初始化创世区块
https://github.com/ethereum/go-ethereum2 w/ [7 g( [) m i
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value
- cd home
- mkdir blockChain&& cd blockChain2 O* I9 [8 M( W4 z3 F, p/ i4 x
- vim genesis.json
- //摘自官网
- {
- "config": {
- "chainId": 14,; p+ Q4 i, j! g5 I+ O+ `. E% k; W5 x
- "homesteadBlock": 0,6 F! u5 {" c& R! f( S/ V- O
- "eip155Block": 0,' R9 `' i# }4 H1 ?+ X& [! F$ ~+ J7 ?3 _* d
- "eip158Block": 0. v: c+ Y3 [& X3 w) ^$ b2 b
- },
- "alloc" : {},: Z3 [# J. Z2 G) ~6 w( a/ a7 I& Z
- "coinbase" : "0x0000000000000000000000000000000000000000",8 f" ^* h/ n5 q m
- "difficulty" : "0x05000",0 W5 S! F5 |: e. g
- "extraData" : "",- k* t* a2 F }2 ?5 I$ ?
- "gasLimit" : "0x2fefd8",( m: d E% S' c" Y6 i1 t2 q
- "nonce" : "0x0000000000000042",
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",) y+ K6 B/ `8 }" b5 h5 R0 N
- "timestamp" : "0x00"% \9 l! ^/ n. y) e. u$ P$ ^) T
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。! A( n( `, q. x# a
geth --datadir /home/blockChain/data/00 init genesis.json% g F1 J% w# Q; ~
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console
/*
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络, {6 {8 z, @" P6 H' Q6 I
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值8 ]! n8 I& h1 b& `; |0 B% t
--rpc 启用ipc服务,默认端口号85459 Q* n& v$ \# n+ X+ N. q8 ]( I7 `
--rpcapi 表示可以通过ipc调用的对象* X: X# ~/ `* d# B" ]5 w
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问' ^% j0 Z0 | Y/ |8 n* U% ?3 {
console 打开一个可交互的javascript环境
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options3 ^ Q9 k6 y. R# Q( J
*/! u, k3 v. t) N" L0 U1 ? R
挖矿以及转账
//创建coinbase账户5 `% w* _6 Y0 G0 y9 l
personal.newAccount("coinbase")
//查看账户列表5 p9 Z t6 t, i( |
eth.accounts
//开始挖矿
miner.start(); v% @1 t, R4 ^! h5 u8 b8 t( F
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务3 K" h, y- n/ T5 C& G
geth attach ipc:/home/blockChain/data/00/geth.ipc" l" J! p1 v* ]
//查看coinbase账户余额
baseAccount = eth.accounts[0]% y, V9 `# X' K2 N, j3 F9 ?
num = eth.getBalance(baseAccount). F/ Q6 ]& B" ~, j
//换算单位为eth3 } v; x/ t5 _+ j/ D. }: f* ?6 ]1 ?
web3.fromWei(num)- u) S: D. X; Z4 J5 a( m
//新建一个账户/ V4 H) }/ C" [- n+ m
personal.newAccount("account")
//查看新建账户的余额6 Y; A2 H: x, K" u h5 n$ N. l9 c
account1 = eth.accounts[1]
eth.getBalance(account1)
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")) `4 b% e4 T4 w- H _
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额' _: H" r! {+ Y" i! ~, t) m% r9 n
eth.getBalance(account1)
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
: d/ x" @5 o( v
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。
通过mist客户端访问私链
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。) S* v+ `* A5 [) O# Q- ~. r
然后打开cmd,并切换到mist目录,执行' p; a. X* k& T% B
mist --rpc http://ip_address:port! G; l0 I6 Q4 \$ |- L3 |" G
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。% y1 \! q' e W' Q( M5 r9 [% f
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.