undefined: proto.ProtoPackageIsVersion3

undefined: proto.ProtoPackageIsVersion3

Hyperledger Fabric使用了大量的grpc,如果需要修改客戶端和服務端的通信消息,就需要修改相關的proto文件。

按照gRPC官網的教程,安裝gRPC、Protocol BUffers v3以及protoc的Go插件,並測試google.golang.org/grpc/examples/helloworld。

go get -u google.golang.org/grpc

直接下載編譯好的proto,解壓後設置好環境變量,mac上第一次使用需要在安全性於隱私上,點擊仍然允許

➜  ~ protoc --version
libprotoc 3.11.1
➜  ~ 

安裝protoc-gen-go插件

go get -u github.com/golang/protobuf/protoc-gen-go

測試:

編譯proto文件

➜  helloworld git:(master)ls
greeter_client  greeter_server  helloworld      mock_helloworld
➜  helloworld git:(master)rm helloworld/helloworld.pb.go*
➜  helloworld git:(master) ✗ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
➜  helloworld git:(master) ls helloworld
helloworld.pb.go helloworld.proto
➜  helloworld git:(master) ls
greeter_client  greeter_server  helloworld      mock_helloworld

啓動服務端

➜  helloworld git:(master) go run greeter_server/main.go
2019/12/19 15:41:47 Received: world
^Csignal: interrupt
➜  helloworld git:(master)

啓動客戶端

➜  helloworld git:(master) go run greeter_client/main.go
2019/12/19 15:41:47 Greeting: Hello world

一切順利!


根據需要修改chaincode.proto(Fabric v1.4.2),並重新生成chaincode.pb.go

protoc -I protos protos/peer/chaincode.proto --go_out=$GOPATH/src

生成正常

➜  fabric git:(c6cc550cb)rm protos/peer/chaincode.pb.go 
➜  fabric git:(c6cc550cb) ✗ protoc -I protos protos/peer/chaincode.proto --go_out=$GOPATH/src
➜  fabric git:(c6cc550cb)ls protos/peer 
admin.pb.go              chaincode.proto          chaincode_shim.proto     configuration.proto      lifecycle                proposal.pb.go           proposal_response.proto  resources.proto          transaction.pb.go
admin.proto              chaincode_event.pb.go    chaincodeunmarshall.go   events.pb.go             peer.pb.go               proposal.proto           query.pb.go              signed_cc_dep_spec.pb.go transaction.proto
chaincode.go             chaincode_event.proto    configuration.go         events.proto             peer.proto               proposal_response.go     query.proto              signed_cc_dep_spec.proto
chaincode.pb.go          chaincode_shim.pb.go     configuration.pb.go      github.com               proposal.go              proposal_response.pb.go  resources.pb.go          transaction.go
➜  fabric git:(c6cc550cb)

測試運行,報出undefined: proto.ProtoPackageIsVersion3


➜  fabric git:(c6cc550cb) ✗ go run peer/main.go
go: finding golang.org/x/tools v0.0.0-20181026183834-f60e5f99f081
# github.com/hyperledger/fabric/protos/peer
protos/peer/chaincode.pb.go:21:11: undefined: proto.ProtoPackageIsVersion3
➜  fabric git:(c6cc550cb)

看下生成的chaincode.pb.go

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: peer/chaincode.proto

package peer

import (
	fmt "fmt"
	proto "github.com/golang/protobuf/proto"
	math "math"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package   #----->生成了v3版本的

// Confidentiality Levels
type ConfidentialityLevel int32

const (
	ConfidentialityLevel_PUBLIC       ConfidentialityLevel = 0
	ConfidentialityLevel_CONFIDENTIAL ConfidentialityLevel = 1
)

這裏生成的是v3版本的

const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

再看下其他proto生成的pb.go文件

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: peer/peer.proto

package peer // import "github.com/hyperledger/fabric/protos/peer"

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"

import (
	context "golang.org/x/net/context"
	grpc "google.golang.org/grpc"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package  //-------->Fabric v1.4.2使用的是v2版本的

type PeerID struct {
	Name                 string   `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

...

這裏兩個版本不一致,導致了編譯異常

const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package  //-------->Fabric v1.4.2使用的是v2版本的

我們安裝的是最新版本的protobuf和protoc-gen-go都是是v3版本的,但是Fabric v1.4.2指明瞭v2版本,所以我們修改的部分proto後就沒法跟其他proto生成的v3文件進行編譯。


接下來就是如何安裝v2版本的相關工具。
參考undefined:proto.ProtoPackageIsVersion3,確定將protobuf check到v1.2.0重新編譯可以得到v2的編譯器。

➜  protobuf git:(master) cd $GOPATH/src/github.com/golang/protobuf
➜  protobuf git:(master) ls
AUTHORS       LICENSE       README.md     go.mod        jsonpb        protoc-gen-go regenerate.sh
CONTRIBUTORS  Makefile      descriptor    go.sum        proto         ptypes
➜  protobuf git:(master) git checkout -f v1.2.0
Note: checking out 'v1.2.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at aa810b61 proto: fix handling of required fields after multiple violations (#679)

➜  protobuf git:(aa810b61) cd proto-gen-go
cd: no such file or directory: proto-gen-go
➜  protoc-gen-go git:(aa810b61) go install

重新生成pb.go文件,生成了v2的文件
編譯測試peer

➜  fabric git:(c6cc550cb) ✗ protoc -I protos protos/peer/chaincode.proto --go_out=$GOPATH/src
➜  fabric git:(c6cc550cb) ✗ 
➜  fabric git:(c6cc550cb) ✗ go run peer/main.go 
Usage:
  peer [command]

Available Commands:
  chaincode   Operate a chaincode: install|instantiate|invoke|package|query|signpackage|upgrade|list.
  channel     Operate a channel: create|fetch|join|list|update|signconfigtx|getinfo.
  help        Help about any command
  logging     Logging configuration: getlevel|setlevel|getlogspec|setlogspec|revertlevels.
  node        Operate a peer node: start|status|reset|rollback.
  version     Print fabric peer version.

Flags:
  -h, --help   help for peer

Use "peer [command] --help" for more information about a command.
➜  fabric git:(c6cc550cb)

再看下生成的pb.go文件,確是v2的

// Code generated by protoc-gen-go. DO NOT EDIT.
// source: peer/chaincode.proto

package peer // import "github.com/hyperledger/fabric/protos/peer"

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package  //----->v2

// Confidentiality Levels
type ConfidentialityLevel int32

const (
	ConfidentialityLevel_PUBLIC       ConfidentialityLevel = 0
	ConfidentialityLevel_CONFIDENTIAL ConfidentialityLevel = 1
)
發佈了412 篇原創文章 · 獲贊 68 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章