/
chainsupport.go
312 lines (266 loc) · 10.2 KB
/
chainsupport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package multichannel
import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/crypto"
"github.com/hyperledger/fabric/common/ledger/blockledger"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/orderer/common/blockcutter"
"github.com/hyperledger/fabric/orderer/common/msgprocessor"
"github.com/hyperledger/fabric/orderer/consensus"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)
// ChainSupport holds the resources for a particular channel.
type ChainSupport struct {
*ledgerResources
msgprocessor.Processor
*BlockWriter
consensus.Chain
cutter blockcutter.Receiver
crypto.LocalSigner
}
func newChainSupport(
registrar *Registrar,
ledgerResources *ledgerResources,
consenters map[string]consensus.Consenter,
signer crypto.LocalSigner,
blockcutterMetrics *blockcutter.Metrics,
) *ChainSupport {
// Read in the last block and metadata for the channel
lastBlock := blockledger.GetBlock(ledgerResources, ledgerResources.Height()-1)
metadata, err := utils.GetMetadataFromBlock(lastBlock, cb.BlockMetadataIndex_ORDERER)
// Assuming a block created with cb.NewBlock(), this should not
// error even if the orderer metadata is an empty byte slice
if err != nil {
logger.Fatalf("[channel: %s] Error extracting orderer metadata: %s", ledgerResources.ConfigtxValidator().ChainID(), err)
}
// Construct limited support needed as a parameter for additional support
cs := &ChainSupport{
ledgerResources: ledgerResources,
LocalSigner: signer,
cutter: blockcutter.NewReceiverImpl(
ledgerResources.ConfigtxValidator().ChainID(),
ledgerResources,
blockcutterMetrics,
),
}
// Set up the msgprocessor
cs.Processor = msgprocessor.NewStandardChannel(cs, msgprocessor.CreateStandardChannelFilters(cs))
// Set up the block writer
cs.BlockWriter = newBlockWriter(lastBlock, registrar, cs)
// Set up the consenter
consenterType := ledgerResources.SharedConfig().ConsensusType()
consenter, ok := consenters[consenterType]
if !ok {
logger.Panicf("Error retrieving consenter of type: %s", consenterType)
}
cs.Chain, err = consenter.HandleChain(cs, metadata)
if err != nil {
logger.Panicf("[channel: %s] Error creating consenter: %s", cs.ChainID(), err)
}
logger.Debugf("[channel: %s] Done creating channel support resources", cs.ChainID())
return cs
}
// DetectConsensusMigration identifies restart after consensus-type migration.
// Restart after migration is detected by:
// 1. The last block and the block before it carry a config-tx
// 2. In both blocks we have ConsensusType.State == MAINTENANCE
// 3. The ConsensusType.Type is different in these two blocks
// 4. The last block metadata is empty
func (cs *ChainSupport) DetectConsensusMigration() bool {
if !cs.ledgerResources.SharedConfig().Capabilities().ConsensusTypeMigration() {
logger.Debugf("[channel: %s] Orderer capability ConsensusTypeMigration is disabled", cs.ChainID())
return false
}
if cs.Height() <= 1 {
return false
}
lastConfigIndex, err := utils.GetLastConfigIndexFromBlock(cs.lastBlock)
if err != nil {
logger.Panicf("[channel: %s] Chain did not have appropriately encoded last config in its latest block: %s",
cs.ChainID(), err)
}
logger.Debugf("[channel: %s] lastBlockNumber=%d, lastConfigIndex=%d",
cs.support.ChainID(), cs.lastBlock.Header.Number, lastConfigIndex)
if lastConfigIndex != cs.lastBlock.Header.Number {
return false
}
currentState := cs.support.SharedConfig().ConsensusState()
logger.Debugf("[channel: %s] last block ConsensusState=%s", cs.ChainID(), currentState)
if currentState != orderer.ConsensusType_STATE_MAINTENANCE {
return false
}
metadata, err := utils.GetMetadataFromBlock(cs.lastBlock, cb.BlockMetadataIndex_ORDERER)
if err != nil {
logger.Panicf("[channel: %s] Error extracting orderer metadata: %s", cs.ChainID(), err)
}
metaLen := len(metadata.Value)
logger.Debugf("[channel: %s] metadata.Value length=%d", cs.ChainID(), metaLen)
if metaLen > 0 {
return false
}
prevBlock := blockledger.GetBlock(cs.Reader(), cs.lastBlock.Header.Number-1)
prevConfigIndex, err := utils.GetLastConfigIndexFromBlock(prevBlock)
if err != nil {
logger.Panicf("Chain did not have appropriately encoded last config in block %d: %s",
prevBlock.Header.Number, err)
}
if prevConfigIndex != prevBlock.Header.Number {
return false
}
prevPayload, prevChanHdr := cs.extractPayloadHeaderOrPanic(prevBlock)
switch cb.HeaderType(prevChanHdr.Type) {
case cb.HeaderType_ORDERER_TRANSACTION:
return false
case cb.HeaderType_CONFIG:
return cs.isConsensusTypeChange(prevPayload, prevChanHdr.ChannelId, prevBlock.Header.Number)
default:
logger.Panicf("[channel: %s] config block with unknown header type in block %d: %v",
cs.ChainID(), prevBlock.Header.Number, prevChanHdr.Type)
return false
}
}
func (cs *ChainSupport) extractPayloadHeaderOrPanic(prevBlock *cb.Block) (*cb.Payload, *cb.ChannelHeader) {
configTx, err := utils.ExtractEnvelope(prevBlock, 0)
if err != nil {
logger.Panicf("[channel: %s] Error extracting configtx from block %d: %s",
cs.ChainID(), prevBlock.Header.Number, err)
}
payload, err := utils.UnmarshalPayload(configTx.Payload)
if err != nil {
logger.Panicf("[channel: %s] configtx payload is invalid in block %d: %s",
cs.ChainID(), prevBlock.Header.Number, err)
}
if payload.Header == nil {
logger.Panicf("[channel: %s] configtx payload header is missing in block %d",
cs.ChainID(), prevBlock.Header.Number)
}
chdr, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
logger.Panicf("[channel: %s] invalid channel header in block %d: %s",
cs.ChainID(), prevBlock.Header.Number, err)
}
return payload, chdr
}
func (cs *ChainSupport) isConsensusTypeChange(payload *cb.Payload, channelId string, headerNumber uint64) bool {
configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data)
if err != nil {
logger.Panicf("[channel: %s] Error extracting config envelope in block %d: %s",
cs.ChainID(), headerNumber, err)
}
bundle, err := cs.CreateBundle(channelId, configEnvelope.Config)
if err != nil {
logger.Panicf("[channel: %s] Error converting config to a bundle in block %d: %s",
cs.ChainID(), headerNumber, err)
}
oc, ok := bundle.OrdererConfig()
if !ok {
logger.Panicf("[channel: %s] OrdererConfig missing from bundle in block %d",
cs.ChainID(), headerNumber)
}
prevState := oc.ConsensusState()
logger.Debugf("[channel: %s] previous block ConsensusState=%s", cs.ChainID(), prevState)
if prevState != orderer.ConsensusType_STATE_MAINTENANCE {
return false
}
currentType := cs.SharedConfig().ConsensusType()
prevType := oc.ConsensusType()
logger.Debugf("[channel: %s] block ConsensusType: previous=%s, current=%s", cs.ChainID(), prevType, currentType)
if currentType == prevType {
return false
}
logger.Infof("[channel: %s] Consensus-type migration detected, ConsensusState=%s, ConsensusType changed from %s to %s",
cs.ChainID(), cs.support.SharedConfig().ConsensusState(), prevType, currentType)
return true
}
// Block returns a block with the following number,
// or nil if such a block doesn't exist.
func (cs *ChainSupport) Block(number uint64) *cb.Block {
if cs.Height() <= number {
return nil
}
return blockledger.GetBlock(cs.Reader(), number)
}
func (cs *ChainSupport) Reader() blockledger.Reader {
return cs
}
// Signer returns the crypto.Localsigner for this channel.
func (cs *ChainSupport) Signer() crypto.LocalSigner {
return cs
}
func (cs *ChainSupport) start() {
cs.Chain.Start()
}
// BlockCutter returns the blockcutter.Receiver instance for this channel.
func (cs *ChainSupport) BlockCutter() blockcutter.Receiver {
return cs.cutter
}
// Validate passes through to the underlying configtx.Validator
func (cs *ChainSupport) Validate(configEnv *cb.ConfigEnvelope) error {
return cs.ConfigtxValidator().Validate(configEnv)
}
// ProposeConfigUpdate passes through to the underlying configtx.Validator
func (cs *ChainSupport) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
env, err := cs.ConfigtxValidator().ProposeConfigUpdate(configtx)
if err != nil {
return nil, err
}
bundle, err := cs.CreateBundle(cs.ChainID(), env.Config)
if err != nil {
return nil, err
}
if err = checkResources(bundle); err != nil {
return nil, errors.Wrap(err, "config update is not compatible")
}
return env, cs.ValidateNew(bundle)
}
// ChainID passes through to the underlying configtx.Validator
func (cs *ChainSupport) ChainID() string {
return cs.ConfigtxValidator().ChainID()
}
// ConfigProto passes through to the underlying configtx.Validator
func (cs *ChainSupport) ConfigProto() *cb.Config {
return cs.ConfigtxValidator().ConfigProto()
}
// Sequence passes through to the underlying configtx.Validator
func (cs *ChainSupport) Sequence() uint64 {
return cs.ConfigtxValidator().Sequence()
}
// Append appends a new block to the ledger in its raw form,
// unlike WriteBlock that also mutates its metadata.
func (cs *ChainSupport) Append(block *cb.Block) error {
return cs.ledgerResources.ReadWriter.Append(block)
}
// VerifyBlockSignature verifies a signature of a block.
// It has an optional argument of a configuration envelope
// which would make the block verification to use validation rules
// based on the given configuration in the ConfigEnvelope.
// If the config envelope passed is nil, then the validation rules used
// are the ones that were applied at commit of previous blocks.
func (cs *ChainSupport) VerifyBlockSignature(sd []*cb.SignedData, envelope *cb.ConfigEnvelope) error {
policyMgr := cs.PolicyManager()
// If the envelope passed isn't nil, we should use a different policy manager.
if envelope != nil {
bundle, err := channelconfig.NewBundle(cs.ChainID(), envelope.Config)
if err != nil {
return err
}
policyMgr = bundle.PolicyManager()
}
policy, exists := policyMgr.GetPolicy(policies.BlockValidation)
if !exists {
return errors.Errorf("policy %s wasn't found", policies.BlockValidation)
}
err := policy.Evaluate(sd)
if err != nil {
return errors.Wrap(err, "block verification failed")
}
return nil
}