且知且行·区块链

【区块链】安装Filecoin(八十九)

2019-02-20  本文已影响1人  浩然H_H

今天参考官方Filecoin上手指南 安装试玩一下Filecoin。本想安装试玩,结果编译太耗时间,只完成了安装工作。

安装Filecoin及相关程序

参考官方安装文档,目前只能从源代码安装,而且只支持Linux和MacOS,Windows再次被歧视(我手头这个旧Macbook Pro是能试试的)。

官方文档是先把源码下载了再说,用下面的命令。

mkdir -p ${GOPATH}/src/github.com/filecoin-project
git clone https://github.com/filecoin-project/go-filecoin.git ${GOPATH}/src/github.com/filecoin-project/go-filecoin

但实际上,这条命令依赖于 ${GOPATH}这个环境变量,以及Go语言环境的目录。所以,最好还是先检查相关的程序与环境是否已经OK。

安装Go 和 Rust

编译go-filecoin需要的环境如下

修改环境变量

修改${GOPATH}${PATH}环境变量,和我电脑上Go的安装目录保持一致。在~/.bash_profile 中增加 export GOPATH="/Users/Hooman/go" export PATH="$PATH:$GOPATH/bin"

$ go env GOPATH
/Users/Hooman/go
$ . ~/.bash_profile
$ echo ${GOPATH}
/Users/Hooman/go

下载Filecoin源码

mkdir -p ${GOPATH}/src/github.com/filecoin-project
git clone https://github.com/filecoin-project/go-filecoin.git ${GOPATH}/src/github.com/filecoin-project/go-filecoin

安装Go-filecoin依赖的软件包

go-filecoin 依赖一些Rust写的子模块代码,需要确保cargo已经安装好了。

这些依赖的软件包用gx管理,运行下面这两条命令安装gx, gometalinter等等编译、测试相关的一堆东西:

cd ${GOPATH}/src/github.com/filecoin-project/go-filecoin
FILECOIN_USE_PRECOMPILED_RUST_PROOFS=true go run ./build/*.go deps

这会花点时间,如果发现这个错误

gx install
panic: exec: "gx": executable file not found in $PATH

那么是因为没有把${GOPATH}/bin 增加到 ${PATH} 里,照着上面把环境变量设置好再来一次。

不知为何,我的电脑上提示没找到Rust的预编译包,结果整个重新编译,用了相当长时间(远远不止一杯咖啡☕️)

using precompiled rust-proofs
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5685  100  5685    0     0   3146      0  0:00:01  0:00:01 --:--:--  3146
./scripts/install-rust-proofs.sh: line 10: jq: command not found
./scripts/install-rust-proofs.sh: line 18: jq: command not found
curl: (3) <url> malformed
usage: basename string [suffix]
       basename [-a] [-s suffix] string [...]
curl: (3) <url> malformed
asset failed to be downloaded
failed to find or obtain precompiled rust-proofs, falling back to local

趁此机会,我大致浏览了一下Filecoin 的代码介绍/Codewalk,这是一个了解代码结构的绝佳入手点,下面这张图就是摘自这里。

           ┌─────────────────────────────────────┐
           │                                     │
  Network  │  network (gossipsub, bitswap, etc.) │                 | | \/
           │                                     │                 |_| /\
           └─────▲────────────▲────────────▲─────┘
                 │            │            │           ┌────────────────────────────┐
           ┌─────▼────┐ ┌─────▼─────┐ ┌────▼─────┐     │                            │
           │          │ │           │ │          │     │    Commands / REST API     │
Protocols  │ Storage  │ │  Mining   │ │Retrieval │     │                            │
           │ Protocol │ │ Protocol  │ │ Protocol │     └────────────────────────────┘
           │          │ │           │ │          │                    │
           └──────────┘ └───────────┘ └──────────┘                    │
                 │            │             │                         │
                 └──────────┬─┴─────────────┴───────────┐             │
                            ▼                           ▼             ▼
           ┌────────────────────────────────┐ ┌───────────────────┬─────────────────┐
 Internal  │            Core API            │ │     Porcelain     │     Plumbing    │
      API  │                                │ ├───────────────────┘                 │
           └────────────────────────────────┘ └─────────────────────────────────────┘
                            │                                    │
                  ┌─────────┴────┬──────────────┬──────────────┬─┴────────────┐
                  ▼              ▼              ▼              ▼              ▼
           ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐
           │            │ │            │ │            │ │            │ │            │
     Core  │  Message   │ │   Chain    │ │ Processor  │ │   Block    │ │   Wallet   │
           │    Pool    │ │   Store    │ │            │ │  Service   │ │            │
           │            │ │            │ │            │ │            │ │            │
           └────────────┘ └────────────┘ └────────────┘ └────────────┘ └────────────┘

最终如果没错,命令的输出会类似下面这样:

$ cd ${GOPATH}/src/github.com/filecoin-project/go-filecoin
$ FILECOIN_USE_PRECOMPILED_RUST_PROOFS=true go run ./build/*.go deps
pkg-config --version
0.29.2
Installing dependencies...
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
gx install
[get ] [fetch]         QmYxivS34F2M2n44WQQnRHGAKS8aoRUxwGpi9wk4Cdn4Jf go-libp2p
......
......
gx-go rewrite5s
go get -u github.com/alecthomas/gometalinter
gometalinter --install
......
......
go get -u github.com/jstemmer/go-junit-report
go get -u github.com/pmezard/go-difflib/difflib
./scripts/install-rust-proofs.sh
......
......
./scripts/copy-groth-params.sh
/tmp/filecoin-proof-parameters ~/go/src/github.com/filecoin-project/go-filecoin
~/go/src/github.com/filecoin-project/go-filecoin

编译、测试、安装

# First, build the binary
go run ./build/*.go build

# Install go-filecoin to ${GOPATH}/bin (necessary for tests)
go run ./build/*.go install

# Then, run the tests.
go run ./build/*.go test

# Build and test can be combined!
go run ./build/*.go best

上面这些命令,只需执行前三个,或者最后一个三合一版本的命令。注意,第三个命令是执行测试,测试的时间其实也挺长的。另外一些编译时用得上的命令如下,

# Check the code for style and correctness issues
go run ./build/*.go lint

# Test with a coverage report
go run ./build/*.go test -cover

# Test with Go's race-condition instrumentation and warnings (see https://blog.golang.org/race-detector)
go run ./build/*.go test -race

# Deps, Lint, Build, Test (any args will be passed to `test`)
go run ./build/*.go all

如果遇到问题就可以去查询一下Troubleshooting & FAQ Wiki 页面,比如我遇到的"panic: exec: gx-go executable file is not found in $PATH"这个错误,在这里就有类似解决方案。

上一篇 下一篇

猜你喜欢

热点阅读