|
| 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 sharedconfig |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + cb "github.com/hyperledger/fabric/protos/common" |
| 23 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | + "github.com/op/go-logging" |
| 27 | +) |
| 28 | + |
| 29 | +// ConsensusTypeKey is the cb.ConfigurationItem type key name for the ConsensusType message |
| 30 | +const ConsensusTypeKey = "ConsensusType" |
| 31 | + |
| 32 | +// BatchSizeKey is the cb.ConfigurationItem type key name for the BatchSize message |
| 33 | +const BatchSizeKey = "BatchSize" |
| 34 | + |
| 35 | +var logger = logging.MustGetLogger("orderer/common/sharedconfig") |
| 36 | + |
| 37 | +func init() { |
| 38 | + logging.SetLevel(logging.DEBUG, "") |
| 39 | +} |
| 40 | + |
| 41 | +// Manager stores the common shared orderer configuration |
| 42 | +// It is intended to be the primary accessor of ManagerImpl |
| 43 | +// It is intended to discourage use of the other exported ManagerImpl methods |
| 44 | +// which are used for updating the orderer configuration by the ConfigManager |
| 45 | +type Manager interface { |
| 46 | + // ConsensusType returns the configured consensus type |
| 47 | + ConsensusType() string |
| 48 | + |
| 49 | + // BatchSize returns the maximum number of messages to include in a block |
| 50 | + BatchSize() int |
| 51 | +} |
| 52 | + |
| 53 | +type ordererConfig struct { |
| 54 | + consensusType string |
| 55 | + batchSize int |
| 56 | +} |
| 57 | + |
| 58 | +// ManagerImpl is an implementation of Manager and configtx.ConfigHandler |
| 59 | +// In general, it should only be referenced as an Impl for the configtx.ConfigManager |
| 60 | +type ManagerImpl struct { |
| 61 | + pendingConfig *ordererConfig |
| 62 | + config *ordererConfig |
| 63 | +} |
| 64 | + |
| 65 | +// NewManagerImpl creates a new ManagerImpl with the given CryptoHelper |
| 66 | +func NewManagerImpl() *ManagerImpl { |
| 67 | + return &ManagerImpl{ |
| 68 | + config: &ordererConfig{}, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// ConsensusType returns the configured consensus type |
| 73 | +func (pm *ManagerImpl) ConsensusType() string { |
| 74 | + return pm.config.consensusType |
| 75 | +} |
| 76 | + |
| 77 | +// BatchSize returns the maximum number of messages to include in a block |
| 78 | +func (pm *ManagerImpl) BatchSize() int { |
| 79 | + return pm.config.batchSize |
| 80 | +} |
| 81 | + |
| 82 | +// BeginConfig is used to start a new configuration proposal |
| 83 | +func (pm *ManagerImpl) BeginConfig() { |
| 84 | + if pm.pendingConfig != nil { |
| 85 | + logger.Fatalf("Programming error, cannot call begin in the middle of a proposal") |
| 86 | + } |
| 87 | + pm.pendingConfig = &ordererConfig{} |
| 88 | +} |
| 89 | + |
| 90 | +// RollbackConfig is used to abandon a new configuration proposal |
| 91 | +func (pm *ManagerImpl) RollbackConfig() { |
| 92 | + pm.pendingConfig = nil |
| 93 | +} |
| 94 | + |
| 95 | +// CommitConfig is used to commit a new configuration proposal |
| 96 | +func (pm *ManagerImpl) CommitConfig() { |
| 97 | + if pm.pendingConfig == nil { |
| 98 | + logger.Fatalf("Programming error, cannot call commit without an existing proposal") |
| 99 | + } |
| 100 | + pm.config = pm.pendingConfig |
| 101 | + pm.pendingConfig = nil |
| 102 | +} |
| 103 | + |
| 104 | +// ProposeConfig is used to add new configuration to the configuration proposal |
| 105 | +func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigurationItem) error { |
| 106 | + if configItem.Type != cb.ConfigurationItem_Orderer { |
| 107 | + return fmt.Errorf("Expected type of ConfigurationItem_Orderer, got %v", configItem.Type) |
| 108 | + } |
| 109 | + |
| 110 | + switch configItem.Key { |
| 111 | + case ConsensusTypeKey: |
| 112 | + consensusType := &ab.ConsensusType{} |
| 113 | + err := proto.Unmarshal(configItem.Value, consensusType) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("Unmarshaling error for ConsensusType: %s", err) |
| 116 | + } |
| 117 | + if pm.config.consensusType == "" { |
| 118 | + // The first configuration we accept the consensus type regardless |
| 119 | + pm.config.consensusType = consensusType.Type |
| 120 | + } |
| 121 | + if consensusType.Type != pm.config.consensusType { |
| 122 | + return fmt.Errorf("Attempted to change the consensus type from %s to %s after init", pm.config.consensusType, consensusType.Type) |
| 123 | + } |
| 124 | + |
| 125 | + pm.pendingConfig.consensusType = consensusType.Type |
| 126 | + case BatchSizeKey: |
| 127 | + batchSize := &ab.BatchSize{} |
| 128 | + err := proto.Unmarshal(configItem.Value, batchSize) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("Unmarshaling error for BatchSize: %s", err) |
| 131 | + } |
| 132 | + |
| 133 | + if batchSize.Messages <= 0 { |
| 134 | + return fmt.Errorf("Attempted to set the batch size to %d which is less than or equal to 0", batchSize.Messages) |
| 135 | + } |
| 136 | + |
| 137 | + pm.pendingConfig.batchSize = int(batchSize.Messages) |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | +} |
0 commit comments