环境搭建
下载代码后,需要修改一下路径,否则不能够解析到vendor里的代码,目录结构如下:
pct@Chandler:~/workspace/ABA/Codes$ tree -L 5
.
├── Chorus
│ └── src
│ └── github.com
│ └── Baptist-Publication
│ └── chorus
工程设置如下:
代码模块
程序分成好几个部分进行编译:
.PHONY: chorus chorustool test all
all: chorus chorustool test
chorus:
go build -gcflags "-N -l" -ldflags "-X github.com/Baptist-Publication/chorus/src/chain/version.commitVer=`git rev-parse HEAD`" -o ./build/chorus ./src/chain
chorustool:
go build -ldflags "-X github.com/Baptist-Publication/chorus/src/client/main.version=`git rev-parse HEAD`" -o ./build/chorustool ./src/client
test:
go test ./src/tools/state
proto:
protoc --proto_path=$(GOPATH)/src --proto_path=src/chain/app/remote --go_out=plugins=grpc:src/chain/app/remote src/chain/app/remote/*.proto
protoc --proto_path=$(GOPATH)/src --proto_path=src/example/types --go_out=plugins=grpc:src/example/types src/example/types/*.proto
#protoc --proto_path=$(GOPATH)/src --proto_path=src/chain/node/protos --gofast_out=plugins=grpc:src/chain/node/protos src/chain/node/protos/*.proto
protoc --proto_path=src/types --go_out=src/types src/types/*.proto
我们先看chorus部分。
chorus模块
主函数
主函数文件位置为:/home/pct/workspace/ABA/Codes/Chorus/src/github.com/Baptist-Publication/chorus/src/chain/main.go main函数主体很简单:
func main() {
defer xlog.DumpStack()
cmd.Execute()
}
第一个函数是用来获取程序运行错误,并且将错误输入到日志中的。 第二个函数执行到下面文件中: /home/pct/workspace/ABA/Codes/Chorus/src/github.com/Baptist-Publication/chorus/src/chain/cmd/root.go 这个文件调用了两个开源库:
"github.com/spf13/cobra"
"github.com/spf13/viper"
其中,第一个库是用来实现命令行功能的,类似于option,第二个库用来实现读取配置的功能。 上面的Execute执行了第一个cobra库的启动,cmd/root.go文件还执行了初始化init:
func init() {
cobra.OnInitialize(initConfig, initApp)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringP("runtime", "r", "", fmt.Sprintf("angine runtime dir (default is $ANGINE_RUNTIME/%v)", agconf.DEFAULT_RUNTIME))
RootCmd.PersistentFlags().StringP("config", "c", "", "config file (default is $CIVIL_CONFPATH/.chorus.toml)")
viper.BindPFlag("runtime", RootCmd.PersistentFlags().Lookup("runtime"))
viper.BindPFlag("config", RootCmd.PersistentFlags().Lookup("config"))
}
initConfig函数设置了配置文件的路径和名称,为用户目录下的.chorus。这里并没有设置格式,格式的设置应该在其他地方。 我们启动程序使用的命令是chorus run,这部分的实现是在文件: /home/pct/workspace/ABA/Codes/Chorus/src/github.com/Baptist-Publication/chorus/src/chain/cmd/run.go 中:
Run: func(cmd *cobra.Command, args []string) {
env := viper.GetString("environment")
logpath := viper.GetString("log_path")
if logpath == "" {
var err error
if logpath, err = os.Getwd(); err != nil {
cmd.Println(err)
os.Exit(1)
}
}
viper.Set("log_path", logpath)
logger := log.Initialize(env, path.Join(logpath, "node.output.log"), path.Join(logpath, "node.err.log"))
node.RunNode(logger, viper.GetViper())
},
外部引用
从上而下的读代码会导致层次太多而没有头绪,我们先整理一下这个项目用到的各种三方库,以及每个库的作用,这样就做到有基本的认知了。
目录结构
三方库都是放在vendor目录下的,它的目录结构如下:
pct@Chandler:~/workspace/ABA/Codes/Chorus/src/github.com/Baptist-Publication/chorus/vendor$ tree -L 2
.
├── github.com
│ ├── Baptist-Publication
│ ├── bford
│ ├── btcsuite
│ ├── fsnotify
│ ├── gogo
│ ├── go-kit
│ ├── golang
│ ├── go-logfmt
│ ├── go-playground
│ ├── gorilla
│ ├── go-stack
│ ├── hashicorp
│ ├── inconshreveable
│ ├── jmhodges
│ ├── jmoiron
│ ├── kr
│ ├── lestrrat
│ ├── magiconair
│ ├── mattn
│ ├── mitchellh
│ ├── patrickmn
│ ├── pelletier
│ ├── pkg
│ ├── rcrowley
│ ├── spf13
│ ├── syndtr
│ ├── tendermint
│ ├── utahta
│ ├── vmihailenco
│ └── yuin
├── golang.org
│ └── x
├── google.golang.org
│ └── grpc
├── gopkg.in
│ ├── go-playground
│ ├── karalabe
│ ├── urfave
│ └── yaml.v2
├── go.uber.org
│ ├── atomic
│ ├── multierr
│ └── zap
├── layeh.com
│ ├── gopher-json
│ └── gopher-luar
└── vendor.json
github.com/Baptist-Publication/angine
Angine的介绍如下:
Angine is a completely self-contained blockchain consensus engine. At the core, we use Tendermint BFT. It is an implementation of a Byzantine Fault Tolerant PoS consensus algorithm. So thank you Tendermint team. We just wrap those many things that tendermint already offered together under a concept, “Angine”.
Angine 是一个实现拜占庭容错机制的共识引擎模块,用来实现区块链的共识机制。
github.com/Baptist-Publication/chorus-module
chorus-module是作者自己实现的区块链主要功能的一些模块,包括P2P通信,数据结构,配置,加密,总线等功能。
github.com/bford/golang-x-crypto/ed25519
实现加密算法
github.com/btcsuite/btcd
btcd是一个用go实现的完整的比特币节点,本工程用到了里面的btcec包,这个包实现了比特币的椭圆曲线加密算法。
github.com/fsnotify/fsnotify
从描述文档看,这个包实现了平台下文件的通知管理功能。
github.com/go-kit/kit
这个包是用go构建微服务时用到的工具集,它解决了一些跨平台分布式的常见问题。
github.com/go-logfmt/logfmt
实现了logfmt格式数据的打包和解析过程。
github.com/go-playground/locales
实现多语言的Unicode编码集
github.com/go-playground/universal-translator
一个国际化的golang库
github.com/go-stack/stack
实现了对堆栈的捕捉,操作和格式化等功能
github.com/gogo/protobuf
谷歌定义的一种通信格式
github.com/jmhodges/levigo
对levelDB的封装
github.com/urfave/cli
实现命令行APP的工具包