github.com/yimialmonte/fabric@v2.1.1+incompatible/gossip/privdata/config.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package privdata
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"github.com/spf13/viper"
    14  )
    15  
    16  const (
    17  	reconcileSleepIntervalDefault         = time.Minute
    18  	reconcileBatchSizeDefault             = 10
    19  	implicitCollectionMaxPeerCountDefault = 1
    20  )
    21  
    22  // PrivdataConfig is the struct that defines the Gossip Privdata configurations.
    23  type PrivdataConfig struct {
    24  	// ReconcileSleepInterval determines the time reconciler sleeps from end of an interation until the beginning of the next
    25  	// reconciliation iteration.
    26  	ReconcileSleepInterval time.Duration
    27  	// ReconcileBatchSize determines the maximum batch size of missing private data that will be reconciled in a single iteration.
    28  	ReconcileBatchSize int
    29  	// ReconciliationEnabled is a flag that indicates whether private data reconciliation is enabled or not.
    30  	ReconciliationEnabled bool
    31  	// ImplicitCollectionDisseminationPolicy specifies the dissemination  policy for the peer's own implicit collection.
    32  	ImplicitCollDisseminationPolicy ImplicitCollectionDisseminationPolicy
    33  }
    34  
    35  // ImplicitCollectionDisseminationPolicy specifies the dissemination  policy for the peer's own implicit collection.
    36  // It is not applicable to private data for other organizations' implicit collections.
    37  type ImplicitCollectionDisseminationPolicy struct {
    38  	// RequiredPeerCount defines the minimum number of eligible peers to which each endorsing peer must successfully
    39  	// disseminate private data for its own implicit collection. Default is 0.
    40  	RequiredPeerCount int
    41  	// MaxPeerCount defines the maximum number of eligible peers to which each endorsing peer will attempt to
    42  	// disseminate private data for its own implicit collection. Default is 1.
    43  	MaxPeerCount int
    44  }
    45  
    46  // GlobalConfig obtains a set of configuration from viper, build and returns the config struct.
    47  func GlobalConfig() *PrivdataConfig {
    48  	c := &PrivdataConfig{}
    49  	c.loadPrivDataConfig()
    50  	return c
    51  }
    52  
    53  func (c *PrivdataConfig) loadPrivDataConfig() {
    54  	c.ReconcileSleepInterval = viper.GetDuration("peer.gossip.pvtData.reconcileSleepInterval")
    55  	if c.ReconcileSleepInterval == 0 {
    56  		logger.Warning("Configuration key peer.gossip.pvtData.reconcileSleepInterval isn't set, defaulting to", reconcileSleepIntervalDefault)
    57  		c.ReconcileSleepInterval = reconcileSleepIntervalDefault
    58  	}
    59  
    60  	c.ReconcileBatchSize = viper.GetInt("peer.gossip.pvtData.reconcileBatchSize")
    61  	if c.ReconcileBatchSize == 0 {
    62  		logger.Warning("Configuration key peer.gossip.pvtData.reconcileBatchSize isn't set, defaulting to", reconcileBatchSizeDefault)
    63  		c.ReconcileBatchSize = reconcileBatchSizeDefault
    64  	}
    65  
    66  	c.ReconciliationEnabled = viper.GetBool("peer.gossip.pvtData.reconciliationEnabled")
    67  
    68  	requiredPeerCount := viper.GetInt("peer.gossip.pvtData.implicitCollectionDisseminationPolicy.requiredPeerCount")
    69  
    70  	maxPeerCount := implicitCollectionMaxPeerCountDefault
    71  	if viper.Get("peer.gossip.pvtData.implicitCollectionDisseminationPolicy.maxPeerCount") != nil {
    72  		// allow override maxPeerCount to 0 that will effectively disable dissemination on the peer
    73  		maxPeerCount = viper.GetInt("peer.gossip.pvtData.implicitCollectionDisseminationPolicy.maxPeerCount")
    74  	}
    75  
    76  	if requiredPeerCount < 0 {
    77  		panic(fmt.Sprintf("peer.gossip.pvtData.implicitCollectionDisseminationPolicy.requiredPeerCount (%d) cannot be less than zero",
    78  			requiredPeerCount))
    79  	}
    80  	if maxPeerCount < requiredPeerCount {
    81  		panic(fmt.Sprintf("peer.gossip.pvtData.implicitCollectionDisseminationPolicy.maxPeerCount (%d) cannot be less than requiredPeerCount (%d)",
    82  			maxPeerCount, requiredPeerCount))
    83  
    84  	}
    85  
    86  	c.ImplicitCollDisseminationPolicy.RequiredPeerCount = requiredPeerCount
    87  	c.ImplicitCollDisseminationPolicy.MaxPeerCount = maxPeerCount
    88  }