区块链教程Fabric1.0源代码分析scc(系统链码),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。
scc,system chain codes,即系统链码。包括:
scc代码分布在core/common/sysccprovider和core/scc目录下,目录结构如下:
* sccproviderimpl.go,SystemChaincodeProvider和SystemChaincodeProviderFactory接口实现,即sccProviderFactory和sccProviderImpl结构体及方法。
* importsysccs.go,scc工具函数。
接口定义如下:
type SystemChaincodeProviderFactory interface {
//创建SystemChaincodeProvider
NewSystemChaincodeProvider() SystemChaincodeProvider
}
//代码在core/common/sysccprovider/sysccprovider.go
全局变量及相关函数:
var sccFactory SystemChaincodeProviderFactory
//为sccFactory赋值
func RegisterSystemChaincodeProviderFactory(sccfact SystemChaincodeProviderFactory)
//获取sccFactory.NewSystemChaincodeProvider()
func GetSystemChaincodeProvider() SystemChaincodeProvider {
//代码在core/common/sysccprovider/sysccprovider.go
补充ChaincodeInstance结构体定义:
type ChaincodeInstance struct {
ChainID string //ID
ChaincodeName string //名称
ChaincodeVersion string //版本
}
//代码在core/common/sysccprovider/sysccprovider.go
接口定义如下:
type SystemChaincodeProvider interface {
IsSysCC(name string) bool //是否系统链码
IsSysCCAndNotInvokableCC2CC(name string) bool //确认是系统链码且不可通过CC2CC调用
IsSysCCAndNotInvokableExternal(name string) bool //确认是系统链码且不可通过提案调用
GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error) //获取账本的查询执行器
}
//代码在core/common/sysccprovider/sysccprovider.go
SystemChaincodeProviderFactory接口实现,即sccProviderFactory结构体及方法:
type sccProviderFactory struct {
}
//构造sccProviderImpl{}
func (c *sccProviderFactory) NewSystemChaincodeProvider() sysccprovider.SystemChaincodeProvider
//代码在core/scc/sccproviderimpl.go
SystemChaincodeProvider接口实现,即sccProviderImpl结构体及方法:
type sccProviderImpl struct {
}
func (c *sccProviderImpl) IsSysCC(name string) bool //IsSysCC(name)
func (c *sccProviderImpl) IsSysCCAndNotInvokableCC2CC(name string) bool //IsSysCCAndNotInvokableCC2CC(name)
//l := peer.GetLedger(cid)
//l.NewQueryExecutor()
func (c *sccProviderImpl) GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error)
//IsSysCCAndNotInvokableExternal(name)
func (c *sccProviderImpl) IsSysCCAndNotInvokableExternal(name string) bool
//代码在core/scc/sccproviderimpl.go
systemChaincodes定义:
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.LifeCycleSysCC{},
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
},
}
//代码在core/scc/importsysccs.go
涉及scc工具函数如下:
func RegisterSysCCs() //遍历systemChaincodes,调用RegisterSysCC(sysCC)
func DeploySysCCs(chainID string)//遍历systemChaincodes,调用deploySysCC(chainID, sysCC)
func DeDeploySysCCs(chainID string)//遍历systemChaincodes,调用DeDeploySysCC(chainID, sysCC)
func IsSysCC(name string) bool //是否系统链码
func IsSysCCAndNotInvokableExternal(name string) bool //确认是系统链码且不可被发送到此节点的提案调用
func IsSysCCAndNotInvokableCC2CC(name string) bool //确认是系统链码且不可通过chaincode-to-chaincode方式调用
//代码在core/scc/importsysccs.go
type SystemChaincode struct {
Name string //系统链码唯一名称
Path string //系统链码路径,当前未使用
InitArgs [][]byte //启动系统链码的初始化参数
Chaincode shim.Chaincode //实际的shim.Chaincode对象
InvokableExternal bool //跟踪是否可以被发送到此节点的提案调用
InvokableCC2CC bool //跟踪是否可以通过chaincode-to-chaincode方式调用
Enabled bool //启用或禁用
}
//注册系统链码,调用inproccontroller.Register(syscc.Path, syscc.Chaincode)
func RegisterSysCC(syscc *SystemChaincode) error
func deploySysCC(chainID string, syscc *SystemChaincode) error //部署链码
func DeDeploySysCC(chainID string, syscc *SystemChaincode) error //停止链码
func buildSysCC(context context.Context, spec *pb.ChaincodeSpec) (*pb.ChaincodeDeploymentSpec, error) //编译链码
func isWhitelisted(syscc *SystemChaincode) bool //是否在白名单,基于chaincode.system配置
//代码在core/scc/sysccapi.go
感谢关注兄弟连区块链教程分享!