Edit me

[TOC]

通信机制

Fabric是使用grpc进行通信的,因此它需要使用自定义的proto文件,我们可以通过解析这些文件来了解fabric的消息类型。 文件的目录结构如下:

pct@Chandler:~/go/src/github.com/hyperledger/fabric/protos$ tree -L 1
.
├── common
├── gossip
├── idemix
├── ledger
├── msp
├── orderer
├── peer
├── testutils
└── utils

9 directories, 0 files

proto协议

  • import可以导入已存在的proto文件。
  • 生成go文件:
     protoc --go_out=plugins=grpc:. tx.proto 
    
  • repeated可以用来生成如数组列表等变量,它代表修饰的变量可以多个循环
    message BlockData {
      repeated bytes data = 1;
    }
    
  • 变量后面的标号表示变量在结构体中的位置指示,不能重复,枚举的第一个变量的标号必须为0

    注:[1,15]之内的标识号在编码的时候会占用一个字节。[16,2047]之内的标识号则占用2个字节。所以应该为那些频繁出现的消息元素保留 [1,15]之内的标识号。切记:要为将来有可能添加的、频繁出现的标识号预留一些标识号。

  • 支持的数据类型: 02-1


  • common目录

    common目录下是基础的数据类型,我们首先打开common.proto文件,看看里面的定义:

    import "google/protobuf/timestamp.proto";
    

    首先,导入了官方的时间戳proto,这个文件是在安装proto时安装的,目录为:

    ./src/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto
    

    里面定义了一个时间戳的消息类型: ``` message Timestamp {

// Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. int64 seconds = 1;

// Non-negative fractions of a second at nanosecond resolution. Negative // second values with fractions must still have non-negative nanos values // that count forward in time. Must be from 0 to 999,999,999 // inclusive. int32 nanos = 2; }

我们可以使用这个消息类型定义自己的消息变量:

google.protobuf.Timestamp timestamp = 3; ```

Tags: