區塊鏈教程Fabric1.0源代碼分析policy(背書策略),2018年下半年,區塊鏈行業正逐漸褪去發展之初的浮躁、迴歸理性,表面上看相關人才需求與身價似乎正在回落。但事實上,正是初期泡沫的漸退,讓人們更多的關注點放在了區塊鏈真正的技術之上。
Fabric 1.0源代碼筆記 之 policy(背書策略)
1、policy概述
policy代碼分佈在core/policy、core/policyprovider、common/policies目錄下。目錄結構如下:
- core/policy/policy.go,PolicyChecker接口定義及實現、PolicyCheckerFactory接口定義。
- core/policyprovider/provider.go,PolicyChecker工廠默認實現。
- common/policies目錄
* policy.go,ChannelPolicyManagerGetter接口及實現。
* implicitmeta_util.go,通道策略工具函數。
2、PolicyChecker工廠
2.1、PolicyCheckerFactory接口定義
type PolicyCheckerFactory interface {
NewPolicyChecker() PolicyChecker //構造PolicyChecker實例
}
var pcFactory PolicyCheckerFactory //全局變量定義及賦值函數
func RegisterPolicyCheckerFactory(f PolicyCheckerFactory) {
pcFactory = f
}
//代碼在core/policy/policy.go
2.2、PolicyCheckerFactory接口默認實現
type defaultFactory struct{}
//構造policy.PolicyChecker
func (f *defaultFactory) NewPolicyChecker() policy.PolicyChecker {
return policy.NewPolicyChecker(
peer.NewChannelPolicyManagerGetter(), //&channelPolicyManagerGetter{}
mgmt.GetLocalMSP(),
mgmt.NewLocalMSPPrincipalGetter(),
)
}
//獲取policy.PolicyChecker,即調用policy.GetPolicyChecker()
func GetPolicyChecker() policy.PolicyChecker
func init() { //初始化全局變量pcFactory
policy.RegisterPolicyCheckerFactory(&defaultFactory{})
}
3、PolicyChecker接口定義及實現
3.1、PolicyChecker接口定義
type PolicyChecker interface {
CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error
CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) error
CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error
}
//代碼在core/policy/policy.go
3.2、PolicyChecker接口實現
PolicyChecker接口實現,即policyChecker結構體及方法。
type policyChecker struct {
channelPolicyManagerGetter policies.ChannelPolicyManagerGetter //通道策略管理器
localMSP msp.IdentityDeserializer //身份
principalGetter mgmt.MSPPrincipalGetter //委託人
}
//構造policyChecker
func NewPolicyChecker(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localMSP msp.IdentityDeserializer, principalGetter mgmt.MSPPrincipalGetter) PolicyChecker
//檢查簽名提案是否符合通道策略
func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error
func (p *policyChecker) CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error
//檢查簽名數據是否符合通道策略,獲取策略並調取policy.Evaluate(sd)
func (p *policyChecker) CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) error
func GetPolicyChecker() PolicyChecker //pcFactory.NewPolicyChecker()
//代碼在core/policy/policy.go
func (p policyChecker) CheckPolicy(channelID, policyName string, signedProp pb.SignedProposal) error代碼如下:
func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error {
if channelID == "" { //channelID爲空,調取CheckPolicyNoChannel()
return p.CheckPolicyNoChannel(policyName, signedProp)
}
policyManager, _ := p.channelPolicyManagerGetter.Manager(channelID)
proposal, err := utils.GetProposal(signedProp.ProposalBytes) //獲取proposal
header, err := utils.GetHeader(proposal.Header)
shdr, err := utils.GetSignatureHeader(header.SignatureHeader) //SignatureHeader
sd := []*common.SignedData{&common.SignedData{
Data: signedProp.ProposalBytes,
Identity: shdr.Creator,
Signature: signedProp.Signature,
}}
return p.CheckPolicyBySignedData(channelID, policyName, sd)
}
//代碼在core/policy/policy.go
4、ChannelPolicyManagerGetter接口及實現
4.1、ChannelPolicyManagerGetter接口定義
type ChannelPolicyManagerGetter interface {
Manager(channelID string) (Manager, bool)
}
//代碼在common/policies/policy.go
4.2、ChannelPolicyManagerGetter接口實現
ChannelPolicyManagerGetter接口實現,即ManagerImpl結構體及方法。
type ManagerImpl struct {
parent *ManagerImpl
basePath string
fqPrefix string
providers map[int32]Provider //type Provider interface
config *policyConfig //type policyConfig struct
pendingConfig map[interface{}]*policyConfig //type policyConfig struct
pendingLock sync.RWMutex
SuppressSanityLogMessages bool
}
type Provider interface {
NewPolicy(data []byte) (Policy, proto.Message, error)
}
type policyConfig struct {
policies map[string]Policy //type Policy interface
managers map[string]*ManagerImpl
imps []*implicitMetaPolicy
}
type Policy interface {
//對給定的簽名數據,按規則檢驗確認是否符合約定的條件
Evaluate(signatureSet []*cb.SignedData) error
}
//構造ManagerImpl
func NewManagerImpl(basePath string, providers map[int32]Provider) *ManagerImpl
//獲取pm.basePath
func (pm *ManagerImpl) BasePath() string
//獲取pm.config.policies,即map[string]Policy中Key列表
func (pm *ManagerImpl) PolicyNames() []string
//獲取指定路徑的子管理器
func (pm *ManagerImpl) Manager(path []string) (Manager, bool)
//獲取pm.config.policies[relpath]
//獲取Policy
func (pm *ManagerImpl) GetPolicy(id string) (Policy, bool)
func (pm *ManagerImpl) BeginPolicyProposals(tx interface{}, groups []string) ([]Proposer, error)
func (pm *ManagerImpl) RollbackProposals(tx interface{})
func (pm *ManagerImpl) PreCommit(tx interface{}) error
func (pm *ManagerImpl) CommitProposals(tx interface{})
func (pm *ManagerImpl) ProposePolicy(tx interface{}, key string, configPolicy *cb.ConfigPolicy) (proto.Message, error)
//代碼在common/policies/policy.go
type implicitMetaPolicy struct {
conf *cb.ImplicitMetaPolicy
threshold int
subPolicies []Policy
}
//代碼在common/policies/implicitmeta.go
5、通道策略工具函數
type ImplicitMetaPolicy_Rule int32
const (
ImplicitMetaPolicy_ANY ImplicitMetaPolicy_Rule = 0 //任意
ImplicitMetaPolicy_ALL ImplicitMetaPolicy_Rule = 1 //所有
ImplicitMetaPolicy_MAJORITY ImplicitMetaPolicy_Rule = 2 //大多數
)
//代碼在protos/common/policies.pb.go
//構造cb.Policy
func ImplicitMetaPolicyWithSubPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigPolicy
func TemplateImplicitMetaPolicyWithSubPolicy(path []string, policyName string, subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigGroup
//調取TemplateImplicitMetaPolicyWithSubPolicy(path, policyName, policyName, rule)
func TemplateImplicitMetaPolicy(path []string, policyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigGroup
//任意,TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_ANY)
func TemplateImplicitMetaAnyPolicy(path []string, policyName string) *cb.ConfigGroup
//所有,TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_ALL)
func TemplateImplicitMetaAllPolicy(path []string, policyName string) *cb.ConfigGroup
//大多數,TemplateImplicitMetaPolicy(path, policyName, cb.ImplicitMetaPolicy_MAJORITY)
func TemplateImplicitMetaMajorityPolicy(path []string, policyName string) *cb.ConfigGroup
//代碼在common/policies/implicitmeta_util.go