基於ETHereum的鏈環境方式
想知道更多區塊鏈技術實戰,請百度【鏈客區塊鏈技術問答社群】
單結點部署,一般情況適用於本地開發階段或線上測試階段,方便智慧合約的開發與除錯。
建立工程目錄
mkdir -p ~/workmeta/EduEthereumServerDeploy/deploy_1/data
cd ~/workmeta/EduEthereumServerDeploy/deploy_1
此目錄主要用於儲存所有鏈生產過程中產生的區塊、索引資料以及帳戶keystore資料。
預先建立挖礦帳戶及預置資金帳戶
1. 建立密碼檔案,後續生成帳戶統一使用此密碼
echo “111111” > .passwd
2. 建立兩個帳戶,一個挖礦帳戶,一個預置資金帳戶
for ((n=0;n<2;n++)); do geth account new --password .passwd --datadir ./data; done
INFO [10-30|10:37:29] Maximum peer count ETH=25 LES=0 total=25
Address: {127eb97163f790dd25c174429d85c3fa7c3cf116} //預設此地址為: 挖礦帳戶
INFO [10-30|10:37:30] Maximum peer count ETH=25 LES=0 total=25
Address: {438a22e068b7d62d50a25706476545ab896e8510} //預設此地址為: 預置資金帳戶
生成創世區塊配置
挖礦帳戶: 127eb97163f790dd25c174429d85c3fa7c3cf116
預置資金帳戶: 438a22e068b7d62d50a25706476545ab896e8510
具體操作步驟可參考[五、配置創世區塊]
注: 因大家在本地生成的帳戶與教程中的帳戶不同,所以需要根據自己生成帳戶進行創世區塊配置。
初始化鏈資料
4.1 根據創始配置(pubchain.json)初始化資料目錄
geth init --datadir ./data pubchain.json
1
執行結果如下:
INFO [10-26|13:42:00] Maximum peer count ETH=25 LES=0 total=25
INFO [10-26|13:42:00] Allocated cache and file handles database=/Users/nolan/workmeta/pubchain/data/geth/chaindata cache=16 handles=16
INFO [10-26|13:42:00] Writing custom genesis block
INFO [10-26|13:42:00] Persisted trie from memory database nodes=356 size=65.35kB time=3.010068ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-26|13:42:00] Successfully wrote genesis state database=chaindata hash=f6fcaa…44829b
INFO [10-26|13:42:00] Allocated cache and file handles database=/Users/nolan/workmeta/pubchain/data/geth/lightchaindata cache=16 handles=16
INFO [10-26|13:42:00] Writing custom genesis block
INFO [10-26|13:42:00] Persisted trie from memory database nodes=356 size=65.35kB time=2.1111ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-26|13:42:00] Successfully wrote genesis state database=lightchaindata hash=f6fcaa…44829b
4.2 查詢工程目錄結構
tree
1
工程目錄如下:
.
├── data
│ ├── geth //根據創世配置生成的檔案目錄
│ │ ├── chaindata
│ │ │ ├── 000001.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK
│ │ │ ├── LOG
│ │ │ └── MANIFEST-000000
│ │ └── lightchaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore //[geth account new]命令生成的keystore帳戶資訊
│ ├── UTC–2018-10-30T02-37-29.289558112Z–127eb97163f790dd25c174429d85c3fa7c3cf116
│ └── UTC–2018-10-30T02-37-30.431416849Z–438a22e068b7d62d50a25706476545ab896e8510
└── pubchain.json //創世區塊配置
5 directories, 13 files
工程可參考: https://github.com/ChainDesk/EduEthereumServerDeploy/tree/master/deploy_1
啟動節點
執行啟動命令
geth --datadir ./data --rpc --rpcaddr “0.0.0.0” --rpcport 8545 --rpcapi “eth,web3,personal,net,miner,admin,debug,db” --unlock 127eb97163f790dd25c174429d85c3fa7c3cf116 --password .passwd --mine
1
整個啟動過程如下:
INFO [10-30|10:49:32] Maximum peer count ETH=25 LES=0 total=25
INFO [10-30|10:49:32] Starting peer-to-peer node instance=Geth/v1.8.2-stable/darwin-amd64/go1.10
INFO [10-30|10:49:32] Allocated cache and file handles database=/Users/nolan/workmeta/EduEthereumServerDeploy/deploy_1/data/geth/chaindata cache=768 handles=1024
INFO [10-30|10:49:32] Initialised chain configuration config="{ChainID: 100 Homestead: 1 DAO: DAOSupport: false EIP150: 2 EIP155: 3 EIP158: 3 Byzantium: 4 Constantinople: Engine: clique}"
INFO [10-30|10:49:32] Initialising Ethereum protocol versions="[63 62]"
INFO [10-30|10:49:32] Starting P2P networking
…省去一些日誌,要不太長了
INFO [10-30|10:49:35] Starting mining operation
INFO [10-30|10:49:35] Commit new mining work number=1 txs=0 uncles=0 elapsed=130.025µs
INFO [10-30|10:49:35] Successfully sealed new block number=1 hash=9c8e09…919dbe
INFO [10-30|10:49:35] � mined potential block number=1 hash=9c8e09…919dbe
INFO [10-30|10:49:35] Commit new mining work number=2 txs=0 uncles=0 elapsed=585.992µs
^CINFO [10-30|10:49:36] Got interrupt, shutting down…
INFO [10-30|10:49:36] HTTP endpoint closed url=http://0.0.0.0:8545
INFO [10-30|10:49:36] IPC endpoint closed endpoint=/Users/nolan/workmeta/EduEthereumServerDeploy/deploy_1/data/geth.ipc
INFO [10-30|10:49:36] Writing cached state to disk block=1 hash=9c8e09…919dbe root=94b9df…184701
INFO [10-30|10:49:36] Persisted trie from memory database nodes=0 size=0.00B time=3.947µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
…
到此,整個節點就算啟動成功了。接下來我們將驗證其內部轉帳服務。
節點服務驗證
6.1 進入geth命令視窗
geth attach http://localhost:8545 //進入geth客戶端
1
進入的命令視窗如下所示:
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.17-stable-8bbe7207/linux-amd64/go1.10.4
coinbase: 0xd08e33b7f310aadfe52b5f94c8a04045d2594195
at block: 841 (Tue, 30 Oct 2018 09:04:09 CST)
datadir: /opt/node1
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
6.2 檢視轉帳前金額
檢視所有帳戶
personal.listAccounts
[“0x127eb97163f790dd25c174429d85c3fa7c3cf116”, “0x438a22e068b7d62d50a25706476545ab896e8510”]
檢視帳戶餘額(0x127eb97163f790dd25c174429d85c3fa7c3cf116)
web3.fromWei(eth.getBalance(web3.personal.listAccounts[0]), “ether”)
0
檢視帳戶餘額(0x438a22e068b7d62d50a25706476545ab896e8510)
web3.fromWei(eth.getBalance(web3.personal.listAccounts[1]), “ether”)
9.04625697166532776746648320380374280103671755200316906558262375061821325312e+56
6.3 執行轉帳操作
轉帳10eth, 0x438a22e068b7d62d50a25706476545ab896e8510 -> 0x127eb97163f790dd25c174429d85c3fa7c3cf116
1
web3.personal.unlockAccount(web3.personal.listAccounts[1])
Unlock account 0x438a22e068b7d62d50a25706476545ab896e8510
Passphrase:
true
web3.eth.sendTransaction({from: web3.personal.listAccounts[1], to: web3.personal.listAccounts[0], value: web3.toWei(10, “ether”)})
"0x9a1057c619659a08ee15882f0954b9953435b7d065a64ec37fd5d5c8b0ef6247“
6.4 檢視轉帳後金額
檢視帳戶餘額(0x127eb97163f790dd25c174429d85c3fa7c3cf116)
web3.fromWei(eth.getBalance(web3.personal.listAccounts[0]), “ether”)
10.000378
檢視帳戶餘額(0x438a22e068b7d62d50a25706476545ab896e8510)
web3.fromWei(eth.getBalance(web3.personal.listAccounts[1]), “ether”)
9.04625697166532776746648320380374280103671755200316906548261997061821325312e+56