https://github.com/ethereum/go-ethereum/wiki
http://book.8btc.com/books/6/ethereum/_book/public-chain-chain.html
https://www.cnblogs.com/zl03jsj/p/6876064.html
安装客户端
yum -y install golang
go version
> go version go1.8.3 linux/amd64
wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.7.3-4bb3c89d.tar.gz
tar xvfz geth-alltools-linux-amd64-1.7.3-4bb3c89d.tar.gz
export PATH=$PATH:/root/geth-alltools-linux-amd64-1.7.3-4bb3c89d
创始块初始化文件genesis.json
{
"config": {
"chainId": 123,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000033",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000",
"difficulty": "0x100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
#参数名称 参数描述
#mixhash 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
#nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
#difficulty 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
#alloc 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。
#coinbase 矿工的账号,随便填
#timestamp 设置创世块的时间戳
#parentHash 上一个区块的hash值,因为是创世块,所以这个值是0
#extraData 附加信息,随便填,可以填你的个性信息
#gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。
私有链节点初始化
mkdir /opt/blockchain
geth --datadir /opt/blockchain init genesis.json
控制台方式连接私有链
geth --networkid 123 --datadir /opt/blockchain console
建立矿工帐号
# 第1个建立的帐号默认为矿工帐号
> personal.newAccount("me")
"0xdd3381d3d81593bcc4440a634fdf964e19744ed9"
节点默认矿工帐号
> eth.coinbase
Error: etherbase address must be explicitly specified
at web3.js:3143:20
at web3.js:6347:15
at get (web3.js:6247:38)
at <unknown>
建立以后就有了
> eth.coinbase
"0xdd3381d3d81593bcc4440a634fdf964e19744ed9"
后台启动
nohup geth --networkid 123 --datadir /opt/blockchain --mine --nodiscover --rpc --rpcaddr 0.0.0.0 --rpcapi "db,eth,net,web3,admin,personal" --ipcpath /opt/blockchain/geth.ipc &
以太坊钱包连接
/opt/Ethereum Wallet/ethereumwallet --rpc /opt/blockchain/geth.ipc
连接到以太坊私链
geth attach /opt/blockchain/geth.ipc
geth attach http://127.0.0.1:8545
挖矿
miner.start(1)
矿工的以太WEI
> eth.getBalance(eth.coinbase)
2.031e+22
> web3.fromWei(eth.getBalance(eth.coinbase)) # 转成以太币
当前节点值
> admin.nodeInfo.enode
"enode://7ea7808369eef9718e4490b3913bbacc0176f88fe0d8ad6085ca3e614b30eb244a70aa226015aa8cfad58938915946dcefc78ef960f0447c526e221ac62077b2@[::]:30303?discport=0"
当前块号
> eth.blockNumber
255
当前块内容
> eth.getBlock(255)
多节点配置(开发环境的话,1个节点上建立多个帐户即可)
必须使用同样创始块初始化文件genesis.json创建节点,参考前面的操作。
节点1 192.168.48.111
节点2 192.168.48.112
节点3 192.168.48.113
节点4 192.168.48.114
# 查询节点1信息
> admin.nodeInfo.enode or admin.nodeInfo
"enode://9d12900c5b7105d6db5769bacbf16e02574e19f8a8db4d16c8fad2b10a17c566f09e81619c298e3d4bbca0f0f5476ee3ef60e16a2f8cd3fe323ea884025d3fe8@[::]:30303?discport=0"
# 节点2, 3, 4分别添加节点1
# 节点1信息中的 [::] 需要替换为 ip地址 , 使用同一个 networkid
> personal.newAccount("he")
> personal.newAccount("she")
> personal.newAccount("it")
> admin.addPeer("enode://9d12900c5b7105d6db5769bacbf16e02574e19f8a8db4d16c8fad2b10a17c566f09e81619c298e3d4bbca0f0f5476ee3ef60e16a2f8cd3fe323ea884025d3fe8@192.168.48.111:30303?discport=0")
true
#随后自动同步区位块
> INFO [02-09|10:55:42] Block synchronisation started
INFO [02-09|10:55:42] Imported new state entries count=1 elapsed=117.431µs processed=1 pending=0 retry=0 duplicate=0 unexpected=0
INFO [02-09|10:55:45] Imported new block headers count=192 elapsed=2.803s number=192 hash=0a09dc…8adbb1 ignored=0
INFO [02-09|10:55:45] Imported new block receipts count=192 elapsed=3.552ms bytes=768 number=192 hash=0a09dc…8adbb1 ignored=0
INFO [02-09|10:55:45] Imported new block headers count=192 elapsed=106.312ms number=384 hash=a976d1…9df3ae ignored=0
INFO [02-09|10:55:45] Imported new block receipts count=192 elapsed=3.790ms bytes=768 number=384 hash=a976d1…9df3ae ignored=0
INFO [02-09|10:55:46] Imported new block headers count=2048 elapsed=1.262s number=2432 hash=cc5639…98edb1 ignored=0
INFO [02-09|10:55:47] Imported new block receipts count=2048 elapsed=96.143ms bytes=8192 number=2432 hash=cc5639…98edb1 ignored=0
INFO [02-09|10:55:48] Generating ethash verification cache epoch=1 percentage=67 elapsed=3.080s
INFO [02-09|10:55:49] Generated ethash verification cache epoch=1 elapsed=4.515s
INFO [02-09|10:55:59] Imported new block headers count=1792 elapsed=12.194s number=4224 hash=34b889…a9ffca ignored=0
INFO [02-09|10:55:59] Imported new block receipts count=1514 elapsed=79.429ms bytes=6056 number=3946 hash=37d0fc…7484d9 ignored=0
INFO [02-09|10:55:59] Imported new state entries count=1 elapsed=69.436µs processed=2 pending=0 retry=0 duplicate=0 unexpected=0
INFO [02-09|10:55:59] Imported new block receipts count=1 elapsed=102.927µs bytes=4 number=3947 hash=96d4bd…b936ca ignored=0
INFO [02-09|10:55:59] Committed new head block number=3947 hash=96d4bd…b936ca
INFO [02-09|10:55:59] Imported new chain segment blocks=277 txs=0 mgas=0.000 elapsed=239.845ms mgasps=0.000 number=4224 hash=34b889…a9ffca
INFO [02-09|10:55:59] Imported new block headers count=39 elapsed=544.628ms number=4263 hash=df5c71…ee6138 ignored=0
WARN [02-09|10:55:59] Skipping deep transaction reorg depth=4224
INFO [02-09|10:55:59] Imported new chain segment blocks=39 txs=0 mgas=0.000 elapsed=15.711ms mgasps=0.000 number=4263 hash=df5c71…ee6138
INFO [02-09|10:55:59] Fast sync complete, auto disabling
INFO [02-09|10:56:00] Imported new chain segment blocks=3 txs=0 mgas=0.000 elapsed=20.714ms mgasps=0.000 number=4266 hash=327227…dad296
INFO [02-09|10:56:04] Imported new chain segment blocks=1 txs=0 mgas=0.000 elapsed=7.450ms mgasps=0.000 number=4267 hash=759130…d8d811
# 登录节点1查看
> admin.peers
[{
caps: ["eth/63"],
id: "60823ad532b30cebf94013295d08010b657fb4a3c226fd6a3a96578bb85292f9987a4d0b3f24d92cd448dba3d083418dde368f811797c3bc966c3a9775e19553",
name: "Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2",
network: {
localAddress: "192.168.48.111:30303",
remoteAddress: "192.168.48.113:37136"
},
protocols: {
eth: {
difficulty: 2097152,
head: "0xb059eea745254c042c4736749d8cbd10d67ea4276b9e312a669e60d56992d967",
version: 63
}
}
}, {
caps: ["eth/63"],
id: "693ea792c9d1ce90c1242e0650cefcaf931de9f4f21ef5cd551047b409a45ee0654bfd769a4b8aa85e57da5aa35fdf86f950979d0e4e9b699857f86152e9d28c",
name: "Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2",
network: {
localAddress: "192.168.48.111:30303",
remoteAddress: "192.168.48.112:52756"
},
protocols: {
eth: {
difficulty: 2097152,
head: "0xb059eea745254c042c4736749d8cbd10d67ea4276b9e312a669e60d56992d967",
version: 63
}
}
}, {
caps: ["eth/63"],
id: "b8419370552fb2089d140b78be5c552a69c026b03d76ef88d443e9d5e5b48d27c07726a4e7ad18820426a7d881c82ac94f3d752329b7942d728d87b7c666ddbc",
name: "Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.2",
network: {
localAddress: "192.168.48.111:30303",
remoteAddress: "192.168.48.114:44970"
},
protocols: {
eth: {
difficulty: 2097152,
head: "0xb059eea745254c042c4736749d8cbd10d67ea4276b9e312a669e60d56992d967",
version: 63
}
}
}]
# 这种控制台添加节点方式,一旦退出控制台就失效,所以使用命令方式添加
nohup geth --networkid 123 --datadir /opt/blockchain --identity "TestChain_112" --mine --port 30303 --nodiscover --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "db,eth,net,web3" --bootnodes "enode://9d12900c5b7105d6db5769bacbf16e02574e19f8a8db4d16c8fad2b10a17c566f09e81619c298e3d4bbca0f0f5476ee3ef60e16a2f8cd3fe323ea884025d3fe8@192.168.48.111:30303" &
nohup geth --networkid 123 --datadir /opt/blockchain --identity "TestChain_113" --mine --port 30303 --nodiscover --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "db,eth,net,web3" --bootnodes "enode://9d12900c5b7105d6db5769bacbf16e02574e19f8a8db4d16c8fad2b10a17c566f09e81619c298e3d4bbca0f0f5476ee3ef60e16a2f8cd3fe323ea884025d3fe8@192.168.48.111:30303" &
nohup geth --networkid 123 --datadir /opt/blockchain --identity "TestChain_114" --mine --port 30303 --nodiscover --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi "db,eth,net,web3" --bootnodes "enode://9d12900c5b7105d6db5769bacbf16e02574e19f8a8db4d16c8fad2b10a17c566f09e81619c298e3d4bbca0f0f5476ee3ef60e16a2f8cd3fe323ea884025d3fe8@192.168.48.111:30303" &