Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit fa4badf

Browse files
committed
[FABG-788] Set PeerChannelConfig defaults properly
When a channels->peers entry is specified with NO PeerChannelConfig in config.yaml, make sure the defaults are filled in correctly. Change-Id: I5032d914cc037998c93d797102ba114025d16449 Signed-off-by: Nye Liu <nye@blockdaemon.com>
1 parent 3081d70 commit fa4badf

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

pkg/fab/endpointconfig.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,22 @@ func peerChannelConfigHookFunc() mapstructure.DecodeHookFunc {
17491749
t reflect.Type,
17501750
data interface{}) (interface{}, error) {
17511751

1752-
//If target is of type 'fab.PeerChannelConfig', then only hook should work
1752+
//Run through each PeerChannelConfig, create empty config map if value is nil
1753+
if t == reflect.TypeOf(map[string]PeerChannelConfig{}) {
1754+
dataMap, ok := data.(map[string]interface{})
1755+
if ok {
1756+
for k, v := range dataMap {
1757+
if v == nil {
1758+
// Make an empty map. It will be filled in with defaults
1759+
// in other hook below
1760+
dataMap[k] = make(map[string]interface{})
1761+
}
1762+
}
1763+
return dataMap, nil
1764+
}
1765+
}
1766+
1767+
//If target is of type 'fab.PeerChannelConfig', fill in defaults if not already specified
17531768
if t == reflect.TypeOf(PeerChannelConfig{}) {
17541769
dataMap, ok := data.(map[string]interface{})
17551770
if ok {

pkg/fab/endpointconfig_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,21 +1107,27 @@ func TestPeerChannelConfig(t *testing.T) {
11071107
assert.Equal(t, 6*time.Second, eventPolicies.PeerMonitorPeriod, "Unexpected value for PeerMonitorPeriod")
11081108

11091109
//Test if custom hook for (default=true) func is working
1110-
assert.True(t, len(networkConfig.Channels[orgChannelID].Peers) == 2)
1110+
assert.True(t, len(networkConfig.Channels[orgChannelID].Peers) == 3)
11111111
//test orgchannel peer1 (EndorsingPeer should be true as set, remaining should be default = true)
11121112
orgChannelPeer1 := networkConfig.Channels[orgChannelID].Peers["peer0.org1.example.com"]
11131113
assert.True(t, orgChannelPeer1.EndorsingPeer)
11141114
assert.True(t, orgChannelPeer1.LedgerQuery)
11151115
assert.True(t, orgChannelPeer1.EventSource)
11161116
assert.True(t, orgChannelPeer1.ChaincodeQuery)
11171117

1118-
//test orgchannel peer1 (EndorsingPeer should be false as set, remaining should be default = true)
1118+
//test orgchannel peer2 (EndorsingPeer should be false as set, remaining should be default = true)
11191119
orgChannelPeer2 := networkConfig.Channels[orgChannelID].Peers["peer0.org2.example.com"]
11201120
assert.False(t, orgChannelPeer2.EndorsingPeer)
11211121
assert.True(t, orgChannelPeer2.LedgerQuery)
11221122
assert.True(t, orgChannelPeer2.EventSource)
11231123
assert.True(t, orgChannelPeer2.ChaincodeQuery)
11241124

1125+
//test orgchannel peer3 (All should be true)
1126+
orgChannelPeer3 := networkConfig.Channels[orgChannelID].Peers["peer0.org3.example.com"]
1127+
assert.True(t, orgChannelPeer3.EndorsingPeer)
1128+
assert.True(t, orgChannelPeer3.LedgerQuery)
1129+
assert.True(t, orgChannelPeer3.EventSource)
1130+
assert.True(t, orgChannelPeer3.ChaincodeQuery)
11251131
}
11261132

11271133
func TestEndpointConfigWithMultipleBackends(t *testing.T) {
@@ -1281,6 +1287,7 @@ func tamperPeerChannelConfig(backend *mocks.MockConfigBackend) {
12811287
"peers": map[string]interface{}{
12821288
"peer0.org1.example.com": map[string]interface{}{"endorsingpeer": true},
12831289
"peer0.org2.example.com": map[string]interface{}{"endorsingpeer": false},
1290+
"peer0.org3.example.com": nil,
12841291
},
12851292
}
12861293
(channelsMap.(map[string]interface{}))[orgChannelID] = orgChannel

0 commit comments

Comments
 (0)