Skip to content

Commit 2cdafd0

Browse files
author
Jason Yellick
committed
[FAB-1777] Refactor orderer multichain package
https://jira.hyperledger.org/browse/FAB-1777 The orderer multichain package has become a little unwieldy as more and more configuration based handlers have been added to it. This changeset consolidates these many parameters into embedded structures to alleviate this problem in preparation for adding the chain config handler. Change-Id: I9b999d9e52dfa38d3aa42a9379892530357d4b14 Signed-off-by: Jason Yellick <[email protected]>
1 parent b3f03b1 commit 2cdafd0

File tree

4 files changed

+70
-84
lines changed

4 files changed

+70
-84
lines changed

orderer/multichain/chainsupport.go

+24-52
Original file line numberDiff line numberDiff line change
@@ -82,80 +82,68 @@ type ChainSupport interface {
8282

8383
broadcast.Support
8484
ConsenterSupport
85-
86-
// ConfigTxManager returns the corresponding configtx.Manager for this chain
87-
ConfigTxManager() configtx.Manager
8885
}
8986

9087
type chainSupport struct {
91-
chain Chain
92-
cutter blockcutter.Receiver
93-
configManager configtx.Manager
94-
policyManager policies.Manager
95-
sharedConfigManager sharedconfig.Manager
96-
ledger ordererledger.ReadWriter
97-
filters *filter.RuleSet
98-
signer crypto.LocalSigner
99-
lastConfiguration uint64
100-
lastConfigSeq uint64
88+
*ledgerResources
89+
chain Chain
90+
cutter blockcutter.Receiver
91+
filters *filter.RuleSet
92+
signer crypto.LocalSigner
93+
lastConfiguration uint64
94+
lastConfigSeq uint64
10195
}
10296

10397
func newChainSupport(
10498
filters *filter.RuleSet,
105-
configManager configtx.Manager,
106-
policyManager policies.Manager,
107-
backing ordererledger.ReadWriter,
108-
sharedConfigManager sharedconfig.Manager,
99+
ledgerResources *ledgerResources,
109100
consenters map[string]Consenter,
110101
signer crypto.LocalSigner,
111102
) *chainSupport {
112103

113-
cutter := blockcutter.NewReceiverImpl(sharedConfigManager, filters)
114-
consenterType := sharedConfigManager.ConsensusType()
104+
cutter := blockcutter.NewReceiverImpl(ledgerResources.SharedConfig(), filters)
105+
consenterType := ledgerResources.SharedConfig().ConsensusType()
115106
consenter, ok := consenters[consenterType]
116107
if !ok {
117108
logger.Fatalf("Error retrieving consenter of type: %s", consenterType)
118109
}
119110

120111
cs := &chainSupport{
121-
configManager: configManager,
122-
policyManager: policyManager,
123-
sharedConfigManager: sharedConfigManager,
124-
cutter: cutter,
125-
filters: filters,
126-
ledger: backing,
127-
signer: signer,
112+
ledgerResources: ledgerResources,
113+
cutter: cutter,
114+
filters: filters,
115+
signer: signer,
128116
}
129117

130118
var err error
131119
cs.chain, err = consenter.HandleChain(cs)
132120
if err != nil {
133-
logger.Fatalf("Error creating consenter for chain %x: %s", configManager.ChainID(), err)
121+
logger.Fatalf("Error creating consenter for chain %x: %s", ledgerResources.ChainID(), err)
134122
}
135123

136124
return cs
137125
}
138126

