Fabric 1.0原始碼分析(41)putils(protos/utils工具包)
# Fabric 1.0原始碼筆記 之 putils(protos/utils工具包)
## 1、putils概述
putils,即protos/utils工具包,程式碼分佈在:protos/utils目錄下。
包括:txutils.go、proputils.go、commonutils.go、blockutils.go。
## 2、txutils
```go
//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
```go
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
```go
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
```go
//[]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
```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(2) blockfile(區塊檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(19) Ledger #statedb(狀態資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(21)Ledger #historydb(歷史資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(22)Ledger #blkstorage(block檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(32) Peer #peer node start命令實現原始碼
- Fabric 1.0原始碼分析(42)scc(系統鏈碼) #cscc(通道相關)原始碼
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(38) Peer #BroadcastClient(Broadcast客戶端)原始碼ASTclient客戶端
- 以太坊原始碼分析(41)hashimoto原始碼分析原始碼
- Fabric 1.0原始碼分析(4)Chaincode(鏈碼)#platforms(鏈碼語言平臺)原始碼AIPlatform
- Fabric 1.0原始碼分析(27) Orderer #configupdate(處理通道配置更新)原始碼
- Fabric 1.0原始碼分析(33) Peer #peer channel命令及子命令實現原始碼
- Fabric 1.0原始碼分析(1)BCCSP(區塊鏈加密服務提供者)原始碼區塊鏈加密
- Fabric 1.0原始碼分析(9)configtx(配置交易)體系介紹原始碼
- Fabric 1.0原始碼分析(11)consenter(共識外掛) #filter(過濾器)原始碼Filter過濾器