当前位置:首页区块链那么什么是超账本结构中的Protobuf呢?

那么什么是超账本结构中的Protobuf呢?

在本文中,我将解释如何使用Protobuf在超账本结构中序列化和构造数据。Protobuf简化了超级账本结构中的数据处理和格式化过程。它使用特殊生成的源代码来生成数据,这些数据可以在同一个智能契约中轻松地编写和读取

那么什么是超账本结构中的Protobuf呢?

作者是| Deeptiman Pattnaik

协调编辑|卡罗尔

|区块链camp (blockchain_camp)

在本文中,我将解释如何使用Protobuf在超账本结构中序列化和构造数据。Protobuf简化了超级账本结构中的数据处理和格式化过程。它使用特殊生成的源代码来生成数据,这些数据可以在同一个智能契约中轻松地编写和读取。

Chaincode和SmartContract

在超级账本结构中,Chaincode是一个特定的程序,用于处理区块链网络中参与者一致同意的核心业务逻辑。Hyperledger fabric还使用了一种名为SmartContract的数据格式技术,该技术是为Chaincode中的一组特定数据模定义的。Chaincode可以有多组smartcontract来控制不同数据模的事务逻辑。简单地说,SmartContract管理事务,而Chaincode管理如何部署SmartContract。

例如,如果某些用户信息记录需要存储在分类帐中,那么我们需要一个SmartContract来定义一条记录所需的数据字段。

用户(用户)SmartContract

