|
| 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 chainconfig |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/hyperledger/fabric/common/util" |
| 23 | + cb "github.com/hyperledger/fabric/protos/common" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | + "github.com/op/go-logging" |
| 27 | +) |
| 28 | + |
| 29 | +// Chain config keys |
| 30 | +const ( |
| 31 | + // HashingAlgorithmKey is the cb.ConfigurationItem type key name for the HashingAlgorithm message |
| 32 | + HashingAlgorithmKey = "HashingAlgorithm" |
| 33 | +) |
| 34 | + |
| 35 | +// Hashing algorithm types |
| 36 | +const ( |
| 37 | + // SHAKE256 is the algorithm type for the sha3 shake256 hashing algorithm with 512 bits of output |
| 38 | + SHA3Shake256 = "SHAKE256" |
| 39 | +) |
| 40 | + |
| 41 | +var logger = logging.MustGetLogger("common/chainconfig") |
| 42 | + |
| 43 | +// Descriptor stores the common chain configuration |
| 44 | +// It is intended to be the primary accessor of DescriptorImpl |
| 45 | +// It is intended to discourage use of the other exported DescriptorImpl methods |
| 46 | +// which are used for updating the chain configuration by the configtx.Manager |
| 47 | +type Descriptor interface { |
| 48 | + // HashingAlgorithm returns the default algorithm to be used when hashing |
| 49 | + // such as computing block hashes, and CreationPolicy digests |
| 50 | + HashingAlgorithm() func(input []byte) []byte |
| 51 | +} |
| 52 | + |
| 53 | +type chainConfig struct { |
| 54 | + hashingAlgorithm func(input []byte) []byte |
| 55 | +} |
| 56 | + |
| 57 | +// DescriptorImpl is an implementation of Manager and configtx.ConfigHandler |
| 58 | +// In general, it should only be referenced as an Impl for the configtx.Manager |
| 59 | +type DescriptorImpl struct { |
| 60 | + pendingConfig *chainConfig |
| 61 | + config *chainConfig |
| 62 | +} |
| 63 | + |
| 64 | +// NewDescriptorImpl creates a new DescriptorImpl with the given CryptoHelper |
| 65 | +func NewDescriptorImpl() *DescriptorImpl { |
| 66 | + return &DescriptorImpl{ |
| 67 | + config: &chainConfig{}, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// HashingAlgorithm returns a function pointer to the chain hashing algorihtm |
| 72 | +func (pm *DescriptorImpl) HashingAlgorithm() func(input []byte) []byte { |
| 73 | + return pm.config.hashingAlgorithm |
| 74 | +} |
| 75 | + |
| 76 | +// BeginConfig is used to start a new configuration proposal |
| 77 | +func (pm *DescriptorImpl) BeginConfig() { |
| 78 | + if pm.pendingConfig != nil { |
| 79 | + logger.Panicf("Programming error, cannot call begin in the middle of a proposal") |
| 80 | + } |
| 81 | + pm.pendingConfig = &chainConfig{} |
| 82 | +} |
| 83 | + |
| 84 | +// RollbackConfig is used to abandon a new configuration proposal |
| 85 | +func (pm *DescriptorImpl) RollbackConfig() { |
| 86 | + pm.pendingConfig = nil |
| 87 | +} |
| 88 | + |
| 89 | +// CommitConfig is used to commit a new configuration proposal |
| 90 | +func (pm *DescriptorImpl) CommitConfig() { |
| 91 | + if pm.pendingConfig == nil { |
| 92 | + logger.Panicf("Programming error, cannot call commit without an existing proposal") |
| 93 | + } |
| 94 | + pm.config = pm.pendingConfig |
| 95 | + pm.pendingConfig = nil |
| 96 | +} |
| 97 | + |
| 98 | +// ProposeConfig is used to add new configuration to the configuration proposal |
| 99 | +func (pm *DescriptorImpl) ProposeConfig(configItem *cb.ConfigurationItem) error { |
| 100 | + if configItem.Type != cb.ConfigurationItem_Chain { |
| 101 | + return fmt.Errorf("Expected type of ConfigurationItem_Chain, got %v", configItem.Type) |
| 102 | + } |
| 103 | + |
| 104 | + switch configItem.Key { |
| 105 | + case HashingAlgorithmKey: |
| 106 | + hashingAlgorithm := &cb.HashingAlgorithm{} |
| 107 | + if err := proto.Unmarshal(configItem.Value, hashingAlgorithm); err != nil { |
| 108 | + return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err) |
| 109 | + } |
| 110 | + switch hashingAlgorithm.Name { |
| 111 | + case SHA3Shake256: |
| 112 | + pm.pendingConfig.hashingAlgorithm = util.ComputeCryptoHash |
| 113 | + default: |
| 114 | + return fmt.Errorf("Unknown hashing algorithm type: %s", hashingAlgorithm.Name) |
| 115 | + } |
| 116 | + default: |
| 117 | + logger.Warningf("Uknown Chain configuration item with key %s", configItem.Key) |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments