最近需要开始研读以太坊源码,准备写一个系列的文章记录下学习过程。
首先需要准备下学习环境:基于mac,源码阅读采用Visual Studio Code,调试采用Delve。
官网地址:https://code.visualstudio.com
使用以下命令安装:
brew install go-delve/delve/delve
安装完后测试一下:dlv version,能正常输出就说明安装成功了。
但是我用上面的命令安装时遇到一个坑:
Updating Homebrew...
==> Installing delve from go-delve/delve
==> Downloading https://github.com/derekparker/delve/archive/v1.0.0.tar.gz
Already downloaded: /Users/macbook/Library/Caches/Homebrew/delve-1.0.0.tar.gz
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
==> Generating dlv-cert
==> openssl req -new -newkey rsa:2048 -x509 -days 3650 -nodes -config dlv-cert.cfg -extensions
==> [SUDO] Installing dlv-cert as root
==> sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain dlv-ce
Last 15 lines from /Users/macbook/Library/Logs/Homebrew/delve/02.sudo:
2018-05-08 23:19:48 +0800
sudo
security
add-trusted-cert
-d
-r
trustRoot
-k
/Library/Keychains/System.keychain
dlv-cert.cer
If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
https://github.com/go-delve/homebrew-delve/issues
百思不得其解,最终在一个老外的blog上找到了答案,应该是一个bug,需要到homebrew的cache中手动生成证书:
cd $HOME/Library/Caches/Homebrew
tar xf delve-*.gz
cd delve-*
sh scripts/gencert.sh
# Type in your sudo password
brew install go-delve/delve/delve
首先创建一个代码根目录$WORKSPACE,然后依次建立以下目录结构:
$WORKSPACE/src/github.com/ethereum
在ethereum目录下,下载以太坊源码:
git clone https://github.com/ethereum/go-ethereum
建立这样的目录目录结构主要是为了导入Visual Studio Code,否则会报找不到文件的错误。
打开Visual Studio Code,点击“打开文件夹”,选择刚才的$WORKSPACE,就可以成功导入了。
打开入口文件cmd/geth/main.go,菜单栏上点击“调试”->“启动调试”,这时会跳到调试面板,提示错误:没有启动配置。
点击小齿轮图标新建一个配置,这时会在$WORKSPACE下创建一个.vscode/launch.json文件,修改为以下内容:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {
"GOPATH": "${workspaceRoot}"
},
"args": [],
"showLog": true
}
]
}
主要改动了env这一行,需要把GOPATH设置成我们的workspace目录,否则会到默认的系统目录里去找,会报很多package找不到的错误。
设置好了启动配置,这下该能正常运行了吧?结果又遇到一个坑:
2018/05/08 23:49:21 server.go:73: Using API v1
2018/05/08 23:49:21 debugger.go:98: launching process with args: [/Users/macbook/Code/Go/HelloWorld/debug]
could not launch process: exec: "lldb-server": executable file not found in $PATH
Process exiting with code: 1
网上查了一下,据说是由于我本机的CommandLineTools版本太高不支持,吐血。。。解决方案:
sudo rm -rf /Library/Developer/CommandLineTools
去 https://developer.apple.com/download/more/ 下载Command Line Tools (macOS 10.13) for Xcode 9.2
搞定!这样以太坊客户端就运行起来了,还可以设置断点进行调试,可以开始愉快的源码研读之路了~
更多文章欢迎关注“鑫鑫点灯”专栏:https://blog.csdn.net/turkeycock/article/category/7669858
或关注飞久微信公众号: