区块链教程Fabric1.0源代码分析putils(protos/utils工具包)-兄弟连区块链

简介:

  区块链教程Fabric1.0源代码分析putils(protos/utils工具包),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。

Fabric1.0源代码笔记之putils(protos/utils工具包)

1、putils概述

putils,即protos/utils工具包,代码分布在:protos/utils目录下。
包括:txutils.go、proputils.go、commonutils.go、blockutils.go。

2、txutils

//TransactionAction.Payload => ChaincodeActionPayload
//ChaincodeActionPayload.Action.ProposalResponsePayload => ProposalResponsePayload
//ProposalResponsePayload.Extension => ChaincodeAction
//从TransactionAction中获取ChaincodeActionPayload和ChaincodeAction
func GetPayloads(txActions *peer.TransactionAction) (*peer.ChaincodeActionPayload, *peer.ChaincodeAction, error)
//[]byte反序列化为Envelope
func GetEnvelopeFromBlock(data []byte) (*common.Envelope, error)
func CreateSignedEnvelope(txType common.HeaderType, channelID string, signer crypto.LocalSigner, dataMsg proto.Message, msgVersion int32, epoch uint64) (*common.Envelope, error) 
//由Proposal创建签名交易Envelope
func CreateSignedTx(proposal *peer.Proposal, signer msp.SigningIdentity, resps ...*peer.ProposalResponse) (*common.Envelope, error) {
func CreateProposalResponse(hdrbytes []byte, payl []byte, response *peer.Response, results []byte, events []byte, ccid *peer.ChaincodeID, visibility []byte, signingEndorser msp.SigningIdentity) (*peer.ProposalResponse, error)
func CreateProposalResponseFailure(hdrbytes []byte, payl []byte, response *peer.Response, results []byte, events []byte, ccid *peer.ChaincodeID, visibility []byte) (*peer.ProposalResponse, error)
//签名Proposal
func GetSignedProposal(prop *peer.Proposal, signer msp.SigningIdentity) (*peer.SignedProposal, error)
func GetSignedEvent(evt *peer.Event, signer msp.SigningIdentity) (*peer.SignedEvent, error)
func MockSignedEndorserProposalOrPanic(chainID string, cs *peer.ChaincodeSpec, creator, signature []byte) (*peer.SignedProposal, *peer.Proposal)
func MockSignedEndorserProposal2OrPanic(chainID string, cs *peer.ChaincodeSpec, signer msp.SigningIdentity) (*peer.SignedProposal, *peer.Proposal)
func GetBytesProposalPayloadForTx(payload *peer.ChaincodeProposalPayload, visibility []byte) ([]byte, error)
func GetProposalHash2(header *common.Header, ccPropPayl []byte) ([]byte, error)
func GetProposalHash1(header *common.Header, ccPropPayl []byte, visibility []byte) ([]byte, error)
//代码在protos/utils/txutils.go

3、proputils

func GetChaincodeInvocationSpec(prop *peer.Proposal) (*peer.ChaincodeInvocationSpec, error)
func GetChaincodeProposalContext(prop *peer.Proposal) ([]byte, map[string][]byte, error)
//反序列化为common.Header
func GetHeader(bytes []byte) (*common.Header, error)
func GetNonce(prop *peer.Proposal) ([]byte, error)
//Header.ChannelHeader反序列化为peer.ChaincodeHeaderExtension
func GetChaincodeHeaderExtension(hdr *common.Header) (*peer.ChaincodeHeaderExtension, error)
func GetProposalResponse(prBytes []byte) (*peer.ProposalResponse, error)
func GetChaincodeDeploymentSpec(code []byte) (*peer.ChaincodeDeploymentSpec, error)
func GetChaincodeAction(caBytes []byte) (*peer.ChaincodeAction, error)
func GetResponse(resBytes []byte) (*peer.Response, error)
func GetChaincodeEvents(eBytes []byte) (*peer.ChaincodeEvent, error)
func GetProposalResponsePayload(prpBytes []byte) (*peer.ProposalResponsePayload, error)
func GetProposal(propBytes []byte) (*peer.Proposal, error)
//e.Payload反序列化为Payload
func GetPayload(e *common.Envelope) (*common.Payload, error)
//[]byte反序列化为Transaction
func GetTransaction(txBytes []byte) (*peer.Transaction, error)
func GetChaincodeActionPayload(capBytes []byte) (*peer.ChaincodeActionPayload, error)
//反序列化为peer.ChaincodeProposalPayload
func GetChaincodeProposalPayload(bytes []byte) (*peer.ChaincodeProposalPayload, error)
//反序列化为common.SignatureHeader
func GetSignatureHeader(bytes []byte) (*common.SignatureHeader, error)
func CreateChaincodeProposal(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, string, error)
func CreateChaincodeProposalWithTransient(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte, transientMap map[string][]byte) (*peer.Proposal, string, error)
func CreateChaincodeProposalWithTxIDNonceAndTransient(txid string, typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, nonce, creator []byte, transientMap map[string][]byte) (*peer.Proposal, string, error)
func GetBytesProposalResponsePayload(hash []byte, response *peer.Response, result []byte, event []byte, ccid *peer.ChaincodeID) ([]byte, error)
func GetBytesChaincodeProposalPayload(cpp *peer.ChaincodeProposalPayload) ([]byte, error)
func GetBytesResponse(res *peer.Response) ([]byte, error)
func GetBytesChaincodeEvent(event *peer.ChaincodeEvent) ([]byte, error)
func GetBytesChaincodeActionPayload(cap *peer.ChaincodeActionPayload) ([]byte, error)
func GetBytesProposalResponse(pr *peer.ProposalResponse) ([]byte, error)
func GetBytesProposal(prop *peer.Proposal) ([]byte, error)
func GetBytesHeader(hdr *common.Header) ([]byte, error)
func GetBytesSignatureHeader(hdr *common.SignatureHeader) ([]byte, error)
func GetBytesTransaction(tx *peer.Transaction) ([]byte, error)
func GetBytesPayload(payl *common.Payload) ([]byte, error)
func GetBytesEnvelope(env *common.Envelope) ([]byte, error)
//从envBytes []byte中获取ChaincodeAction
func GetActionFromEnvelope(envBytes []byte) (*peer.ChaincodeAction, error)
func CreateProposalFromCIS(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte) (*peer.Proposal, string, error)
func CreateInstallProposalFromCDS(ccpack proto.Message, creator []byte) (*peer.Proposal, string, error)
//按ChaincodeDeploymentSpec创建DeployProposal
func CreateDeployProposalFromCDS(chainID string, cds *peer.ChaincodeDeploymentSpec, creator []byte, policy []byte, escc []byte, vscc []byte) (*peer.Proposal, string, error)
func CreateUpgradeProposalFromCDS(chainID string, cds *peer.ChaincodeDeploymentSpec, creator []byte, policy []byte, escc []byte, vscc []byte) (*peer.Proposal, string, error)
func createProposalFromCDS(chainID string, msg proto.Message, creator []byte, policy []byte, escc []byte, vscc []byte, propType string) (*peer.Proposal, string, error)
func ComputeProposalTxID(nonce, creator []byte) (string, error)
func CheckProposalTxID(txid string, nonce, creator []byte) error
func ComputeProposalBinding(proposal *peer.Proposal) ([]byte, error)
func computeProposalBindingInternal(nonce, creator []byte, epoch uint64) ([]byte, error)
//代码在protos/utils/proputils.go

4、commonutils

func MarshalOrPanic(pb proto.Message) []byte
func Marshal(pb proto.Message) ([]byte, error)
func CreateNonceOrPanic() []byte
func CreateNonce() ([]byte, error)
func UnmarshalPayloadOrPanic(encoded []byte) *cb.Payload
func UnmarshalPayload(encoded []byte) (*cb.Payload, error)
func UnmarshalEnvelopeOrPanic(encoded []byte) *cb.Envelope
func UnmarshalEnvelope(encoded []byte) (*cb.Envelope, error)
func UnmarshalEnvelopeOfType(envelope *cb.Envelope, headerType cb.HeaderType, message proto.Message) (*cb.ChannelHeader, error)
func ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope
func ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error)
func ExtractPayloadOrPanic(envelope *cb.Envelope) *cb.Payload
func ExtractPayload(envelope *cb.Envelope) (*cb.Payload, error)
func MakeChannelHeader(headerType cb.HeaderType, version int32, chainID string, epoch uint64) *cb.ChannelHeader
func MakeSignatureHeader(serializedCreatorCertChain []byte, nonce []byte) *cb.SignatureHeader
func SetTxID(channelHeader *cb.ChannelHeader, signatureHeader *cb.SignatureHeader) error
func MakePayloadHeader(ch *cb.ChannelHeader, sh *cb.SignatureHeader) *cb.Header
func NewSignatureHeaderOrPanic(signer crypto.LocalSigner) *cb.SignatureHeader
func SignOrPanic(signer crypto.LocalSigner, msg []byte) []byte
//[]byte反序列化为ChannelHeader
func UnmarshalChannelHeader(bytes []byte) (*cb.ChannelHeader, error)
func UnmarshalChaincodeID(bytes []byte) (*pb.ChaincodeID, error)
func IsConfigBlock(block *cb.Block) bool
//代码在protos/utils/commonutils.go

