iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 6
0
Blockchain

以太坊-探索智能合約的多種面向系列 第 6

Day 6 搭建私有鏈(二) - 開啟區塊鏈,創建帳戶

  • 分享至 

  • xImage
  •  

(5)寫入區塊
將配置好的genesis.json初始化 :
geth --datadir “./testnet” init genesis.json
初始化之後,會在testnet的資料夾中,創建兩個資料夾,
“keystore(存放帳戶資訊)”和”geth(存放區塊資訊”。
說明 :
datadir 指定存放區塊鏈數據的資料夾。
geth init 初始化的命令,帶有一個參數(json檔案)。
區塊初始化:

INFO [10-21|19:34:14.130] Maximum peer count                       ETH=25 LES=0 total=25
INFO [10-21|19:34:14.131] Allocated cache and file handles         database=/home/name/Iron/iron/geth/chaindata cache=16 handles=16
INFO [10-21|19:34:14.241] Writing custom genesis block
INFO [10-21|19:34:14.241] Persisted trie from memory database      nodes=1 size=150.00B time=61.072µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-21|19:34:14.241] Successfully wrote genesis state         database=chaindata                          hash=68eb29…695376
INFO [10-21|19:34:14.241] Allocated cache and file handles         database=/home/name/Iron/iron/geth/lightchaindata cache=16 handles=16
INFO [10-21|19:34:14.341] Writing custom genesis block
INFO [10-21|19:34:14.341] Persisted trie from memory database      nodes=1 size=150.00B time=67.934µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-21|19:34:14.342] Successfully wrote genesis state         database=lightchaindata                          hash=68eb29…695376

(6)開啟區塊鏈
輸入:
geth --datadir “./testnet” --rpc --rpcaddr --rpcport --rpcapi “eth,web3,net,personal,admin,txpool,miner” --nodiscover --networkid 20
--port –minerthreads 1 console
說明:
datadir : 指定存放區塊鏈數據的資料夾。
rpc : 開啟HTTP-RPC,進行智能合約的佈署和呼叫。
rpcaddr : HTTP-RPC的監聽地址,default為localhost。
rpcport : HTTP-RPC的監聽接口,default為8545。
rpcapi : 可連接的客戶端應用。
nodiscover : 關閉自動同步其他節點。
networkid : 指定以太坊網路。
port : 以太坊網路的監聽接口,default為30303。
minerthreads : 挖礦時,CPU占用的執行序數量,default為4。
console : 開啟JavaScript控制台。
開啟區塊鏈:

INFO [10-21|20:08:17.257] Maximum peer count                       ETH=25 LES=0 total=25
INFO [10-21|20:08:17.258] Starting peer-to-peer node               instance=Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4
INFO [10-21|20:08:17.258] Allocated cache and file handles         database=/home/name/Iron/iron/geth/chaindata cache=768 handles=512
INFO [10-21|20:08:17.409] Initialised chain configuration          config="{ChainID: 20 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [10-21|20:08:17.409] Disk storage enabled for ethash caches   dir=/home/name/Iron/iron/geth/ethash count=3
INFO [10-21|20:08:17.409] Disk storage enabled for ethash DAGs     dir=/home/name/.ethash               count=2
INFO [10-21|20:08:17.409] Initialising Ethereum protocol           versions="[63 62]" network=20
INFO [10-21|20:08:17.410] Loaded most recent local header          number=0 hash=68eb29…695376 td=1 age=49y6mo5d
INFO [10-21|20:08:17.410] Loaded most recent local full block      number=0 hash=68eb29…695376 td=1 age=49y6mo5d
INFO [10-21|20:08:17.410] Loaded most recent local fast block      number=0 hash=68eb29…695376 td=1 age=49y6mo5d
INFO [10-21|20:08:17.410] Loaded local transaction journal         transactions=0 dropped=0
INFO [10-21|20:08:17.410] Regenerated local transaction journal    transactions=0 accounts=0
INFO [10-21|20:08:17.571] IPC endpoint opened                      url=/home/name/Iron/iron/geth.ipc
INFO [10-21|20:08:17.571] New local node record                    seq=13 id=10bab1be60605979 ip=127.0.0.1 udp=0 tcp=30304
INFO [10-21|20:08:17.571] Started P2P networking                   self="enode://0e3e52878db32d5089959185160d6aad101c72de38cdb025234b1d7b22a84ba21d116627d7e1bc7001be29cd98b463dc71d0b858966a8e5ca7c35fea5f167535@127.0.0.1:30304?discport=0"
INFO [10-21|20:08:17.572] HTTP endpoint opened                     url=http://0.0.0.0:8545          cors=* vhosts=localhost
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.18-unstable-88b41a9e/linux-amd64/go1.10.4
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>

