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
- sudo apt-get update
- sudo apt-get install ethereum
初始化创世区块3 H# g# ]4 D& O, l* ~6 A1 M/ _
https://github.com/ethereum/go-ethereum
chainId不能为0,否则交易会报错: insufficient funds for gas*price+value+ e8 }2 u5 y- p' K* ]- V
- cd home
- mkdir blockChain&& cd blockChain. r! C0 t& O9 o( c7 }7 f2 n
- vim genesis.json% T y9 ?# b, `' ^ F
- //摘自官网# B% D( K0 ~8 s4 z
- {; c& o- z' j1 s% _3 p, W
- "config": { F9 y6 f2 Y/ o3 E: [9 }& E
- "chainId": 14,( x8 s& R' D: C6 ~" R. e8 h3 }
- "homesteadBlock": 0,+ U2 Q) a$ l- q; u% H( ~
- "eip155Block": 0,
- "eip158Block": 01 a- j& r9 T$ N/ ~: l9 ~
- },
- "alloc" : {},) Z* G) I8 w: @9 y. X
- "coinbase" : "0x0000000000000000000000000000000000000000",& Y0 p @' r2 e# J: V: ~
- "difficulty" : "0x05000",$ q/ S- {4 y2 q4 q" `" i e0 [
- "extraData" : "",
- "gasLimit" : "0x2fefd8",
- "nonce" : "0x0000000000000042",
- "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
- "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",# q- g. X+ N) U$ f: C
- "timestamp" : "0x00" h5 b! F& N4 |1 D7 p3 y
- }
官方文档中的chainId为0,应该修改为非0的值,不然会导致 insufficient funds for gas * price + value
difficulty表示挖矿的难度值,官方文档中为0x20000,由于测试机器配置低,修改为0x05000。
geth --datadir /home/blockChain/data/00 init genesis.json# {& `% M. F" v
geth --networkid 14 --nodiscover --datadir /home/blockChain/data/00 --rpc --rpcapi net,eth,web3,personal --rpcaddr ip_address console, H0 Y. }% z# r5 D8 I
/*' e" t: f# H6 a. N2 s; W/ E
--nodiscover 关闭p2p网络的自动发现,需要手动添加节点,这样有利于我们隐藏私有网络
--datadir 区块链数据存储目录
--networkid 网络标识,私有链取一个大于4的随意的值
--rpc 启用ipc服务,默认端口号85456 }& X! n& N: J; d0 Z+ {
--rpcapi 表示可以通过ipc调用的对象
--rpcaddr ipc监听地址,默认为127.0.0.1,只能本地访问% t6 v, e/ J, Q% h9 j* I3 x
console 打开一个可交互的javascript环境 c9 V, V a/ x U. M: b! S4 z$ Z& _
更多参数:https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options
*/
挖矿以及转账
//创建coinbase账户
personal.newAccount("coinbase")
//查看账户列表- ]% E! J ^6 V2 N% Q
eth.accounts
//开始挖矿- D S9 d6 P; h7 A
miner.start()
//继续挖矿,不要停~
挖矿一段时间之后,再打开一个shell通过ipc连接到刚才的服务
geth attach ipc:/home/blockChain/data/00/geth.ipc$ X: K! ?8 m2 v7 [, P; G# C
//查看coinbase账户余额
baseAccount = eth.accounts[0]
num = eth.getBalance(baseAccount)* C) C' h9 O) r
//换算单位为eth
web3.fromWei(num)
//新建一个账户7 K: Z3 `: l) _1 }" a) r
personal.newAccount("account")$ O. O5 m2 R2 Y6 d5 ^6 j6 w1 v
//查看新建账户的余额) y; |* G* E( e' w: N4 O& R, R
account1 = eth.accounts[1]
eth.getBalance(account1)
//从coinbase账户转给account1账户1eth
personal.unlockAccount(baseAccount ,"coinbase")
eth.sendTransaction({from : baseAccount, to : account1 , value : web3.toWei(1,"ether")})3 @( r: E7 l, b
//观察挖矿shell的输出,等待交易打包完成后,查询account1的账户余额3 M: @- n% d# k r( z4 c
eth.getBalance(account1)/ |* o$ R* A; R1 ]0 V* s9 S- k" n
提交转账之后,观察挖矿shell的输出,会出现一条打包转账交易的记录,等待新的区块继续生成的时候,account1 账户的余额才会更新。
如果交易很久之后才能被确认,很有可能是服务器性能不足导致的。
通过mist客户端访问私链5 {, i( T0 Y# P# v
从以太坊爱好者或者官网,下载mist客户端:http://ethfans.org/wikis/Mist-Mirror
安装完成之后,找到mist安装目录 C:\Program Files\Mist,双击mist启动。
然后打开cmd,并切换到mist目录,执行9 C0 N% p8 h9 k- p! ` |: X0 V& w
mist --rpc http://ip_address:port) {; \: m2 V$ R! m! p- e
其中,ip_address是私链的服务器地址,port是该节点运行的端口号,默认为8545。' \+ D% _8 k9 z
如果不先运行一个mist程序,直接通过命令行连接私链的话,会报错 Couldn’t start swarm process.