|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package policy |
| 8 | + |
| 9 | +import ( |
| 10 | + "github.com/hyperledger/fabric-lib-go/bccsp" |
| 11 | + cb "github.com/hyperledger/fabric-protos-go-apiv2/common" |
| 12 | + "github.com/hyperledger/fabric-x-common/common/channelconfig" |
| 13 | + "github.com/hyperledger/fabric-x-common/common/configtx" |
| 14 | + "github.com/hyperledger/fabric-x-common/common/policies" |
| 15 | + "github.com/hyperledger/fabric-x-common/protoutil" |
| 16 | + "github.com/pkg/errors" |
| 17 | +) |
| 18 | + |
| 19 | +// BuildPolicyManagerFromBlock builds a new policy manager from block. |
| 20 | +func BuildPolicyManagerFromBlock(configTX *cb.Envelope, bccsp bccsp.BCCSP) (policies.Manager, error) { |
| 21 | + payload, err := protoutil.UnmarshalPayload(configTX.Payload) |
| 22 | + if err != nil { |
| 23 | + return nil, errors.WithMessage(err, "error unmarshaling envelope to payload") |
| 24 | + } |
| 25 | + |
| 26 | + if payload.Header == nil { |
| 27 | + return nil, errors.New("missing channel header") |
| 28 | + } |
| 29 | + |
| 30 | + chdr, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader) |
| 31 | + if err != nil { |
| 32 | + return nil, errors.WithMessage(err, "error unmarshalling channel header") |
| 33 | + } |
| 34 | + |
| 35 | + configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data) |
| 36 | + if err != nil { |
| 37 | + return nil, errors.WithMessage(err, "error umarshaling config envelope from payload data") |
| 38 | + } |
| 39 | + |
| 40 | + bundle, err := channelconfig.NewBundle(chdr.ChannelId, configEnvelope.Config, bccsp) |
| 41 | + if err != nil { |
| 42 | + return nil, errors.WithMessage(err, "error creating channelconfig bundle") |
| 43 | + } |
| 44 | + |
| 45 | + err = checkResources(bundle) |
| 46 | + if err != nil { |
| 47 | + return nil, errors.WithMessagef(err, "error checking bundle for channel: %s", chdr.ChannelId) |
| 48 | + } |
| 49 | + |
| 50 | + policyManager := bundle.PolicyManager() |
| 51 | + return policyManager, nil |
| 52 | +} |
| 53 | + |
| 54 | +// checkResources makes sure that the channel config is compatible with this binary and logs sanity checks |
| 55 | +func checkResources(res channelconfig.Resources) error { |
| 56 | + channelconfig.LogSanityChecks(res) |
| 57 | + oc, ok := res.OrdererConfig() |
| 58 | + if !ok { |
| 59 | + return errors.New("config does not contain orderer config") |
| 60 | + } |
| 61 | + if err := oc.Capabilities().Supported(); err != nil { |
| 62 | + return errors.WithMessagef(err, "config requires unsupported orderer capabilities: %s", err) |
| 63 | + } |
| 64 | + if err := res.ChannelConfig().Capabilities().Supported(); err != nil { |
| 65 | + return errors.WithMessagef(err, "config requires unsupported channel capabilities: %s", err) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
0 commit comments