Skip to content

Commit

Permalink
ch fetcher/block deliverer mocks, rm cfg from pool
Browse files Browse the repository at this point in the history
  • Loading branch information
bogatyr285 committed Feb 11, 2022
1 parent 1d037d5 commit a5c5ae1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions api/chaincode_cscc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type CSCC interface {
GetConfigBlock(ctx context.Context, channelName string) (*common.Block, error)
// GetChannelConfig returns channel configuration
GetChannelConfig(ctx context.Context, channelName string) (*common.Config, error)
ChannelsFetcher
}

type ChannelsFetcher interface {
// GetChannels returns list of joined channels
GetChannels(ctx context.Context) (*peer.ChannelQueryResponse, error)
}
3 changes: 1 addition & 2 deletions client/chaincode/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
"github.com/s7techlab/hlf-sdk-go/v2/api"
"github.com/s7techlab/hlf-sdk-go/v2/api/config"
"github.com/s7techlab/hlf-sdk-go/v2/client"
"github.com/s7techlab/hlf-sdk-go/v2/client/chaincode"
"github.com/s7techlab/hlf-sdk-go/v2/client/chaincode/txwaiter"
Expand Down Expand Up @@ -262,7 +261,7 @@ func TestInvokeBuilder_Do(t *testing.T) {
}
)

peerPool := pool.New(context.Background(), logger.DefaultLogger, config.PoolConfig{})
peerPool := pool.New(context.Background(), logger.DefaultLogger)
peerPool.Add(`org1msp`, peerOrg1, defaultAlivePeer)
peerPool.Add(`org2msp`, peerOrg2, defaultAlivePeer)
peerPool.Add(`org3msp`, peerOrg3, defaultAlivePeer)
Expand Down
2 changes: 1 addition & 1 deletion client/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func NewCore(mspId string, identity api.Identity, opts ...CoreOpt) (api.Core, er
if core.config == nil {
return nil, api.ErrEmptyConfig
}
core.peerPool = pool.New(core.ctx, core.logger, core.config.Pool)
core.peerPool = pool.New(core.ctx, core.logger)
for _, mspConfig := range core.config.MSP {
for _, peerConfig := range mspConfig.Endorsers {
p, err := peer.New(peerConfig, core.logger)
Expand Down
17 changes: 5 additions & 12 deletions client/testing/block_deliverer_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@ import (
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
"github.com/s7techlab/hlf-sdk-go/v2/api"
)

type blocksReader struct {
type BlocksDelivererMock struct {
// <channel-name> => [<block1.pb>,...<blockN.pb>]
data map[string][]*common.Block
closeWhenAllRead bool
closer func() error
}

// NewBlocksDelivererMock - read all blocks in proto format from provided folder.
// returns sdkapi.BlocksDeliverer - interface
// closeWhenAllRead - close channel when all data have been written
// closerFunc - close func that will be returned from Blocks()
func NewBlocksDelivererMock(rootPath string, closeWhenAllRead bool, closerFunc func() error) (api.BlocksDeliverer, error) {
func NewBlocksDelivererMock(rootPath string, closeWhenAllRead bool) (*BlocksDelivererMock, error) {
var err error

dc := &blocksReader{
dc := &BlocksDelivererMock{
data: make(map[string][]*common.Block),
closeWhenAllRead: closeWhenAllRead,
closer: closerFunc,
}

channels := make(map[string]map[int][]byte)
Expand Down Expand Up @@ -113,7 +110,7 @@ func NewBlocksDelivererMock(rootPath string, closeWhenAllRead bool, closerFunc f
return dc, nil
}

func (m *blocksReader) Blocks(
func (m *BlocksDelivererMock) Blocks(
ctx context.Context,
channelName string,
identity msp.SigningIdentity,
Expand All @@ -122,11 +119,7 @@ func (m *blocksReader) Blocks(
if _, ok := m.data[channelName]; !ok {
return nil, nil, fmt.Errorf("have no mocked data for this channel")
}
if m.closer == nil {
closer = func() error { return nil }
} else {
closer = m.closer
}
closer = func() error { return nil }

var (
blockRangeFrom int64 = 0
Expand Down
28 changes: 28 additions & 0 deletions client/testing/channels_fetcher_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testing

import (
"context"

"github.com/hyperledger/fabric-protos-go/peer"
)

type ChannelsFetcherMock struct {
channels []string
}

func NewChannelsFetcherMock(channels []string) *ChannelsFetcherMock {
return &ChannelsFetcherMock{
channels: channels,
}
}

func (c ChannelsFetcherMock) GetChannels(ctx context.Context) (*peer.ChannelQueryResponse, error) {
channels := []*peer.ChannelInfo{}
for i := range c.channels {
channels = append(channels, &peer.ChannelInfo{ChannelId: c.channels[i]})
}

return &peer.ChannelQueryResponse{
Channels: channels,
}, nil
}
6 changes: 1 addition & 5 deletions peer/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ import (
"google.golang.org/grpc/status"

"github.com/s7techlab/hlf-sdk-go/v2/api"
"github.com/s7techlab/hlf-sdk-go/v2/api/config"
)

type peerPool struct {
config config.PoolConfig

log *zap.Logger
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -196,14 +193,13 @@ func (p *peerPool) Close() error {
return nil
}

func New(ctx context.Context, log *zap.Logger, config config.PoolConfig) api.PeerPool {
func New(ctx context.Context, log *zap.Logger) api.PeerPool {
ctx, cancel := context.WithCancel(ctx)

return &peerPool{
store: make(map[string][]*peerPoolPeer),
log: log.Named(`PeerPool`),
ctx: ctx,
cancel: cancel,
config: config,
}
}

0 comments on commit a5c5ae1

Please sign in to comment.