博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
区块链教程Fabric1.0源代码分析policy(背书策略
阅读量:6273 次
发布时间:2019-06-22

本文共 6720 字,大约阅读时间需要 22 分钟。

  区块链教程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.PolicyCheckerfunc (f *defaultFactory) NewPolicyChecker() policy.PolicyChecker {    return policy.NewPolicyChecker(        peer.NewChannelPolicyManagerGetter(), //&channelPolicyManagerGetter{}        mgmt.GetLocalMSP(),        mgmt.NewLocalMSPPrincipalGetter(),    )}//获取policy.PolicyChecker,即调用policy.GetPolicyChecker()func GetPolicyChecker() policy.PolicyCheckerfunc 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 //委托人}//构造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代码如下:

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}//构造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
type implicitMetaPolicy struct {    conf        *cb.ImplicitMetaPolicy    threshold   int    subPolicies []Policy}//代码在common/policies/implicitmeta.go

5、通道策略工具函数

type ImplicitMetaPolicy_Rule int32const (    ImplicitMetaPolicy_ANY      ImplicitMetaPolicy_Rule = 0 //任意    ImplicitMetaPolicy_ALL      ImplicitMetaPolicy_Rule = 1 //所有    ImplicitMetaPolicy_MAJORITY ImplicitMetaPolicy_Rule = 2 //大多数)//代码在protos/common/policies.pb.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

转载于:https://blog.51cto.com/14041296/2313968

你可能感兴趣的文章
机智云开源框架初始化顺序
查看>>
Spark修炼之道(进阶篇)——Spark入门到精通:第五节 Spark编程模型(二)
查看>>
一线架构师实践指南:云时代下双活零切换的七大关键点
查看>>
ART世界探险(19) - 优化编译器的编译流程
查看>>
玩转Edas应用部署
查看>>
music-音符与常用记号
查看>>
sql操作命令
查看>>
zip 数据压缩
查看>>
Python爬虫学习系列教程
查看>>
【数据库优化专题】MySQL视图优化(二)
查看>>
【转载】每个程序员都应该学习使用Python或Ruby
查看>>
mongoose crud
查看>>
[Head First设计模式]山西面馆中的设计模式——装饰者模式
查看>>
PHP高级编程之守护进程,实现优雅重启
查看>>
PHP字符编码转换类3
查看>>
【2016阿里安全峰会】解读安全人才缺乏困境破解之法【附PDF下载】
查看>>
50条大牛C++编程开发学习建议
查看>>
rsync同步服务配置手记
查看>>
Android下创建一个sqlite数据库
查看>>
数组<=>xml 相互转换
查看>>