參考文章:《Debug以太坊go-ethereum實(shí)戰(zhàn)》,實(shí)踐中有些問題,這里記錄一下
1、下載goland ide
打開https://www.jetbrains.com/go/download/#section=windows下載windows版本,安裝后使用《Goland激活碼》提供的激活碼
2、打開工程go-ethereum
goland啟動后直接打開工程,定位到go-ethereum代碼目錄,打開
3、初始化工作
大部分參考文檔《Debug以太坊go-ethereum實(shí)戰(zhàn)》
3.1 下載代碼并編譯
參考:Installation instructions for Windows
git clone?https://github.com/ethereum/go-ethereum.git
go install -v ./cmd/...
然后 where geth確保能夠找到geth,并且geth是自己編譯的(路徑里面包含%GOPATH%\bin)
3.2 自定義genesis.json
{
? "config": {
? ? ? ? "chainId": 1314,
? ? ? ? "homesteadBlock": 0,
? ? ? ? "eip155Block": 0,
? ? ? ? "eip158Block": 0
? ? },
? ? "coinbase" : "0x0000000000000000000000000000000000000000",
? ? "difficulty" : "0x200",
? ? "extraData" : "",
? ? "gasLimit" : "0xffffffff",
? ? "nonce" : "0x0000000000000042",
? ? "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
? ? "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
? ? "timestamp" : "0x00",
? ? "alloc": { }
}
3.3?初始化節(jié)點(diǎn)
打開命令行執(zhí)行
geth --datadir geth-data0 init genesis.json
對于我本機(jī)geth-data0目錄位于d:/temp/go-ethereum/geth-data0
3.4 啟動節(jié)點(diǎn)
繼續(xù)執(zhí)行
geth --datadir "geth-data0" --networkid 1314 --nodiscover --rpc --rpcport 6001 --port 30001 --ipcpath geth1.ipc console
3.5 其他操作
再打開一個(gè)命令行,執(zhí)行
geth attach ipc:\\.\pipe\geth1.ipc
我用的是windows,如果是linux需要用類似:
geth attach ./geth-data0/geth1.ipc
創(chuàng)建兩個(gè)賬號:
personal.newAccount("123")
personal.newAccount("123")
查看賬號信息:
eth.accounts
顯示:
["0xc691f65382b4c915efc2852b5e7dace798fa28b1", "0xcb2105744928a2b8f93391409321b8f51e87537f"]
配置coinbase
miner.setEtherbase(eth.accounts[0])
挖礦
miner.start()
可以在上個(gè)終端里面執(zhí)行,這樣就不需要停止了
查看賬號信息:
eth.getBalance(eth.accounts[0])
這個(gè)時(shí)候一個(gè)有以太幣了
轉(zhuǎn)賬:
personal.unlockAccount(eth.accounts[0])
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(10,"ether")})
如果還在繼續(xù)挖礦,查詢
eth.getBalance(eth.accounts[1])
會有轉(zhuǎn)賬的10個(gè)以太幣
4、goland的配置
先使用exit退出3.4,3.5中g(shù)eth的執(zhí)行,避免影響這里的配置
點(diǎn)擊菜單的run,edit configurations,左上角的+號,選擇go build,配置信息如下:

package path:
github.com/ethereum/go-ethereum/cmd/geth
program arguments中同3.4的參數(shù)
--datadir "d:/temp/go-ethereum/geth-data0" --networkid 1314 --nodiscover --rpc --rpcport 6001 --port 30001 --ipcpath geth1.ipc console
選擇ok后退出這個(gè)界面
點(diǎn)擊debug按鈕啟動程序調(diào)試,啟動成功后在console窗口輸入:
eth.accounts
確保看到的是兩個(gè)自己剛才創(chuàng)建的賬戶,如果是[],說明--data設(shè)置錯(cuò)誤
打開/internal/ethapi/api.go在(s *PublicTransactionPoolAPI) SendTransaction方法上打上斷點(diǎn)(L1194)【注意:打上斷點(diǎn)以后,直接debug你會發(fā)現(xiàn)程序并沒有在斷點(diǎn)出停。只有當(dāng)你在控制臺發(fā)送一條交易或者本地通過rpc發(fā)交易才會觸發(fā)程序在斷點(diǎn)處停掉】
在console窗口輸入
personal.unlockAccount(eth.accounts[0])
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(10,"ether")})
回車后就可以發(fā)現(xiàn)程序斷下來了,左下角的debug窗口可以單步執(zhí)行,并查看變量值,只是這個(gè)變量值看不明白