5、blockutils

//[]byte转换为Block,从Block中获取ChainID(即ChannelId)
func GetChainIDFromBlockBytes(bytes []byte) (string, error)
//从Block中获取ChainID(即ChannelId)
func GetChainIDFromBlock(block *cb.Block) (string, error)
//从Block中按index获取Metadata
func GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Metadata, error)
//从Block中按index获取Metadata,如果失败则Panic
func GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *cb.Metadata
//从Block.Metadata.Metadata中获取LastConfig
func GetLastConfigIndexFromBlock(block *cb.Block) (uint64, error)
//从Block.Metadata.Metadata中获取LastConfig,如果失败则Panic
func GetLastConfigIndexFromBlockOrPanic(block *cb.Block) uint64
//[]byte转换为Block
func GetBlockFromBlockBytes(blockBytes []byte) (*cb.Block, error)
//拷贝Block.Metadata
func CopyBlockMetadata(src *cb.Block, dst *cb.Block)
//初始化Block.Metadata.Metadata
func InitBlockMetadata(block *cb.Block)
//代码在protos/utils/blockutils.go
相关文章
|
2月前
|
存储 供应链 监控
区块链技术在供应链管理中的应用与前景分析
随着信息化时代的到来,供应链管理面临着越来越多的挑战和机遇。本文主要探讨了区块链技术在供应链管理中的应用,以及未来的发展前景。通过对区块链技术的特点和优势进行分析,结合实际案例和趋势展望,展示了区块链技术在提升供应链透明度、效率和安全性方面的潜力,以及未来发展的可能方向。
|
2月前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
Developing a blockchain points mall system involves multiple aspects such as blockchain technology, smart contracts, front-end development, and business logic design. The following is the general process for developing a blockchain points mall system
|
2月前
|
存储 算法 API
面向企业的区块链教程(一)(2)
面向企业的区块链教程(一)
52 6
|
4天前
|
存储 安全 区块链
元宇宙与区块链技术的关系可以从多个角度进行阐述。以下是对这两者之间关系的详细分析
**元宇宙:虚拟世界融合现实元素,强调交互与沉浸;区块链:去中心化、安全的分布式账本。两者结合,区块链确保元宇宙中虚拟资产安全、支付高效、身份验证私密、治理透明,支撑其经济体系与用户信任,驱动未来发展。**
|
2月前
|
存储 供应链 安全
基于区块链技术的智能合约安全性分析
【5月更文挑战第31天】本文深入探讨了区块链技术中智能合约的安全性问题,通过分析现有智能合约的安全漏洞和攻击手段,提出了一系列增强智能合约安全性的策略。文章首先介绍了区块链和智能合约的基本概念,随后详细讨论了智能合约面临的安全挑战,包括代码漏洞、重入攻击等问题,并对比分析了不同平台下智能合约的安全性差异。最后,文章提出了一系列提高智能合约安全性的建议,旨在为区块链应用的健康发展提供参考。
|
11天前
|
区块链
近期区块链市场趋势分析
**区块链市场趋势摘要:** - 跨链技术成熟,提升互操作性,助力区块链网络融合。 - DeFi持续繁荣,智能合约与AMM创新活跃,市场竞争驱动市场壮大。 - NFT市场多样化,拓展至游戏、音乐等领域,实用性增强。 - 区块链寻求绿色转型,通过PoS共识与绿色能源减少能耗。 - 技术模块化、可组合性提升,降低成本,增强系统灵活性。 这些趋势展现区块链潜力,带来机遇与挑战,促进行业创新。
|
2月前
|
存储 算法 安全
区块链系统开发技术规则分析
区块链核心技术包括:1) 哈希算法,利用单向函数将任意数据转化为固定长度代码,确保安全验证;2) 非对称加密,使用公钥和私钥一对进行加密解密,保证信息安全;3) 共识机制,如PoW、PoS、DPoS等,实现快速交易验证和确认;4) 智能合约,自动执行的可信代码,一旦编写即不可更改,用于自动化交易;5) 分布式存储,将数据分散存储在网络各处,涵盖结构化、非结构化和半结构化数据。
|
2月前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
252 15
|
2月前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
Developing blockchain game systems has been a highly anticipated field in recent years. By combining blockchain technology and game mechanics, players can enjoy a brand new gaming experience and higher game credibility.
|
12月前
Minecraft Fabric 教程 #8 添加附魔书
这就创建了一个FireBoom附魔书 onTargetDamaged //当目标被攻击 在mc FireballEntity类有一个 方法就是当火球碰撞了就创建一个火焰爆炸的效果
51 0

热门文章

最新文章