欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

undefined: proto.ProtoPackageIsVersion3

程序员文章站 2022-07-14 16:01:39
...

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
)
相关标签: Hyperledger Fabric

上一篇: EventBus3.0 初探

下一篇: EventBus3.0 二