https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu0 N6 |# T8 B+ Y% _1 I
- sudo apt-get install software-properties-common
- sudo add-apt-repository -y ppa:ethereum/ethereum# I. [0 U/ l k6 ^- h0 G* d
- sudo apt-get update3 H- N! M! H. A5 O* ]
- sudo apt-get install ethereum
初始化创世区块) O# b- r! Z. S. [
https://github.com/ethereum/go-ethereum
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value
- cd home
- mkdir blockChain&& cd blockChain
- vim genesis.json; v, M2 Q7 E; a, E6 [
- //摘自官网
- {) T+ ~" Y% _' o+ f5 T& [
- "config": {! y3 S u; L; \4 e6 \' _8 p
- "chainId": 14,
- "homesteadBlock": 0,$ ?9 E5 l5 W8 F/ H) S; v$ t
- "eip155Block": 0,
- "eip158Block": 0! l, A9 R& {- L9 y3 V
- },9 G0 a6 ?( |' w v3 b) W5 ^; \% b- O
- "alloc" : {},
- "coinbase" : "0x0000000000000000000000000000000000000000",6 ?1 V# P) e- p3 C. M7 b7 P* {
- "difficulty" : "0x05000",
- "extraData" : "",& ^+ K- I+ D( v3 c0 R+ r
- "gasLimit" : "0x2fefd8",3 j# A, p& T [) _) I; v
- "nonce" : "0x0000000000000042",
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",' o9 e, }. A, Y( E2 Y9 c- C, s
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",% Q! l4 N* A5 u* L( d0 R. w
- "timestamp" : "0x00"
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。
geth --datadir /home/blockChain/data/00 init genesis.json, i$ Y8 f' s+ E3 J
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console8 x/ d7 U9 ^2 G* w: ~7 m7 J
/*
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值' R8 f% `# W6 F% V6 m7 Q* l# O
--rpc 启用ipc服务,默认端口号85451 e( x. I' V; ?9 Z' w$ ^. O9 Q
--rpcapi 表示可以通过ipc调用的对象
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问
console 打开一个可交互的javascript环境5 a1 P) o) p4 w$ G& f
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options
*/4 `5 R% P0 Y$ p3 _& Y8 Q. a" W5 f
挖矿以及转账
//创建coinbase账户
personal.newAccount("coinbase")
//查看账户列表& c6 F0 f& O4 m( D4 e
eth.accounts
//开始挖矿
miner.start()- k- Y' t/ C. G3 D5 }# }
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务
geth attach ipc:/home/blockChain/data/00/geth.ipc
//查看coinbase账户余额
baseAccount = eth.accounts[0]
num = eth.getBalance(baseAccount)& h2 p* m+ V1 D. o3 i# A
//换算单位为eth
web3.fromWei(num): e- a7 {! L: i, T) X4 ^+ m
//新建一个账户) U4 \ ^' ]" [
personal.newAccount("account")0 x" i# s; h2 y( f
//查看新建账户的余额
account1 = eth.accounts[1]) i* N7 n6 n4 O2 A4 S' C$ q
eth.getBalance(account1)
//从coinbase账户转给account1账户1eth! o' q9 _: J8 q' R2 z% e j2 T4 P0 D
personal.unlockAccount(baseAccount ,"coinbase"): S, h! {4 i5 l$ U. L" U6 z
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})$ R6 g8 i: {1 j
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额
eth.getBalance(account1)
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。5 ~% ]0 q$ O5 j' G9 F0 l& {6 `
通过mist客户端访问私链
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。
然后打开cmd,并切换到mist目录,执行
mist --rpc http://ip_address:port" a9 P# F. P! p# a
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.