Skip to content

Commit

Permalink
[FAB-11832] Extract registrar initialization logic
Browse files Browse the repository at this point in the history
This change sets simply extracts the registrar's initialization
logic into a separate function, in order to be able to
populate the consenter mapping passed into the registrar's
constructor after the registrar's creation.

Change-Id: Id722083a8b570cb889c1617dfed799dd4712328b
Signed-off-by: yacovm <yacovm@il.ibm.com>
  • Loading branch information
yacovm committed Sep 17, 2018
1 parent e7f1cff commit 555ddbc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
23 changes: 12 additions & 11 deletions orderer/common/multichannel/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -119,19 +118,23 @@ func getConfigTx(reader blockledger.Reader) *cb.Envelope {
}

// NewRegistrar produces an instance of a *Registrar.
func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]consensus.Consenter,
func NewRegistrar(ledgerFactory blockledger.Factory,
signer crypto.LocalSigner, callbacks ...func(bundle *channelconfig.Bundle)) *Registrar {
r := &Registrar{
chains: make(map[string]*ChainSupport),
ledgerFactory: ledgerFactory,
consenters: consenters,
signer: signer,
callbacks: callbacks,
}

existingChains := ledgerFactory.ChainIDs()
return r
}

func (r *Registrar) Initialize(consenters map[string]consensus.Consenter) {
r.consenters = consenters
existingChains := r.ledgerFactory.ChainIDs()
for _, chainID := range existingChains {
rl, err := ledgerFactory.GetOrCreate(chainID)
rl, err := r.ledgerFactory.GetOrCreate(chainID)
if err != nil {
logger.Panicf("Ledger factory reported chainID %s but could not retrieve it: %s", chainID, err)
}
Expand All @@ -149,8 +152,8 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
chain := newChainSupport(
r,
ledgerResources,
consenters,
signer)
r.consenters,
r.signer)
r.templator = msgprocessor.NewDefaultTemplator(chain)
chain.Processor = msgprocessor.NewSystemChannel(chain, r.templator, msgprocessor.CreateSystemChannelFilters(r, chain))

Expand All @@ -176,8 +179,8 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
chain := newChainSupport(
r,
ledgerResources,
consenters,
signer)
r.consenters,
r.signer)
r.chains[chainID] = chain
chain.start()
}
Expand All @@ -187,8 +190,6 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
if r.systemChannelID == "" {
logger.Panicf("No system chain found. If bootstrapping, does your system channel contain a consortiums group definition?")
}

return r
}

// SystemChannelID returns the ChannelID for the system channel.
Expand Down
17 changes: 12 additions & 5 deletions orderer/common/multichannel/registrar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func TestNoSystemChain(t *testing.T) {
consenters := make(map[string]consensus.Consenter)
consenters[conf.Orderer.OrdererType] = &mockConsenter{}

assert.Panics(t, func() { NewRegistrar(lf, consenters, mockCrypto()) }, "Should have panicked when starting without a system chain")
assert.Panics(t, func() {
NewRegistrar(lf, mockCrypto()).Initialize(consenters)
}, "Should have panicked when starting without a system chain")
}

// This test checks to make sure that the orderer refuses to come up if there are multiple system channels
Expand All @@ -127,7 +129,9 @@ func TestMultiSystemChannel(t *testing.T) {
consenters := make(map[string]consensus.Consenter)
consenters[conf.Orderer.OrdererType] = &mockConsenter{}

assert.Panics(t, func() { NewRegistrar(lf, consenters, mockCrypto()) }, "Two system channels should have caused panic")
assert.Panics(t, func() {
NewRegistrar(lf, mockCrypto()).Initialize(consenters)
}, "Two system channels should have caused panic")
}

// This test essentially brings the entire system up and is ultimately what main.go will replicate
Expand All @@ -137,7 +141,8 @@ func TestManagerImpl(t *testing.T) {
consenters := make(map[string]consensus.Consenter)
consenters[conf.Orderer.OrdererType] = &mockConsenter{}

manager := NewRegistrar(lf, consenters, mockCrypto())
manager := NewRegistrar(lf, mockCrypto())
manager.Initialize(consenters)

_, ok := manager.GetChain("Fake")
assert.False(t, ok, "Should not have found a chain that was not created")
Expand Down Expand Up @@ -174,7 +179,8 @@ func TestNewChain(t *testing.T) {
consenters := make(map[string]consensus.Consenter)
consenters[conf.Orderer.OrdererType] = &mockConsenter{}

manager := NewRegistrar(lf, consenters, mockCrypto())
manager := NewRegistrar(lf, mockCrypto())
manager.Initialize(consenters)
orglessChannelConf := configtxgentest.Load(genesisconfig.SampleSingleMSPChannelProfile)
orglessChannelConf.Application.Organizations = nil
envConfigUpdate, err := encoder.MakeChannelCreationTransaction(newChainID, mockCrypto(), orglessChannelConf)
Expand Down Expand Up @@ -331,7 +337,8 @@ func TestResourcesCheck(t *testing.T) {
func TestBroadcastChannelSupportRejection(t *testing.T) {
ledgerFactory, _ := NewRAMLedgerAndFactory(10)
mockConsenters := map[string]consensus.Consenter{conf.Orderer.OrdererType: &mockConsenter{}}
registrar := NewRegistrar(ledgerFactory, mockConsenters, mockCrypto())
registrar := NewRegistrar(ledgerFactory, mockCrypto())
registrar.Initialize(mockConsenters)
randomValue := 1
configTx := makeConfigTx(genesisconfig.TestChainID, randomValue)
_, _, _, err := registrar.BroadcastChannelSupport(configTx)
Expand Down
4 changes: 3 additions & 1 deletion orderer/common/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ func initializeMultichannelRegistrar(conf *localconfig.TopLevel, signer crypto.L
consenters["solo"] = solo.New()
consenters["kafka"] = kafka.New(conf.Kafka)

return multichannel.NewRegistrar(lf, consenters, signer, callbacks...)
registrar := multichannel.NewRegistrar(lf, signer, callbacks...)
registrar.Initialize(consenters)
return registrar
}

func updateTrustedRoots(srv *comm.GRPCServer, rootCASupport *comm.CASupport,
Expand Down

0 comments on commit 555ddbc

Please sign in to comment.