-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadsysccs.go
90 lines (75 loc) · 2.35 KB
/
loadsysccs.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package scc
import (
"fmt"
"os"
"plugin"
"sync"
"github.com/hyperledger/fabric/common/viperutil"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/pkg/errors"
)
const (
sccFactoryMethod = "New"
)
// PluginConfig SCC plugin configuration
type PluginConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled"`
Name string `mapstructure:"name" yaml:"name"`
Path string `mapstructure:"path" yaml:"path"`
InvokableExternal bool `mapstructure:"invokableExternal" yaml:"invokableExternal"`
InvokableCC2CC bool `mapstructure:"invokableCC2CC" yaml:"invokableCC2CC"`
}
var once sync.Once
var sccPlugins []*SystemChaincode
// loadSysCCs reads system chaincode plugin configuration and loads them
func loadSysCCs(p *Provider) []*SystemChaincode {
once.Do(func() {
var config []*PluginConfig
err := viperutil.EnhancedExactUnmarshalKey("chaincode.systemPlugins", &config)
if err != nil {
panic(errors.WithMessage(err, "could not load YAML config"))
}
loadSysCCsWithConfig(config)
})
return sccPlugins
}
func loadSysCCsWithConfig(configs []*PluginConfig) {
for _, conf := range configs {
plugin := loadPlugin(conf.Path)
chaincode := &SystemChaincode{
Enabled: conf.Enabled,
Name: conf.Name,
Path: conf.Path,
Chaincode: *plugin,
InvokableExternal: conf.InvokableExternal,
InvokableCC2CC: conf.InvokableCC2CC,
}
sccPlugins = append(sccPlugins, chaincode)
sysccLogger.Infof("Successfully loaded SCC %s from path %s", chaincode.Name, chaincode.Path)
}
}
func loadPlugin(path string) *shim.Chaincode {
if _, err := os.Stat(path); err != nil {
panic(fmt.Errorf("Could not find plugin at path %s: %s", path, err))
}
p, err := plugin.Open(path)
if err != nil {
panic(fmt.Errorf("Error opening plugin at path %s: %s", path, err))
}
sccFactorySymbol, err := p.Lookup(sccFactoryMethod)
if err != nil {
panic(fmt.Errorf(
"Could not find symbol %s. Plugin must export this method", sccFactoryMethod))
}
sccFactory, ok := sccFactorySymbol.(func() shim.Chaincode)
if !ok {
panic(fmt.Errorf("Function %s does not match expected definition func() shim.Chaincode",
sccFactoryMethod))
}
scc := sccFactory()
return &scc
}