首页 > 编程语言 >从源码中解析fabric区块数据结构(一)

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

时间:2022-10-26 10:22:38浏览次数:84  
标签:fabric -- 源码 byte 区块 class Block string

从源码中解析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"`
}

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

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

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


标签:fabric,--,源码,byte,区块,class,Block,string
From: https://www.cnblogs.com/lianshuiwuyi/p/16827357.html

相关文章

  • rest_framework权限源码分析
    位置APIView---->dispatch方法---->initial方法--->self.check_permissions(request)(APIView的对象方法)分析defcheck_permissions(self,request):"""Chec......
  • rest_framework认证源码分析
    认证源码分析位置:APIVIew----》dispatch方法---》self.initial(request,*args,**kwargs)---->有认证,权限,频率三个版块分析:只读认证源码:self.perform_authenticatio......
  • 源码分析之ViewSetMixin类
    在rest_framework中继承了ModelViewSet类的视图函数,其内部ModelViewSet类继承了一个ViewSetMixin类ViewSetMixin类重新写了as_view()方法,使得我们可以更加方便快速的写出......
  • 模糊测试工具AFL源码浅析
    前言AFL是一款著名的模糊测试的工具,最近在阅读AFL源码,记录一下,方便以后查阅。环境项目:AFL编译项目:将编译的优化选项关闭,即改写成-O0afl-gcc.c使用gdb加载afl-......
  • 45.限流Throttling及源码解析
    什么是限流?限流类似于权限机制,它也决定是否接受当前请求,用于控制客户端在某段时间内允许向API发出请求的次数,也就是频率假设有客户端(比如爬虫程序)短时间发起大量请......
  • 爱上源码,重学Spring AOP深入
    AOP(AspectOrientProgramming):直译过来就是面向切面编程。AOP是一种编程思想用途:Transactions(事务调用方法前开启事务,调用方法后提交关闭事务)、日志、性能(监控方法......
  • 直播带货源码,如何用Android Studio实现登录跳转
    直播带货源码,如何用AndroidStudio实现登录跳转一、基本要求实现一个简单的用户登录界面,功能如下: 1、默认不存储用户信息,默认隐藏密码。 2、能通过勾选框记住密码......
  • CentOS 7.2 源码安装 PostgreSQL 9.0
    安装PGSQLPGSQL源码地址:https://ftp.postgresql.org/pub/source/下载9.0版本源码[root@localhostsoft]#wgethttps://ftp.postgresql.org/pub/source/v9.0.0/postgres......
  • vue源码中的nextTick是怎样实现的
    一、Vue.nextTick内部逻辑在执行initGlobalAPI(Vue)初始化Vue全局API中,这么定义Vue.nextTick。functioninitGlobalAPI(Vue){//...Vue.nextTick=ne......
  • 硬核剖析ThreadLocal源码,面试官看了直呼内行
    工作面试中经常遇到ThreadLocal,但是很多同学并不了解ThreadLocal实现原理,到底为什么会发生内存泄漏也是一知半解?今天一灯带你深入剖析ThreadLocal源码,总结ThreadLocal使用......