@@ -10,13 +10,15 @@ import (
10
10
"fmt"
11
11
12
12
"github.com/hyperledger/fabric/common/cauthdsl"
13
+ "github.com/hyperledger/fabric/common/channelconfig"
13
14
"github.com/hyperledger/fabric/common/configtx"
14
15
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
15
16
"github.com/hyperledger/fabric/common/flogging"
16
17
"github.com/hyperledger/fabric/common/policies"
17
- "github.com/hyperledger/fabric/msp"
18
18
cb "github.com/hyperledger/fabric/protos/common"
19
19
"github.com/hyperledger/fabric/protos/utils"
20
+
21
+ "github.com/pkg/errors"
20
22
)
21
23
22
24
// RootGroupKey is the namespace in the config tree for this set of config
@@ -26,29 +28,18 @@ var logger = flogging.MustGetLogger("common/config/resource")
26
28
27
29
// Bundle stores an immutable group of resources configuration
28
30
type Bundle struct {
29
- rg * resourceGroup
30
- cm configtxapi.Manager
31
- pm policies.Manager
32
- rpm policies.Manager
31
+ channelID string
32
+ resConf * cb.Config
33
+ chanConf channelconfig.Resources
34
+ rg * resourceGroup
35
+ cm configtxapi.Manager
36
+ pm * policyRouter
33
37
}
34
38
35
- // New creates a new resources config bundle
36
- // TODO, change interface to take config and not an envelope
39
+ // NewBundleFromEnvelope creates a new resources config bundle.
40
+ // TODO, this method should probably be removed, as the resourcesconfig is never in an Envelope naturally.
37
41
// TODO, add an atomic BundleSource
38
- func New (envConfig * cb.Envelope , mspManager msp.MSPManager , channelPolicyManager policies.Manager ) (* Bundle , error ) {
39
- policyProviderMap := make (map [int32 ]policies.Provider )
40
- for pType := range cb .Policy_PolicyType_name {
41
- rtype := cb .Policy_PolicyType (pType )
42
- switch rtype {
43
- case cb .Policy_UNKNOWN :
44
- // Do not register a handler
45
- case cb .Policy_SIGNATURE :
46
- policyProviderMap [pType ] = cauthdsl .NewPolicyProvider (mspManager )
47
- case cb .Policy_MSP :
48
- // Add hook for MSP Handler here
49
- }
50
- }
51
-
42
+ func NewBundleFromEnvelope (envConfig * cb.Envelope , chanConf channelconfig.Resources ) (* Bundle , error ) {
52
43
payload , err := utils .UnmarshalPayload (envConfig .Payload )
53
44
if err != nil {
54
45
return nil , err
@@ -63,31 +54,28 @@ func New(envConfig *cb.Envelope, mspManager msp.MSPManager, channelPolicyManager
63
54
return nil , fmt .Errorf ("config is nil" )
64
55
}
65
56
66
- resourcesPolicyManager , err := policies . NewManagerImpl ( RootGroupKey , policyProviderMap , configEnvelope . Config . ChannelGroup )
57
+ chdr , err := utils . UnmarshalChannelHeader ( payload . Header . ChannelHeader )
67
58
if err != nil {
68
- return nil , err
59
+ return nil , errors . Wrap ( err , "failed to unmarshal channel header" )
69
60
}
70
61
71
- resourceGroup , err := newResourceGroup (configEnvelope .Config .ChannelGroup )
62
+ return NewBundle (chdr .ChannelId , configEnvelope .Config , chanConf )
63
+ }
64
+
65
+ // NewBundle creates a resources config bundle which implements the Resources interface.
66
+ func NewBundle (channelID string , config * cb.Config , chanConf channelconfig.Resources ) (* Bundle , error ) {
67
+ resourceGroup , err := newResourceGroup (config .ChannelGroup )
72
68
if err != nil {
73
69
return nil , err
74
70
}
75
71
76
72
b := & Bundle {
77
- rpm : resourcesPolicyManager ,
78
- rg : resourceGroup ,
79
- pm : & policyRouter {
80
- channelPolicyManager : channelPolicyManager ,
81
- resourcesPolicyManager : resourcesPolicyManager ,
82
- },
83
- }
84
-
85
- b .cm , err = configtx .NewManagerImpl (envConfig , b )
86
- if err != nil {
87
- return nil , err
73
+ channelID : channelID ,
74
+ rg : resourceGroup ,
75
+ resConf : config ,
88
76
}
89
77
90
- return b , nil
78
+ return b . NewFromChannelConfig ( chanConf )
91
79
}
92
80
93
81
// RootGroupKey returns the name of the key for the root group (the namespace for this config).
@@ -114,3 +102,66 @@ func (b *Bundle) APIPolicyMapper() PolicyMapper {
114
102
func (b * Bundle ) ChaincodeRegistry () ChaincodeRegistry {
115
103
return b .rg .chaincodesGroup
116
104
}
105
+
106
+ // ChannelConfig returns the channel config which this resources config depends on.
107
+ // Note, consumers of the resourcesconfig should almost never refer to the PolicyManager
108
+ // within the channel config, and should instead refer to the PolicyManager exposed by
109
+ // this Bundle.
110
+ func (b * Bundle ) ChannelConfig () channelconfig.Resources {
111
+ return b .chanConf
112
+ }
113
+
114
+ // ValidateNew is currently a no-op. The idea is that there may be additional checking which needs to be done
115
+ // between an old resource configuration and a new one. In the channel resource configuration case, we add some
116
+ // checks to make sure that the MSP IDs have not changed, and that the consensus type has not changed. There is
117
+ // currently no such check required in the peer resources, but, in the interest of following the pattern established
118
+ // by the channel configuration, it is included here.
119
+ func (b * Bundle ) ValidateNew (resources Resources ) error {
120
+ return nil
121
+ }
122
+
123
+ // NewFromChannelConfig builds a new Bundle, based on this Bundle, but with a different underlying
124
+ // channel config. This is usually invoked when a channel config update is processed.
125
+ func (b * Bundle ) NewFromChannelConfig (chanConf channelconfig.Resources ) (* Bundle , error ) {
126
+ policyProviderMap := make (map [int32 ]policies.Provider )
127
+ for pType := range cb .Policy_PolicyType_name {
128
+ rtype := cb .Policy_PolicyType (pType )
129
+ switch rtype {
130
+ case cb .Policy_UNKNOWN :
131
+ // Do not register a handler
132
+ case cb .Policy_SIGNATURE :
133
+ policyProviderMap [pType ] = cauthdsl .NewPolicyProvider (chanConf .MSPManager ())
134
+ case cb .Policy_MSP :
135
+ // Add hook for MSP Handler here
136
+ }
137
+ }
138
+
139
+ resourcesPolicyManager , err := policies .NewManagerImpl (RootGroupKey , policyProviderMap , b .resConf .ChannelGroup )
140
+
141
+ if err != nil {
142
+ return nil , err
143
+ }
144
+
145
+ result := & Bundle {
146
+ channelID : b .channelID ,
147
+ chanConf : chanConf ,
148
+ pm : & policyRouter {
149
+ channelPolicyManager : chanConf .PolicyManager (),
150
+ resourcesPolicyManager : resourcesPolicyManager ,
151
+ },
152
+ rg : b .rg ,
153
+ resConf : b .resConf ,
154
+ }
155
+
156
+ env , err := utils .CreateSignedEnvelope (cb .HeaderType_CONFIG , b .channelID , nil , & cb.ConfigEnvelope {Config : b .resConf }, 0 , 0 )
157
+ if err != nil {
158
+ return nil , errors .Wrap (err , "creating envelope for configtx manager failed" )
159
+ }
160
+
161
+ result .cm , err = configtx .NewManagerImpl (env , b )
162
+ if err != nil {
163
+ return nil , err
164
+ }
165
+
166
+ return result , nil
167
+ }
0 commit comments