-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
systemchain.go
189 lines (152 loc) · 5.38 KB
/
systemchain.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package multichain
import (
"fmt"
"github.com/hyperledger/fabric/common/config"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/orderer/common/filter"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/golang/protobuf/proto"
)
// Define some internal interfaces for easier mocking
type chainCreator interface {
NewChannelConfig(envConfigUpdate *cb.Envelope) (configtxapi.Manager, error)
newChain(configTx *cb.Envelope)
channelsCount() int
}
type limitedSupport interface {
SharedConfig() config.Orderer
}
type systemChainCommitter struct {
filter *systemChainFilter
configTx *cb.Envelope
}
func (scc *systemChainCommitter) Isolated() bool {
return true
}
func (scc *systemChainCommitter) Commit() {
scc.filter.cc.newChain(scc.configTx)
}
type systemChainFilter struct {
cc chainCreator
support limitedSupport
}
func newSystemChainFilter(ls limitedSupport, cc chainCreator) filter.Rule {
return &systemChainFilter{
support: ls,
cc: cc,
}
}
func (scf *systemChainFilter) Apply(env *cb.Envelope) (filter.Action, filter.Committer) {
msgData := &cb.Payload{}
err := proto.Unmarshal(env.Payload, msgData)
if err != nil {
return filter.Forward, nil
}
if msgData.Header == nil {
return filter.Forward, nil
}
chdr, err := utils.UnmarshalChannelHeader(msgData.Header.ChannelHeader)
if err != nil {
return filter.Forward, nil
}
if chdr.Type != int32(cb.HeaderType_ORDERER_TRANSACTION) {
return filter.Forward, nil
}
maxChannels := scf.support.SharedConfig().MaxChannelsCount()
if maxChannels > 0 {
// We check for strictly greater than to accommodate the system channel
if uint64(scf.cc.channelsCount()) > maxChannels {
logger.Warningf("Rejecting channel creation because the orderer has reached the maximum number of channels, %d", maxChannels)
return filter.Reject, nil
}
}
configTx := &cb.Envelope{}
err = proto.Unmarshal(msgData.Data, configTx)
if err != nil {
return filter.Reject, nil
}
err = scf.authorizeAndInspect(configTx)
if err != nil {
logger.Debugf("Rejecting channel creation because: %s", err)
return filter.Reject, nil
}
return filter.Accept, &systemChainCommitter{
filter: scf,
configTx: configTx,
}
}
func (scf *systemChainFilter) authorize(configEnvelope *cb.ConfigEnvelope) (configtxapi.Manager, error) {
if configEnvelope.LastUpdate == nil {
return nil, fmt.Errorf("Must include a config update")
}
configManager, err := scf.cc.NewChannelConfig(configEnvelope.LastUpdate)
if err != nil {
return nil, fmt.Errorf("Error constructing new channel config from update: %s", err)
}
newChannelConfigEnv, err := configManager.ProposeConfigUpdate(configEnvelope.LastUpdate)
if err != nil {
return nil, err
}
err = configManager.Apply(newChannelConfigEnv)
if err != nil {
return nil, err
}
return configManager, nil
}
func (scf *systemChainFilter) inspect(proposedManager, configManager configtxapi.Manager) error {
proposedEnv := proposedManager.ConfigEnvelope()
actualEnv := configManager.ConfigEnvelope()
// reflect.DeepEqual will not work here, because it considers nil and empty maps as unequal
if !proto.Equal(proposedEnv.Config, actualEnv.Config) {
return fmt.Errorf("config proposed by the channel creation request did not match the config received with the channel creation request")
}
return nil
}
func (scf *systemChainFilter) authorizeAndInspect(configTx *cb.Envelope) error {
payload := &cb.Payload{}
err := proto.Unmarshal(configTx.Payload, payload)
if err != nil {
return fmt.Errorf("Rejecting chain proposal: Error unmarshaling envelope payload: %s", err)
}
if payload.Header == nil {
return fmt.Errorf("Rejecting chain proposal: Not a config transaction")
}
chdr, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return fmt.Errorf("Rejecting chain proposal: Error unmarshaling channel header: %s", err)
}
if chdr.Type != int32(cb.HeaderType_CONFIG) {
return fmt.Errorf("Rejecting chain proposal: Not a config transaction")
}
configEnvelope := &cb.ConfigEnvelope{}
err = proto.Unmarshal(payload.Data, configEnvelope)
if err != nil {
return fmt.Errorf("Rejecting chain proposal: Error unmarshalling config envelope from payload: %s", err)
}
// Make sure that the config was signed by the appropriate authorized entities
proposedManager, err := scf.authorize(configEnvelope)
if err != nil {
return err
}
initializer := configtx.NewInitializer()
configManager, err := configtx.NewManagerImpl(configTx, initializer, nil)
if err != nil {
return fmt.Errorf("Failed to create config manager and handlers: %s", err)
}
// Make sure that the config does not modify any of the orderer
return scf.inspect(proposedManager, configManager)
}