根据位于以下位置的系统链代码文档:
https://hyperledger-fabric.readthedocs.io/en/latest/systemchaincode.html
存储库中应该有一个示例:
“每个系统链码都必须实现 Chaincode 接口并导出一个构造方法,该方法与主包中的签名 func New() shim.Chaincode 相匹配。可以在 examples/plugin/scc 的存储库中找到一个示例。”
然而文件夹 examples/plugin/scc 不存在于 github fabric 存储库的任何地方......
有人可以指出我正确的方向吗?谢谢
编辑
我找到的唯一样本是
https://github.com/hyperledger/fabric/tree/release/core/scc/samplesyscc
这是文档所指的样本吗?如果是,也许更新你的文档......
事实上,实现常规链码和系统链码之间没有太大区别。唯一的区别是系统链代码被编译到对等点并在对等进程中运行,或者可以作为 plugging.
所以,是的,你找到的例子是你可以用来自己实现系统链代码的例子,例如https://github.com/hyperledger/fabric/blob/release/core/scc/samplesyscc/samplesyscc.go.
此外,您可以查看其他系统链码以获得更一般的想法,例如您可以从QSCC(查询系统链码)中学习。
为了启用系统链代码,您需要确保在
core.yaml
文件中启用它,例如,这是将系统链代码编译成对等代码的样子:
# system chaincodes whitelist. To add system chaincode "myscc" to the
# whitelist, add "myscc: enable" to the list below, and register in
# chaincode/importsysccs.go
system:
cscc: enable
lscc: enable
escc: enable
vscc: enable
qscc: enable
rscc: disable
另外你需要在里面列出
importsysccs.go
,例如:
var systemChaincodes = []*SystemChaincode{
{
Enabled: true,
Name: "cscc",
Path: "github.com/hyperledger/fabric/core/scc/cscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &cscc.PeerConfiger{},
InvokableExternal: true, // cscc is invoked to join a channel
},
{
Enabled: true,
Name: "lscc",
Path: "github.com/hyperledger/fabric/core/scc/lscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: lscc.NewLifeCycleSysCC(),
InvokableExternal: true, // lscc is invoked to deploy new chaincodes
InvokableCC2CC: true, // lscc can be invoked by other chaincodes
},
{
Enabled: true,
Name: "escc",
Path: "github.com/hyperledger/fabric/core/scc/escc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &escc.EndorserOneValidSignature{},
},
{
Enabled: true,
Name: "vscc",
Path: "github.com/hyperledger/fabric/core/scc/vscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &vscc.ValidatorOneValidSignature{},
},
{
Enabled: true,
Name: "qscc",
Path: "github.com/hyperledger/fabric/core/chaincode/qscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &qscc.LedgerQuerier{},
InvokableExternal: true, // qscc can be invoked to retrieve blocks
InvokableCC2CC: true, // qscc can be invoked to retrieve blocks also by a cc
},
{
Enabled: true,
Name: "rscc",
Path: "github.com/hyperledger/fabric/core/chaincode/rscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: rscc.NewRscc(),
InvokableExternal: true, // rscc can be invoked to update policies
InvokableCC2CC: false, // rscc cannot be invoked from a cc
},
}
作为替代方案,您可以将系统链代码作为插件,要启用它,您需要在
core.yaml
: 中进行设置
# System chaincode plugins: in addition to being imported and compiled
# into fabric through core/chaincode/importsysccs.go, system chaincodes
# can also be loaded as shared objects compiled as Go plugins.
# See examples/plugins/scc for an example.
# Like regular system chaincodes, plugins must also be white listed in the
# chaincode.system section above.
systemPlugins:
# example configuration:
# - enabled: true
# name: myscc
# path: /opt/lib/myscc.so
# invokableExternal: true
# invokableCC2CC: true
请注意,从 v2.0 开始,系统链代码插件支持已被删除。这是 v2.0 的发行说明:
作为一般远离 go 插件作为扩展的一部分 Fabric 的机制,通过 go 添加系统链代码的能力 插件已被删除。希望使用自定义扩展 Fabric 的用户 系统链代码可以用系统重建对等二进制文件 二进制文件中内置的链码。这个系统链码应该是 像任何其他用户链代码一样定义和初始化。这 新模型与插件模型非常相似(这需要 插件将在 Fabric 的同一提交上构建)和地址 围绕生命周期和验证的重大缺陷 系统链码交易。
仍然可以将系统链代码构建到对等二进制文件/映像中。您可以检查 internal/peer/node/start.go 并搜索像
qscc
这样的系统链代码作为系统链代码如何注册的参考示例,然后扩展对等启动代码以将新的系统链代码包含在同样的方式。
您还需要将系统链代码添加到
core.yaml
chaincode.system
属性以确保它在运行时在对等点上启用。