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" &