Skip to content

Commit

Permalink
reformate-proto-block
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Neznaemov committed Oct 6, 2023
1 parent 065e706 commit 25ca2f8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 65 deletions.
9 changes: 4 additions & 5 deletions proto/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func ParseBlock(block *common.Block, opts ...ParseBlockOpt) (*Block, error) {

txFilter := txflags.ValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
if parsedBlock.Data, err = ParseBlockData(block.GetData().GetData(), txFilter); err != nil {
return nil, fmt.Errorf("parsing envelopes: %w", err)
return nil, fmt.Errorf("parse block data: %w", err)
}

// parse Raft orderer identity
raftOrdererIdentity, err := ParseOrdererIdentity(block)
if err != nil {
return nil, fmt.Errorf("parsing orderer identity from block: %w", err)
return nil, fmt.Errorf("parse orderer identity from block: %w", err)
}

if raftOrdererIdentity != nil && raftOrdererIdentity.IdBytes != nil {
Expand All @@ -62,7 +62,7 @@ func ParseBlock(block *common.Block, opts ...ParseBlockOpt) (*Block, error) {
var bftOrdererIdentities []*OrdererSignature
bftOrdererIdentities, err = ParseBTFOrderersIdentities(block, parsingOpts.configBlock)
if err != nil {
return nil, fmt.Errorf("parsing bft orderers identities: %w", err)
return nil, fmt.Errorf("parse bft orderers identities: %w", err)
}

parsedBlock.Metadata.OrdererSignatures = append(parsedBlock.Metadata.OrdererSignatures, bftOrdererIdentities...)
Expand All @@ -88,8 +88,7 @@ func ParseOrdererIdentity(cb *common.Block) (*msp.SerializedIdentity, error) {
return nil, fmt.Errorf("unmarshaling signature header from metadata signature header: %w", err)
}

serializedIdentity := &msp.SerializedIdentity{}
err = proto.Unmarshal(signatureHeader.Creator, serializedIdentity)
serializedIdentity, err := protoutil.UnmarshalSerializedIdentity(signatureHeader.Creator)
if err != nil {
return nil, fmt.Errorf("unmarshaling serialized indentity from signature header: %w", err)
}
Expand Down
36 changes: 18 additions & 18 deletions proto/block.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ message SignatureHeader {
bytes nonce = 2;
}

// field numbers correspond to HeaderType https://github.com/hyperledger/fabric-protos/blob/main/common/common.proto#L27
message Transaction {
repeated TransactionAction actions = 1;
hlfsdkgo.proto.ChannelConfig channel_config = 2;
hlfsdkgo.proto.ChannelConfig channel_config = 1;
repeated TransactionAction actions = 3;
}

message TransactionAction {
Expand Down
31 changes: 13 additions & 18 deletions proto/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ func ParseBlockData(blockData [][]byte, txFilter txflags.ValidationFlags) (*Bloc
func ParseEnvelope(envelopeData []byte, validationCode peer.TxValidationCode) (*Envelope, error) {
envelope, err := protoutil.GetEnvelopeFromBlock(envelopeData)
if err != nil {
return nil, fmt.Errorf(`envelope from block data: %w`, err)
return nil, fmt.Errorf("get envelope from block: %w", err)
}

payload, err := protoutil.UnmarshalPayload(envelope.Payload)
if err != nil {
return nil, fmt.Errorf(`payload from envelope: %w`, err)
return nil, fmt.Errorf("unmarshal payload from envelope: %w", err)
}

channelHeader, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return nil, fmt.Errorf(`channel header from envelope payload: %w`, err)
return nil, fmt.Errorf("unmarshal channel header from envelope payload: %w", err)
}

signatureHeader := &SignatureHeader{}
sigHeader, err := protoutil.UnmarshalSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, fmt.Errorf("signature header: %w", err)
return nil, fmt.Errorf("unmarshal signature header: %w", err)
}
creator, err := protoutil.UnmarshalSerializedIdentity(sigHeader.Creator)
if err != nil {
return nil, fmt.Errorf("parse transaction creator: %w", err)
return nil, fmt.Errorf("unmarshal envelope creator: %w", err)
}
signatureHeader.Creator = creator
signatureHeader.Nonce = sigHeader.Nonce
Expand All @@ -59,27 +59,22 @@ func ParseEnvelope(envelopeData []byte, validationCode peer.TxValidationCode) (*
protoutil.SetTxID(channelHeader, sigHeader)
}

var tx *Transaction
tx := &Transaction{}
switch common.HeaderType(channelHeader.Type) {
case common.HeaderType_ENDORSER_TRANSACTION:
tx, err = ParseTransaction(payload, common.HeaderType(channelHeader.Type))
if err != nil {
return nil, fmt.Errorf(`endorser transaction from envelope: %w`, err)
}
case common.HeaderType_CONFIG:
tx, err = ParseTransaction(payload, common.HeaderType(channelHeader.Type))
if err != nil {
return nil, fmt.Errorf(`endorser transaction from envelope: %w`, err)
}

ce := &common.ConfigEnvelope{}
if err = proto.Unmarshal(payload.Data, ce); err != nil {
return nil, err
return nil, fmt.Errorf("unmarshal payload data to config envelope: %w", err)
}

tx.ChannelConfig, err = ParseChannelConfig(*ce.Config)
if err != nil {
return nil, fmt.Errorf(`parse channel config: %w`, err)
return nil, fmt.Errorf("parse channel config: %w", err)
}
case common.HeaderType_ENDORSER_TRANSACTION:
tx, err = ParseEndorserTransaction(payload)
if err != nil {
return nil, fmt.Errorf("parse endorser transaction: %w", err)
}
}

Expand Down
20 changes: 10 additions & 10 deletions proto/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package proto

import (
"fmt"

"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
Expand All @@ -16,18 +18,16 @@ func (x *Transaction) Events() []*peer.ChaincodeEvent {
return events
}

func ParseTransaction(payload *common.Payload, transactionType common.HeaderType) (*Transaction, error) {
func ParseEndorserTransaction(payload *common.Payload) (*Transaction, error) {
var actions []*TransactionAction
if transactionType == common.HeaderType_ENDORSER_TRANSACTION {
tx, err := protoutil.UnmarshalTransaction(payload.Data)
if err != nil {
return nil, err
}
tx, err := protoutil.UnmarshalTransaction(payload.Data)
if err != nil {
return nil, fmt.Errorf("unmarshal transaction from payload data: %w", err)
}

actions, err = ParseTxActions(tx.Actions)
if err != nil {
return nil, err
}
actions, err = ParseTxActions(tx.Actions)
if err != nil {
return nil, fmt.Errorf("parse transaction actions: %w", err)
}

return &Transaction{
Expand Down
24 changes: 12 additions & 12 deletions proto/tx_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ParseTxActions(txActions []*peer.TransactionAction) ([]*TransactionAction,
for _, action := range txActions {
txAction, err := ParseTxAction(action)
if err != nil {
return nil, fmt.Errorf(`tx action: %w`, err)
return nil, fmt.Errorf("parse transaction action: %w", err)
}
parsedTxActions = append(parsedTxActions, txAction)
}
Expand All @@ -39,17 +39,17 @@ func ParseTxActions(txActions []*peer.TransactionAction) ([]*TransactionAction,
func ParseTxAction(txAction *peer.TransactionAction) (*TransactionAction, error) {
sigHeader, err := protoutil.UnmarshalSignatureHeader(txAction.Header)
if err != nil {
return nil, fmt.Errorf("get signature header: %w", err)
return nil, fmt.Errorf("unmarshal signature header: %w", err)
}

creator, err := protoutil.UnmarshalSerializedIdentity(sigHeader.Creator)
if err != nil {
return nil, fmt.Errorf("parse transaction creator: %w", err)
return nil, fmt.Errorf("unmarshal transaction creator: %w", err)
}

actionPayload, err := protoutil.UnmarshalChaincodeActionPayload(txAction.Payload)
if err != nil {
return nil, fmt.Errorf("get chaincode action from action payload: %w", err)
return nil, fmt.Errorf("unmarshal chaincode action from action payload: %w", err)
}

ccEndorserAction, err := ParseChaincodeEndorsedAction(actionPayload)
Expand Down Expand Up @@ -95,12 +95,12 @@ func ParseTxAction(txAction *peer.TransactionAction) (*TransactionAction, error)
func ParseChaincodeProposalPayload(actionPayload *peer.ChaincodeActionPayload) (*ChaincodeProposalPayload, error) {
chaincodeProposalPayload, err := protoutil.UnmarshalChaincodeProposalPayload(actionPayload.ChaincodeProposalPayload)
if err != nil {
return nil, fmt.Errorf("get chaincode proposal from action payload: %w", err)
return nil, fmt.Errorf("unmarshal chaincode proposal from action payload: %w", err)
}

input, err := protoutil.UnmarshalChaincodeInvocationSpec(chaincodeProposalPayload.Input)
if err != nil {
return nil, fmt.Errorf("get chaincode invocation spec from action payload: %w", err)
return nil, fmt.Errorf("unmarshal chaincode invocation spec from action payload: %w", err)
}

return &ChaincodeProposalPayload{
Expand All @@ -117,7 +117,7 @@ func ParseChaincodeEndorsedAction(actionPayload *peer.ChaincodeActionPayload) (*

chaincodeAction, err := protoutil.UnmarshalChaincodeAction(proposalResponsePayload.Extension)
if err != nil {
return nil, fmt.Errorf(`chaincode action from proposal response: %w`, err)
return nil, fmt.Errorf("unmarshal chaincode action from proposal extention: %w", err)
}

txReadWriteSet, err := ParseTxReadWriteSet(chaincodeAction)
Expand All @@ -127,14 +127,14 @@ func ParseChaincodeEndorsedAction(actionPayload *peer.ChaincodeActionPayload) (*

events, err := protoutil.UnmarshalChaincodeEvents(chaincodeAction.Events)
if err != nil {
return nil, fmt.Errorf(`event from chaincode action: %w`, err)
return nil, fmt.Errorf("unmarshal cc event from chaincode action: %w", err)
}

var endorsements []*Endorsement
for _, endorsement := range actionPayload.Action.Endorsements {
endorser, err := protoutil.UnmarshalSerializedIdentity(endorsement.Endorser)
if err != nil {
return nil, fmt.Errorf("parse transaction endorser: %w", err)
return nil, fmt.Errorf("unmarshal transaction endorser: %w", err)
}

endorsements = append(endorsements, &Endorsement{
Expand All @@ -160,21 +160,21 @@ func ParseChaincodeEndorsedAction(actionPayload *peer.ChaincodeActionPayload) (*
func ParseTxReadWriteSet(chaincodeAction *peer.ChaincodeAction) (*TxReadWriteSet, error) {
txReadWriteSet := &rwset.TxReadWriteSet{}
if err := proto.Unmarshal(chaincodeAction.Results, txReadWriteSet); err != nil {
return nil, fmt.Errorf("get txReadWriteSet: %w", err)
return nil, fmt.Errorf("unmarshal txReadWriteSet from cc action result: %w", err)
}

var nsReadWriteSets []*NsReadWriteSet
for _, nsRWset := range txReadWriteSet.NsRwset {
kvRWSet := &kvrwset.KVRWSet{}
if err := proto.Unmarshal(nsRWset.Rwset, kvRWSet); err != nil {
return nil, fmt.Errorf("get kvReadWriteSet: %w", err)
return nil, fmt.Errorf("unmarshal kvReadWriteSet from nsRWSet: %w", err)
}

var collectionHashedRwset []*CollectionHashedReadWriteSet
for _, hashedRwsetItem := range nsRWset.CollectionHashedRwset {
hashedRwset := &kvrwset.HashedRWSet{}
if err := proto.Unmarshal(hashedRwsetItem.HashedRwset, hashedRwset); err != nil {
return nil, fmt.Errorf("get HashedRwset: %w", err)
return nil, fmt.Errorf("unmarshal HashedRWset from collection hashedRWSet: %w", err)
}

collectionHashedRwset = append(collectionHashedRwset, &CollectionHashedReadWriteSet{
Expand Down

0 comments on commit 25ca2f8

Please sign in to comment.