Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Neznaemov committed Apr 3, 2024
1 parent 7394cac commit d31cf2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
12 changes: 12 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"

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

type CurrentIdentity interface {
Expand Down Expand Up @@ -43,6 +45,16 @@ type BlocksDeliverer interface {
) (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)
}

type Querier interface {
CurrentIdentity
// Query - shortcut for querying chaincodes
Expand Down
41 changes: 40 additions & 1 deletion client/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/s7techlab/hlf-sdk-go/api"
"github.com/s7techlab/hlf-sdk-go/api/config"
"github.com/s7techlab/hlf-sdk-go/block"
"github.com/s7techlab/hlf-sdk-go/client/channel"
"github.com/s7techlab/hlf-sdk-go/client/deliver"
grpcclient "github.com/s7techlab/hlf-sdk-go/client/grpc"
Expand Down Expand Up @@ -140,7 +141,7 @@ func (p *peer) Query(
return response.Response, nil
}

func (p *peer) Blocks(ctx context.Context, channel string, identity msp.SigningIdentity, blockRange ...int64) (blockChan <-chan *common.Block, closer func() error, err error) {
func (p *peer) Blocks(ctx context.Context, channel string, identity msp.SigningIdentity, blockRange ...int64) (<-chan *common.Block, func() error, error) {
p.logger.Debug(`peer blocks request`,
zap.String(`uri`, p.Uri()),
zap.String(`channel`, channel),
Expand Down Expand Up @@ -169,6 +170,44 @@ func (p *peer) Blocks(ctx context.Context, channel string, identity msp.SigningI
return bs.Blocks(), bs.Close, nil
}

func (p *peer) ParsedBlocks(ctx context.Context, channel string, identity msp.SigningIdentity, blockRange ...int64) (<-chan *block.Block, func() error, error) {
commonBlocks, commonCloser, err := p.Blocks(ctx, channel, identity, blockRange...)
if err != nil {
return nil, nil, err
}

parsedBlockChan := make(chan *block.Block)
go func() {
for {
select {
case b, ok := <-commonBlocks:
if !ok {
return
}

parsedBlock, err := block.ParseBlock(b)
if err != nil {
p.logger.Error("parse block", zap.String("channel", channel), zap.Uint64("number", b.Header.Number))
continue
}

parsedBlockChan <- parsedBlock
}
}
}()

parsedCloser := func() error {
if closerErr := commonCloser(); closerErr != nil {
return closerErr
}

close(parsedBlockChan)
return nil
}

return parsedBlockChan, parsedCloser, nil
}

func (p *peer) Events(ctx context.Context, channel string, chaincode string, identity msp.SigningIdentity, blockRange ...int64) (events chan interface {
Event() *fabricPeer.ChaincodeEvent
Block() uint64
Expand Down

0 comments on commit d31cf2f

Please sign in to comment.