基於區塊鏈的雲筆記小程序開發(一)


 

 今天主要任務:

  1. 本地搭建私有鏈

  2. 啟動私有鏈

  3. 進入私有鏈控制台

 

 

1. 本地搭建私有鏈

⚠️注意:本文針對的mac os操作系統下

安裝geth

 

geth是go-ethereum的簡寫,以太坊智能合約常用的命令行工具。

如果沒有安裝brew,請先安裝brew,打開終端terminal並輸入:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

mac通過brew包管理工具下載:

brew tap ethereum/ethereum
brew install ethereum

具體見官方文檔:https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac

安裝完成后,在終端terminal輸入:

geth -h

出現下面結果,就說明安裝成功:

NAME:
   geth - the go-ethereum command line interface

   Copyright 2013-2018 The go-ethereum Authors

USAGE:
   geth [options] command [command options] [arguments...]
   
VERSION:
   1.8.27-stable
   
COMMANDS:
   account           Manage accounts
   attach            Start an interactive JavaScript environment (connect to node)
...
...

geth也裝好了,可以開始后續的步驟了。

 

初始化創始區塊

1. 為了方便操作,我在桌面新建一個文件夾privatechain,用來存放私有鏈節點node1

node1所在的位置:

/Users/xxxx/Desktop/privatechain/node1

⚠️這里的xxxx是自己電腦用戶名稱

2. 在node1中創建 genesis.json文件,並將下面內容寫入后保存:

{
  "config": {
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

3. 初始化

打開terminal,進入到node1中,開始初始化創始區塊:

cd /Users/xxxx/Desktop/privatechain/node1
geth init ./genesis.json  --datadir ./

初始化完成,node1下會生成geth和keystore兩個文件夾

  • geth:保存鏈上的區塊數據
  • keystore:保存鏈上的賬戶信息

 

2. 啟動私有鏈

通過終端terminal進入node1,啟動私有鏈:

cd /Users/xxxx/Desktop/privatechain/node1
geth --identity "node1" --rpc --rpcport 1111 --rpccorsdomain "*" --datadir "./" --port 6666  --ipcpath "geth/geth1.ipc" --networkid 10 --rpcapi eth,web3,admin,personal,net --ethash.dagdir=./geth/ethash

關於參數:

--identity "node1"                自定義節點名node1

--rpc                      啟用HTTP-RPC服務器

--rpcport 1111                 設置HTTP-RPC服務器監聽端口1111(默認值:8545)
--rpccorsdomain "*"              允許跨域請求的域名列表(逗號分隔)(瀏覽器強制) 
--datadir "./"                  存放數據庫和keystore密鑰的數據目錄
--port 6666                   設置網卡監聽端口6666(默認值:30303)
--ipcpath "geth/geth1.ipc"         包含在datadir里的IPC socket/pipe文件名(轉義過的顯式路徑)
--networkid 10                 網絡標識符(整型, 1=Frontier, 2=Morden (棄用), 3=Ropsten, 4=Rinkeby) (默認: 1)

--rpcapi eth,web3,admin,personal,net   基於HTTP-RPC接口提供的API 
--ethash.dagdir=./geth/ethash        存ethash DAGs目錄 (默認 = 用戶hom目錄)

成功啟動私有鏈:

INFO [06-03|21:46:50.366] Maximum peer count                       ETH=25 LES=0 total=25
INFO [06-03|21:46:50.380] Starting peer-to-peer node               instance=Geth/node1/v1.8.27-stable/darwin-amd64/go1.12.4
INFO [06-03|21:46:50.380] Allocated cache and file handles         database=/Users/xxxx/Desktop/privatechain/node1/geth/chaindata cache=512 handles=5120
INFO [06-03|21:46:50.412] Initialised chain configuration          config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil>  ConstantinopleFix: <nil> Engine: unknown}"
INFO [06-03|21:46:50.412] Disk storage enabled for ethash caches   dir=/Users/woshinixxk/Desktop/privatechain/geth/ethash count=3
INFO [06-03|21:46:50.412] Disk storage enabled for ethash DAGs     dir=geth/ethash                                        count=2
INFO [06-03|21:46:50.412] Initialising Ethereum protocol           versions="[63 62]" network=10
...
...

 

3. 進入私有鏈控制台

 保持上面的已啟動好的私有鏈,另外再打開一個終端

 通過終端terminal進入node1節點,並輸入命令進入控制台:

geth attach geth/geth1.ipc

新建一個賬戶:

personal.newAccount('123456')

123456是賬戶的密碼

控制台返回的內容就是賬戶地址

有了賬戶后,就可以開始挖礦了:

miner.start()

 

未完待續~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM