Skip to content

Commit 7918d5e

Browse files
author
Jason Yellick
committed
[FAB-3831] Report empty application/orderer groups
The config interface currently returns the application/orderer config groups as an interface definition. Because of this, a simple nil check will not generally determine whether the config is actually set, and consequently attempting to access the config may cause a nil pointer dereference (even when the nil check returns false). This CR adds a bool to this interface, to allow the caller to determine whether the interface has been populated. Change-Id: I37e0ba25dcacffb273e5712f072d65786206b05b Signed-off-by: Jason Yellick <[email protected]>
1 parent f664fdf commit 7918d5e

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

common/configtx/api/api.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ type Resources interface {
5959
ChannelConfig() config.Channel
6060

6161
// OrdererConfig returns the config.Orderer for the channel
62-
OrdererConfig() config.Orderer
62+
// and whether the Orderer config exists
63+
OrdererConfig() (config.Orderer, bool)
6364

6465
// ConsortiumsConfig() returns the config.Consortiums for the channel
6566
// and whether the consortiums config exists
6667
ConsortiumsConfig() (config.Consortiums, bool)
6768

6869
// ApplicationConfig returns the configtxapplication.SharedConfig for the channel
69-
ApplicationConfig() config.Application
70+
// and whether the Application config exists
71+
ApplicationConfig() (config.Application, bool)
7072

7173
// MSPManager returns the msp.MSPManager for the chain
7274
MSPManager() msp.MSPManager

common/configtx/initializer.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,21 @@ func (r *resources) ChannelConfig() config.Channel {
4747
}
4848

4949
// OrdererConfig returns the api.OrdererConfig for the chain
50-
func (r *resources) OrdererConfig() config.Orderer {
51-
return r.configRoot.Orderer()
50+
func (r *resources) OrdererConfig() (config.Orderer, bool) {
51+
result := r.configRoot.Orderer()
52+
if result == nil {
53+
return nil, false
54+
}
55+
return result, true
5256
}
5357

5458
// ApplicationConfig returns the api.ApplicationConfig for the chain
55-
func (r *resources) ApplicationConfig() config.Application {
56-
return r.configRoot.Application()
59+
func (r *resources) ApplicationConfig() (config.Application, bool) {
60+
result := r.configRoot.Application()
61+
if result == nil {
62+
return nil, false
63+
}
64+
return result, true
5765
}
5866

5967
// ConsortiumsConfig returns the api.ConsortiumsConfig for the chain and whether or not

common/mocks/configtx/configtx.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func (r *Resources) ChannelConfig() config.Channel {
5757
}
5858

5959
// Returns the OrdererConfigVal
60-
func (r *Resources) OrdererConfig() config.Orderer {
61-
return r.OrdererConfigVal
60+
func (r *Resources) OrdererConfig() (config.Orderer, bool) {
61+
return r.OrdererConfigVal, r.OrdererConfigVal == nil
6262
}
6363

6464
// Returns the ApplicationConfigVal
65-
func (r *Resources) ApplicationConfig() config.Application {
66-
return r.ApplicationConfigVal
65+
func (r *Resources) ApplicationConfig() (config.Application, bool) {
66+
return r.ApplicationConfigVal, r.ApplicationConfigVal == nil
6767
}
6868

6969
func (r *Resources) ConsortiumsConfig() (config.Consortiums, bool) {

core/peer/peer.go

+24-9
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,14 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
185185
gossipEventer := service.GetGossipService().NewConfigEventer()
186186

187187
gossipCallbackWrapper := func(cm configtxapi.Manager) {
188+
ac, ok := configtxInitializer.ApplicationConfig()
189+
if !ok {
190+
// TODO, handle a missing ApplicationConfig more gracefully
191+
ac = nil
192+
}
188193
gossipEventer.ProcessConfigUpdate(&chainSupport{
189194
Manager: cm,
190-
Application: configtxInitializer.ApplicationConfig(),
195+
Application: ac,
191196
})
192197
service.GetGossipService().SuspectPeers(func(identity api.PeerIdentityType) bool {
193198
// TODO: this is a place-holder that would somehow make the MSP layer suspect
@@ -214,9 +219,13 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
214219
// TODO remove once all references to mspmgmt are gone from peer code
215220
mspmgmt.XXXSetMSPManager(cid, configtxManager.MSPManager())
216221

222+
ac, ok := configtxInitializer.ApplicationConfig()
223+
if !ok {
224+
ac = nil
225+
}
217226
cs := &chainSupport{
218227
Manager: configtxManager,
219-
Application: configtxManager.ApplicationConfig(), // TODO, refactor as this is accessible through Manager
228+
Application: ac, // TODO, refactor as this is accessible through Manager
220229
ledger: ledger,
221230
}
222231

@@ -386,10 +395,14 @@ func buildTrustedRootsForChain(cm configtxapi.Manager) {
386395
ordererRootCAs := [][]byte{}
387396
appOrgMSPs := make(map[string]struct{})
388397

389-
//loop through app orgs and build map of MSPIDs
390-
for _, appOrg := range cm.ApplicationConfig().Organizations() {
391-
appOrgMSPs[appOrg.MSPID()] = struct{}{}
398+
ac, ok := cm.ApplicationConfig()
399+
if ok {
400+
//loop through app orgs and build map of MSPIDs
401+
for _, appOrg := range ac.Organizations() {
402+
appOrgMSPs[appOrg.MSPID()] = struct{}{}
403+
}
392404
}
405+
393406
cid := cm.ChainID()
394407
peerLogger.Debugf("updating root CAs for channel [%s]", cid)
395408
msps, err := cm.MSPManager().GetMSPs()
@@ -452,13 +465,15 @@ func GetMSPIDs(cid string) []string {
452465
return mockMSPIDGetter(cid)
453466
}
454467
if c, ok := chains.list[cid]; ok {
455-
if c == nil || c.cs == nil ||
456-
c.cs.ApplicationConfig() == nil ||
457-
c.cs.ApplicationConfig().Organizations() == nil {
468+
if c == nil || c.cs == nil {
469+
return nil
470+
}
471+
ac, ok := c.cs.ApplicationConfig()
472+
if !ok || ac.Organizations() == nil {
458473
return nil
459474
}
460475

461-
orgs := c.cs.ApplicationConfig().Organizations()
476+
orgs := ac.Organizations()
462477
toret := make([]string, len(orgs))
463478
i := 0
464479
for _, org := range orgs {

orderer/multichain/manager.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ type configResources struct {
5757
}
5858

5959
func (cr *configResources) SharedConfig() config.Orderer {
60-
return cr.OrdererConfig()
60+
oc, ok := cr.OrdererConfig()
61+
if !ok {
62+
logger.Panicf("[channel %s] has no orderer configuration", cr.ChainID())
63+
}
64+
return oc
6165
}
6266

6367
type ledgerResources struct {

0 commit comments

Comments
 (0)