Skip to content

Commit

Permalink
Implement block parsing
Browse files Browse the repository at this point in the history
Removed Block and Transaction interfaces and unused statusCode function.
Using the struct instead of the interfaces now.
  • Loading branch information
twoGiants committed Dec 7, 2024
1 parent 4ba9efe commit fc2ff58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 63 deletions.
87 changes: 42 additions & 45 deletions off_chain_data/application-go/parser/blockParser.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,74 @@
package parser

import (
"fmt"
"offChainData/utils"

"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
"google.golang.org/protobuf/proto"
)

type Block struct {
block *common.Block
validationCodes []byte
transactions []Transaction
block *common.Block
transactions []*Transaction
}

func ParseBlock(block *common.Block) Block {
validationCodes := extractTransactionValidationCodes(block)

return Block{block, validationCodes, nil}
func ParseBlock(block *common.Block) *Block {
return &Block{block, nil}
}

func (b *Block) Number() uint64 {
header := utils.AssertDefined(b.block.GetHeader(), "missing block header")
return header.GetNumber()
}

// TODO: needs cache, getPayloads, parsePayload
func (b *Block) Transactions() []Transaction {
return nil
}

func (b *Block) ToProto() *common.Block {
return nil
}

func (b *Block) payloads() []*common.Payload {
var payloads []*common.Payload

for _, envelopeBytes := range b.block.GetData().GetData() {
// TODO: needs cache; decompose
func (b *Block) Transactions() []*Transaction {
envelopes := []*common.Envelope{}
for _, blockData := range b.block.GetData().GetData() {
envelope := &common.Envelope{}
if err := proto.Unmarshal(envelopeBytes, envelope); err != nil {
if err := proto.Unmarshal(blockData, envelope); err != nil {
panic(err)
}
envelopes = append(envelopes, envelope)
}

payload := &common.Payload{}
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
commonPayloads := []*common.Payload{}
for _, envelope := range envelopes {
commonPayload := &common.Payload{}
if err := proto.Unmarshal(envelope.GetPayload(), commonPayload); err != nil {
panic(err)
}

payloads = append(payloads, payload)
commonPayloads = append(commonPayloads, commonPayload)
}

return payloads
}

// TODO not sure about this
func (pb *Block) statusCode(txIndex int) peer.TxValidationCode {
blockMetadata := utils.AssertDefined(
pb.block.GetMetadata(),
"missing block metadata",
)

metadata := blockMetadata.GetMetadata()
if int(common.BlockMetadataIndex_TRANSACTIONS_FILTER) >= len(metadata) {
return peer.TxValidationCode_INVALID_OTHER_REASON
validationCodes := b.extractTransactionValidationCodes()
payloads := []*PayloadImpl{}
for i, commonPayload := range commonPayloads {
payload := ParsePayload(
commonPayload,
int32(utils.AssertDefined(
validationCodes[i],
fmt.Sprint("missing validation code index", i),
),
),
)
if payload.IsEndorserTransaction() {
payloads = append(payloads, payload)
}
}

statusCodes := metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]
if txIndex >= len(statusCodes) {
return peer.TxValidationCode_INVALID_OTHER_REASON
result := []*Transaction{}
for _, payload := range payloads {
result = append(result, NewTransaction(payload))
}

return peer.TxValidationCode(statusCodes[txIndex])
return result
}

func extractTransactionValidationCodes(block *common.Block) []byte {
func (b *Block) extractTransactionValidationCodes() []byte {
metadata := utils.AssertDefined(
block.GetMetadata(),
b.block.GetMetadata(),
"missing block metadata",
)

Expand All @@ -85,3 +77,8 @@ func extractTransactionValidationCodes(block *common.Block) []byte {
"missing transaction validation code",
)
}

// TODO remove unused?
func (b *Block) ToProto() *common.Block {
return b.block
}
30 changes: 12 additions & 18 deletions off_chain_data/application-go/parser/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,19 @@ import (
"google.golang.org/protobuf/proto"
)

type Transaction interface {
ChannelHeader() *common.ChannelHeader
Creator() identity.Identity
ValidationCode() int32
IsValid() bool
NamespaceReadWriteSets() []NamespaceReadWriteSet
ToProto() *common.Payload
}

type TransactionImpl struct {
type Transaction struct {
payload Payload
}

func NewTransactionImpl(payload Payload) *TransactionImpl {
return &TransactionImpl{payload}
func NewTransaction(payload Payload) *Transaction {
return &Transaction{payload}
}

func (t *TransactionImpl) ChannelHeader() *common.ChannelHeader {
func (t *Transaction) ChannelHeader() *common.ChannelHeader {
return t.payload.ChannelHeader()
}

func (t *TransactionImpl) Creator() identity.Identity {
func (t *Transaction) Creator() identity.Identity {
creator := &msp.SerializedIdentity{}
if err := proto.Unmarshal(t.payload.SignatureHeader().GetCreator(), creator); err != nil {
panic(err)
Expand All @@ -41,7 +32,7 @@ func (t *TransactionImpl) Creator() identity.Identity {
return &identityImpl{creator}
}

func (t *TransactionImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
func (t *Transaction) NamespaceReadWriteSets() []NamespaceReadWriteSet {
result := []NamespaceReadWriteSet{}
for _, readWriteSet := range t.payload.EndorserTransaction().ReadWriteSets() {
result = append(result, readWriteSet.NamespaceReadWriteSets()...)
Expand All @@ -50,15 +41,18 @@ func (t *TransactionImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
return result
}

func (t *TransactionImpl) ValidationCode() int32 {
// TODO remove unused?
func (t *Transaction) ValidationCode() int32 {
return t.payload.TransactionValidationCode()
}

func (t *TransactionImpl) IsValid() bool {
// TODO remove unused?
func (t *Transaction) IsValid() bool {
return t.payload.IsValid()
}

func (t *TransactionImpl) ToProto() *common.Payload {
// TODO remove unused?
func (t *Transaction) ToProto() *common.Payload {
return t.payload.ToProto()
}

Expand Down

0 comments on commit fc2ff58

Please sign in to comment.