Skip to content

Commit 1f3ec39

Browse files
committed
[FAB-9551] New config object for ChaincodeSupport
Change-Id: Ie9879f7051bd82b5071aa7578d938d706ee1147f Signed-off-by: Saad Karim <skarim@us.ibm.com> Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent ddbe6fc commit 1f3ec39

8 files changed

+237
-116
lines changed

core/chaincode/chaincode_support.go

+17-75
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ package chaincode
88

99
import (
1010
"fmt"
11-
"strconv"
12-
"strings"
1311
"time"
1412

1513
"github.com/golang/protobuf/proto"
16-
"github.com/hyperledger/fabric/common/flogging"
1714
"github.com/hyperledger/fabric/core/chaincode/accesscontrol"
1815
"github.com/hyperledger/fabric/core/common/ccprovider"
1916
"github.com/hyperledger/fabric/core/container"
2017
"github.com/hyperledger/fabric/core/container/ccintf"
2118
pb "github.com/hyperledger/fabric/protos/peer"
22-
logging "github.com/op/go-logging"
2319
"github.com/pkg/errors"
2420
"github.com/spf13/viper"
2521
"golang.org/x/net/context"
@@ -70,99 +66,49 @@ func (cs *ChaincodeSupport) preLaunchSetup(chaincode string, notify chan bool) {
7066

7167
// NewChaincodeSupport creates a new ChaincodeSupport instance
7268
func NewChaincodeSupport(
69+
config *Config,
7370
peerAddress string,
7471
userrunsCC bool,
7572
ccstartuptimeout time.Duration,
7673
caCert []byte,
7774
certGenerator CertGenerator,
7875
) *ChaincodeSupport {
7976
ccprovider.SetChaincodesPath(ccprovider.GetCCsPath())
80-
pnid := viper.GetString("peer.networkId")
81-
pid := viper.GetString("peer.id")
8277

83-
theChaincodeSupport := &ChaincodeSupport{
84-
caCert: caCert,
85-
peerNetworkID: pnid,
86-
peerID: pid,
78+
cs := &ChaincodeSupport{
79+
caCert: caCert,
80+
peerNetworkID: config.PeerNetworkID,
81+
peerID: config.PeerID,
82+
userRunsCC: userrunsCC,
83+
ccStartupTimeout: ccstartuptimeout,
84+
keepalive: config.Keepalive,
85+
executetimeout: config.ExecuteTimeout,
8786
runningChaincodes: &runningChaincodes{
8887
chaincodeMap: make(map[string]*chaincodeRTEnv),
8988
launchStarted: make(map[string]bool),
9089
},
9190
}
9291

93-
theChaincodeSupport.userRunsCC = userrunsCC
94-
theChaincodeSupport.ccStartupTimeout = ccstartuptimeout
95-
theChaincodeSupport.peerTLS = viper.GetBool("peer.tls.enabled")
96-
97-
kadef := 0
98-
if ka := viper.GetString("chaincode.keepalive"); ka == "" {
99-
theChaincodeSupport.keepalive = time.Duration(kadef) * time.Second
100-
} else {
101-
t, terr := strconv.Atoi(ka)
102-
if terr != nil {
103-
chaincodeLogger.Errorf("Invalid keepalive value %s (%s) defaulting to %d", ka, terr, kadef)
104-
t = kadef
105-
} else if t <= 0 {
106-
chaincodeLogger.Debugf("Turn off keepalive(value %s)", ka)
107-
t = kadef
108-
}
109-
theChaincodeSupport.keepalive = time.Duration(t) * time.Second
110-
}
111-
112-
//default chaincode execute timeout is 30 secs
113-
execto := time.Duration(30) * time.Second
114-
if eto := viper.GetDuration("chaincode.executetimeout"); eto <= time.Duration(1)*time.Second {
115-
chaincodeLogger.Errorf("Invalid execute timeout value %s (should be at least 1s); defaulting to %s", eto, execto)
116-
} else {
117-
chaincodeLogger.Debugf("Setting execute timeout value to %s", eto)
118-
execto = eto
119-
}
120-
121-
theChaincodeSupport.executetimeout = execto
122-
123-
viper.SetEnvPrefix("CORE")
124-
viper.AutomaticEnv()
125-
replacer := strings.NewReplacer(".", "_")
126-
viper.SetEnvKeyReplacer(replacer)
127-
128-
theChaincodeSupport.chaincodeLogLevel = getLogLevelFromViper("level")
129-
theChaincodeSupport.shimLogLevel = getLogLevelFromViper("shim")
130-
theChaincodeSupport.logFormat = viper.GetString("chaincode.logging.format")
131-
13292
// Keep TestQueries working
133-
if !theChaincodeSupport.peerTLS {
93+
if !config.TLSEnabled {
13494
certGenerator = nil
13595
}
13696

137-
theChaincodeSupport.ContainerRuntime = &ContainerRuntime{
97+
cs.ContainerRuntime = &ContainerRuntime{
13898
CertGenerator: certGenerator,
13999
Processor: ProcessFunc(container.VMCProcess),
140100
CACert: caCert,
141101
PeerAddress: peerAddress,
142-
PeerID: pid,
143-
PeerNetworkID: pnid,
102+
PeerID: config.PeerID,
103+
PeerNetworkID: config.PeerNetworkID,
144104
CommonEnv: []string{
145-
"CORE_CHAINCODE_LOGGING_LEVEL=" + theChaincodeSupport.chaincodeLogLevel,
146-
"CORE_CHAINCODE_LOGGING_SHIM=" + theChaincodeSupport.shimLogLevel,
147-
"CORE_CHAINCODE_LOGGING_FORMAT=" + theChaincodeSupport.logFormat,
105+
"CORE_CHAINCODE_LOGGING_LEVEL=" + config.LogLevel,
106+
"CORE_CHAINCODE_LOGGING_SHIM=" + config.ShimLogLevel,
107+
"CORE_CHAINCODE_LOGGING_FORMAT=" + config.LogFormat,
148108
},
149109
}
150110

151-
return theChaincodeSupport
152-
}
153-
154-
// getLogLevelFromViper gets the chaincode container log levels from viper
155-
func getLogLevelFromViper(module string) string {
156-
levelString := viper.GetString("chaincode.logging." + module)
157-
_, err := logging.LogLevel(levelString)
158-
159-
if err == nil {
160-
chaincodeLogger.Debugf("CORE_CHAINCODE_%s set to level %s", strings.ToUpper(module), levelString)
161-
} else {
162-
chaincodeLogger.Warningf("CORE_CHAINCODE_%s has invalid log level %s. defaulting to %s", strings.ToUpper(module), levelString, flogging.DefaultLevel())
163-
levelString = flogging.DefaultLevel()
164-
}
165-
return levelString
111+
return cs
166112
}
167113

168114
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
@@ -174,12 +120,8 @@ type ChaincodeSupport struct {
174120
peerNetworkID string
175121
peerID string
176122
keepalive time.Duration
177-
chaincodeLogLevel string
178-
shimLogLevel string
179-
logFormat string
180123
executetimeout time.Duration
181124
userRunsCC bool
182-
peerTLS bool
183125
ContainerRuntime Runtime
184126
}
185127

core/chaincode/chaincode_support_test.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func initMockPeer(chainIDs ...string) error {
172172
ccStartupTimeout := time.Duration(10) * time.Second
173173
ca, _ := accesscontrol.NewCA()
174174
certGenerator := accesscontrol.NewAuthenticator(ca)
175-
theChaincodeSupport = NewChaincodeSupport("0.0.0.0:7052", false, ccStartupTimeout, ca.CertBytes(), certGenerator)
175+
theChaincodeSupport = NewChaincodeSupport(GlobalConfig(), "0.0.0.0:7052", false, ccStartupTimeout, ca.CertBytes(), certGenerator)
176176
SideEffectInitialize(theChaincodeSupport)
177177
theChaincodeSupport.executetimeout = time.Duration(1) * time.Second
178178

@@ -893,12 +893,9 @@ func TestLaunchAndWaitSuccess(t *testing.T) {
893893
}
894894

895895
newCCSupport := &ChaincodeSupport{
896-
peerTLS: false,
897-
chaincodeLogLevel: "debug",
898-
shimLogLevel: "info",
899-
ccStartupTimeout: time.Duration(10) * time.Second,
900-
peerNetworkID: "networkID",
901-
peerID: "peerID",
896+
ccStartupTimeout: time.Duration(10) * time.Second,
897+
peerNetworkID: "networkID",
898+
peerID: "peerID",
902899
runningChaincodes: &runningChaincodes{
903900
chaincodeMap: make(map[string]*chaincodeRTEnv),
904901
launchStarted: make(map[string]bool),
@@ -928,12 +925,9 @@ func TestLaunchAndWaitTimeout(t *testing.T) {
928925
}
929926

930927
newCCSupport := &ChaincodeSupport{
931-
peerTLS: false,
932-
chaincodeLogLevel: "debug",
933-
shimLogLevel: "info",
934-
ccStartupTimeout: 500 * time.Millisecond,
935-
peerNetworkID: "networkID",
936-
peerID: "peerID",
928+
ccStartupTimeout: 500 * time.Millisecond,
929+
peerNetworkID: "networkID",
930+
peerID: "peerID",
937931
runningChaincodes: &runningChaincodes{
938932
chaincodeMap: make(map[string]*chaincodeRTEnv),
939933
launchStarted: make(map[string]bool),
@@ -962,12 +956,9 @@ func TestLaunchAndWaitNotificationError(t *testing.T) {
962956
}
963957

964958
newCCSupport := &ChaincodeSupport{
965-
peerTLS: false,
966-
chaincodeLogLevel: "debug",
967-
shimLogLevel: "info",
968-
ccStartupTimeout: 10 * time.Second,
969-
peerNetworkID: "networkID",
970-
peerID: "peerID",
959+
ccStartupTimeout: 10 * time.Second,
960+
peerNetworkID: "networkID",
961+
peerID: "peerID",
971962
runningChaincodes: &runningChaincodes{
972963
chaincodeMap: make(map[string]*chaincodeRTEnv),
973964
launchStarted: make(map[string]bool),
@@ -995,12 +986,9 @@ func TestLaunchAndWaitLaunchError(t *testing.T) {
995986
}
996987

997988
newCCSupport := &ChaincodeSupport{
998-
peerTLS: false,
999-
chaincodeLogLevel: "debug",
1000-
shimLogLevel: "info",
1001-
ccStartupTimeout: time.Duration(10) * time.Second,
1002-
peerNetworkID: "networkID",
1003-
peerID: "peerID",
989+
ccStartupTimeout: time.Duration(10) * time.Second,
990+
peerNetworkID: "networkID",
991+
peerID: "peerID",
1004992
runningChaincodes: &runningChaincodes{
1005993
chaincodeMap: make(map[string]*chaincodeRTEnv),
1006994
launchStarted: make(map[string]bool),

core/chaincode/config.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
logging "github.com/op/go-logging"
16+
"github.com/spf13/viper"
17+
)
18+
19+
const (
20+
defaultExecutionTimeout = 30 * time.Second
21+
minimumStartupTimeout = 5 * time.Second
22+
)
23+
24+
type Config struct {
25+
PeerNetworkID string
26+
PeerID string
27+
TLSEnabled bool
28+
Keepalive time.Duration
29+
ExecuteTimeout time.Duration
30+
StartupTimeout time.Duration
31+
LogFormat string
32+
LogLevel string
33+
ShimLogLevel string
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.PeerNetworkID = viper.GetString("peer.networkId")
49+
c.PeerID = viper.GetString("peer.id")
50+
c.TLSEnabled = viper.GetBool("peer.tls.enabled")
51+
52+
c.Keepalive = toSeconds(viper.GetString("chaincode.keepalive"), 0)
53+
c.ExecuteTimeout = viper.GetDuration("chaincode.executetimeout")
54+
if c.ExecuteTimeout < time.Second {
55+
c.ExecuteTimeout = defaultExecutionTimeout
56+
}
57+
c.StartupTimeout = viper.GetDuration("chaincode.startuptimeout")
58+
if c.StartupTimeout < minimumStartupTimeout {
59+
c.StartupTimeout = 5 * time.Second
60+
}
61+
62+
c.LogFormat = viper.GetString("chaincode.logging.format")
63+
c.LogLevel = getLogLevelFromViper("chaincode.logging.level")
64+
c.ShimLogLevel = getLogLevelFromViper("chaincode.logging.shim")
65+
}
66+
67+
func toSeconds(s string, def int) time.Duration {
68+
seconds, err := strconv.Atoi(s)
69+
if err != nil {
70+
return time.Duration(def) * time.Second
71+
}
72+
73+
return time.Duration(seconds) * time.Second
74+
}
75+
76+
// getLogLevelFromViper gets the chaincode container log levels from viper
77+
func getLogLevelFromViper(key string) string {
78+
levelString := viper.GetString(key)
79+
_, err := logging.LogLevel(levelString)
80+
if err != nil {
81+
chaincodeLogger.Warningf("%s has invalid log level %s. defaulting to %s", key, levelString, flogging.DefaultLevel())
82+
levelString = flogging.DefaultLevel()
83+
}
84+
85+
return levelString
86+
}

0 commit comments

Comments
 (0)