Go环境搭建
今天开始有计划的阅读以太坊源码啦,真的想认认真真把区块链学好,以后好赚钱呢。
主要参考:https://github.com/ZtesoftCS/go-ethereum-code-analysis
我借助这个开始入门,然后做到对每一个函数做注释,甚至每一行代码做注释,内容太多,可能我没时间写博客,立下flag,把以太坊源码看完之后把我的注释后的代码发出来。
今天主要是配置了环境,把代码跑通,上面参考的博客还是有缺陷的地方的。
一、配置Go环境
-
下载安装Go 和MinGW(在windows上,因为我日常办公是Windows,用不惯mac,Ubuntu办公软件太少)
MinGW:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/。Go是基于C的,编译的时候可能会用到gcc,g++,make等。注意把bin文件夹添加到环境变量哦。
注意安装成64位的,下面第二部分写了我踩的坑,就是误装成32位的了
Go: 官网:https://golang.org/可能需要翻墙,我前面的博客里写了怎么翻哦,或者自行搜索国内安装包。这个他会自动添加到环境变量。
注意,一定要设置好GOPATH哦
-
安装Goland编辑器
https://www.gaoredu.com/jiaocheng/jetbrains-software-activation.html
(嘘!!!其他方式我试了都不行,就这个好用)
-
配置Go代理。很重要!!!但是别人都没说
就因为我们是在国内,https://golang.org/被墙了,安装包都安装不上,运行程序都不正常,报错:
go: golang.org/x/crypto@v0.0.0-20200622213623-75b288015ac9: Get "https://proxy.golang.org/golang.org/x/crypto/@v/v0.0.0-20200622213623-75b288015ac9.mod": dial tcp 34.64.4.81:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
所以我们要设置国内代理,主要参考:https://learnku.com/go/wikis/38122
它还分Unix和Windows平台了,我觉得完全不需要分呀。
# 启用 Go Modules 功能 go env -w GO111MODULE=on # 配置 GOPROXY 环境变量,以下三选一 # 1. 七牛 CDN go env -w GOPROXY=https://goproxy.cn,direct # 2. 阿里云 go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct # 3. 官方 go env -w GOPROXY=https://goproxy.io,direct
选用七牛那个是效果最好的
#查看go的环境配置 go env
然后就在某一行可以看到
GOPROXY=”https://goproxy.cn”
这就好啦,可以随便测试一下
go get golang.org/x/text
就可以正常安装啦,输出:
E:\Ethereum\go-ethereum\rlp>go get golang.org/x/text go: golang.org/x/text upgrade => v0.3.3 go: downloading golang.org/x/text v0.3.3
-
重启Goland(重启才会应用上面的代理配置,我就没重启,不正常,愣了一会儿才想起来要重启)
-
用git或者Go下载以太坊的源代码即可,这都是基本操作。
安装git:https://git-scm.com/
#20200823更新,这里最好还是用go下载,其中涉及到包的调用问题,我之后编译就总是报错。 go get -u github.com/ethereum/go-ethereum #git clone https://github.com/ethereum/go-ethereum.git
-
在Goland里打开项目文件夹,运行
go-ethereum/rlp/decode_test.go
,如果上面go环境,代理配置好了,应该就没问题了…
=== RUN ExampleDecode — PASS: ExampleDecode (0.00s) === RUN ExampleDecode_structTagNil — PASS: ExampleDecode_structTagNil (0.00s) === RUN ExampleStream — PASS: ExampleStream (0.00s) PASS
Process finished with exit code 0
exit code是0就代码运行正常,1,2我都遇到过,哈哈哈,就是有错误。
二、编译源码
可以直接在`go-ethereum文件夹下执行
make geth
注:make命令在mingw-w64里叫mingw-w64-make.exe,复制粘贴一份改名为make即可。
但我报错
E:\Ethereum\go-ethereum>make geth env GO111MODULE=on go run build/ci.go install ./cmd/geth process_begin: CreateProcess(NULL, env GO111MODULE=on go run build/ci.go install ./cmd/geth, …) failed. make (e=2): 系统找不到指定的文件。 make: *** [Makefile:16: geth] Error 2
应该不是文件找不到,而是makefile代码写的有问题,那我就直接执行build命令去了。又报错
E:\Ethereum\go-ethereum>go run build/ci.go install ./cmd/geth
\# github.com/ethereum/go-ethereum/crypto/secp256k1
cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
cc1.exe是mingw里的命令,意思就是mingw安装的是32位的,而Go是64位的,我傻了。。。
在安装过程中一定要把Architecture
改成x86_64
呀!!!
改正完又报错
E:\Ethereum\go-ethereum>go run build/ci.go install ./cmd/geth
>>> D:\Go\bin\go install -ldflags -X main.gitCommit=54add4255064f722b33fe1c13e46db0b875fe325 -X main.gitDate=20200814 -trimpath -v ./cmd/geth
accounts\accounts.go:24:2: import "github.com/ethereum/go-ethereum" is a program, not an importable package
util.go:45: exit status 1
exit status 1
暂时不能解决。。。
20200830更新,我解决啦,还是包的引用问题,在运行go get -u github.com/ethereum/go-ethereum
后,会在GOPATH里面的文件夹里找到下载好的源文件,然后在cmd/geth/
目录下运行
go build
就完美编译啦。以下是我运行的时候的信息
C:\Users\Albert>go env -w GO111MODULE=on
C:\Users\Albert>go env -w GOPROXY=https://goproxy.cn,direct
C:\Users\Albert>go get github.com/ethereum/go-ethereum
go: downloading github.com/ethereum/go-ethereum v1.9.19
go: github.com/ethereum/go-ethereum upgrade => v1.9.19
go: downloading golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: downloading github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570
go: downloading github.com/VictoriaMetrics/fastcache v1.5.7
go: downloading github.com/go-stack/stack v1.8.0
go: downloading github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847
go: downloading github.com/shirou/gopsutil v2.20.5+incompatible
go: downloading github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6
go: downloading github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3
go: downloading github.com/cespare/xxhash v1.1.0
go: downloading github.com/golang/snappy v0.0.2-0.20200707131729-196ae77b8a26
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd
go: downloading github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6
go: downloading github.com/go-ole/go-ole v1.2.1
C:\Users\Albert>
E:\Ethereum\go-ethereum-bak\cmd\geth>go build
go: downloading github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
go: downloading github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc
go: downloading github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
go: downloading github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c
go: downloading github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150
go: downloading github.com/naoina/go-stringutil v0.1.0
go: downloading github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00
go: downloading github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222
go: downloading github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
go: downloading github.com/hashicorp/golang-lru v0.5.4
go: downloading github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
go: downloading github.com/golang/protobuf v1.3.2-0.20190517061210-b285ee9cfc6c
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356
go: downloading github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87
go: downloading gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
go: downloading github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
go: downloading golang.org/x/sync v0.0.0-20181108010431-42b317875d0f
go: downloading golang.org/x/text v0.3.2
go: downloading github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035
go: downloading github.com/mattn/go-runewidth v0.0.4
go: downloading github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
go: downloading github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989
go: downloading golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
go: downloading gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6
go: downloading gopkg.in/urfave/cli.v1 v1.20.0
go: downloading github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521
go: downloading github.com/mattn/go-colorable v0.1.0
go: downloading github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
go: downloading golang.org/x/net v0.0.0-20200625001655-4c5254603344
go: downloading github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea
go: downloading github.com/fatih/color v1.3.0
go: downloading github.com/huin/goupnp v1.0.0
go: downloading github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277
go: downloading github.com/dlclark/regexp2 v1.2.0
go: downloading github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458
go: downloading github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c
go: downloading github.com/go-sourcemap/sourcemap v2.1.2+incompatible
go: downloading github.com/opentracing/opentracing-go v1.1.0
go: downloading github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883
go: downloading github.com/holiman/uint256 v1.1.1
go: downloading github.com/pkg/errors v0.8.1
# gopkg.in/olebedev/go-duktape.v3
duk_minimal_printf.c: In function 'duk__parse_pointer':
duk_minimal_printf.c:126:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
*out = (void *) val;
^
duk_minimal_printf.c: In function 'duk_minimal_vsnprintf':
duk_minimal_printf.c:236:76: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
off = duk__format_long(str, size, off, sizeof(void *) * 2, '0', 16, 0, (unsigned long) v);
对本文有任何问题,请在下面评论: