Fabric 1.0源代码分析(39) policy(背书策略)
生活随笔
收集整理的這篇文章主要介紹了
Fabric 1.0源代码分析(39) policy(背书策略)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# 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接口定義
```gotype PolicyCheckerFactory interface {NewPolicyChecker() PolicyChecker //構造PolicyChecker實例}
var pcFactory PolicyCheckerFactory //全局變量定義及賦值函數func RegisterPolicyCheckerFactory(f PolicyCheckerFactory) {pcFactory = f}//代碼在core/policy/policy.go```
### 2.2、PolicyCheckerFactory接口默認實現
```gotype defaultFactory struct{}
//構造policy.PolicyCheckerfunc (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() { //初始化全局變量pcFactorypolicy.RegisterPolicyCheckerFactory(&defaultFactory{})}```
## 3、PolicyChecker接口定義及實現
### 3.1、PolicyChecker接口定義
```gotype PolicyChecker interface {CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) errorCheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) errorCheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error}//代碼在core/policy/policy.go```
### 3.2、PolicyChecker接口實現
PolicyChecker接口實現,即policyChecker結構體及方法。
```gotype policyChecker struct {channelPolicyManagerGetter policies.ChannelPolicyManagerGetter //通道策略管理器localMSP msp.IdentityDeserializer //身份principalGetter mgmt.MSPPrincipalGetter //委托人}
//構造policyCheckerfunc NewPolicyChecker(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localMSP msp.IdentityDeserializer, principalGetter mgmt.MSPPrincipalGetter) PolicyChecker//檢查簽名提案是否符合通道策略func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) errorfunc (p *policyChecker) CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error//檢查簽名數據是否符合通道策略,獲取策略并調取policy.Evaluate(sd)func (p *policyChecker) CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) errorfunc GetPolicyChecker() PolicyChecker //pcFactory.NewPolicyChecker()//代碼在core/policy/policy.go```
func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error代碼如下:
```gofunc (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) //獲取proposalheader, err := utils.GetHeader(proposal.Header)shdr, err := utils.GetSignatureHeader(header.SignatureHeader) //SignatureHeadersd := []*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接口定義
```gotype ChannelPolicyManagerGetter interface {Manager(channelID string) (Manager, bool)}//代碼在common/policies/policy.go```
### 4.2、ChannelPolicyManagerGetter接口實現
ChannelPolicyManagerGetter接口實現,即ManagerImpl結構體及方法。
```gotype ManagerImpl struct {parent *ManagerImplbasePath stringfqPrefix stringproviders map[int32]Provider //type Provider interfaceconfig *policyConfig //type policyConfig structpendingConfig map[interface{}]*policyConfig //type policyConfig structpendingLock sync.RWMutexSuppressSanityLogMessages bool}
type Provider interface {NewPolicy(data []byte) (Policy, proto.Message, error)}
type policyConfig struct {policies map[string]Policy //type Policy interfacemanagers map[string]*ManagerImplimps []*implicitMetaPolicy}
type Policy interface {//對給定的簽名數據,按規則檢驗確認是否符合約定的條件Evaluate(signatureSet []*cb.SignedData) error}
//構造ManagerImplfunc NewManagerImpl(basePath string, providers map[int32]Provider) *ManagerImpl//獲取pm.basePathfunc (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]//獲取Policyfunc (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{}) errorfunc (pm *ManagerImpl) CommitProposals(tx interface{})func (pm *ManagerImpl) ProposePolicy(tx interface{}, key string, configPolicy *cb.ConfigPolicy) (proto.Message, error)//代碼在common/policies/policy.go```
```gotype implicitMetaPolicy struct {conf *cb.ImplicitMetaPolicythreshold intsubPolicies []Policy}//代碼在common/policies/implicitmeta.go```
## 5、通道策略工具函數
```gotype ImplicitMetaPolicy_Rule int32const (ImplicitMetaPolicy_ANY ImplicitMetaPolicy_Rule = 0 //任意ImplicitMetaPolicy_ALL ImplicitMetaPolicy_Rule = 1 //所有ImplicitMetaPolicy_MAJORITY ImplicitMetaPolicy_Rule = 2 //大多數)//代碼在protos/common/policies.pb.go```
```go//構造cb.Policyfunc ImplicitMetaPolicyWithSubPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigPolicyfunc 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```
## 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接口定義
```gotype PolicyCheckerFactory interface {NewPolicyChecker() PolicyChecker //構造PolicyChecker實例}
var pcFactory PolicyCheckerFactory //全局變量定義及賦值函數func RegisterPolicyCheckerFactory(f PolicyCheckerFactory) {pcFactory = f}//代碼在core/policy/policy.go```
### 2.2、PolicyCheckerFactory接口默認實現
```gotype defaultFactory struct{}
//構造policy.PolicyCheckerfunc (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() { //初始化全局變量pcFactorypolicy.RegisterPolicyCheckerFactory(&defaultFactory{})}```
## 3、PolicyChecker接口定義及實現
### 3.1、PolicyChecker接口定義
```gotype PolicyChecker interface {CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) errorCheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) errorCheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error}//代碼在core/policy/policy.go```
### 3.2、PolicyChecker接口實現
PolicyChecker接口實現,即policyChecker結構體及方法。
```gotype policyChecker struct {channelPolicyManagerGetter policies.ChannelPolicyManagerGetter //通道策略管理器localMSP msp.IdentityDeserializer //身份principalGetter mgmt.MSPPrincipalGetter //委托人}
//構造policyCheckerfunc NewPolicyChecker(channelPolicyManagerGetter policies.ChannelPolicyManagerGetter, localMSP msp.IdentityDeserializer, principalGetter mgmt.MSPPrincipalGetter) PolicyChecker//檢查簽名提案是否符合通道策略func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) errorfunc (p *policyChecker) CheckPolicyNoChannel(policyName string, signedProp *pb.SignedProposal) error//檢查簽名數據是否符合通道策略,獲取策略并調取policy.Evaluate(sd)func (p *policyChecker) CheckPolicyBySignedData(channelID, policyName string, sd []*common.SignedData) errorfunc GetPolicyChecker() PolicyChecker //pcFactory.NewPolicyChecker()//代碼在core/policy/policy.go```
func (p *policyChecker) CheckPolicy(channelID, policyName string, signedProp *pb.SignedProposal) error代碼如下:
```gofunc (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) //獲取proposalheader, err := utils.GetHeader(proposal.Header)shdr, err := utils.GetSignatureHeader(header.SignatureHeader) //SignatureHeadersd := []*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接口定義
```gotype ChannelPolicyManagerGetter interface {Manager(channelID string) (Manager, bool)}//代碼在common/policies/policy.go```
### 4.2、ChannelPolicyManagerGetter接口實現
ChannelPolicyManagerGetter接口實現,即ManagerImpl結構體及方法。
```gotype ManagerImpl struct {parent *ManagerImplbasePath stringfqPrefix stringproviders map[int32]Provider //type Provider interfaceconfig *policyConfig //type policyConfig structpendingConfig map[interface{}]*policyConfig //type policyConfig structpendingLock sync.RWMutexSuppressSanityLogMessages bool}
type Provider interface {NewPolicy(data []byte) (Policy, proto.Message, error)}
type policyConfig struct {policies map[string]Policy //type Policy interfacemanagers map[string]*ManagerImplimps []*implicitMetaPolicy}
type Policy interface {//對給定的簽名數據,按規則檢驗確認是否符合約定的條件Evaluate(signatureSet []*cb.SignedData) error}
//構造ManagerImplfunc NewManagerImpl(basePath string, providers map[int32]Provider) *ManagerImpl//獲取pm.basePathfunc (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]//獲取Policyfunc (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{}) errorfunc (pm *ManagerImpl) CommitProposals(tx interface{})func (pm *ManagerImpl) ProposePolicy(tx interface{}, key string, configPolicy *cb.ConfigPolicy) (proto.Message, error)//代碼在common/policies/policy.go```
```gotype implicitMetaPolicy struct {conf *cb.ImplicitMetaPolicythreshold intsubPolicies []Policy}//代碼在common/policies/implicitmeta.go```
## 5、通道策略工具函數
```gotype ImplicitMetaPolicy_Rule int32const (ImplicitMetaPolicy_ANY ImplicitMetaPolicy_Rule = 0 //任意ImplicitMetaPolicy_ALL ImplicitMetaPolicy_Rule = 1 //所有ImplicitMetaPolicy_MAJORITY ImplicitMetaPolicy_Rule = 2 //大多數)//代碼在protos/common/policies.pb.go```
```go//構造cb.Policyfunc ImplicitMetaPolicyWithSubPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.ConfigPolicyfunc 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```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機資料
區塊鏈技術交流QQ群:756146052??備注:CSDN
尹成學院微信:備注:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機資料
區塊鏈技術交流QQ群:756146052??備注:CSDN
尹成學院微信:備注:CSDN
總結
以上是生活随笔為你收集整理的Fabric 1.0源代码分析(39) policy(背书策略)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明星证券化之殇|一点财经
- 下一篇: iOS-底层原理 06: cls 与类的