|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 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 service |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/hyperledger/fabric/gossip/api" |
| 23 | + "github.com/hyperledger/fabric/protos/common" |
| 24 | + "github.com/hyperledger/fabric/protos/peer" |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | + "github.com/hyperledger/fabric/protos/utils" |
| 27 | +) |
| 28 | + |
| 29 | +// unMarshal is used to un-marshall proto-buffer types. |
| 30 | +// In tests, I override this variable with a function |
| 31 | +var unMarshal func (buf []byte, pb proto.Message) error |
| 32 | + |
| 33 | +func init() { |
| 34 | + unMarshal = proto.Unmarshal |
| 35 | +} |
| 36 | + |
| 37 | +// This is an implementation of api.JoinChannelMessage. |
| 38 | +// This object is created from a *common.Block |
| 39 | +type joinChannelMessage struct { |
| 40 | + seqNum uint64 |
| 41 | + anchorPeers []api.AnchorPeer |
| 42 | +} |
| 43 | + |
| 44 | +// JoinChannelMessageFromBlock returns an api.JoinChannelMessage, nil |
| 45 | +// or nil, error if the block is not a valid channel configuration block |
| 46 | +func JoinChannelMessageFromBlock(block *common.Block) (api.JoinChannelMessage, error) { |
| 47 | + anchorPeers, err := parseBlock(block) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + jcm := &joinChannelMessage{seqNum: block.Header.Number, anchorPeers: []api.AnchorPeer{}} |
| 52 | + for _, ap := range anchorPeers.AnchorPees { |
| 53 | + anchorPeer := api.AnchorPeer{ |
| 54 | + Host: ap.Host, |
| 55 | + Port: int(ap.Port), |
| 56 | + Cert: api.PeerIdentityType(ap.Cert), |
| 57 | + } |
| 58 | + jcm.anchorPeers = append(jcm.anchorPeers, anchorPeer) |
| 59 | + } |
| 60 | + return jcm, nil |
| 61 | +} |
| 62 | + |
| 63 | +// parseBlock parses a configuration block, and returns error |
| 64 | +// if it's not a valid channel configuration block |
| 65 | +func parseBlock(block *common.Block) (*peer.AnchorPeers, error) { |
| 66 | + confEnvelope, err := extractConfigurationEnvelope(block) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + // Find anchor peer configuration |
| 71 | + found := false |
| 72 | + var anchorPeerConfig *common.ConfigurationItem |
| 73 | + for _, item := range confEnvelope.Items { |
| 74 | + rawConfItem := item.ConfigurationItem |
| 75 | + confItem := &common.ConfigurationItem{} |
| 76 | + if err := unMarshal(rawConfItem, confItem); err != nil { |
| 77 | + return nil, fmt.Errorf("Failed unmarshalling configuration item") |
| 78 | + } |
| 79 | + if confItem.Header.Type != int32(common.HeaderType_CONFIGURATION_ITEM) { |
| 80 | + continue |
| 81 | + } |
| 82 | + if confItem.Type != common.ConfigurationItem_Peer { |
| 83 | + continue |
| 84 | + } |
| 85 | + if confItem.Key != utils.AnchorPeerConfItemKey { |
| 86 | + continue |
| 87 | + } |
| 88 | + if found { |
| 89 | + return nil, fmt.Errorf("Found multiple definition of AnchorPeers instead of a single one") |
| 90 | + } |
| 91 | + found = true |
| 92 | + anchorPeerConfig = confItem |
| 93 | + } |
| 94 | + if ! found { |
| 95 | + return nil, fmt.Errorf("Didn't find AnchorPeer definition in configuration block") |
| 96 | + } |
| 97 | + rawAnchorPeersBytes := anchorPeerConfig.Value |
| 98 | + anchorPeers := &peer.AnchorPeers{} |
| 99 | + if err := unMarshal(rawAnchorPeersBytes, anchorPeers); err != nil { |
| 100 | + return nil, fmt.Errorf("Failed deserializing anchor peers from configuration item") |
| 101 | + } |
| 102 | + if len(anchorPeers.AnchorPees) == 0 { |
| 103 | + return nil, fmt.Errorf("AnchorPeers field in configuration block was found, but is empty") |
| 104 | + } |
| 105 | + return anchorPeers, nil |
| 106 | +} |
| 107 | + |
| 108 | +// extractConfigurationEnvelope extracts the configuration envelope from a block, |
| 109 | +// or returns nil and an error if extraction fails |
| 110 | +func extractConfigurationEnvelope(block *common.Block) (*common.ConfigurationEnvelope, error) { |
| 111 | + if block.Header == nil { |
| 112 | + return nil, fmt.Errorf("Block header is empty") |
| 113 | + } |
| 114 | + if block.Data == nil { |
| 115 | + return nil, fmt.Errorf("Block data is empty") |
| 116 | + } |
| 117 | + env := &common.Envelope{} |
| 118 | + if len(block.Data.Data) == 0 { |
| 119 | + return nil, fmt.Errorf("Empty data in block") |
| 120 | + } |
| 121 | + if len(block.Data.Data) != 1 { |
| 122 | + return nil, fmt.Errorf("More than 1 transaction in a block") |
| 123 | + } |
| 124 | + if err := unMarshal(block.Data.Data[0], env); err != nil { |
| 125 | + return nil, fmt.Errorf("Failed unmarshalling envelope from block: %v", err) |
| 126 | + } |
| 127 | + payload := &common.Payload{} |
| 128 | + if err := unMarshal(env.Payload, payload); err != nil { |
| 129 | + return nil, fmt.Errorf("Failed unmarshalling payload from envelope: %v", err) |
| 130 | + } |
| 131 | + confEnvelope := &common.ConfigurationEnvelope{} |
| 132 | + if err := unMarshal(payload.Data, confEnvelope); err != nil { |
| 133 | + return nil, fmt.Errorf("Failed unmarshalling configuration envelope from payload: %v", err) |
| 134 | + } |
| 135 | + if len(confEnvelope.Items) == 0 { |
| 136 | + return nil, fmt.Errorf("Empty configuration envelope, no items detected") |
| 137 | + } |
| 138 | + return confEnvelope, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (jcm *joinChannelMessage) SequenceNumber() uint64 { |
| 142 | + return jcm.seqNum |
| 143 | +} |
| 144 | + |
| 145 | +func (jcm *joinChannelMessage) AnchorPeers() []api.AnchorPeer { |
| 146 | + return jcm.anchorPeers |
| 147 | +} |
0 commit comments