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 c6d784a
Show file tree
Hide file tree
Showing 4 changed files with 63 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
}
2 changes: 2 additions & 0 deletions off_chain_data/application-go/parser/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/protobuf/proto"
)

// TODO remove interface, use struct; encapsulate
type Payload interface {
ChannelHeader() *common.ChannelHeader
EndorserTransaction() EndorserTransaction
Expand Down Expand Up @@ -77,6 +78,7 @@ func (p *PayloadImpl) IsValid() bool {
return p.statusCode == int32(peer.TxValidationCode_VALID)
}

// TODO remove unused
func (p *PayloadImpl) ToProto() *common.Payload {
return p.payload
}
4 changes: 4 additions & 0 deletions off_chain_data/application-go/parser/readWriteSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"google.golang.org/protobuf/proto"
)

// TODO remove interface, use struct; encapsulate; extract into file
type ReadWriteSet interface {
NamespaceReadWriteSets() []NamespaceReadWriteSet
ToProto() *rwset.TxReadWriteSet
Expand All @@ -28,10 +29,12 @@ func (p *ReadWriteSetImpl) NamespaceReadWriteSets() []NamespaceReadWriteSet {
return result
}

// TODO remove unused
func (p *ReadWriteSetImpl) ToProto() *rwset.TxReadWriteSet {
return p.readWriteSet
}

// TODO remove interface, use struct
type NamespaceReadWriteSet interface {
Namespace() string
ReadWriteSet() *kvrwset.KVRWSet
Expand Down Expand Up @@ -60,6 +63,7 @@ func (p *NamespaceReadWriteSetImpl) ReadWriteSet() *kvrwset.KVRWSet {
return &result
}

// TODO remove unused
func (p *NamespaceReadWriteSetImpl) ToProto() *rwset.NsReadWriteSet {
return p.nsReadWriteSet
}
33 changes: 15 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,20 @@ 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 {
// TODO remove unused?
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 +33,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,18 +42,22 @@ 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()
}

// TODO remove interface, use struct; encapsulate; extract into file
type EndorserTransaction interface {
ReadWriteSets() []ReadWriteSet
ToProto() *peer.Transaction
Expand Down Expand Up @@ -164,6 +160,7 @@ func (*EndorserTransactionImpl) parseReadWriteSets(txReadWriteSets []*rwset.TxRe
return result
}

// TODO remove unused
func (p *EndorserTransactionImpl) ToProto() *peer.Transaction {
return p.transaction
}

0 comments on commit c6d784a

Please sign in to comment.