Skip to content

Commit

Permalink
[FABG-763] Use hard-coded channel config if no _default
Browse files Browse the repository at this point in the history
If no default channel config is specified then a hard-coded
default channel config should be used instead of returning
nil.

Change-Id: Ib1f9c3ec88f0feca91accfa253949736100083cf
Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn committed Sep 18, 2018
1 parent 1eb1d28 commit cf7dc62
Show file tree
Hide file tree
Showing 26 changed files with 264 additions and 323 deletions.
11 changes: 4 additions & 7 deletions pkg/client/common/discovery/dynamicdiscovery/chservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ func (s *ChannelService) queryPeers() ([]fab.Peer, error) {
}

func (s *ChannelService) getTargets(ctx contextAPI.Client) ([]fab.PeerConfig, error) {
chPeers, ok := ctx.EndpointConfig().ChannelPeers(s.channelID)
if !ok {
return nil, errors.Errorf("failed to get channel peer configs for channel [%s]", s.channelID)
chPeers := ctx.EndpointConfig().ChannelPeers(s.channelID)
if len(chPeers) == 0 {
return nil, errors.Errorf("no channel peers configured for channel [%s]", s.channelID)
}

chConfig, ok := ctx.EndpointConfig().ChannelConfig(s.channelID)
if !ok {
return nil, errors.Errorf("failed to get channel endpoint configs for channel [%s]", s.channelID)
}
chConfig := ctx.EndpointConfig().ChannelConfig(s.channelID)

//pick number of peers given in channel policy
return random.PickRandomNPeerConfigs(chPeers, chConfig.Policies.Discovery.MaxTargets), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ type config struct {
peers []pfab.ChannelPeer
}

func (c *config) ChannelPeers(name string) ([]pfab.ChannelPeer, bool) {
if len(c.peers) == 0 {
return nil, false
}
return c.peers, true
func (c *config) ChannelPeers(name string) []pfab.ChannelPeer {
return c.peers
}
6 changes: 3 additions & 3 deletions pkg/client/common/discovery/staticdiscovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewService(config fab.EndpointConfig, peerCreator peerCreator, channelID st
}

// Use configured channel peers
chPeers, ok := config.ChannelPeers(channelID)
if !ok {
return nil, errors.New("unable to read configuration for channel peers")
chPeers := config.ChannelPeers(channelID)
if len(chPeers) == 0 {
return nil, errors.Errorf("no channel peers configured for channel [%s]", channelID)
}

peers := []fab.Peer{}
Expand Down
7 changes: 1 addition & 6 deletions pkg/client/common/filter/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ const (
func NewEndpointFilter(ctx context.Channel, et EndpointType) *EndpointFilter {

// Retrieve channel peers
chPeers, ok := ctx.EndpointConfig().ChannelPeers(ctx.ChannelID())
if !ok {
// Setting channel peers to empty due to config error, should not happen
chPeers = []fab.ChannelPeer{}
}

chPeers := ctx.EndpointConfig().ChannelPeers(ctx.ChannelID())
return &EndpointFilter{endpointType: et, ctx: ctx, chPeers: chPeers}

}
Expand Down
11 changes: 4 additions & 7 deletions pkg/client/common/selection/fabricselection/fabricselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,12 @@ func (s *Service) query(req *discclient.Request, chaincodes []*fab.ChaincodeCall

func (s *Service) getTargets(ctx contextAPI.Client) ([]fab.PeerConfig, error) {

chpeers, ok := ctx.EndpointConfig().ChannelPeers(s.channelID)
if !ok {
return nil, errors.Errorf("failed to get peer configs for channel [%s]", s.channelID)
chpeers := ctx.EndpointConfig().ChannelPeers(s.channelID)
if len(chpeers) == 0 {
return nil, errors.Errorf("no channel peers configured for channel [%s]", s.channelID)
}

chConfig, ok := ctx.EndpointConfig().ChannelConfig(s.channelID)
if !ok {
return nil, errors.Errorf("failed to get channel endpoint config for channel [%s]", s.channelID)
}
chConfig := ctx.EndpointConfig().ChannelConfig(s.channelID)

//pick number of peers based on channel policy
return random.PickRandomNPeerConfigs(chpeers, chConfig.Policies.Discovery.MaxTargets), nil
Expand Down
7 changes: 2 additions & 5 deletions pkg/client/common/selection/fabricselection/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,8 @@ type config struct {
peers []fab.ChannelPeer
}

func (c *config) ChannelPeers(name string) ([]fab.ChannelPeer, bool) {
if len(c.peers) == 0 {
return nil, false
}
return c.peers, true
func (c *config) ChannelPeers(name string) []fab.ChannelPeer {
return c.peers
}

func (c *config) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ func newFilter(channelID string, ctx contextAPI.Client, peers []fab.Peer, filter
}

func resolvePeerSorter(channelID string, ctx contextAPI.Client) options.PeerSorter {
channelConfig, ok := ctx.EndpointConfig().ChannelConfig(channelID)
if !ok {
logger.Debugf("Unable to get channel config for channel [%s]. Using block height priority selection sorter by default.", channelID)
return blockheightsorter.New()
}
channelConfig := ctx.EndpointConfig().ChannelConfig(channelID)
return resolveSortingStrategy(channelID, channelConfig, resolveBalancer(channelID, channelConfig))
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,17 +1146,14 @@ func (rc *Client) requestOrderer(opts *requestOptions, channelID string) (fab.Or
}

func (rc *Client) ordererConfig(channelID string) (*fab.OrdererConfig, error) {
orderers, ok := rc.ctx.EndpointConfig().ChannelOrderers(channelID)
orderers := rc.ctx.EndpointConfig().ChannelOrderers(channelID)

// TODO: Not sure that we should fallback to global orderers section.
// For now - not doing so.
//if err != nil || len(orderers) == 0 {
// orderers, err = rc.ctx.Config().OrderersConfig()
//}

if !ok {
return nil, errors.New("orderers lookup failed")
}
if len(orderers) == 0 {
return nil, errors.New("no orderers found")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) {

err = rc.JoinChannel("mychannel", WithTargets(peer1))
assert.NotNil(t, err, "Should have failed to join channel since no orderer has been configured")
assert.Contains(t, err.Error(), "orderer not found: orderers lookup failed")
assert.Contains(t, err.Error(), "orderer not found: no orderers found")

// Misconfigured channel orderer
configBackend = getInvalidChannelOrdererBackend(backend...)
Expand Down
6 changes: 3 additions & 3 deletions pkg/common/providers/fab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ type EndpointConfig interface {
PeerConfig(nameOrURL string) (*PeerConfig, bool)
NetworkConfig() *NetworkConfig
NetworkPeers() []NetworkPeer
ChannelConfig(name string) (*ChannelEndpointConfig, bool)
ChannelPeers(name string) ([]ChannelPeer, bool)
ChannelOrderers(name string) ([]OrdererConfig, bool)
ChannelConfig(name string) *ChannelEndpointConfig
ChannelPeers(name string) []ChannelPeer
ChannelOrderers(name string) []OrdererConfig
TLSCACertPool() CertPool
EventServiceConfig() EventServiceConfig
TLSClientCerts() []tls.Certificate
Expand Down
15 changes: 6 additions & 9 deletions pkg/common/providers/test/mockfab/mockfab.gen.go

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

17 changes: 17 additions & 0 deletions pkg/common/providers/test/mockmsp/mockmsp.gen.go

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

6 changes: 1 addition & 5 deletions pkg/fab/channel/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ func orderersFromChannelCfg(ctx context.Client, cfg fab.ChannelCfg) ([]fab.Order
//will return empty list when orderers are not found in endpoint config
func orderersFromChannel(ctx context.Client, channelID string) ([]fab.Orderer, error) {

chNetworkConfig, ok := ctx.EndpointConfig().ChannelConfig(channelID)
if !ok {
return []fab.Orderer{}, nil
}

chNetworkConfig := ctx.EndpointConfig().ChannelConfig(channelID)
orderers := []fab.Orderer{}
for _, chOrderer := range chNetworkConfig.Orderers {

Expand Down
39 changes: 12 additions & 27 deletions pkg/fab/chconfig/chconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ var logger = logging.NewLogger("fabsdk/fab")
var overrideRetryHandler retry.Handler
var versionCapabilityPattern = regexp.MustCompile(`^V(\d+)_(\d+)$`)

const (
defaultMinResponses = 1
defaultMaxTargets = 2
)

// Opts contains options for retrieving channel configuration
type Opts struct {
Orderer fab.Orderer // if configured, channel config will be retrieved from this orderer
Expand Down Expand Up @@ -202,9 +197,9 @@ func (c *ChannelConfig) queryPeers(reqCtx reqContext.Context) (*ChannelCfg, erro

func (c *ChannelConfig) calculateTargetsFromConfig(ctx context.Client) ([]fab.ProposalProcessor, error) {
targets := []fab.ProposalProcessor{}
chPeers, ok := ctx.EndpointConfig().ChannelPeers(c.channelID)
if !ok {
return nil, errors.New("read configuration for channel peers failed")
chPeers := ctx.EndpointConfig().ChannelPeers(c.channelID)
if len(chPeers) == 0 {
return nil, errors.Errorf("no channel peers configured for channel [%s]", c.channelID)
}

for _, p := range chPeers {
Expand Down Expand Up @@ -241,14 +236,12 @@ func (c *ChannelConfig) resolveOptsFromConfig(ctx context.Client) error {
return nil
}

//If missing from opts, check config and update opts from config
chSdkCfg, ok := ctx.EndpointConfig().ChannelConfig(c.channelID)
if ok {
//resolve opts
c.resolveMaxResponsesOptsFromConfig(chSdkCfg)
c.resolveMinResponsesOptsFromConfig(chSdkCfg)
c.resolveRetryOptsFromConfig(chSdkCfg)
}
chSdkCfg := ctx.EndpointConfig().ChannelConfig(c.channelID)

//resolve opts
c.resolveMaxResponsesOptsFromConfig(chSdkCfg)
c.resolveMinResponsesOptsFromConfig(chSdkCfg)
c.resolveRetryOptsFromConfig(chSdkCfg)

//apply default to missing opts
c.applyDefaultOpts()
Expand All @@ -257,33 +250,25 @@ func (c *ChannelConfig) resolveOptsFromConfig(ctx context.Client) error {
}

func (c *ChannelConfig) resolveMaxResponsesOptsFromConfig(chSdkCfg *fab.ChannelEndpointConfig) {
if c.opts.MaxTargets == 0 && &chSdkCfg.Policies != nil && &chSdkCfg.Policies.QueryChannelConfig != nil {
if c.opts.MaxTargets == 0 {
c.opts.MaxTargets = chSdkCfg.Policies.QueryChannelConfig.MaxTargets
}
}

func (c *ChannelConfig) resolveMinResponsesOptsFromConfig(chSdkCfg *fab.ChannelEndpointConfig) {
if c.opts.MinResponses == 0 && &chSdkCfg.Policies != nil && &chSdkCfg.Policies.QueryChannelConfig != nil {
if c.opts.MinResponses == 0 {
c.opts.MinResponses = chSdkCfg.Policies.QueryChannelConfig.MinResponses
}
}

func (c *ChannelConfig) resolveRetryOptsFromConfig(chSdkCfg *fab.ChannelEndpointConfig) {
if c.opts.RetryOpts.RetryableCodes == nil {
if c.opts.RetryOpts.RetryableCodes == nil && &chSdkCfg.Policies != nil && &chSdkCfg.Policies.QueryChannelConfig != nil {
c.opts.RetryOpts = chSdkCfg.Policies.QueryChannelConfig.RetryOpts
}
c.opts.RetryOpts = chSdkCfg.Policies.QueryChannelConfig.RetryOpts
c.opts.RetryOpts.RetryableCodes = retry.ChannelConfigRetryableCodes
}
}

func (c *ChannelConfig) applyDefaultOpts() {
if c.opts.MaxTargets == 0 {
c.opts.MaxTargets = defaultMaxTargets
}
if c.opts.MinResponses == 0 {
c.opts.MinResponses = defaultMinResponses
}
if c.opts.RetryOpts.Attempts == 0 {
c.opts.RetryOpts.Attempts = retry.DefaultAttempts
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fab/chconfig/chconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ type customMockConfig struct {
called bool
}

func (c *customMockConfig) ChannelConfig(name string) (*fab.ChannelEndpointConfig, bool) {
func (c *customMockConfig) ChannelConfig(name string) *fab.ChannelEndpointConfig {
c.called = true
return c.chConfig, true
return c.chConfig
}

//customRetryHandler is wrapper around retry handler which keeps count of attempts for unit-testing
Expand Down
Loading

0 comments on commit cf7dc62

Please sign in to comment.