|
| 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 sharedconfig |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + cb "github.com/hyperledger/fabric/protos/common" |
| 23 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | + "github.com/op/go-logging" |
| 27 | +) |
| 28 | + |
| 29 | +// Peer config keys |
| 30 | +const ( |
| 31 | + // AnchorPeersKey is the cb.ConfigurationItem type key name for the AnchorPeers message |
| 32 | + AnchorPeersKey = "AnchorPeers" |
| 33 | +) |
| 34 | + |
| 35 | +var logger = logging.MustGetLogger("peer/sharedconfig") |
| 36 | + |
| 37 | +// Descriptor stores the common peer configuration |
| 38 | +// It is intended to be the primary accessor of DescriptorImpl |
| 39 | +// It is intended to discourage use of the other exported DescriptorImpl methods |
| 40 | +// which are used for updating the chain configuration by the configtx.Manager |
| 41 | +type Descriptor interface { |
| 42 | + // AnchorPeers returns the list of anchor peers for the channel |
| 43 | + AnchorPeers() []*pb.AnchorPeer |
| 44 | +} |
| 45 | + |
| 46 | +type sharedConfig struct { |
| 47 | + anchorPeers []*pb.AnchorPeer |
| 48 | +} |
| 49 | + |
| 50 | +// DescriptorImpl is an implementation of Manager and configtx.ConfigHandler |
| 51 | +// In general, it should only be referenced as an Impl for the configtx.Manager |
| 52 | +type DescriptorImpl struct { |
| 53 | + pendingConfig *sharedConfig |
| 54 | + config *sharedConfig |
| 55 | +} |
| 56 | + |
| 57 | +// NewDescriptorImpl creates a new DescriptorImpl with the given CryptoHelper |
| 58 | +func NewDescriptorImpl() *DescriptorImpl { |
| 59 | + return &DescriptorImpl{ |
| 60 | + config: &sharedConfig{}, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver |
| 65 | +func (di *DescriptorImpl) AnchorPeers() []*pb.AnchorPeer { |
| 66 | + return di.config.anchorPeers |
| 67 | +} |
| 68 | + |
| 69 | +// BeginConfig is used to start a new configuration proposal |
| 70 | +func (di *DescriptorImpl) BeginConfig() { |
| 71 | + logger.Debugf("Beginning a possible new peer shared configuration") |
| 72 | + if di.pendingConfig != nil { |
| 73 | + logger.Panicf("Programming error, cannot call begin in the middle of a proposal") |
| 74 | + } |
| 75 | + di.pendingConfig = &sharedConfig{} |
| 76 | +} |
| 77 | + |
| 78 | +// RollbackConfig is used to abandon a new configuration proposal |
| 79 | +func (di *DescriptorImpl) RollbackConfig() { |
| 80 | + logger.Debugf("Rolling back proposed peer shared configuration") |
| 81 | + di.pendingConfig = nil |
| 82 | +} |
| 83 | + |
| 84 | +// CommitConfig is used to commit a new configuration proposal |
| 85 | +func (di *DescriptorImpl) CommitConfig() { |
| 86 | + logger.Debugf("Committing new peer shared configuration") |
| 87 | + if di.pendingConfig == nil { |
| 88 | + logger.Panicf("Programming error, cannot call commit without an existing proposal") |
| 89 | + } |
| 90 | + di.config = di.pendingConfig |
| 91 | + di.pendingConfig = nil |
| 92 | +} |
| 93 | + |
| 94 | +// ProposeConfig is used to add new configuration to the configuration proposal |
| 95 | +func (di *DescriptorImpl) ProposeConfig(configItem *cb.ConfigurationItem) error { |
| 96 | + if configItem.Type != cb.ConfigurationItem_Peer { |
| 97 | + return fmt.Errorf("Expected type of ConfigurationItem_Peer, got %v", configItem.Type) |
| 98 | + } |
| 99 | + |
| 100 | + switch configItem.Key { |
| 101 | + case AnchorPeersKey: |
| 102 | + anchorPeers := &pb.AnchorPeers{} |
| 103 | + if err := proto.Unmarshal(configItem.Value, anchorPeers); err != nil { |
| 104 | + return fmt.Errorf("Unmarshaling error for %s: %s", configItem.Key, err) |
| 105 | + } |
| 106 | + if logger.IsEnabledFor(logging.DEBUG) { |
| 107 | + logger.Debugf("Setting %s to %v", configItem.Key, anchorPeers.AnchorPeers) |
| 108 | + } |
| 109 | + di.pendingConfig.anchorPeers = anchorPeers.AnchorPeers |
| 110 | + default: |
| 111 | + logger.Warningf("Uknown Peer configuration item with key %s", configItem.Key) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments