Skip to content

Commit

Permalink
Merge pull request #91 from s7techlab/chan-cfg-parsers
Browse files Browse the repository at this point in the history
block parser bug fixes
  • Loading branch information
bogatyr285 authored Jan 31, 2022
2 parents 81fd256 + ecffb41 commit 0585c58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion proto/chan_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (c ChannelConfig) GetAllCertificates() ([]Certificate, error) {
}

for mspID := range c.Orderers {
cs, err := c.Applications[mspID].MSP.GetAllCertificates()
cs, err := c.Orderers[mspID].MSP.GetAllCertificates()
if err != nil {
return nil, fmt.Errorf("get all orderers certificates: %w", err)
}
Expand Down
15 changes: 14 additions & 1 deletion proto/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func ParseEnvelope(envelopeData []byte, validationCode peer.TxValidationCode) (*
return nil, fmt.Errorf(`channel header from envelope payload: %w`, err)
}

if channelHeader.TxId == "" {
sigHeader, err := protoutil.UnmarshalSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, fmt.Errorf("signature header: %w", err)
}
protoutil.SetTxID(channelHeader, sigHeader)
}

parsedEnvelope := &Envelope{
Signature: envelope.Signature,
ChannelHeader: channelHeader,
Expand All @@ -61,7 +69,7 @@ func ParseEnvelope(envelopeData []byte, validationCode peer.TxValidationCode) (*

switch common.HeaderType(channelHeader.Type) {
case common.HeaderType_ENDORSER_TRANSACTION:
parsedEnvelope.Transaction, err = ParseEndorserTx(payload.Data)
parsedEnvelope.Transaction, err = ParseTransaction(payload, common.HeaderType(channelHeader.Type))
if err != nil {
return nil, fmt.Errorf(`endorser transaction from envelope: %w`, err)
}
Expand All @@ -75,6 +83,11 @@ func ParseEnvelope(envelopeData []byte, validationCode peer.TxValidationCode) (*
if err != nil {
return nil, fmt.Errorf(`parse channel config: %w`, err)
}

parsedEnvelope.Transaction, err = ParseTransaction(payload, common.HeaderType(channelHeader.Type))
if err != nil {
return nil, fmt.Errorf(`endorser transaction from envelope: %w`, err)
}
}

return parsedEnvelope, nil
Expand Down
51 changes: 21 additions & 30 deletions proto/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,52 @@ package proto
import (
"fmt"

"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)

type (
Transaction struct {
Actions TransactionsActions `json:"transaction_actions"`
CreatorIdentity msp.SerializedIdentity `json:"creator_identity"`
Signature []byte `json:"signature"`
}

Transactions []*Transaction
)

func ParseEndorserTx(envelopePayloadBytes []byte) (*Transaction, error) {
tx, err := protoutil.UnmarshalTransaction(envelopePayloadBytes)
func ParseTransaction(payload *common.Payload, transactionType common.HeaderType) (*Transaction, error) {
sigHeader, err := protoutil.UnmarshalSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get signature header: %w", err)
}

parsedTx := &Transaction{}
parsedTx.Actions, err = ParseTxActions(tx.Actions)
si, err := protoutil.UnmarshalSerializedIdentity(sigHeader.Creator)
if err != nil {
return nil, err
return nil, fmt.Errorf("parse transaction creator: %w", err)
}

payload, err := protoutil.UnmarshalPayload(envelopePayloadBytes)
if err != nil {
return nil, errors.Wrap(err, `failed to get payload from envelope`)
}
var actions []*TransactionAction
if transactionType == common.HeaderType_ENDORSER_TRANSACTION {
tx, err := protoutil.UnmarshalTransaction(payload.Data)
if err != nil {
return nil, err
}

sigHeader, err := protoutil.UnmarshalSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, fmt.Errorf("failed to get signature header: %w", err)
actions, err = ParseTxActions(tx.Actions)
if err != nil {
return nil, err
}
}

si, err := protoutil.UnmarshalSerializedIdentity(sigHeader.Creator)
if err != nil {
// in some transactions we get some unknown proto message with chaincodes(!?), dont know how to deal with it now
// ---- example
// �
// � 
// _lifecycle�
// "ApproveChaincodeDefinitionForMyOrg
// ^basic1.0JNL
// Jbasic_1.0:770d76a4369f9121d4945c6782dd42c4db7c130c3c7b77b73d9894414b5a3da9
// log.Println("[hlf.ptoto.tx-parser] got error, skipping it. transaction creator is not 'msp.SerializedIdentity': ", string(sigHeader.Creator))
err = nil
//return nil, fmt.Errorf("parse transaction creator: %w", err)
parsedTx := &Transaction{
Actions: actions,
CreatorIdentity: *si,
}
parsedTx.CreatorIdentity = *si

return parsedTx, err
return parsedTx, nil

}

func (t *Transaction) Events() []*peer.ChaincodeEvent {
Expand Down

0 comments on commit 0585c58

Please sign in to comment.