|
| 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 configtx |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/hyperledger/fabric/common/util" |
| 21 | + cb "github.com/hyperledger/fabric/protos/common" |
| 22 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 23 | + "github.com/hyperledger/fabric/protos/utils" |
| 24 | + |
| 25 | + "github.com/golang/protobuf/proto" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + CreationPolicyKey = "CreationPolicy" |
| 30 | + msgVersion = int32(0) |
| 31 | + epoch = 0 |
| 32 | +) |
| 33 | + |
| 34 | +// Template can be used to faciliate creation of configuration transactions |
| 35 | +type Template interface { |
| 36 | + // Items returns a set of SignedConfigurationItems for the given chainID |
| 37 | + Items(chainID string) ([]*cb.SignedConfigurationItem, error) |
| 38 | +} |
| 39 | + |
| 40 | +type simpleTemplate struct { |
| 41 | + items []*cb.ConfigurationItem |
| 42 | +} |
| 43 | + |
| 44 | +// NewSimpleTemplate creates a Template using the supplied items |
| 45 | +func NewSimpleTemplate(items ...*cb.ConfigurationItem) Template { |
| 46 | + return &simpleTemplate{items: items} |
| 47 | +} |
| 48 | + |
| 49 | +// Items returns a set of SignedConfigurationItems for the given chainID, and errors only on marshaling errors |
| 50 | +func (st *simpleTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) { |
| 51 | + signedItems := make([]*cb.SignedConfigurationItem, len(st.items)) |
| 52 | + for i := range st.items { |
| 53 | + st.items[i].Header = &cb.ChainHeader{ChainID: chainID} |
| 54 | + mItem, err := proto.Marshal(st.items[i]) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + signedItems[i] = &cb.SignedConfigurationItem{ConfigurationItem: mItem} |
| 59 | + } |
| 60 | + |
| 61 | + return signedItems, nil |
| 62 | +} |
| 63 | + |
| 64 | +type compositeTemplate struct { |
| 65 | + templates []Template |
| 66 | +} |
| 67 | + |
| 68 | +// NewSimpleTemplate creates a Template using the source Templates |
| 69 | +func NewCompositeTemplate(templates ...Template) Template { |
| 70 | + return &compositeTemplate{templates: templates} |
| 71 | +} |
| 72 | + |
| 73 | +// Items returns a set of SignedConfigurationItems for the given chainID, and errors only on marshaling errors |
| 74 | +func (ct *compositeTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) { |
| 75 | + items := make([][]*cb.SignedConfigurationItem, len(ct.templates)) |
| 76 | + var err error |
| 77 | + for i := range ct.templates { |
| 78 | + items[i], err = ct.templates[i].Items(chainID) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return join(items...), nil |
| 85 | +} |
| 86 | + |
| 87 | +type newChainTemplate struct { |
| 88 | + creationPolicy string |
| 89 | + template Template |
| 90 | +} |
| 91 | + |
| 92 | +// NewChainCreationTemplate takes a CreationPolicy and a Template to produce a Template which outputs an appropriately |
| 93 | +// constructed list of SignedConfigurationItem including an appropriate digest. Note, using this Template in |
| 94 | +// a CompositeTemplate will invalidate the CreationPolicy |
| 95 | +func NewChainCreationTemplate(creationPolicy string, template Template) Template { |
| 96 | + return &newChainTemplate{ |
| 97 | + creationPolicy: creationPolicy, |
| 98 | + template: template, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Items returns a set of SignedConfigurationItems for the given chainID, and errors only on marshaling errors |
| 103 | +func (nct *newChainTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) { |
| 104 | + items, err := nct.template.Items(chainID) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + creationPolicy := &cb.SignedConfigurationItem{ |
| 110 | + ConfigurationItem: utils.MarshalOrPanic(&cb.ConfigurationItem{ |
| 111 | + Header: utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, chainID, epoch), |
| 112 | + Key: CreationPolicyKey, |
| 113 | + Value: utils.MarshalOrPanic(&ab.CreationPolicy{ |
| 114 | + Policy: CreationPolicyKey, |
| 115 | + Digest: HashItems(items), |
| 116 | + }), |
| 117 | + }), |
| 118 | + } |
| 119 | + |
| 120 | + return join([]*cb.SignedConfigurationItem{creationPolicy}, items), nil |
| 121 | +} |
| 122 | + |
| 123 | +// HashItems is a utility method for computing the hash of the concatenation of the marshaled ConfigurationItems |
| 124 | +// in a []*cb.SignedConfigurationItem |
| 125 | +func HashItems(items []*cb.SignedConfigurationItem) []byte { |
| 126 | + sourceBytes := make([][]byte, len(items)) |
| 127 | + for i := range items { |
| 128 | + sourceBytes[i] = items[i].ConfigurationItem |
| 129 | + } |
| 130 | + return util.ComputeCryptoHash(util.ConcatenateBytes(sourceBytes...)) |
| 131 | +} |
| 132 | + |
| 133 | +// join takes a number of SignedConfigurationItem slices and produces a single item |
| 134 | +func join(sets ...[]*cb.SignedConfigurationItem) []*cb.SignedConfigurationItem { |
| 135 | + total := 0 |
| 136 | + for _, set := range sets { |
| 137 | + total += len(set) |
| 138 | + } |
| 139 | + result := make([]*cb.SignedConfigurationItem, total) |
| 140 | + last := 0 |
| 141 | + for _, set := range sets { |
| 142 | + for i := range set { |
| 143 | + result[i+last] = set[i] |
| 144 | + } |
| 145 | + last += len(set) |
| 146 | + } |
| 147 | + |
| 148 | + return result |
| 149 | +} |
0 commit comments