github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/chaincode/config.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode
     8  
     9  import (
    10  	"strconv"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/hyperledger/fabric/common/flogging"
    15  	"github.com/spf13/viper"
    16  )
    17  
    18  const (
    19  	defaultExecutionTimeout = 30 * time.Second
    20  	minimumStartupTimeout   = 5 * time.Second
    21  )
    22  
    23  type Config struct {
    24  	TotalQueryLimit int
    25  	TLSEnabled      bool
    26  	Keepalive       time.Duration
    27  	ExecuteTimeout  time.Duration
    28  	InstallTimeout  time.Duration
    29  	StartupTimeout  time.Duration
    30  	LogFormat       string
    31  	LogLevel        string
    32  	ShimLogLevel    string
    33  	SCCWhitelist    map[string]bool
    34  }
    35  
    36  func GlobalConfig() *Config {
    37  	c := &Config{}
    38  	c.load()
    39  	return c
    40  }
    41  
    42  func (c *Config) load() {
    43  	viper.SetEnvPrefix("CORE")
    44  	viper.AutomaticEnv()
    45  	replacer := strings.NewReplacer(".", "_")
    46  	viper.SetEnvKeyReplacer(replacer)
    47  
    48  	c.TLSEnabled = viper.GetBool("peer.tls.enabled")
    49  
    50  	c.Keepalive = toSeconds(viper.GetString("chaincode.keepalive"), 0)
    51  	c.ExecuteTimeout = viper.GetDuration("chaincode.executetimeout")
    52  	if c.ExecuteTimeout < time.Second {
    53  		c.ExecuteTimeout = defaultExecutionTimeout
    54  	}
    55  	c.InstallTimeout = viper.GetDuration("chaincode.installTimeout")
    56  	c.StartupTimeout = viper.GetDuration("chaincode.startuptimeout")
    57  	if c.StartupTimeout < minimumStartupTimeout {
    58  		c.StartupTimeout = minimumStartupTimeout
    59  	}
    60  
    61  	c.SCCWhitelist = map[string]bool{}
    62  	for k, v := range viper.GetStringMapString("chaincode.system") {
    63  		c.SCCWhitelist[k] = parseBool(v)
    64  	}
    65  
    66  	c.LogFormat = viper.GetString("chaincode.logging.format")
    67  	c.LogLevel = getLogLevelFromViper("chaincode.logging.level")
    68  	c.ShimLogLevel = getLogLevelFromViper("chaincode.logging.shim")
    69  
    70  	c.TotalQueryLimit = 10000 // need a default just in case it's not set
    71  	if viper.IsSet("ledger.state.totalQueryLimit") {
    72  		c.TotalQueryLimit = viper.GetInt("ledger.state.totalQueryLimit")
    73  	}
    74  }
    75  
    76  func parseBool(s string) bool {
    77  	switch strings.ToLower(strings.TrimSpace(s)) {
    78  	case "true", "t", "1", "enable", "enabled", "yes":
    79  		return true
    80  	default:
    81  		return false
    82  	}
    83  }
    84  
    85  func toSeconds(s string, def int) time.Duration {
    86  	seconds, err := strconv.Atoi(s)
    87  	if err != nil {
    88  		return time.Duration(def) * time.Second
    89  	}
    90  
    91  	return time.Duration(seconds) * time.Second
    92  }
    93  
    94  // getLogLevelFromViper gets the chaincode container log levels from viper
    95  func getLogLevelFromViper(key string) string {
    96  	levelString := viper.GetString(key)
    97  	if !flogging.IsValidLevel(levelString) {
    98  		chaincodeLogger.Warningf("%s has invalid log level %s. defaulting to %s", key, levelString, flogging.DefaultLevel())
    99  		levelString = flogging.DefaultLevel()
   100  	}
   101  
   102  	return flogging.NameToLevel(levelString).String()
   103  }
   104  
   105  // DevModeUserRunsChaincode enables chaincode execution in a development
   106  // environment
   107  const DevModeUserRunsChaincode string = "dev"
   108  
   109  // IsDevMode returns true if the peer was configured with development-mode
   110  // enabled.
   111  func IsDevMode() bool {
   112  	mode := viper.GetString("chaincode.mode")
   113  
   114  	return mode == DevModeUserRunsChaincode
   115  }