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