TypeUserstruct {IDstring ' json: "id" ' Emailstring ' ' json: "email" ' Namestring ' ' json: "name" ' Mobilestring ' ' json: "mobile" ' Agestring ' ' age ' '}

在SmartContract中,有ID、电子邮件、姓名、手机、年龄和其他与个人用户记录相关的数据。同样,如果我们需要存储每个用户的财务记录,我们需要另一个名为Financial的smartcontract。

金融SmartContract

{IDstring ' json:" id" ' BankNamestring ' ' json:" bankName" ' IFSCCodestring ' ' ifscCode ' AccNumberstring ' ' json:" accNumber ' CreatedDatestring ' '

这两个smartcontract将被部署到Chaincode,并处理这两个分类状态。AMPL 区块链的世界状态和事务逻辑。

SmartContract在世界状态下主要执行Put、Get、Delete和GetHistory操作。

1. PutState—AMPL 为每个不同的键创建新对象,或覆盖现有对象。

2. GetState AMPL ndash;AMPL 从分类帐状态检索具有不同键值的对象。

3.DelState AMPL AMPL 从分类账的世界状态中移除对象。

4. GetHistoryForKey AMPL AMPL 返回跨时间键的所有事务的历史记录。

所有记录都作为世界状态记录存储在CouchDB中。对象以JSON格式存储为键-值对。CouchDB可以从数据库快速查询JSON集合。在区块链状态下,所有这些记录都以字节存储,并且是不可变的。

Protobuf

协议缓冲区(protobuf)是谷歌的序列化结构化数据,它是语言和平台无关的,具有可扩展的机制。与传统的数据格式(如XML或JSON)相比,序列化的结构化数据是按字节编译的,因此它更小、更快、更简单。

为什么使用Protobuf ?

我们可以看到,有两个smartcontract, User和Financial,存储在同一个用户的分类账中,即User存储用户的基本信息,Financial存储用户银行账户的详细信息。

但是,如果我们从查询目的的角度来看smartcontract,两个数据集之间没有关系。我们不能为用户和金融数据模定义相同的ID作为键,因为分类帐数据存储在键-值对中,如果出现相同的键,则信息将被覆盖。

那么什么是超账本结构中的Protobuf呢?1

这两条记录以两种不同的id存储在分类帐状态中

为了解决这个问题,Protobuf提供了一个更快、更灵活的解决方案。我们要做的就是写出a。描述数据结构的原文件,在本例中,是我们想要存储的金融数据结构。

因此,protobuf消息格式中的字节结果被直接调用给用户SmartContract, Financial SmartContract被完全删除。

Protobuf是如何工作的?

接下来,我们将了解如何设置protobuf编译器并生成protobuf消息格式。

安装

首先,我们需要遵循安装过程来使用protobuf编译器。

$gogetgithub.com/golang/protobuf $gogetgithub.com/golang/protobuf/proto $goget-ugithub.com/golang/protobuf/protoc-gen-go exportPATH =路径:美元美元GOPATH / bin

现在,安装protobuf编译器

$sudoaptinstallprotobuf -编译器

然后,在命令行上输入protoc’。它应该显示;输入fileAMPLrsquo失踪;(缺少输入文件),这意味着成功安装了protobuf编译器。

样例

首先,我们需要创建一个财务。原文件。它由Financial类的消息格式组成,包含四个字段:银行名称、ifsc代码、帐号和创建日期。

金融。Protosyntax = " proto3”;Packagemain;MessageFinancial {stringbankName = 1;StringifscCode = 2;StringaccNumber = 3;StringcreatedDate = 4;}

编译原文件,生成用于财务消息格式的protobuf数据模文件。

$protoc——go_out =*原

您将看到protobuf文件已经作为financial.pb.go生成。此文件是与proto包兼容的数据模,将用于将proto消息格式转换为字节。

金融。Pb。/ / Codegeneratedbyprotoc - gen - Go。DONOTEDIT。/ /资料来源:财务。Protopackagemainimport (FMT "FMT" proto github.com/golang/protobuf/proto "math") / / referenceimportstosuppresserrorsif他们没有其他用途。Var_ =原。Marshalar_ = FMT。Errorfar_ = math.h Inf / / Thisisacompile - timeassertiontosurETHisgeneratedfile / /与protopackageitisbeingcompiledagainst兼容。/ / a ompilationerrat this linelikelymeansyourcopyof / / protopackageneedstobeupdated。Const_ =原。protopackagetypefinancialstruct {BankNamestring ' protobuf:“字节,1、选择的名字= bankName p Roto3 " json:“bankName, omitempty”“IfscCodestring”protobuf:“字节,2、选择、名称= ifscCode proto3“json:“ifscCode, omitempty”“AccNumberstring”protobuf:“字节、3、选择、名称= accNumber proto3“json:“accNumber omitempt Y”“CreatedDatestring”protobuf:“字节,4、选择、名称= createdDate proto3“json:“createdDate,omitempty“XXX_NoUnkeyedLiteralstruct{}的json:“——”XXX_unrecognized byte [] json:”——“XXX_sizecacheint32 json:”——“‘} func (M *(金融)重置()M ={*金融{}}func (M *金融)字符串String () {returnproto.Com pactTextString (M)} func(*金融)ProtoMessage () {func(*金融)描述符()(byte [] [] int) {returnfileDescriptor_a283e Be7677acfbc, int [] {0}} func (M *金融)XXX_Unmarshal byte [] (b) {returnxxx_messageInfo_Financial的错误。func (m * Financial) XXX_Marshal (byte [], b确定性bool) (byte [], the error) {returnxxx_message Info_Financial。marshall (b, m,确定性)}func (m * Financial) XXX_Merge (srcproto)消息){xxx_messageInfo_Financial。func (m * Financial) XXX_Size (int) {returnxxx_messageInfo_Financial。Size (m)}是一个有趣的函数C (m * Financial) xxx_dunknown () {xxx_messageInfo_Financial。DiscardUnknown (m)} arxxx_messageInfo_Financialproto。imfofunc (m * Financial) GetBankName (string) {ifm!。= nil {returnm BankName} return ""} func (m * Financial) GetIfscCode (string) {ifm!。= nil {returnm IfscCode} return ""} func (m * Financial) GetAccNumber (string) {ifm!。= nil {returnm AccNumber} return ""} func (m * Financial) GetCreatedDate (string) {ifm!。= nil {returnm CreatedDate} return ""} funcinit () {proto。RegisterType ((nil), Financial(*)”main Financial ")} funcinit () {proto。RegisterFile(“金融。原,“fileDescriptor_a283ebe7677acfbc)} arfileDescriptor_a Ebe7677acfbc = 283字节[]{x8b bytesofagzippedfiledescriptorproto0x1f / / 136, 0, 0 (0 x00 0 x00 0 x00到0 x00 0 x00 0 x02, 0 XFF, 0 xe2 0 xe2 0 x4f 0 XCB 0 XCC, 0 x4b 0 XCC, 0 x4b xce 0, 0 x4c 0 XCC, 0 xd1 0 x2b 0 x28 0 xca 0 x2f 0 xc9 0 x1 7 0 x62 0 xc9 0 x4d 0 XCC, 0 XCC, 0 x53 0 x6a 0 x66 0 xe4 0 xe2 0 x74 0 x83 0 xc9, 0 (0 x49 0 x71 0 x71 0 x24 0 25, 0 xe6 0 x65 0 XFB 0 25,0 xe6, 0 xa6 0 x4a 0 x30, 0 x2a 0 x30 0 x6a 0 x70、0 x06 0 xc1之前,0 xf9 0 x20, 0 xb9 0 XCC, 0 xb4 0 xe2 0 x6 XFC 4 0 xe7, 0, 0, x94 x54 0, 0 x09 0 x26 0 x88 0 x1c 0 x8c x2f, 0 0 x24 xc3 0, 0 xc5 0 x99 0 x98 0 x9c xec 0, 0 x57 0 x9a 0 x9b 0 x94 0 x5a 0 x24 0 xc1之前,0 x0c 0 x96, 0 x44, 0 (0 (0 x29 0 x70、0 x71 0 x27 0 x17, 0 xa5 0 x26 0 x96, 0 xa4 0 xa6 0 xb xa4 8 0 x24 0 x96, 0, 0 x4a 0 xb0 0 x80 0 xe5 0 x91 0 x85, 0 x92 0 xd8 0 xc0 0 x4e 0 x32,0 x06, 0 x04, 0 x00到0 XFF, 0 XFF, 0 x44, 0 x01, 0 xf8, 0 x14, 0 xa5, 0 x00到0,现在我们将在User smartcontract字段is financial中创建一个附加数据。{id: "id" " Emailstring " "email" " Namestring " "name" " Mobilestring " "mobile" " Agestring " "age" " Financialstring " "financial" '}

财务信息格式参考

金融:= 金融{ID: "F1"银行名称:" HellenicBank IFSCCode:" 1234 ",账号:" 8765",创建日期:" 12/12/08,}

当您将用户记录添加到分类账时,您还可以将财务消息格式添加到相同的用户smartcontract。

Packagemainimport (" FMT "" log" "github.com/golang/protobuf/proto") funcmain () {financial: = AMPL AMPLFinancial {IFSCCode BankName: "HellenicBank": "1234", AccNumber: "8765", CreatedDate: "12/12/08,} financialdata, err: = proto。元帅(金融)iferr != nil {the Fatal (" marshalingerror: "err)} userdata: = AMPL AMPL用户{ID: "1", Email: "[protected] Email AMPL # 160;" Name: "James ", " Mobile: "8765432",年龄:"34",Financial: string (financialdata),} userDataJSONasBytes, err: = json。元帅(用户数据)iferr != nil {returnshim。indexName: = "id" userNameIndexKey, err: = stub。创建ecompositekey (indexName, string [] {userdata)。Id}) iferr != nil {returnshim。Error (err, Error ())} err =存根。PutState (userNameIndexKey, userDataJSONasBytes) iferr!= nil {returnshim。错误(犯错。错误())

解析Protobuf

解析protobuf数据很简单,因为它以字节的形式存储记录,可以使用Financial proto轻松地解析这些记录。

FinancialByteData, err: =原。元帅(financialData) iferr != nil {the Fatal (" marshalingerror: "err)} / / Parsingthefinancialbytedatafinancial: = AMPL AMPLFinancial {} err = proto。整理(金融数据,金融)= nil {the Fatal (" unmarshalingerror: "err)} FMT。Println (" BankName:" + financial。FMT GetBankName ()。Println (" IFSCCode: "+ financial。FMT GetIfscCode ()。Println (" AccNumber:" + financial。FMT GetAccNumber ()。Println (" CreatedDate: "+ financial GetCreatedDate ())

结论

Protobuf简化了数据处理和格式化。它使用特殊生成的源代码来构造数据,使得在同一个smartcontract中编写和读取数据变得很容易。

参考

1. https://deelopers.google.com/protocol-buffers

2. https://deelopers.google.com/protocol-buffers/docs/gotutorial

这是对Protobu的基本概述它将被用于超账本结构一直到SmartContract。

希望你觉得有用!

原始链接:https://hackernoon.com/what-protobuf -in-hyperledger- fabric-explaining-gk7s32fz

温馨提示:

文章标题:那么什么是超账本结构中的Protobuf呢?

文章链接:https://www.btchangqing.cn/21655.html

更新时间:2020年05月17日

本站大部分内容均收集于网络,若内容若侵犯到您的权益,请联系我们,我们将第一时间处理。

那么什么是超账本结构中的Protobuf呢?2
区块链

被勒索后,大学生程序员如何反击?

2020-5-17 17:58:46

区块链

波卡是什么?看看这篇文章你就知道

2020-5-17 18:45:58

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索