Skip to content

Commit

Permalink
observer refactoring to generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Neznaemov committed Apr 8, 2024
1 parent d172d35 commit e5ee138
Show file tree
Hide file tree
Showing 55 changed files with 1,162 additions and 1,618 deletions.
6 changes: 4 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"

"github.com/s7techlab/hlf-sdk-go/block"
hlfproto "github.com/s7techlab/hlf-sdk-go/block"
)

type CurrentIdentity interface {
Expand Down Expand Up @@ -43,14 +43,16 @@ type BlocksDeliverer interface {
identity msp.SigningIdentity,
blockRange ...int64,
) (blockChan <-chan *common.Block, closer func() error, err error)
}

type ParsedBlocksDeliverer interface {
// ParsedBlocks the same as BlocksDeliverer.Blocks, but returns a channel with parsed blocks
ParsedBlocks(
ctx context.Context,
channel string,
identity msp.SigningIdentity,
blockRange ...int64,
) (parsedBlockChan <-chan *block.Block, parsedCloser func() error, err error)
) (parsedBlockChan <-chan *hlfproto.Block, parsedCloser func() error, err error)
}

type Querier interface {
Expand Down
2 changes: 2 additions & 0 deletions api/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Peer interface {

BlocksDeliverer

ParsedBlocksDeliverer

EventsDeliverer

// DeliverClient returns DeliverClient
Expand Down
14 changes: 7 additions & 7 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func ParseBlock(block *common.Block, opts ...ParseBlockOpt) (*Block, error) {
Metadata: &BlockMetadata{},
}

if block.GetMetadata().GetMetadata() != nil {
parsedBlock.Metadata.RawUnparsedMetadataSignatures = block.GetMetadata().GetMetadata()[common.BlockMetadataIndex_SIGNATURES]
}

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("parse block data: %w", err)
Expand All @@ -63,7 +59,7 @@ func ParseBlock(block *common.Block, opts ...ParseBlockOpt) (*Block, error) {
// parse BFT orderer identities, if there is at least one config block was sent
if parsingOpts.configBlock != nil {
var bftOrdererIdentities []*OrdererSignature
bftOrdererIdentities, err = ParseBTFOrderersIdentities(parsedBlock.Metadata.RawUnparsedMetadataSignatures, parsingOpts.configBlock)
bftOrdererIdentities, err = ParseBTFOrderersIdentities(block, parsingOpts.configBlock)
if err != nil {
return nil, fmt.Errorf("parse bft orderers identities: %w", err)
}
Expand All @@ -75,6 +71,10 @@ func ParseBlock(block *common.Block, opts ...ParseBlockOpt) (*Block, error) {
}

func ParseOrdererIdentity(cb *common.Block) (*msp.SerializedIdentity, error) {
if cb == nil {
return nil, nil
}

meta, err := protoutil.GetMetadataFromBlock(cb, common.BlockMetadataIndex_SIGNATURES)
if err != nil {
return nil, fmt.Errorf("get metadata from block: %w", err)
Expand All @@ -99,13 +99,13 @@ func ParseOrdererIdentity(cb *common.Block) (*msp.SerializedIdentity, error) {
return serializedIdentity, nil
}

func ParseBTFOrderersIdentities(metadataSignatures []byte, configBlock *common.Block) ([]*OrdererSignature, error) {
func ParseBTFOrderersIdentities(block *common.Block, configBlock *common.Block) ([]*OrdererSignature, error) {
if configBlock == nil {
return nil, nil
}

bftMeta := &bftcommon.BFTMetadata{}
if err := proto.Unmarshal(metadataSignatures, bftMeta); err != nil {
if err := proto.Unmarshal(block.Metadata.Metadata[common.BlockMetadataIndex_SIGNATURES], bftMeta); err != nil {
return nil, fmt.Errorf("unmarshaling bft block metadata from metadata: %w", err)
}

Expand Down
47 changes: 17 additions & 30 deletions block/block.pb.go

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

2 changes: 0 additions & 2 deletions block/block.pb.validate.go

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

1 change: 0 additions & 1 deletion block/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ message Endorsement {

message BlockMetadata {
repeated OrdererSignature ordererSignatures = 1;
bytes raw_unparsed_metadata_signatures = 2;
}

message OrdererSignature {
Expand Down
36 changes: 17 additions & 19 deletions observer/transform/action.go → block/transform/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/mohae/deepcopy"

hlfproto "github.com/s7techlab/hlf-sdk-go/block"
"github.com/s7techlab/hlf-sdk-go/observer"
)

type (
Expand Down Expand Up @@ -68,56 +67,55 @@ func NewAction(actionMach TxActionMatch, opts ...ActionOpt) *Action {
return a
}

func (s *Action) Transform(block *observer.ParsedBlock) error {
if block.Block == nil {
return nil
func (s *Action) Transform(block *hlfproto.Block) (transformed *hlfproto.Block, original *hlfproto.Block, err error) {
if block == nil {
return nil, nil, nil
}

// if block is transformed, copy of block will be saved to block.BlockOriginal
blockCopy := deepcopy.Copy(block.Block).(*hlfproto.Block)
blockCopy := deepcopy.Copy(block).(*hlfproto.Block)
blockIsTransformed := false

for _, envelope := range block.Block.Data.Envelopes {
if envelope.Payload.Transaction == nil {
for _, envelope := range block.GetData().GetEnvelopes() {
if envelope.GetPayload().GetTransaction() == nil {
continue
}

for _, txAction := range envelope.Payload.Transaction.Actions {
for _, txAction := range envelope.GetPayload().GetTransaction().GetActions() {
if !s.match(txAction) {
continue
}

for _, argsTransformer := range s.inputArgsTransformers {
if err := argsTransformer.Transform(txAction.ChaincodeSpec().Input.Args); err != nil {
return fmt.Errorf(`transform input args: %w`, err)
if err := argsTransformer.Transform(txAction.ChaincodeSpec().GetInput().GetArgs()); err != nil {
return nil, nil, fmt.Errorf(`transform input args: %w`, err)
}
}

for _, eventTransformer := range s.eventTransformers {
if err := eventTransformer.Transform(txAction.Event()); err != nil {
return fmt.Errorf(`transform event: %w`, err)
return nil, nil, fmt.Errorf(`transform event: %w`, err)
}
}

for _, rwSet := range txAction.NsReadWriteSet() {
for _, write := range rwSet.Rwset.Writes {
for _, write := range rwSet.GetRwset().GetWrites() {
for _, kvWriteTransformer := range s.kvWriteTransformers {
origKey := write.Key
if err := kvWriteTransformer.Transform(write); err != nil {
return fmt.Errorf(`transform KV write with key: %s: %w`, write.Key, err)
return nil, nil, fmt.Errorf(`transform KV write with key: %s: %w`, write.Key, err)
}

if origKey != write.Key {
blockIsTransformed = true
}
}
}

for _, read := range rwSet.Rwset.Reads {
for _, read := range rwSet.GetRwset().GetReads() {
for _, kvReadTransform := range s.kvReadTransformers {
origKey := read.Key
if err := kvReadTransform.Transform(read); err != nil {
return fmt.Errorf(`transform KV read with key: %s: %w`, read.Key, err)
return nil, nil, fmt.Errorf(`transform KV read with key: %s: %w`, read.Key, err)
}
if origKey != read.Key {
blockIsTransformed = true
Expand All @@ -127,18 +125,18 @@ func (s *Action) Transform(block *observer.ParsedBlock) error {

for _, actionPayloadTransform := range s.actionPayloadTransformers {
if err := actionPayloadTransform.Transform(txAction); err != nil {
return fmt.Errorf(`transform action payload: %w`, err)
return nil, nil, fmt.Errorf(`transform action payload: %w`, err)
}
}
}
}
}

if blockIsTransformed {
block.BlockOriginal = blockCopy
return block, blockCopy, nil
}

return nil
return block, nil, nil
}

func TxChaincodeIDMatch(chaincode string) TxActionMatch {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"

"github.com/s7techlab/hlf-sdk-go/observer"
hlfproto "github.com/s7techlab/hlf-sdk-go/block"
)

const (
Expand Down Expand Up @@ -53,7 +53,7 @@ func keyReplace(key string) string {
return key
}

var LifecycleTransformers = []observer.BlockTransformer{
var LifecycleTransformers = []hlfproto.Transformer{
NewAction(
TxChaincodeIDMatch(LifecycleChaincodeName),
WithKVWriteTransformer(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions block/transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package block

// Transformer transforms parsed observer data. For example decrypt, or transformer protobuf state to json
type Transformer interface {
Transform(*Block) (*Block, *Block, error)
}
Loading

0 comments on commit e5ee138

Please sign in to comment.