如何安裝
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum
查看安裝好的geth版本號
wangzhen@wangzhen-VirtualBox:/opt$ geth version
Geth
Version: 1.8.4-stable
Git Commit: 2423ae01e0d2f853512eb2b46954b5ad0754a897
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.10
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.10
開始創(chuàng)建以太坊私有鏈
初始化一個創(chuàng)世區(qū)塊
初始化創(chuàng)世區(qū)塊時专执,要先創(chuàng)建一個genesis.json文件祈纯,內容如下:
genesis.json:
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : {}
}
參數名 | 參數描述 |
---|---|
mixhash | 與nonce配合用于挖礦整份,由上一個區(qū)塊的一部分生成的hash。注意他和nonce的設置需要滿足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章節(jié)所描述的條件套菜。 |
------ | -------- |
nonce | nonce就是一個64位隨機數疮胖,用于挖礦摄职,注意他和mixhash的設置需要滿足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章節(jié)所描述的條件誊役。 |
------ | -------- |
difficulty | 設置當前區(qū)塊的難度,如果難度過大谷市,cpu挖礦就很難蛔垢,這里設置較小難度 |
------ | -------- |
alloc | 用來預置賬號以及賬號的以太幣數量,因為私有鏈挖礦比較容易迫悠,所以我們不需要預置有幣的賬號鹏漆,需要的時候自己創(chuàng)建即可以。 |
------ | -------- |
coinbase | 礦工的賬號创泄,隨便填 |
------ | -------- |
timestamp | 設置創(chuàng)世塊的時間戳 |
------ | -------- |
parentHash | 上一個區(qū)塊的hash值艺玲,因為是創(chuàng)世塊,所以這個值是0 |
------ | -------- |
extraData | 附加信息鞠抑,隨便填饭聚,可以填你的個性信息 |
------ | -------- |
gasLimit | 該值設置對GAS的消耗總量限制,用來限制區(qū)塊能包含的交易信息總和搁拙,因為我們是私有鏈秒梳,所以填最大。 |
------ | -------- |
接下來箕速,我們使用geth init ./genesis.json --datadir "./chain"命令酪碘,來進行創(chuàng)世區(qū)塊的初始化,當前區(qū)塊鏈網絡數據存放的位置會保存在chain目錄中:
wangzhen@wangzhen-VirtualBox:/opt/born$ geth init ./genesis.json --datadir "./chain"
INFO [04-19|16:16:55] Maximum peer count ETH=25 LES=0 total=25
INFO [04-19|16:16:55] Allocated cache and file handles database=/opt/born/chain/geth/chaindata cache=16 handles=16
INFO [04-19|16:16:55] Writing custom genesis block
INFO [04-19|16:16:55] Persisted trie from memory database nodes=0 size=0.00B time=1.845μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-19|16:16:55] Successfully wrote genesis state database=chaindata hash=5e1fc7…d790e0
INFO [04-19|16:16:55] Allocated cache and file handles database=/opt/born/chain/geth/lightchaindata cache=16 handles=16
INFO [04-19|16:16:55] Writing custom genesis block
INFO [04-19|16:16:55] Persisted trie from memory database nodes=0 size=0.00B time=3.16μs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-19|16:16:55] Successfully wrote genesis state database=lightchaindata hash=5e1fc7…d790e0
啟用私有鏈
geth \
--datadir "./chain" \
--nodiscover \
console 2>>eth_output.log
啟動后的效果如下:
wangzhen@wangzhen-VirtualBox:/opt/born$ geth \
> --datadir "./chain" \
> --nodiscover \
> console 2>>eth_output.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.4-stable-2423ae01/linux-amd64/go1.10
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
參數名稱 | 參數描述 |
---|---|
datadir | 設置當前區(qū)塊鏈網絡數據存放的位置 |
---- | ---- |
console | 啟動命令行模式盐茎,可以在Geth中執(zhí)行命令 |
---- | ---- |
nodiscover | 私有鏈地址爽柒,不會被網上看到 |
在當前目錄執(zhí)行tail -f eth_output.log哺窄,可以看到輸出日志枢里。
wangzhen@wangzhen-VirtualBox:/opt/born$ tail -f eth_output.log
INFO [04-19|16:18:29] Disk storage enabled for ethash DAGs dir=/home/wangzhen/.ethash count=2
INFO [04-19|16:18:29] Initialising Ethereum protocol versions="[63 62]" network=1
INFO [04-19|16:18:29] Loaded most recent local header number=0 hash=5e1fc7…d790e0 td=131072
INFO [04-19|16:18:29] Loaded most recent local full block number=0 hash=5e1fc7…d790e0 td=131072
INFO [04-19|16:18:29] Loaded most recent local fast block number=0 hash=5e1fc7…d790e0 td=131072
INFO [04-19|16:18:29] Regenerated local transaction journal transactions=0 accounts=0
INFO [04-19|16:18:29] Starting P2P networking
INFO [04-19|16:18:29] IPC endpoint opened url=/opt/born/chain/geth.ipc
INFO [04-19|16:18:29] Database deduplication successful deduped=0
INFO [04-19|16:18:29] RLPx listener up self="enode://5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09@[::]:30303?discport=0"
后面章節(jié)中的命令邢滑,都是在啟動私有鏈后的Geth javascript console中操作
帳戶的添加和查看
查看帳戶,可以看到當前帳戶是空的
> web3.eth.accounts
[]
>
創(chuàng)建帳戶的方式有兩種窑业,第一種創(chuàng)建帳戶時直接初始化密碼
> web3.personal.newAccount("123456")
"0x25df465d9d9060423ce46137ca25aa5a10fd9a2f"
其中返回的0x25df465d9d9060423ce46137ca25aa5a10fd9a2f是帳戶钦幔,123456是帳戶的密碼
第二種方法是先創(chuàng)建賬戶,然后輸入密碼
> web3.personal.newAccount()
Passphrase:
Repeat passphrase:
"0xd7b8a838f947adb4da47fa7208a2c177cfed6ab3"
這時我們再查看帳戶数冬,能夠看到剛才創(chuàng)建的兩個帳戶已經存在了
> web3.eth.accounts
["0x25df465d9d9060423ce46137ca25aa5a10fd9a2f", "0xd7b8a838f947adb4da47fa7208a2c177cfed6ab3"]
開始挖礦和停止挖礦
挖礦執(zhí)行以下命令:
miner.start()
執(zhí)行以后节槐,通過剛才查看日志的方法tail -f eth_output.log,能夠看到類似下面的日志搀庶,因為只有發(fā)送交易才會自動挖礦拐纱,所以會顯示如下信息。
INFO [04-19|16:30:08] Commit new mining work number=1 txs=0 uncles=0 elapsed=175.12μs
INFO [04-19|16:30:16] Generating DAG in progress epoch=0 percentage=0 elapsed=6.662s
INFO [04-19|16:30:23] Generating DAG in progress epoch=0 percentage=1 elapsed=12.780s
INFO [04-19|16:30:29] Generating DAG in progress epoch=0 percentage=2 elapsed=19.418s
INFO [04-19|16:30:35] Generating DAG in progress epoch=0 percentage=3 elapsed=24.970s
需要在開發(fā)環(huán)境下哥倔,才能進行挖礦秸架,修改上述命令為:
geth --datadir "./chaindev" --nodiscover console 2>>eth_output.log --dev --dev.period 1
挖礦執(zhí)行以下命令:
miner.start()
出現如下信息代表開始挖礦了
INFO [04-19|16:57:28] Successfully sealed new block number=1 hash=eb3609…3747ef
INFO [04-19|16:57:28] ?? mined potential block number=1 hash=eb3609…3747ef
INFO [04-19|16:57:28] Commit new mining work number=2 txs=0 uncles=0 elapsed=417.68μs
挖礦會默認保存到創(chuàng)建的第一個帳戶0x6426ea256ba1d45e966c9aabc0268594093251c4中。
block number=66咆蒿,說明我們已經創(chuàng)建了66個區(qū)塊
在以太坊官方的網絡上东抹,平均每15秒產生一個區(qū)塊
停止挖礦執(zhí)行以下命令:
> miner.stop()
true
停止挖礦后蚂子,以太幣則不會產生,同樣智能合約缭黔、轉帳等操作也不會起作用食茎。
查看帳戶余額
查看帳戶余額的方法如下:
> web3.eth.getBalance("0x6426ea256ba1d45e966c9aabc0268594093251c4")
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77
以太幣最小的單位是wei(18個0)
每次記一長串的地址很麻煩,我們可以通過設置變量來acc0表示帳戶1
> acc0 = web3.eth.accounts[0]
"0x6426ea256ba1d45e966c9aabc0268594093251c4"
查看格式化的以太幣
web3.fromWei(web3.eth.getBalance(acc0), 'ether')
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+59
因為geth javascript console是基于javascript的馏谨,所以也可以創(chuàng)建js函數别渔,查看所有帳戶余額
> function checkAllBalances() {
var totalBal = 0;
for (var acctNum in eth.accounts) {
var acct = eth.accounts[acctNum];
var acctBal = web3.fromWei(eth.getBalance(acct), "ether");
totalBal += parseFloat(acctBal);
console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");
}
console.log(" Total balance: " + totalBal + " ether");
};
> checkAllBalances()
eth.accounts[0]: 0x6426ea256ba1d45e966c9aabc0268594093251c4 balance: 1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+59 ether
eth.accounts[1]: 0x9e8e4b905e054575a7495a6822545da82f197b13 balance: 0 ether
Total balance: 1.157920892373162e+59 ether
>
如果命令較多,可以保存到一個腳本里惧互,使用命令載入腳本:loadScript('/path/script/here.js')
轉帳操作
從帳戶0x6426ea256ba1d45e966c9aabc0268594093251c4轉3個以太幣到0x9e8e4b905e054575a7495a6822545da82f197b13,如果不指定單位ether哎媚,默認轉的是wei。
>web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,"ether")})
Error: authentication needed: password or unlock
at web3.js:3104:20
at web3.js:6191:15
at web3.js:5004:36
at <anonymous>:1:1
當直接執(zhí)行此方法時會拋出異常喊儡,顯示帳號被鎖
執(zhí)行開發(fā)者模式拨与,創(chuàng)建的默認賬戶,不會報這個錯誤
如果報相關錯誤艾猜,執(zhí)行以下語句
> web3.personal.unlockAccount(acc0,"123456")
true
轉賬完成的正確顯示為:
web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,"ether")})
"0x1c59a753ecd2efd7e916912df7bd8ae2973379df23024de9a3cee6ceb242b611"
但此時查看時會發(fā)現接收賬戶依舊為原來數值买喧。此時需要執(zhí)行挖礦命令,才會把轉賬真正完成箩朴。
> checkAllBalances()
eth.accounts[0]: 0x6426ea256ba1d45e966c9aabc0268594093251c4 balance: 1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+59 ether
eth.accounts[1]: 0x9e8e4b905e054575a7495a6822545da82f197b13 balance: 0 ether
Total balance: 1.157920892373162e+59 ether
undefined
> miner.start()
null
> checkAllBalances()
eth.accounts[0]: 0x6426ea256ba1d45e966c9aabc0268594093251c4 balance: 1.15792089237316195423570985008687907853269984665640564039451584007913129639927e+59 ether
eth.accounts[1]: 0x9e8e4b905e054575a7495a6822545da82f197b13 balance: 6 ether
Total balance: 1.157920892373162e+59 ether
undefined
> miner.stop()
true
這里看到轉賬了6個幣岗喉,是因為我執(zhí)行了兩次:)
添加節(jié)點
首先要知道自己的節(jié)點信息,在Geth命令行界面下輸入命令,注意大小寫
admin.nodeInfo
顯示結果為:
> admin.nodeInfo
{
enode: "enode://5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09@[::]:30303?discport=0",
id: "5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09",
ip: "::",
listenAddr: "[::]:30303",
name: "Geth/v1.8.4-stable-2423ae01/linux-amd64/go1.10",
ports: {
discovery: 0,
listener: 30303
},
protocols: {
eth: {
config: {
chainId: 10,
eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
eip155Block: 0,
eip158Block: 0,
homesteadBlock: 0
},
difficulty: 131072,
genesis: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
head: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
network: 1
}
}
}
其中
enode: "enode://5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09@[::]:30303?discport=0"
就是自己節(jié)點的信息炸庞,注意要把“0.0.0.0“換成你自己的IP钱床。將這個信息發(fā)送給其他節(jié)點,在其他節(jié)點的命令行中輸入:
admin.addPeer("enode://5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09@192.168.1.121:30303?discport=0")
如果添加成功埠居,輸入admin.peers會顯示出新添加的節(jié)點查牌。
> admin.peers
[{
caps: ["eth/63"],
id: "5231f4677b0576292a6d7cd11890218ae2f84336338a4ce7b08e296aa3b9007c8dc728bf26b2e10a3de9afb4d5272ab13e86030a1e7db42474d6577aad174d09",
name: "Geth/v1.8.4-stable-2423ae01/linux-amd64/go1.10",
network: {
inbound: false,
localAddress: "192.168.1.112:42702",
remoteAddress: "192.168.1.121:30303",
static: true,
trusted: false
},
protocols: {
eth: {
difficulty: 131072,
head: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
version: 63
}
}
}]
>
發(fā)送交易
解鎖賬戶
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x20869f8cb579e7712f60167d3d153d3f038960c9
Passphrase:
true
發(fā)送交易
> eth.sendTransaction({from: "0x20869f8cb579e7712f60167d3d153d3f038960c9", to: "0x25df465d9d9060423ce46137ca25aa5a10fd9a2f", gas:31000, 'gasPrice': web3.toWei(300, 'gwei'), "value": "1"})
"0xe4ecfa81d367d2906e87a0f45b5bb90a0424f58bbd74d896ef217f3d5f132533"
此時交易已經提交到區(qū)塊鏈,返回了交易的hash滥壕,但還未被處理纸颜,要使交易被處理,必須要挖礦绎橘。
交易成功后再對方賬戶上收到
> web3.fromWei(web3.eth.getBalance(eth.accounts[0]))
5.006300000000000001
參考資料
使用 Go-Ethereum 1.7.2搭建以太坊私有鏈
以太坊執(zhí)行miner.start返回null終極解決方案
Geth官方安裝指南
以太坊私鏈demo胁孙,實現第一筆交易
以太坊中的賬戶、交易称鳞、Gas和區(qū)塊Gas Limit