Windows系統---打開cmd命令列
1.初始化(首先有創世檔案)
geth init "D:\Geth\1_test\genesis.json" --datadir "D:\test1"
檔案內容可自己創建(使用puppeth)
{
"config": {
"chainId": 18,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x2",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
以上結構體中變數的意義為
- coinbase 挖礦后獲得獎勵的賬戶地址
- difficulty 挖礦難度
- gasLimit 一個區塊所能容納gas的上限
- nonce 隨機值
- mixhash 一個256位的哈希證明,與nonce相結合,驗證本塊的有效性
- extraData 附加資訊,隨意填寫
- parentHash 前一塊的hash值,由于是創世塊,所以為0
運行截圖為

2.啟動Geth
geth --datadir "D:\test1" --nodiscover --http --http.port 8546 --port 30304 --networkid 101 console 2>> test.log --allow-insecure-unlock
- --datadir 表示作業目錄,第一次輸入命令時會自動新建一個testNet目錄來存放資料(私鑰公鑰)
- --nodiscover用于新手練習時自主連接節點,否則可能會自動連接
- --http 連接http rpc (舊版命令引數為--rpc)
- --http 默認公有鏈http rpc埠為8545,私有鏈建立時避免重復使用其他數字
- --port --network 同--http
- 2>>test.log 表示把控制日志輸出到test.log檔案
- --allow-insecure-unlock 新版Geth需要設定賬戶解鎖權利(有輸入密碼的權利)
開發者的好處有幾點:
- 自動挖礦
- 無需初始化
- 出塊速度快
- 默認初始化了一個賬戶,金錢無限
- 默認賬戶無需解鎖,也可以部署合約
運行截圖如下

3.準備賬戶
01:查看當前存在的賬戶
eth.accounts
默認不會有賬戶資訊,需要創建后才能有
02:創建賬戶,注意傳入的123456是密碼
personal.newAccount("123456")
>eth.accounts
//查看已有賬戶資訊
>eth.defaultAccount='eth.accounts[0]'
//設定默認賬戶,默認賬戶將運行挖礦命令時自動挖礦
03:啟動挖礦,start內的1是代表1個執行緒挖礦,null并非代表錯誤
miner.start(1)
04:查看賬戶余額,都是挖礦所得
eth.getBalance(eth.accounts[0])
05:再創建一個賬戶,密碼456
personal.newAccount("7878")
06:給兩個賬戶起別名acc0,acc1
>acc0=eth.accounts[0]
>acc1=eth.accounts[1]
07:解鎖賬戶1
personal.unlockAccount(acc0)
08:轉賬給賬戶2,10個ether,回傳值為本次交易哈希值
eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(10)})
//web3.toWei(10)是將10ether轉換成Wei的格式1 ether=10^8Wei
09:查看賬戶2余額
eth.getBalance(acc1)
10:退出geth
exit
運行截圖如下
> eth.accounts
[]
> eth.blockNumber
0
> personal.newAccount('123456')
"0x0a11acf50a1c73c7ec688c86c34b8ae383e335a1"
> personal.newAccount('7878')
"0x323118b34c4dfdeb32fc7ebd5b69238a7fab603d"
> eth.defaultAccount='eth.accounts[0]'
"eth.accounts[0]"
> miner.start()
null
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
0
> eth.blockNumber
22
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
120
> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
140
>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:1000000000000000000})
Error: authentication needed: password or unlock
at web3.js:6357:37(47)
at send (web3.js:5091:62(35))
at <eval>:1:20(16)
//錯誤原因是交易的上一條命令必須是解鎖轉出以太幣賬戶的命令
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x0a11acf50a1c73c7ec688c86c34b8ae383e335a1
Passphrase:
true
>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:1000000000000000000})
"0x21cd774bd1ca30fb9f7279c1b09d924a515c569b074056f37d8f2d95a97c02d9"
//交易回傳值為本次交易對應的哈希值
>eth.getTransaction("0x21cd774bd1ca30fb9f7279c1b09d924a515c569b074056f37d8f2d95a97c02d9")//查看交易內容
{
blockHash: "0xa45679b63a9289054f7a8da4f9af24a25272e3ae3373590425141ae15251046d",
blockNumber: 201,
from: "0x0a11acf50a1c73c7ec688c86c34b8ae383e335a1",
gas: 21000,
gasPrice: 1000000000,
hash: "0x21cd774bd1ca30fb9f7279c1b09d924a515c569b074056f37d8f2d95a97c02d9",
input: "0x",
nonce: 0,
r: "0x7b569cfd1ca028b5d6ffd39d493f402bdeed1e90477ae6efe5d4508139e51980",
s: "0x45d18d464e8119391bf4ede13839982221ade78e21862fd8ca01cf29f93e1e5d",
to: "0x323118b34c4dfdeb32fc7ebd5b69238a7fab603d",
transactionIndex: 0,
type: "0x0",
v: "0x48",
value: 1000000000000000000
}
> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
1
4.建立第二個節點
a.同一局域網下兩個電腦進行連接
b.同一電腦下安裝虛擬機,設定節點進行連接
c.再打開一個cmd命令列,更改作業目錄和埠號,再進行兩個節點的連接
注意:舊版可以同一電腦,新版不可以,因此在一臺電腦上,需要安裝一個虛擬機,并且卻要同一個networkid和同一個創世區塊json檔案,且新建節點不能挖礦否則將產生分叉,
原節點(接上)
> net.peerCount
0
> admin.peers
[]
> admin.nodeInfo.enode
"enode://7277d340a2f83c6846e37f01431b140f5218e280f032e9fec234b65c95a29b3889aa2bbdd4b84f73c6564e6a2b08f61e5c206c9ce228d77a15eae78cdc2e51f0@127.0.0.1:30304?discport=0"
新建節點(新建一個cmd)
C:\Users\91337>geth --datadir "D:\test2" --nodiscover --http --http.port 8547 --port 30305 --networkid 101 console 2>> test2.log --allow-insecure-unlock --ipcdisable
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.10-stable-bb74230f/windows-amd64/go1.17.2
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: D:\test2
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
To exit, press ctrl-d or type exit
> eth.blockNumber
0
> net.peerCount
0
> admin.peers
[]
> admin.addPeer("enode://7277d340a2f83c6846e37f01431b140f5218e280f032e9fec234b65c95a29b3889aa2bbdd4b84f73c6564e6a2b08f61e5c206c9ce228d77a15eae78cdc2e51f0@127.0.0.1:30304?discport=0")
true
再次驗證是否連接成功
> net.peerCount
> admin.peers
也可以在新建cmd命令列下再次查看區塊鏈高度,如果同步則成功,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/336300.html
標籤:區塊鏈
