https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu1 D; U4 j! c& @* d& ^+ O
- sudo apt-get install software-properties-common/ m! m, J2 `# h- C, J6 _8 B
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update
- 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# r. S- C/ Q2 V' R
- vim genesis.json
- //摘自官网
- {
- "config": {
- "chainId": 14,
- "homesteadBlock": 0,1 J; k, E4 W X# |$ `1 K# P
- "eip155Block": 0,. m# b7 {5 T& k: G% D9 N
- "eip158Block": 0# l; a. I' Y/ d
- },
- "alloc" : {},
- "coinbase" : "0x0000000000000000000000000000000000000000",
- "difficulty" : "0x05000",1 n3 n' ~+ ?$ G+ w& ?! y$ X
- "extraData" : "",. c8 M. G7 `) l: T
- "gasLimit" : "0x2fefd8",! ]# `0 B8 G% ^: K
- "nonce" : "0x0000000000000042",
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",$ C$ Y, W2 q/ c. b
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "timestamp" : "0x00"
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。
geth --datadir /home/blockChain/data/00 init genesis.json! x) }/ o7 K! E
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console
/*6 m+ V" d# y& n% X/ ]
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值& q8 ~" w/ X: M, m- D- G
--rpc 启用ipc服务,默认端口号8545
--rpcapi 表示可以通过ipc调用的对象, p2 b/ \9 D! O+ R% M- c' c6 S& J( q e
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问
console 打开一个可交互的javascript环境5 O* i1 R2 I: S9 \8 j
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options
*/* r* W# s. }' U& C9 J
挖矿以及转账6 B* N* @2 F* Z, C
//创建coinbase账户9 S n- w ~8 D) ^5 y
personal.newAccount("coinbase")% \! N4 E7 z% Q6 D9 Q$ S
//查看账户列表
eth.accounts
//开始挖矿
miner.start()- Z, Q" X( s& z! X# r' ~
//继续挖矿,不要停~3 J' v1 g+ g' F3 e& g! W- W& ]# I
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务
geth attach ipc:/home/blockChain/data/00/geth.ipc6 O/ x' A: q$ \4 q6 p" u: |7 ?
//查看coinbase账户余额2 U% z& {1 }; c0 o9 B8 Q" w
baseAccount = eth.accounts[0]
num = eth.getBalance(baseAccount), g6 x* U. t. Q! Z
//换算单位为eth
web3.fromWei(num)
//新建一个账户
personal.newAccount("account")
//查看新建账户的余额
account1 = eth.accounts[1]0 s3 |) H0 }$ n* ?7 O K& L
eth.getBalance(account1)- ^3 ?* K4 K' O* q/ T. A: `3 t. ^& y
//从coinbase账户转给account1账户1eth3 G {. @! b( n+ L/ m4 q
personal.unlockAccount(baseAccount ,"coinbase")
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额8 ?2 z! q' D6 [5 I* z
eth.getBalance(account1)2 ? B2 O) O+ X5 W6 v h7 N
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
3 r& S& Q0 z; A- H
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。0 @, q! Y# q: s1 i2 p
通过mist客户端访问私链4 E0 |! y! V2 N+ v
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。
然后打开cmd,并切换到mist目录,执行
mist --rpc http://ip_address:port; T* K, T/ H+ @" L8 K
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。- G0 R* c# T! I' L
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.