Skip to content

Commit 784212e

Browse files
committed
[FAB-14324] add Peer#Channel(cid string)
- add method to get channel instance from the peer - replace reference to channels map with call to Channel - consolidate channels map serialization to the Channel method Change-Id: I281639c35283a58a960465a112a2b607f0ecbeac Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent c72b3a6 commit 784212e

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

core/peer/peer.go

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,8 @@ func buildTrustedRootsForChain(cm channelconfig.Resources) {
315315

316316
// setCurrConfigBlock sets the current config block of the specified channel
317317
func (p *Peer) setCurrConfigBlock(block *common.Block, cid string) error {
318-
p.mutex.Lock()
319-
defer p.mutex.Unlock()
320-
if c, ok := p.channels[cid]; ok {
321-
c.cb = block
318+
if c := p.Channel(cid); c != nil {
319+
c.cb = block // TODO: serialization?
322320
return nil
323321
}
324322
return errors.Errorf("[channel %s] channel not associated with this peer", cid)
@@ -344,13 +342,10 @@ func NewPeerServer(listenAddress string, serverConfig comm.ServerConfig) (*comm.
344342
type DeliverChainManager struct{}
345343

346344
func (DeliverChainManager) GetChain(chainID string) deliver.Chain {
347-
Default.mutex.RLock()
348-
defer Default.mutex.RUnlock()
349-
channel, ok := Default.channels[chainID]
350-
if !ok {
351-
return nil
345+
if channel := Default.Channel(chainID); channel != nil {
346+
return channel
352347
}
353-
return channel
348+
return nil
354349
}
355350

356351
// fileLedgerBlockStore implements the interface expected by
@@ -379,9 +374,7 @@ type configSupport struct{}
379374
// ConfigProto method of the returned object can be used to get the
380375
// proto representing the channel configuration.
381376
func (*configSupport) GetChannelConfig(cid string) cc.Config {
382-
Default.mutex.RLock()
383-
defer Default.mutex.RUnlock()
384-
channel := Default.channels[cid]
377+
channel := Default.Channel(cid)
385378
if channel == nil {
386379
peerLogger.Errorf("[channel %s] channel not associated with this peer", cid)
387380
return nil
@@ -596,10 +589,17 @@ func (p *Peer) createChannel(
596589
return nil
597590
}
598591

599-
func (p *Peer) StoreForChannel(cid string) transientstore.Store {
592+
func (p *Peer) Channel(cid string) *Channel {
600593
p.mutex.RLock()
601594
defer p.mutex.RUnlock()
602595
if c, ok := p.channels[cid]; ok {
596+
return c
597+
}
598+
return nil
599+
}
600+
601+
func (p *Peer) StoreForChannel(cid string) transientstore.Store {
602+
if c := p.Channel(cid); c != nil {
603603
return c.store
604604
}
605605
return nil
@@ -608,9 +608,7 @@ func (p *Peer) StoreForChannel(cid string) transientstore.Store {
608608
// GetChannelConfig returns the channel configuration of the channel with channel ID. Note that this
609609
// call returns nil if channel cid has not been created.
610610
func (p *Peer) GetChannelConfig(cid string) channelconfig.Resources {
611-
p.mutex.RLock()
612-
defer p.mutex.RUnlock()
613-
if c, ok := p.channels[cid]; ok {
611+
if c := p.Channel(cid); c != nil {
614612
return c.resources
615613
}
616614
return nil
@@ -633,9 +631,7 @@ func (p *Peer) GetChannelsInfo() []*pb.ChannelInfo {
633631
// GetStableChannelConfig returns the stable channel configuration of the channel with channel ID.
634632
// Note that this call returns nil if channel cid has not been created.
635633
func (p *Peer) GetStableChannelConfig(cid string) channelconfig.Resources {
636-
p.mutex.RLock()
637-
defer p.mutex.RUnlock()
638-
if c, ok := p.channels[cid]; ok {
634+
if c := p.Channel(cid); c != nil {
639635
return c.bundleSource.StableBundle()
640636
}
641637
return nil
@@ -644,9 +640,7 @@ func (p *Peer) GetStableChannelConfig(cid string) channelconfig.Resources {
644640
// GetCurrConfigBlock returns the cached config block of the specified channel.
645641
// Note that this call returns nil if channel cid has not been created.
646642
func (p *Peer) GetCurrConfigBlock(cid string) *common.Block {
647-
p.mutex.RLock()
648-
defer p.mutex.RUnlock()
649-
if c, ok := p.channels[cid]; ok {
643+
if c := p.Channel(cid); c != nil {
650644
return c.cb
651645
}
652646
return nil
@@ -655,33 +649,24 @@ func (p *Peer) GetCurrConfigBlock(cid string) *common.Block {
655649
// GetLedger returns the ledger of the channel with channel ID. Note that this
656650
// call returns nil if channel cid has not been created.
657651
func (p *Peer) GetLedger(cid string) ledger.PeerLedger {
658-
p.mutex.RLock()
659-
defer p.mutex.RUnlock()
660-
if c, ok := p.channels[cid]; ok {
652+
if c := p.Channel(cid); c != nil {
661653
return c.ledger
662654
}
663655
return nil
664656
}
665657

666658
// GetMSPIDs returns the ID of each application MSP defined on this channel
667659
func (p *Peer) GetMSPIDs(cid string) []string {
668-
p.mutex.RLock()
669-
defer p.mutex.RUnlock()
670-
671-
c, ok := p.channels[cid]
672-
if !ok {
673-
return nil
660+
if c := p.Channel(cid); c != nil {
661+
return c.GetMSPIDs()
674662
}
675-
676-
return c.GetMSPIDs()
663+
return nil
677664
}
678665

679666
// GetPolicyManager returns the policy manager of the channel with channel ID. Note that this
680667
// call returns nil if channel cid has not been created.
681668
func (p *Peer) GetPolicyManager(cid string) policies.Manager {
682-
p.mutex.RLock()
683-
defer p.mutex.RUnlock()
684-
if c, ok := p.channels[cid]; ok {
669+
if c := p.Channel(cid); c != nil {
685670
return c.resources.PolicyManager()
686671
}
687672
return nil

0 commit comments

Comments
 (0)