Protobuf
message and service definitions allows to model chaincode in high level Interface Definition Language (IDL)- Code generation allows to automate development process of creating API's, SDK, documentation for chainode
- Define model using
.proto
file - Generate code and documentation with generators
- Implement chaincode as service and tests
- Create chaincode binary
- Create API
Generators allows to automatically build lot of useful code and docs : Golang structures, validators, gRPC service interface and client, documentation in Markdown format and Swagger specification, chaincode gateway for implementing API or SDK and mapper for embedding strong typed gRPC service to chaincode implementation.
Go support for Protocol Buffers
A protoc
plugin that generates Validate() error
functions on Go proto structs based on field options inside
.proto
files.
Provides HTTP JSON interface to gRPC service. A small amount of configuration in your service to attach HTTP semantics is all that's needed to generate a reverse-proxy with this library. Optionally emitting API definitions for OpenAPI (Swagger) v2.
documentation generator plugin for the Google Protocol Buffers compiler (protoc). The plugin can generate HTML, JSON, DocBook and Markdown documentation from comments in your .proto files.
cd geterators && ./install.sh
This will place five binaries in generators/bin;
protoc-gen-go
protoc-gen-govalidators
protoc-gen-grpc-gateway
protoc-gen-swagger
protoc-gen-doc
outside of your $GOPATH
# go mod init {put your module name here}
(for example `github.com/s7techlab/hyperledger-fabric-samples)
gRPC technology stack natively supports a clean and powerful way to specify service contracts using the Interface Definition Language (IDL):
- messages defines data structures of the input parameters and return types.
- services definition outlines methods signatures that can be invoked remotely
Chaincode messages and service allows to define chaincode interface and data schema.
Create proto/Makefile for compiling .proto
to Golang
code
.: generate
generate:
@protoc --version
@echo "commercial paper schema proto generation"
@protoc -I=./ \
-I=${GOPATH}/src \
-I=${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=plugins=grpc:./ \
--govalidators_out=./ \
--grpc-gateway_out=logtostderr=true:./ \
--swagger_out=logtostderr=true:./ \
--doc_out=./ --doc_opt=markdown,commercial-paper.md \
--cc-gateway_out=logtostderr=true:./ \
./*.proto
and run it. Following files will be generated:
- commercial-paper.md -documentation
- commercial-paper.pb.cc.go - gateway for chaincode
- commercial-paper.pb.go - Golang structs and gRPC service
- commercial-paper.pb.gw.go - gRPC gateway
- commercial-paper.swagger.json - Swagger specification
- commercial-paper.validator.pb.go - validators
Standard commands like go build
or go test
will automatically add new dependencies as needed to
satisfy imports (updating go.mod and downloading the new dependencies).
# go mod vendor
This command add dependencies to go.mod file and download it to vendor directory Go.mod file will contain:
module github.com/s7techlab/hyperledger-fabric-samples
go 1.12
require (
github.com/golang/protobuf v1.3.2
github.com/grpc-ecosystem/grpc-gateway v1.9.5
github.com/hyperledger/fabric v1.4.4
github.com/mwitkow/go-proto-validators v0.0.0-20190709101305-c00cd28f239a
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/pkg/errors v0.8.1
github.com/s7techlab/cckit v0.6.9
google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64
google.golang.org/grpc v1.22.1
)
Implement chaincode service and chaincode and
test. After that don't forget to call go mod vendor
to download newly added dependencies (Ginkgo and Gomega)
You can test chaincode service with command
#chaincode go test -mod vendor
Check test code coverage: chaincode logic must be covered by test to maximum level. Code coverage of 70-80% is a reasonable goal for system test of most projects with most coverage metrics
#chaincode go test -mod vendor -coverage
With CCKit gateway and generated gRPC service server and gRPC gateway quite ease to implement API for chaincode.
You can run provided mocked example using command
# cd commercial-paper/api/mock
# go run main.go
Then you can use API usage examples and sample payloads:
grpc-gateway
will automatically converts http request to gRPC call, input JSON payloads to protobuf, invokes chaincode
service and then converts returned value from protobuf to JSON. You can also use this service as pure gRPC service.
Chaincode methods can be called with generated gRPC client.
Swagger specification, service and schema documentation are also auto-generated.