通過網上的資料,結合自己的搭建的eth環境,希望能對大家搭建區塊鏈環境有所幫助了解。下面開始進入准備。
本次是windows環境下的私有鏈環境搭建,所以需要做以下准備:
1.win10系統(家庭版),64位
2.以太坊錢包Ethereum-Wallet-win64-0-10-0(我用的版本0.10.0)
3.以太坊geth客戶端(我用的是1.4.11版)
1.1 安裝geth
Windows要求必須是64位系統,從官方網站下載編譯好的win64客戶端,下載解壓后只有一個Geth.exe,運行安裝即可。
安裝后用cmd命令打開命令行,進入到你安裝geth的目錄下,輸入geth –help 命令,如果輸出關於geth的信息如下圖則說明安裝成功。
D:\Study\geth>geth -help
NAME:
geth - the go-ethereum command line interface
USAGE:
geth [options] command [command options] [arguments...]
VERSION:
1.4.11-stable
1.1.1 配置創世塊
配置自己的創世塊是為了區分公有鏈,同一個網絡中,創世塊必須是一樣的,否則無法聯通。在剛剛Geth安裝目錄下放置初始化創世塊文件名字為genesis.json,文件內容如下:
{
"config": {
"chainId": 7878,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "200",
"gasLimit": "4294967295",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
}
}
參數說明:
備
1.1.2 初始化私有鏈節點
執行geth的init命令初始化私鏈節點:
D:\Study\geth>geth --datadir .\data init private.json
I0301 22:59:16.676485 ethdb/database.go:82] Alloted 16MB cache and 16 file handles to data\chaindata
I0301 22:59:16.717405 cmd/geth/main.go:299] successfully wrote genesis block and/or chain rule set: f26adf7a562fc8c4cc60e31d2ac6dad88d57e52c4b38e9fd7337b580e90f025c
這會在當前目錄下創建data目錄,用來保存區塊數據及賬戶信息。
1.1.3 啟動私有鏈節點
執行如下命令:
D:\Study\geth>geth --rpc --datadir .\data --networkid 7878 console
D:\Study\geth>geth --rpc --rpcapi "eth,net,web3,personal" --datadir .\data --networkid 7878 console
I0301 22:59:41.986822 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to data\chaindata
I0301 22:59:42.044666 ethdb/database.go:169] closed db:data\chaindata
I0301 22:59:42.044666 cmd/utils/flags.go:592] WARNING: No etherbase set and no accounts found as default
I0301 22:59:42.046660 ethdb/database.go:82] Alloted 128MB cache and 1024 file handles to data\chaindata
I0301 22:59:42.066609 eth/backend.go:621] upgrading db log bloom bins
I0301 22:59:42.066609 eth/backend.go:629] upgrade completed in 0
I0301 22:59:42.066609 ethdb/database.go:82] Alloted 16MB cache and 16 file handles to data\dapp
I0301 22:59:42.074587 eth/backend.go:172] Protocol Versions: [63 62], Network Id: 7878
I0301 22:59:42.074587 eth/backend.go:201] Blockchain DB Version: 3
I0301 22:59:42.074587 core/blockchain.go:206] Last header: #0 [f26adf7a…] TD=200
I0301 22:59:42.075584 core/blockchain.go:207] Last block: #0 [f26adf7a…] TD=200
I0301 22:59:42.075584 core/blockchain.go:208] Fast block: #0 [f26adf7a…] TD=200
I0301 22:59:42.076581 p2p/server.go:313] Starting Server
I0301 22:59:44.140065 p2p/discover/udp.go:217] Listening, enode://c33036ee5e687263b037973b8e0a2839ae7a6c8152fd952ad52d058c3c16875bdd8310adff378435b60fdba46d8cfdaf39fba8e4c8e8fd43554df2b727a2f390@100.94.99.164:30303
I0301 22:59:44.141062 p2p/server.go:556] Listening on [::]:30303
I0301 22:59:44.143057 node/node.go:296] IPC endpoint opened: \\.\pipe\geth.ipc
I0301 22:59:44.150040 node/node.go:366] HTTP endpoint opened: http://localhost:8545
I0301 22:59:44.198928 p2p/nat/nat.go:111] mapped network port udp:30303 -> 30303 (ethereum discovery) using UPNP IGDv1-IP1
I0301 22:59:44.214865 p2p/nat/nat.go:111] mapped network port tcp:30303 -> 30303 (ethereum p2p) using UPNP IGDv1-IP1
Welcome to the Geth JavaScript console!
instance: Geth/v1.4.11-stable/windows/go1.6.2
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
參數說明:
1.1.4 賬戶管理
l 創建新賬戶
在geth控制台,使用personal對象的newAccount()方法創建一個新賬戶,參數為你自己選擇的密碼。
> personal.newAccount('11111111')
"0x059d9cfef0ea9f8bffff395f436d7bb342858047"
> personal.newAccount('22222222')
"0x9d7aad5c92db134e58620260da9f923ccf78b7e9"
輸出就是新創建的賬戶地址(公鑰),你的輸出不會和上面的示例相同。geth會保存到數據目錄下的keystore文件中。密碼要自己記住,以后還需要用到。
l 查詢賬戶
> eth.accounts
["0x059d9cfef0ea9f8bffff395f436d7bb342858047", "0x9d7aad5c92db134e58620260da9f923ccf78b7e9"]
l 刪除賬戶
Geth沒有提供直接刪除以太坊賬戶的命令,不過可以通過刪除本地文件來刪除以太坊賬戶,因為每一個以太坊賬戶對應一個文件
D:\>cd D:\Study\geth\data\keystore
2019/03/01 22:48 <DIR> .
2019/03/01 22:48 <DIR> ..
2019/03/01 22:48 491 UTC--2019-03-01T14-48-27.614861000Z--465f228819aa4f6b271ccbad6b60d472bbf34149
2019/03/01 22:48 491 UTC--2019-03-01T14-48-44.969795100Z--4b7254d0c7e7fec9a7e459fad506f7c8ef506129
l 查詢賬戶余額
> eth.getBalance(eth.accounts[0])
0
l 挖礦
沒錢的賬戶什么也干不了,因此需要挖礦來掙點錢。 操作方式是在geth控制台執行miner對象的start()方法來啟動挖礦,命令執行如下
> miner.start(1)
I0301 23:01:24.884233 miner/miner.go:119] Starting mining operation (CPU=1 TOT=2)
I0301 23:01:24.884233 miner/worker.go:573] commit new work on block 1 with 0 txs & 0 uncles. Took 0
I0301 23:01:24.885229 eth/backend.go:454] Automatic pregeneration of ethash DAG ON (ethash dir: C:\Users\45014\AppData\Ethash)
I0301 23:01:24.888222 ethash.go:259] Generating DAG for epoch 0 (size 1073739904) (0000000000000000000000000000000000000000000000000000000000000000)
tI0301 23:01:24.888222 eth/backend.go:461] checking DAG (ethash dir: C:\Users\45014\AppData\Ethash)
rue
> I0301 23:01:25.878153 ethash.go:276] Done generating DAG for epoch 0, it took 989.9318ms
I0301 23:01:28.413715 miner/worker.go:339] Mined block (#1 / 9386c990). Wait 5 blocks for confirmation
I0301 23:01:28.413715 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 0
I0301 23:01:28.414718 miner/worker.go:573] commit new work on block 2 with 0 txs & 0 uncles. Took 0
I0301 23:01:29.496834 miner/worker.go:339] Mined block (#2 / 56bc0db5). Wait 5 blocks for confirmation
I0301 23:01:29.496834 miner/worker.go:573] commit new work on block 3 with 0 txs & 0 uncles. Took 0
I0301 23:01:29.497846 miner/worker.go:573] commit new work on block 3 with 0 txs & 0 uncles. Took 0
I0301 23:01:30.054354 miner/worker.go:339] Mined block (#3 / 2c125710). Wait 5 blocks for confirmation
等幾分鍾以后,檢查賬戶余額:
> eth.getBalance(eth.accounts[0])
75000000000000000000
> eth.getBalance(eth.accounts[1])
0
執行miner對象的stop()方法停止挖礦:
> miner.stop()
l 查看區塊數
> eth.blockNumber
15
l 查看區塊信息
eth.getBlock(5)
l 解鎖賬戶
在部署合約時需要一個解鎖的賬戶。在geth控制台使用personal對象的unlockAccount()方法來解鎖指定的賬戶,參數為賬戶地址和賬戶密碼(在創建賬戶時指定的那個密碼):
> personal.unlockAccount(acc0)
Unlock account 0x059d9cfef0ea9f8bffff395f436d7bb342858047
Passphrase:
true
>
l 轉賬操作
acc0 = eth.accounts[0]
acc1= eth.accounts[1]
amount = web3.toWei(0.01)
personal.unlockAccount(acc0)
eth.sendTransaction({from: acc0, to: acc1, value: amount})
查看交易狀態:
> txpool.status
{
pending: 1,
queued: 0
}
執行挖礦:
miner.start()
停止挖礦:
miner.stop()
查詢賬戶1:
> eth.getBalance(acc0)
169677500000000000000
> eth.getBalance(acc1 )
10000000000000000
> txpool.status
{
pending: 0,
queued: 0
}
結論:轉賬操作只有在啟動挖礦后才能生效,切記!!!!
1.2 以太坊錢包
1.2.1 啟動錢包
打開ethwallet文件夾,運行Ethereum-Wallet.exe 如果右上角出現“PRIVATE-NET”則證明錢包客戶端已經連接到了我們的私有節點。
點擊“LAUNCH APPLICATION”
進入錢包主頁面