139127
// createStandardFilters creates the set of filters for a normal (non-system) chain
140-
func createStandardFilters(configManager configtx.Manager, policyManager policies.Manager, sharedConfig sharedconfig.Manager) *filter.RuleSet {
128+
func createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSet {
141129
return filter.NewRuleSet([]filter.Rule{
142130
filter.EmptyRejectRule,
143-
sizefilter.MaxBytesRule(sharedConfig.BatchSize().AbsoluteMaxBytes),
144-
sigfilter.New(sharedConfig.IngressPolicyNames, policyManager),
145-
configtx.NewFilter(configManager),
131+
sizefilter.MaxBytesRule(ledgerResources.SharedConfig().BatchSize().AbsoluteMaxBytes),
132+
sigfilter.New(ledgerResources.SharedConfig().IngressPolicyNames, ledgerResources.PolicyManager()),
133+
configtx.NewFilter(ledgerResources),
146134
filter.AcceptRule,
147135
})
148136

149137
}
150138

151139
// createSystemChainFilters creates the set of filters for the ordering system chain
152-
func createSystemChainFilters(ml *multiLedger, configManager configtx.Manager, policyManager policies.Manager, sharedConfig sharedconfig.Manager) *filter.RuleSet {
140+
func createSystemChainFilters(ml *multiLedger, ledgerResources *ledgerResources) *filter.RuleSet {
153141
return filter.NewRuleSet([]filter.Rule{
154142
filter.EmptyRejectRule,
155-
sizefilter.MaxBytesRule(sharedConfig.BatchSize().AbsoluteMaxBytes),
156-
sigfilter.New(sharedConfig.IngressPolicyNames, policyManager),
143+
sizefilter.MaxBytesRule(ledgerResources.SharedConfig().BatchSize().AbsoluteMaxBytes),
144+
sigfilter.New(ledgerResources.SharedConfig().IngressPolicyNames, ledgerResources.PolicyManager()),
157145
newSystemChainFilter(ml),
158-
configtx.NewFilter(configManager),
146+
configtx.NewFilter(ledgerResources),
159147
filter.AcceptRule,
160148
})
161149
}
@@ -172,22 +160,6 @@ func (cs *chainSupport) Sign(message []byte) ([]byte, error) {
172160
return cs.signer.Sign(message)
173161
}
174162

175-
func (cs *chainSupport) SharedConfig() sharedconfig.Manager {
176-
return cs.sharedConfigManager
177-
}
178-
179-
func (cs *chainSupport) ConfigTxManager() configtx.Manager {
180-
return cs.configManager
181-
}
182-
183-
func (cs *chainSupport) ChainID() string {
184-
return cs.configManager.ChainID()
185-
}
186-
187-
func (cs *chainSupport) PolicyManager() policies.Manager {
188-
return cs.policyManager
189-
}
190-
191163
func (cs *chainSupport) Filters() *filter.RuleSet {
192164
return cs.filters
193165
}
@@ -231,7 +203,7 @@ func (cs *chainSupport) addBlockSignature(block *cb.Block) {
231203
}
232204

233205
func (cs *chainSupport) addLastConfigSignature(block *cb.Block) {
234-
configSeq := cs.configManager.Sequence()
206+
configSeq := cs.Sequence()
235207
if configSeq > cs.lastConfigSeq {
236208
cs.lastConfiguration = block.Header.Number
237209
cs.lastConfigSeq = configSeq

orderer/multichain/chainsupport_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (mc *mockCommitter) Commit() {
6464
func TestCommitConfig(t *testing.T) {
6565
ml := &mockLedgerReadWriter{}
6666
cm := &mockconfigtx.Manager{}
67-
cs := &chainSupport{ledger: ml, configManager: cm, signer: &xxxCryptoHelper{}}
67+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &xxxCryptoHelper{}}
6868
txs := []*cb.Envelope{makeNormalTx("foo", 0), makeNormalTx("bar", 1)}
6969
committers := []filter.Committer{&mockCommitter{}, &mockCommitter{}}
7070
block := cs.CreateNextBlock(txs)
@@ -89,7 +89,7 @@ func TestCommitConfig(t *testing.T) {
8989
func TestWriteBlockSignatures(t *testing.T) {
9090
ml := &mockLedgerReadWriter{}
9191
cm := &mockconfigtx.Manager{}
92-
cs := &chainSupport{ledger: ml, configManager: cm, signer: &xxxCryptoHelper{}}
92+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &xxxCryptoHelper{}}
9393

9494
blockMetadata := func(block *cb.Block) *cb.Metadata {
9595
metadata, err := utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_SIGNATURES)
@@ -107,7 +107,7 @@ func TestWriteBlockSignatures(t *testing.T) {
107107
func TestWriteLastConfiguration(t *testing.T) {
108108
ml := &mockLedgerReadWriter{}
109109
cm := &mockconfigtx.Manager{}
110-
cs := &chainSupport{ledger: ml, configManager: cm, signer: &xxxCryptoHelper{}}
110+
cs := &chainSupport{ledgerResources: &ledgerResources{configResources: &configResources{Manager: cm}, ledger: ml}, signer: &xxxCryptoHelper{}}
111111

112112
lastConfig := func(block *cb.Block) uint64 {
113113
index, err := utils.GetLastConfigurationIndexFromBlock(block)

orderer/multichain/manager.go

+40-26
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121

2222
"github.com/hyperledger/fabric/common/configtx"
23-
"github.com/hyperledger/fabric/common/policies"
2423
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
2524
ordererledger "github.com/hyperledger/fabric/orderer/ledger"
2625
cb "github.com/hyperledger/fabric/protos/common"
@@ -60,6 +59,20 @@ type Manager interface {
6059
ProposeChain(env *cb.Envelope) cb.Status
6160
}
6261

62+
type configResources struct {
63+
configtx.Manager
64+
sharedConfig sharedconfig.Manager
65+
}
66+
67+
func (cr *configResources) SharedConfig() sharedconfig.Manager {
68+
return cr.sharedConfig
69+
}
70+
71+
type ledgerResources struct {
72+
*configResources
73+
ledger ordererledger.ReadWriter
74+
}
75+
6376
type multiLedger struct {
6477
chains map[string]*chainSupport
6578
consenters map[string]Consenter
@@ -101,19 +114,16 @@ func NewManagerImpl(ledgerFactory ordererledger.Factory, consenters map[string]C
101114
if configTx == nil {
102115
logger.Fatalf("Could not find configuration transaction for chain %s", chainID)
103116
}
104-
configManager, policyManager, backingLedger, sharedConfigManager := ml.newResources(configTx)
105-
chainID := configManager.ChainID()
117+
ledgerResources := ml.newLedgerResources(configTx)
118+
chainID := ledgerResources.ChainID()
106119

107-
if sharedConfigManager.ChainCreationPolicyNames() != nil {
120+
if ledgerResources.SharedConfig().ChainCreationPolicyNames() != nil {
108121
if ml.sysChain != nil {
109122
logger.Fatalf("There appear to be two system chains %s and %s", ml.sysChain.support.ChainID(), chainID)
110123
}
111124
logger.Debugf("Starting with system chain: %x", chainID)
112-
chain := newChainSupport(createSystemChainFilters(ml, configManager, policyManager, sharedConfigManager),
113-
configManager,
114-
policyManager,
115-
backingLedger,
116-
sharedConfigManager,
125+
chain := newChainSupport(createSystemChainFilters(ml, ledgerResources),
126+
ledgerResources,
117127
consenters,
118128
signer)
119129
ml.chains[string(chainID)] = chain
@@ -122,11 +132,8 @@ func NewManagerImpl(ledgerFactory ordererledger.Factory, consenters map[string]C
122132
defer chain.start()
123133
} else {
124134
logger.Debugf("Starting chain: %x", chainID)
125-
chain := newChainSupport(createStandardFilters(configManager, policyManager, sharedConfigManager),
126-
configManager,
127-
policyManager,
128-
backingLedger,
129-
sharedConfigManager,
135+
chain := newChainSupport(createStandardFilters(ledgerResources),
136+
ledgerResources,
130137
consenters,
131138
signer)
132139
ml.chains[string(chainID)] = chain
@@ -151,19 +158,23 @@ func (ml *multiLedger) GetChain(chainID string) (ChainSupport, bool) {
151158
return cs, ok
152159
}
153160

154-
func newConfigTxManagerAndHandlers(configEnvelope *cb.ConfigurationEnvelope) (configtx.Manager, policies.Manager, sharedconfig.Manager, error) {
155-
initializer := configtx.NewInitializer()
161+
func newConfigResources(configEnvelope *cb.ConfigurationEnvelope) (*configResources, error) {
156162
sharedConfigManager := sharedconfig.NewManagerImpl()
163+
initializer := configtx.NewInitializer()
157164
initializer.Handlers()[cb.ConfigurationItem_Orderer] = sharedConfigManager
165+
158166
configManager, err := configtx.NewManagerImpl(configEnvelope, initializer)
159167
if err != nil {
160-
return nil, nil, nil, fmt.Errorf("Error unpacking configuration transaction: %s", err)
168+
return nil, fmt.Errorf("Error unpacking configuration transaction: %s", err)
161169
}
162170

163-
return configManager, initializer.PolicyManager(), sharedConfigManager, nil
171+
return &configResources{
172+
Manager: configManager,
173+
sharedConfig: sharedConfigManager,
174+
}, nil
164175
}
165176

166-
func (ml *multiLedger) newResources(configTx *cb.Envelope) (configtx.Manager, policies.Manager, ordererledger.ReadWriter, sharedconfig.Manager) {
177+
func (ml *multiLedger) newLedgerResources(configTx *cb.Envelope) *ledgerResources {
167178
payload := &cb.Payload{}
168179
err := proto.Unmarshal(configTx.Payload, payload)
169180
if err != nil {
@@ -176,38 +187,41 @@ func (ml *multiLedger) newResources(configTx *cb.Envelope) (configtx.Manager, po
176187
logger.Fatalf("Error unmarshaling a config transaction to config envelope: %s", err)
177188
}
178189

179-
configManager, policyManager, sharedConfigManager, err := newConfigTxManagerAndHandlers(configEnvelope)
190+
configResources, err := newConfigResources(configEnvelope)
180191

181192
if err != nil {
182193
logger.Fatalf("Error creating configtx manager and handlers: %s", err)
183194
}
184195

185-
chainID := configManager.ChainID()
196+
chainID := configResources.ChainID()
186197

187198
ledger, err := ml.ledgerFactory.GetOrCreate(chainID)
188199
if err != nil {
189200
logger.Fatalf("Error getting ledger for %s", chainID)
190201
}
191202

192-
return configManager, policyManager, ledger, sharedConfigManager
203+
return &ledgerResources{
204+
configResources: configResources,
205+
ledger: ledger,
206+
}
193207
}
194208

195209
func (ml *multiLedger) systemChain() *systemChain {
196210
return ml.sysChain
197211
}
198212

199213
func (ml *multiLedger) newChain(configtx *cb.Envelope) {
200-
configManager, policyManager, backingLedger, sharedConfig := ml.newResources(configtx)
201-
backingLedger.Append(ordererledger.CreateNextBlock(backingLedger, []*cb.Envelope{configtx}))
214+
ledgerResources := ml.newLedgerResources(configtx)
215+
ledgerResources.ledger.Append(ordererledger.CreateNextBlock(ledgerResources.ledger, []*cb.Envelope{configtx}))
202216

203217
// Copy the map to allow concurrent reads from broadcast/deliver while the new chainSupport is
204218
newChains := make(map[string]*chainSupport)
205219
for key, value := range ml.chains {
206220
newChains[key] = value
207221
}
208222

209-
cs := newChainSupport(createStandardFilters(configManager, policyManager, sharedConfig), configManager, policyManager, backingLedger, sharedConfig, ml.consenters, ml.signer)
210-
chainID := configManager.ChainID()
223+
cs := newChainSupport(createStandardFilters(ledgerResources), ledgerResources, ml.consenters, ml.signer)
224+
chainID := ledgerResources.ChainID()
211225

212226
logger.Debugf("Created and starting new chain %s", chainID)
213227

orderer/multichain/systemchain.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
203203
return cb.Status_SUCCESS
204204
}
205205

206-
func (sc *systemChain) inspect(configTxManager configtx.Manager, policyManager policies.Manager, sharedConfigManager sharedconfig.Manager) cb.Status {
206+
func (sc *systemChain) inspect(configResources *configResources) cb.Status {
207207
// XXX decide what it is that we will require to be the same in the new configuration, and what will be allowed to be different
208208
// Are all keys allowed? etc.
209209

@@ -241,12 +241,12 @@ func (sc *systemChain) authorizeAndInspect(configTx *cb.Envelope) cb.Status {
241241
return status
242242
}
243243

244-
configTxManager, policyManager, sharedConfigManager, err := newConfigTxManagerAndHandlers(configEnvelope)
244+
configResources, err := newConfigResources(configEnvelope)
245245
if err != nil {
246246
logger.Debugf("Failed to create config manager and handlers: %s", err)
247247
return cb.Status_BAD_REQUEST
248248
}
249249

250250
// Make sure that the configuration does not modify any of the orderer
251-
return sc.inspect(configTxManager, policyManager, sharedConfigManager)
251+
return sc.inspect(configResources)
252252
}

0 commit comments

Comments
 (0)