(7)創建帳戶
指令:
輸入personal,可以看到可以呼叫的函數列表 :

> personal
{
  listAccounts: [],
  listWallets: [],
  deriveAccount: function(),
  ecRecover: function(),
  getListAccounts: function(callback),
  getListWallets: function(callback),
  importRawKey: function(),
  lockAccount: function(),
  newAccount: function github.com/ethereum/go-ethereum/console.(*bridge).NewAccount-fm(),
  openWallet: function github.com/ethereum/go-ethereum/console.(*bridge).OpenWallet-fm(),
  sendTransaction: function(),
  sign: function github.com/ethereum/go-ethereum/console.(*bridge).Sign-fm(),
  signTransaction: function(),
  unlockAccount: function github.com/ethereum/go-ethereum/console.(*bridge).UnlockAccount-
  fm()
}

接著輸入 :

personal.newAccount(“123456”)  <= 此欄位為帳戶密碼。
"0x4ca5f4a1612ebe48d8e48c7df7b2f0d2839ebc56"

返回帳戶地址,創建成功,在keystore資料夾中會出現一個帳戶地址名稱的帳戶檔案。


上一篇
Day 5 搭建私有鏈(一)
下一篇
Day 7 搭建私有鏈(二) - 交易,轉帳,查詢
系列文
以太坊-探索智能合約的多種面向20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
ran_feng
iT邦新手 5 級 ‧ 2020-02-11 14:32:53

你好,我在嘗試Day 6 搭建私有鏈(二)遇到了問題想請教

執行geth --datadir “./testnet” init genesis.json
這行指令時跑出了下行錯誤
Fatal: Failed to read genesis file: open genesis.json: permission denied
想請問這是什麼方面的問題,可以用什麼方法解決呢?
謝謝

你好,
這是說 genesis.json 這個檔案沒有open的權限, 你要不要試試看 chmod 這個指令, 先改變權限試試。

sudo chmod 777 "File Name"

ran_feng iT邦新手 5 級 ‧ 2020-02-13 12:45:23 檢舉

你好,謝謝你回應我,這個方法我試過了,發現還是不可行,最後我有查到其實是geth的版本產生這個問題(原本使用1.9.6,後來改舊版本使用1.9.0後這個問題解決了)

另外我發現genesiss.json的difficulty有不小心打錯字,謝謝

0
神威
iT邦研究生 4 級 ‧ 2022-02-20 12:50:05

你好:
我用windows96g6 來實作私有練,有安裝geth
可是我下這個指令時

geth --datadir "./testnet" --rpc --rpcaddr --rpcport --rpcapi "eth,web3,net,personal,admin,txpool,miner" --nodiscover --networkid 20 --port –minerthreads 1 console

結果都會跟我說

Incorrect Usage. flag provided but not defined: -rpc

NAME:
   geth.exe - the go-ethereum command line interface

   Copyright 2013-2021 The go-ethereum Authors

USAGE:
   geth.exe [options] [command] [command options] [arguments...]

VERSION:
   1.10.16-stable-20356e57

請問樓主有遇過這問題嗎?謝謝

我要留言

立即登入留言