forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
267 lines (231 loc) · 7.62 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright IBM Corp, SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package library
import (
"fmt"
"os"
"plugin"
"reflect"
"sync"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/handlers/auth"
"github.com/hyperledger/fabric/core/handlers/decoration"
endorsement2 "github.com/hyperledger/fabric/core/handlers/endorsement/api"
"github.com/hyperledger/fabric/core/handlers/validation/api"
)
var logger = flogging.MustGetLogger("core/handlers")
// Registry defines an object that looks up
// handlers by name
type Registry interface {
// Lookup returns a handler with a given
// registered name, or nil if does not exist
Lookup(HandlerType) interface{}
}
// HandlerType defines custom handlers that can filter and mutate
// objects passing within the peer
type HandlerType int
const (
// Auth handler - reject or forward proposals from clients
Auth HandlerType = iota
// Decoration handler - append or mutate the chaincode input
// passed to the chaincode
Decoration
Endorsement
Validation
authPluginFactory = "NewFilter"
decoratorPluginFactory = "NewDecorator"
pluginFactory = "NewPluginFactory"
)
type registry struct {
filters []auth.Filter
decorators []decoration.Decorator
endorsers map[string]endorsement2.PluginFactory
validators map[string]validation.PluginFactory
}
var once sync.Once
var reg registry
// Config configures the factory methods
// and plugins for the registry
type Config struct {
AuthFilters []*HandlerConfig `mapstructure:"authFilters" yaml:"authFilters"`
Decorators []*HandlerConfig `mapstructure:"decorators" yaml:"decorators"`
Endorsers PluginMapping `mapstructure:"endorsers" yaml:"endorsers"`
Validators PluginMapping `mapstructure:"validators" yaml:"validators"`
}
type PluginMapping map[string]*HandlerConfig
// HandlerConfig defines configuration for a plugin or compiled handler
type HandlerConfig struct {
Name string `mapstructure:"name" yaml:"name"`
Library string `mapstructure:"library" yaml:"library"`
}
// InitRegistry creates the (only) instance
// of the registry
func InitRegistry(c Config) Registry {
once.Do(func() {
reg = registry{
endorsers: make(map[string]endorsement2.PluginFactory),
validators: make(map[string]validation.PluginFactory),
}
reg.loadHandlers(c)
})
return ®
}
// loadHandlers loads the configured handlers
func (r *registry) loadHandlers(c Config) {
for _, config := range c.AuthFilters {
r.evaluateModeAndLoad(config, Auth)
}
for _, config := range c.Decorators {
r.evaluateModeAndLoad(config, Decoration)
}
for chaincodeID, config := range c.Endorsers {
r.evaluateModeAndLoad(config, Endorsement, chaincodeID)
}
for chaincodeID, config := range c.Validators {
r.evaluateModeAndLoad(config, Validation, chaincodeID)
}
}
// evaluateModeAndLoad if a library path is provided, load the shared object
func (r *registry) evaluateModeAndLoad(c *HandlerConfig, handlerType HandlerType, extraArgs ...string) {
if c.Library != "" {
r.loadPlugin(c.Library, handlerType, extraArgs...)
} else {
r.loadCompiled(c.Name, handlerType, extraArgs...)
}
}
// loadCompiled loads a statically compiled handler
func (r *registry) loadCompiled(handlerFactory string, handlerType HandlerType, extraArgs ...string) {
registryMD := reflect.ValueOf(&HandlerLibrary{})
o := registryMD.MethodByName(handlerFactory)
if !o.IsValid() {
logger.Panicf(fmt.Sprintf("Method %s isn't a method of HandlerLibrary", handlerFactory))
}
inst := o.Call(nil)[0].Interface()
if handlerType == Auth {
r.filters = append(r.filters, inst.(auth.Filter))
} else if handlerType == Decoration {
r.decorators = append(r.decorators, inst.(decoration.Decorator))
} else if handlerType == Endorsement {
if len(extraArgs) != 1 {
logger.Panicf("expected 1 argument in extraArgs")
}
r.endorsers[extraArgs[0]] = inst.(endorsement2.PluginFactory)
} else if handlerType == Validation {
if len(extraArgs) != 1 {
logger.Panicf("expected 1 argument in extraArgs")
}
r.validators[extraArgs[0]] = inst.(validation.PluginFactory)
}
}
// loadPlugin loads a pluggagle handler
func (r *registry) loadPlugin(pluginPath string, handlerType HandlerType, extraArgs ...string) {
if _, err := os.Stat(pluginPath); err != nil {
logger.Panicf(fmt.Sprintf("Could not find plugin at path %s: %s", pluginPath, err))
}
p, err := plugin.Open(pluginPath)
if err != nil {
logger.Panicf(fmt.Sprintf("Error opening plugin at path %s: %s", pluginPath, err))
}
if handlerType == Auth {
r.initAuthPlugin(p)
} else if handlerType == Decoration {
r.initDecoratorPlugin(p)
} else if handlerType == Endorsement {
r.initEndorsementPlugin(p, extraArgs...)
} else if handlerType == Validation {
r.initValidationPlugin(p, extraArgs...)
}
}
// initAuthPlugin constructs an auth filter from the given plugin
func (r *registry) initAuthPlugin(p *plugin.Plugin) {
constructorSymbol, err := p.Lookup(authPluginFactory)
if err != nil {
panicWithLookupError(authPluginFactory, err)
}
constructor, ok := constructorSymbol.(func() auth.Filter)
if !ok {
panicWithDefinitionError(authPluginFactory)
}
filter := constructor()
if filter != nil {
r.filters = append(r.filters, filter)
}
}
// initDecoratorPlugin constructs a decorator from the given plugin
func (r *registry) initDecoratorPlugin(p *plugin.Plugin) {
constructorSymbol, err := p.Lookup(decoratorPluginFactory)
if err != nil {
panicWithLookupError(decoratorPluginFactory, err)
}
constructor, ok := constructorSymbol.(func() decoration.Decorator)
if !ok {
panicWithDefinitionError(decoratorPluginFactory)
}
decorator := constructor()
if decorator != nil {
r.decorators = append(r.decorators, constructor())
}
}
func (r *registry) initEndorsementPlugin(p *plugin.Plugin, extraArgs ...string) {
if len(extraArgs) != 1 {
logger.Panicf("expected 1 argument in extraArgs")
}
factorySymbol, err := p.Lookup(pluginFactory)
if err != nil {
panicWithLookupError(pluginFactory, err)
}
constructor, ok := factorySymbol.(func() endorsement2.PluginFactory)
if !ok {
panicWithDefinitionError(pluginFactory)
}
factory := constructor()
if factory == nil {
logger.Panicf("factory instance returned nil")
}
r.endorsers[extraArgs[0]] = factory
}
func (r *registry) initValidationPlugin(p *plugin.Plugin, extraArgs ...string) {
if len(extraArgs) != 1 {
logger.Panicf("expected 1 argument in extraArgs")
}
factorySymbol, err := p.Lookup(pluginFactory)
if err != nil {
panicWithLookupError(pluginFactory, err)
}
constructor, ok := factorySymbol.(func() validation.PluginFactory)
if !ok {
panicWithDefinitionError(pluginFactory)
}
factory := constructor()
if factory == nil {
logger.Panicf("factory instance returned nil")
}
r.validators[extraArgs[0]] = factory
}
// panicWithLookupError panics when a handler constructor lookup fails
func panicWithLookupError(factory string, err error) {
logger.Panicf(fmt.Sprintf("Plugin must contain constructor with name %s. Error from lookup: %s",
factory, err))
}
// panicWithDefinitionError panics when a handler constructor does not match
// the expected function definition
func panicWithDefinitionError(factory string) {
logger.Panicf(fmt.Sprintf("Constructor method %s does not match expected definition",
factory))
}
// Lookup returns a list of handlers with the given
// given type, or nil if none exist
func (r *registry) Lookup(handlerType HandlerType) interface{} {
if handlerType == Auth {
return r.filters
} else if handlerType == Decoration {
return r.decorators
} else if handlerType == Endorsement {
return r.endorsers
} else if handlerType == Validation {
return r.validators
}
return nil
}