Skip to content

Commit 555ddbc

Browse files
committed
[FAB-11832] Extract registrar initialization logic
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>
1 parent e7f1cff commit 555ddbc

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

orderer/common/multichannel/registrar.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
cb "github.com/hyperledger/fabric/protos/common"
2424
ab "github.com/hyperledger/fabric/protos/orderer"
2525
"github.com/hyperledger/fabric/protos/utils"
26-
2726
"github.com/pkg/errors"
2827
)
2928

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

121120
// NewRegistrar produces an instance of a *Registrar.
122-
func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]consensus.Consenter,
121+
func NewRegistrar(ledgerFactory blockledger.Factory,
123122
signer crypto.LocalSigner, callbacks ...func(bundle *channelconfig.Bundle)) *Registrar {
124123
r := &Registrar{
125124
chains: make(map[string]*ChainSupport),
126125
ledgerFactory: ledgerFactory,
127-
consenters: consenters,
128126
signer: signer,
129127
callbacks: callbacks,
130128
}
131129

132-
existingChains := ledgerFactory.ChainIDs()
130+
return r
131+
}
132+
133+
func (r *Registrar) Initialize(consenters map[string]consensus.Consenter) {
134+
r.consenters = consenters
135+
existingChains := r.ledgerFactory.ChainIDs()
133136
for _, chainID := range existingChains {
134-
rl, err := ledgerFactory.GetOrCreate(chainID)
137+
rl, err := r.ledgerFactory.GetOrCreate(chainID)
135138
if err != nil {
136139
logger.Panicf("Ledger factory reported chainID %s but could not retrieve it: %s", chainID, err)
137140
}
@@ -149,8 +152,8 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
149152
chain := newChainSupport(
150153
r,
151154
ledgerResources,
152-
consenters,
153-
signer)
155+
r.consenters,
156+
r.signer)
154157
r.templator = msgprocessor.NewDefaultTemplator(chain)
155158
chain.Processor = msgprocessor.NewSystemChannel(chain, r.templator, msgprocessor.CreateSystemChannelFilters(r, chain))
156159

@@ -176,8 +179,8 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
176179
chain := newChainSupport(
177180
r,
178181
ledgerResources,
179-
consenters,
180-
signer)
182+
r.consenters,
183+
r.signer)
181184
r.chains[chainID] = chain
182185
chain.start()
183186
}
@@ -187,8 +190,6 @@ func NewRegistrar(ledgerFactory blockledger.Factory, consenters map[string]conse
187190
if r.systemChannelID == "" {
188191
logger.Panicf("No system chain found. If bootstrapping, does your system channel contain a consortiums group definition?")
189192
}
190-
191-
return r
192193
}
193194

194195
// SystemChannelID returns the ChannelID for the system channel.

orderer/common/multichannel/registrar_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ func TestNoSystemChain(t *testing.T) {
109109
consenters := make(map[string]consensus.Consenter)
110110
consenters[conf.Orderer.OrdererType] = &mockConsenter{}
111111

112-
assert.Panics(t, func() { NewRegistrar(lf, consenters, mockCrypto()) }, "Should have panicked when starting without a system chain")
112+
assert.Panics(t, func() {
113+
NewRegistrar(lf, mockCrypto()).Initialize(consenters)
114+
}, "Should have panicked when starting without a system chain")
113115
}
114116

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

130-
assert.Panics(t, func() { NewRegistrar(lf, consenters, mockCrypto()) }, "Two system channels should have caused panic")
132+
assert.Panics(t, func() {
133+
NewRegistrar(lf, mockCrypto()).Initialize(consenters)
134+
}, "Two system channels should have caused panic")
131135
}
132136

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

140-
manager := NewRegistrar(lf, consenters, mockCrypto())
144+
manager := NewRegistrar(lf, mockCrypto())
145+
manager.Initialize(consenters)
141146

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

177-
manager := NewRegistrar(lf, consenters, mockCrypto())
182+
manager := NewRegistrar(lf, mockCrypto())
183+
manager.Initialize(consenters)
178184
orglessChannelConf := configtxgentest.Load(genesisconfig.SampleSingleMSPChannelProfile)
179185
orglessChannelConf.Application.Organizations = nil
180186
envConfigUpdate, err := encoder.MakeChannelCreationTransaction(newChainID, mockCrypto(), orglessChannelConf)
@@ -331,7 +337,8 @@ func TestResourcesCheck(t *testing.T) {
331337
func TestBroadcastChannelSupportRejection(t *testing.T) {
332338
ledgerFactory, _ := NewRAMLedgerAndFactory(10)
333339
mockConsenters := map[string]consensus.Consenter{conf.Orderer.OrdererType: &mockConsenter{}}
334-
registrar := NewRegistrar(ledgerFactory, mockConsenters, mockCrypto())
340+
registrar := NewRegistrar(ledgerFactory, mockCrypto())
341+
registrar.Initialize(mockConsenters)
335342
randomValue := 1
336343
configTx := makeConfigTx(genesisconfig.TestChainID, randomValue)
337344
_, _, _, err := registrar.BroadcastChannelSupport(configTx)

orderer/common/server/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ func initializeMultichannelRegistrar(conf *localconfig.TopLevel, signer crypto.L
255255
consenters["solo"] = solo.New()
256256
consenters["kafka"] = kafka.New(conf.Kafka)
257257

258-
return multichannel.NewRegistrar(lf, consenters, signer, callbacks...)
258+
registrar := multichannel.NewRegistrar(lf, signer, callbacks...)
259+
registrar.Initialize(consenters)
260+
return registrar
259261
}
260262

261263
func updateTrustedRoots(srv *comm.GRPCServer, rootCASupport *comm.CASupport,

0 commit comments

Comments
 (0)