区块链教程Fabric1.0源代码分析policy(背书策略)-兄弟连区块链

简介:

  区块链教程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
相关文章
|
5月前
|
存储 供应链 监控
区块链技术在供应链管理中的应用与前景分析
随着信息化时代的到来,供应链管理面临着越来越多的挑战和机遇。本文主要探讨了区块链技术在供应链管理中的应用,以及未来的发展前景。通过对区块链技术的特点和优势进行分析,结合实际案例和趋势展望,展示了区块链技术在提升供应链透明度、效率和安全性方面的潜力,以及未来发展的可能方向。
|
5月前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
Developing a blockchain points mall system involves multiple aspects such as blockchain technology, smart contracts, front-end development, and business logic design. The following is the general process for developing a blockchain points mall system
|
5月前
|
存储 算法 API
面向企业的区块链教程(一)(2)
面向企业的区块链教程(一)
103 6
|
2月前
|
安全 区块链
Massa Layer 1区块链 POS 安全性分析
Massa Labs 回应 Certik 的挑战,通过严格的数学分析证明了其权益证明系统的安全性,抵抗了潜在攻击者试图操纵随机抽签的企图。
56 0
Massa Layer 1区块链 POS 安全性分析
|
5月前
|
存储 供应链 安全
基于区块链技术的智能合约安全性分析
【5月更文挑战第31天】本文深入探讨了区块链技术中智能合约的安全性问题,通过分析现有智能合约的安全漏洞和攻击手段,提出了一系列增强智能合约安全性的策略。文章首先介绍了区块链和智能合约的基本概念,随后详细讨论了智能合约面临的安全挑战,包括代码漏洞、重入攻击等问题,并对比分析了不同平台下智能合约的安全性差异。最后,文章提出了一系列提高智能合约安全性的建议,旨在为区块链应用的健康发展提供参考。
|
4月前
|
存储 安全 区块链
元宇宙与区块链技术的关系可以从多个角度进行阐述。以下是对这两者之间关系的详细分析
**元宇宙:虚拟世界融合现实元素,强调交互与沉浸;区块链:去中心化、安全的分布式账本。两者结合,区块链确保元宇宙中虚拟资产安全、支付高效、身份验证私密、治理透明,支撑其经济体系与用户信任,驱动未来发展。**
|
5月前
|
存储 算法 安全
区块链系统开发技术规则分析
区块链核心技术包括:1) 哈希算法,利用单向函数将任意数据转化为固定长度代码,确保安全验证;2) 非对称加密,使用公钥和私钥一对进行加密解密,保证信息安全;3) 共识机制,如PoW、PoS、DPoS等,实现快速交易验证和确认;4) 智能合约,自动执行的可信代码,一旦编写即不可更改,用于自动化交易;5) 分布式存储,将数据分散存储在网络各处,涵盖结构化、非结构化和半结构化数据。
|
4月前
|
区块链
近期区块链市场趋势分析
**区块链市场趋势摘要:** - 跨链技术成熟,提升互操作性,助力区块链网络融合。 - DeFi持续繁荣,智能合约与AMM创新活跃,市场竞争驱动市场壮大。 - NFT市场多样化,拓展至游戏、音乐等领域,实用性增强。 - 区块链寻求绿色转型,通过PoS共识与绿色能源减少能耗。 - 技术模块化、可组合性提升,降低成本,增强系统灵活性。 这些趋势展现区块链潜力,带来机遇与挑战,促进行业创新。
|
5月前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
614 15
|
5月前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
Developing blockchain game systems has been a highly anticipated field in recent years. By combining blockchain technology and game mechanics, players can enjoy a brand new gaming experience and higher game credibility.