Fabric.Peer源码分析
这部分,主要分析链码安装到实例化(启动容器)的过程。操作链码是通过peer chaincode命令进行的,这就是我们所谓的一个命令行的客户端,通过它我们就可以和fabric交互,注意Peer服务是通过peer node start启动的,即启动一个peer节点。
以下是从e2e_cli/scripts/scripts.sh提取的安装、实例化链码的命令,注意该命令前,要先完成创建通道和加入通道。
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/example02/cmd >&log.txt
peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "AND ('O rg1MSP.peer','Org2MSP.peer')" >&log.txt
cobra:一个用来实现cli的golang package
在阅读Fabric源码前,我们要对go package cobra有个基本的了解,这是一个命令行封装包,能够很方便的实现命令行,但是它的存在,对我们要分析的核心代码,进行了多层封装,又有Fabric的本地调试问题,无法很好的分析数据流,所以这是一个困扰,但熟悉之后会好很多。另外,Fabric使用大量的桩函数,包装函数,对我们分析源码也会造成一定的难度。
Cobra官方地址,通过一个小demo,来了解cobra的基本内容。
先看下,cobra的demo,cobra初始化会创建cmd和main.go,我们在cobrademo下创建开发包simple,之后在cmd/root.go中导入,并做适当修改即可。
liudeMacBook-Pro:cobrademo liu$ cobra init cobrademo
Your Cobra application is ready at
/Users/liu/work/go/src/cobrademo
Give it a try by going there and running `go run main.go`.
liudeMacBook-Pro:src liu$ tree -L 2 cobrademo/
cobrademo/
├── LICENSE
├── cmd
│ └── root.go
├── main.go
└── simple
└── show.go
2 directories, 4 files
在main.go只有一个命令行的执行操作,cmd是准备工作.
func main() {
cmd.Execute()
}
simple是我们的开发包,创建了一个show函数,后边在cmd中绑定到命令行。
liudeMacBook-Pro:cobrademo liu$ cat simple/show.go
package simple
import "fmt"
func Show(name string, age int) {
fmt.Printf("My name is %s, my age is %d\n", name, age)
}
在cmd/root.go中创建了一个cobra.Command对象,我们的开发的show函数就绑定到这个对象的Run成员中。
var name string
var age int
var rootCmd = &cobra.Command{
Use: "cobrademo",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) {
},
Run: func(cmd *cobra.Command, args []string) {
if len(name) == 0 {
cmd.Help()
return
}
simple.Show(name, age)
},
}
之后,在通过设置Flags,就可以解析命令行参数,调用对应的函数。
rootCmd.Flags().StringVarP(&name, "name", "n", "", "persion's name")
rootCmd.Flags().IntVarP(&age, "age", "a", 0, "person's age")
在绑定函数,设置好参数Flags,通过cmd.Execute()进入了执行阶段,解析命令行参数,执行对应函数。
liudeMacBook-Pro:cobrademo liu$ go run main.go
A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.
Usage:
cobrademo [flags]
Flags:
-a, --age int person's age
-h, --help help for cobrademo
-n, --name string persion's name
liudeMacBook-Pro:cobrademo liu$ go run main.go -a 10 -n test
My name is test, my age is 10
liudeMacBook-Pro:cobrademo liu$
注意: cobra还可以通过AddCommand添加子命令,这个我们会经常遇到,例如peer/main.go中,添加各子命令。
//添加子命令
mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(node.Cmd())
mainCmd.AddCommand(chaincode

最低0.47元/天 解锁文章
1813

被折叠的 条评论
为什么被折叠?



