从源码中解析fabric区块数据结构(一)

从源码中解析fabric区块数据结构(一)

前言

最近打算基于fabric-sdk-go实现hyperledger fabric浏览器,其中最重要的一步就是解析fabric的上链区块。虽说fabric是Golang实现的,但直到2021年2月1号才发布了第一个稳定版fabric-sdk-go,而且官方几乎没有响应的文档介绍。对于fabric-sdk-go,基本都是参照源码中的测试用例来使用;而要实现区块链浏览器,仅靠测试用例还差好多,特别是对出块信息的解析。

通过event.Client.RegisterBlockEvent()可以监听fabric的出块事件,但返回的信息中有用的是一个BlockEvent类型的chan,定义如下:

// BlockEvent contains the data for the block event
type BlockEvent struct {
// Block is the block that was committed
Block *cb.Block
// SourceURL specifies the URL of the peer that produced the event
SourceURL string
}

区块链浏览器需要的所有信息基本都包含在Block中,其定义如下:

// This is finalized block structure to be shared among the orderer and peer
// Note that the BlockHeader chains to the previous BlockHeader, and the BlockData hash is embedded
// in the BlockHeader. This makes it natural and obvious that the Data is included in the hash, but
// the Metadata is not.
type Block struct {
Header *BlockHeader `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"`
Data *BlockData `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Metadata *BlockMetadata `protobuf:"bytes,3,opt,name=metadata,proto3" json:"metadata,omitempty"`
}

本文作为区块链浏览器系列的开篇,结构体中各字段的含义会在之后的文章中一一介绍,这里就不再过多介绍。接下来将以类图的形式来解析区块中包含的信息。

Block
Header *BlockHeader
Data *BlockData
Metadata *BlockMetadata
BlockHeader
Number uint64
PreviousHash []byte
DataHash []byte
BlockData
Data [][]byte
BlockMetadata
Metadata [][]byte
Metadata
Value []byte
Signatures []*MetadataSignature
MetadataSignature
SignatureHeader []byte
Signature []byte
IdentifierHeader []byte
SignatureHeader
Creator []byte
Nonce []byte
SerializedIdentity
Mspid string
IdBytes []byte
Certificate
crypto/x509.Certificate
IdentifierHeader
Identifier uint32
Nonce []byte
Envelope
Payload []byte
Signature []byte
Payload
Header *Header
Data []byte
Header
ChannelHeader []byte
SignatureHeader []byte
ChannelHeader
Type int32
Version int32
Timestamp *timestamp.Timestamp
TxId string
Epoch uint64
Extension []byte
TlsCertHash []byte
Transaction
Actions []*TransactionAction
TransactionAction
Header []byte
Payload []byte
ChaincodeActionPayload
ChaincodeProposalPayload []byte
Action *ChaincodeEndorsedAction
ChaincodeEndorsedAction
ProposalResponsePayload []byte
Endorsements []*Endorsement
ProposalResponsePayload
ProposalHash []byte
Extension []byte
Endorsement
Endorser []byte
Signature []byte
ChaincodeProposalPayload
Input []byte
TransientMap map[string][]byte
ChaincodeInvocationSpec
ChaincodeSpec *ChaincodeSpec
ChaincodeSpec
Type ChaincodeSpec_Type
ChaincodeId *ChaincodeID
Input *ChaincodeInput
Timeout int32
ChaincodeID
Path string
Name string
Version string
ChaincodeInput
Args [][]byte
Decorations map[string][]byte
IsInit bool
ChaincodeAction
Results []byte
Events []byte
Response *Response
ChaincodeId *ChaincodeID
TxReadWriteSet
DataModel TxReadWriteSet_DataModel
NsRwset []*NsReadWriteSet
NsReadWriteSet
Namespace string
Rwset []byte
CollectionHashedRwset []*CollectionHashedReadWriteSet
KVRWSet
Reads []*KVRead
RangeQueriesInfo []*RangeQueryInfo
Writes []*KVWrite
MetadataWrites []*KVMetadataWrite

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin92
Github: mengbin92
cnblogs: 恋水无意


posted @   落雷  阅读(405)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示