github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/configtxfilter/filter.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package configtxfilter 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/common/configtx" 23 "github.com/hyperledger/fabric/common/configtx/api" 24 "github.com/hyperledger/fabric/orderer/common/filter" 25 cb "github.com/hyperledger/fabric/protos/common" 26 "github.com/hyperledger/fabric/protos/utils" 27 ) 28 29 type configFilter struct { 30 configManager api.Manager 31 } 32 33 // New creates a new configfilter Rule based on the given Manager 34 func NewFilter(manager api.Manager) filter.Rule { 35 return &configFilter{ 36 configManager: manager, 37 } 38 } 39 40 type configCommitter struct { 41 manager api.Manager 42 configEnvelope *cb.ConfigEnvelope 43 } 44 45 func (cc *configCommitter) Commit() { 46 err := cc.manager.Apply(cc.configEnvelope) 47 if err != nil { 48 panic(fmt.Errorf("Could not apply config transaction which should have already been validated: %s", err)) 49 } 50 } 51 52 func (cc *configCommitter) Isolated() bool { 53 return true 54 } 55 56 // Apply applies the rule to the given Envelope, replying with the Action to take for the message 57 func (cf *configFilter) Apply(message *cb.Envelope) (filter.Action, filter.Committer) { 58 msgData, err := utils.UnmarshalPayload(message.Payload) 59 if err != nil { 60 return filter.Forward, nil 61 } 62 63 if msgData.Header == nil /* || msgData.Header.ChannelHeader == nil */ { 64 return filter.Forward, nil 65 } 66 67 chdr, err := utils.UnmarshalChannelHeader(msgData.Header.ChannelHeader) 68 if err != nil { 69 return filter.Forward, nil 70 } 71 72 if chdr.Type != int32(cb.HeaderType_CONFIG) { 73 return filter.Forward, nil 74 } 75 76 configEnvelope, err := configtx.UnmarshalConfigEnvelope(msgData.Data) 77 if err != nil { 78 return filter.Reject, nil 79 } 80 81 err = cf.configManager.Validate(configEnvelope) 82 if err != nil { 83 return filter.Reject, nil 84 } 85 86 return filter.Accept, &configCommitter{ 87 manager: cf.configManager, 88 configEnvelope: configEnvelope, 89 } 90 }