Skip to content

Commit

Permalink
Pass channel config to service discovery
Browse files Browse the repository at this point in the history
In the current code, service discovery has a dependency that
provides with the latest config block. However, a peer that
bootstraps from a snapshot does not has the historical blocks.

This commit makes changes for service discovery to work with
channel configuration that is supplied from the state.

Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi authored and denyeart committed Oct 22, 2020
1 parent 90733a0 commit 44f6ef5
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 357 deletions.
9 changes: 9 additions & 0 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func MakeGenesisBlock(channelID string) (*cb.Block, error) {
return gb, nil
}

func MakeChannelConfig(channelID string) (*cb.Config, error) {
profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
channelGroup, err := encoder.NewChannelGroup(profile)
if err != nil {
return nil, err
}
return &cb.Config{ChannelGroup: channelGroup}, nil
}

// MakeGenesisBlockWithMSPs creates a genesis block using the MSPs provided for the given channelID
func MakeGenesisBlockFromMSPs(channelID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig, appOrgID, ordererOrgID string) (*cb.Block, error) {
profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
Expand Down
10 changes: 5 additions & 5 deletions core/peer/configtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestConfigTxCreateLedger(t *testing.T) {
ledger, err := ledgerMgr.CreateLedger(channelID, genesisBlock)
require.NoError(t, err)

retrievedchanConf, err := retrievePersistedChannelConfig(ledger)
retrievedchanConf, err := RetrievePersistedChannelConfig(ledger)
require.NoError(t, err)
require.Equal(t, proto.CompactTextString(chanConf), proto.CompactTextString(retrievedchanConf))
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestConfigTxUpdateChanConfig(t *testing.T) {
lgr, err := ledgerMgr.CreateLedger(channelID, genesisBlock)
require.NoError(t, err)

retrievedchanConf, err := retrievePersistedChannelConfig(lgr)
retrievedchanConf, err := RetrievePersistedChannelConfig(lgr)
require.NoError(t, err)
require.Equal(t, proto.CompactTextString(chanConf), proto.CompactTextString(retrievedchanConf))

Expand All @@ -102,7 +102,7 @@ func TestConfigTxUpdateChanConfig(t *testing.T) {
inMemoryChanConf := bs.ConfigtxValidator().ConfigProto()
require.Equal(t, proto.CompactTextString(chanConf), proto.CompactTextString(inMemoryChanConf))

retrievedchanConf, err = retrievePersistedChannelConfig(lgr)
retrievedchanConf, err = RetrievePersistedChannelConfig(lgr)
require.NoError(t, err)
require.Equal(t, proto.CompactTextString(bs.ConfigtxValidator().ConfigProto()), proto.CompactTextString(retrievedchanConf))

Expand Down Expand Up @@ -130,7 +130,7 @@ func TestGenesisBlockCreateLedger(t *testing.T) {

lgr, err := ledgerMgr.CreateLedger("testchain", b)
require.NoError(t, err)
chanConf, err := retrievePersistedChannelConfig(lgr)
chanConf, err := RetrievePersistedChannelConfig(lgr)
require.NoError(t, err)
require.NotNil(t, chanConf)
t.Logf("chanConf = %s", chanConf)
Expand Down Expand Up @@ -219,7 +219,7 @@ func (h *testHelper) constructChannelBundle(channelID string, ledger ledger.Peer
return nil, err
}

chanConf, err := retrievePersistedChannelConfig(ledger)
chanConf, err := RetrievePersistedChannelConfig(ledger)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ func (p *Peer) CreateChannelFromSnapshot(
return nil
}

// retrievePersistedChannelConfig retrieves the persisted channel config from statedb
func retrievePersistedChannelConfig(ledger ledger.PeerLedger) (*common.Config, error) {
// RetrievePersistedChannelConfig retrieves the persisted channel config from statedb
func RetrievePersistedChannelConfig(ledger ledger.PeerLedger) (*common.Config, error) {
qe, err := ledger.NewQueryExecutor()
if err != nil {
return nil, err
Expand All @@ -267,7 +267,7 @@ func (p *Peer) createChannel(
legacyLifecycleValidation plugindispatcher.LifecycleResources,
newLifecycleValidation plugindispatcher.CollectionAndLifecycleResources,
) error {
chanConf, err := retrievePersistedChannelConfig(l)
chanConf, err := RetrievePersistedChannelConfig(l)
if err != nil {
return err
}
Expand Down
64 changes: 23 additions & 41 deletions discovery/support/config/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,53 @@ import (

var logger = flogging.MustGetLogger("discovery.config")

// CurrentConfigBlockGetter enables to fetch the last config block
type CurrentConfigBlockGetter interface {
// GetCurrConfigBlock returns the current config block for the given channel
GetCurrConfigBlock(channel string) *common.Block
// CurrentConfigGetter enables to fetch the last channel config
type CurrentConfigGetter interface {
// GetCurrConfig returns the current channel config for the given channel
GetCurrConfig(channel string) *common.Config
}

// CurrentConfigBlockGetterFunc enables to fetch the last config block
type CurrentConfigBlockGetterFunc func(channel string) *common.Block
// CurrentConfigGetterFunc enables to fetch the last channel config
type CurrentConfigGetterFunc func(channel string) *common.Config

// CurrentConfigBlockGetterFunc enables to fetch the last config block
func (f CurrentConfigBlockGetterFunc) GetCurrConfigBlock(channel string) *common.Block {
// CurrentConfigGetterFunc enables to fetch the last channel config
func (f CurrentConfigGetterFunc) GetCurrConfig(channel string) *common.Config {
return f(channel)
}

// DiscoverySupport implements support that is used for service discovery
// that is related to configuration
type DiscoverySupport struct {
CurrentConfigBlockGetter
CurrentConfigGetter
}

// NewDiscoverySupport creates a new DiscoverySupport
func NewDiscoverySupport(getLastConfigBlock CurrentConfigBlockGetter) *DiscoverySupport {
func NewDiscoverySupport(getLastConfig CurrentConfigGetter) *DiscoverySupport {
return &DiscoverySupport{
CurrentConfigBlockGetter: getLastConfigBlock,
CurrentConfigGetter: getLastConfig,
}
}

// Config returns the channel's configuration
func (s *DiscoverySupport) Config(channel string) (*discovery.ConfigResult, error) {
block := s.GetCurrConfigBlock(channel)
if block == nil {
return nil, errors.Errorf("could not get last config block for channel %s", channel)
}
if block.Data == nil || len(block.Data.Data) == 0 {
return nil, errors.Errorf("no transactions in block")
}
env := &common.Envelope{}
if err := proto.Unmarshal(block.Data.Data[0], env); err != nil {
return nil, errors.Wrap(err, "failed unmarshaling envelope")
}
pl := &common.Payload{}
if err := proto.Unmarshal(env.Payload, pl); err != nil {
return nil, errors.Wrap(err, "failed unmarshaling payload")
}
ce := &common.ConfigEnvelope{}
if err := proto.Unmarshal(pl.Data, ce); err != nil {
return nil, errors.Wrap(err, "failed unmarshaling config envelope")
config := s.GetCurrConfig(channel)
if config == nil {
return nil, errors.Errorf("could not get last config for channel %s", channel)
}

if err := ValidateConfigEnvelope(ce); err != nil {
return nil, errors.Wrap(err, "config envelope is invalid")
if err := ValidateConfig(config); err != nil {
return nil, errors.WithMessage(err, "config is invalid")
}

res := &discovery.ConfigResult{
Msps: make(map[string]*msp.FabricMSPConfig),
Orderers: make(map[string]*discovery.Endpoints),
}
ordererGrp := ce.Config.ChannelGroup.Groups[channelconfig.OrdererGroupKey].Groups
appGrp := ce.Config.ChannelGroup.Groups[channelconfig.ApplicationGroupKey].Groups
ordererGrp := config.ChannelGroup.Groups[channelconfig.OrdererGroupKey].Groups
appGrp := config.ChannelGroup.Groups[channelconfig.ApplicationGroupKey].Groups

var globalEndpoints []string
globalOrderers := ce.Config.ChannelGroup.Values[channelconfig.OrdererAddressesKey]
globalOrderers := config.ChannelGroup.Values[channelconfig.OrdererAddressesKey]
if globalOrderers != nil {
ordererAddressesConfig := &common.OrdererAddresses{}
if err := proto.Unmarshal(globalOrderers.Value, ordererAddressesConfig); err != nil {
Expand Down Expand Up @@ -241,14 +226,11 @@ func appendMSPConfigs(ordererGrp, appGrp map[string]*common.ConfigGroup, output
return nil
}

func ValidateConfigEnvelope(ce *common.ConfigEnvelope) error {
if ce.Config == nil {
return fmt.Errorf("field Config is nil")
}
if ce.Config.ChannelGroup == nil {
func ValidateConfig(c *common.Config) error {
if c.ChannelGroup == nil {
return fmt.Errorf("field Config.ChannelGroup is nil")
}
grps := ce.Config.ChannelGroup.Groups
grps := c.ChannelGroup.Groups
if grps == nil {
return fmt.Errorf("field Config.ChannelGroup.Groups is nil")
}
Expand All @@ -261,7 +243,7 @@ func ValidateConfigEnvelope(ce *common.ConfigEnvelope) error {
return fmt.Errorf("key Config.ChannelGroup.Groups[%s].Groups is nil", field)
}
}
if ce.Config.ChannelGroup.Values == nil {
if c.ChannelGroup.Values == nil {
return fmt.Errorf("field Config.ChannelGroup.Values is nil")
}
return nil
Expand Down
Loading

0 comments on commit 44f6ef5

Please sign in to comment.