区块链教程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
相关文章
|
29天前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
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
|
2月前
|
数据采集 监控 算法
区块链量化交易系统开发策略详细丨需求步骤丨案例设计丨规则玩法丨成熟源码
策略:建立数据采集系统,获取各种市场数据,包括交易数据、新闻情报、社交媒体消息等。
|
26天前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
|
1月前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
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.
|
7月前
|
区块链
区块链的发币流程技术分析
区块链现在是发展的如火如荼,很多人都想趁着这个风口,投入区块链创业的浪潮中。 那么我们该怎么做才能抓住这个机会呢? 进行区块链发币要求是很多的,主要有以下几个步骤。
|
7月前
|
安全 算法 区块链
区块链交易所开发技术说明:智能合约设计与实现步骤实现分析
智能合约是区块链技术的核心应用,其能够自动执行、验证和执行合同,并以可验证的方式进行操作。在区块链交易所中,智能合约扮演着重要的角色,它们保证了交易的透明性、效率和安全性。作为一名专业的交易所开发团队一员,在交易所开发这块拥有相对成熟的开发技术,目前已经有成熟的区块链交易所开发案例。本文将介绍如何设计和实现可靠的智能合约来支持区块链交易所。
|
7月前
|
区块链 安全 数据安全/隐私保护
区块链LP流动性SWAP博饼交易所系统开发分析模式
Web3在生态的每一个要素中,都体现出了去中心化的特点。
|
7月前
|
SQL 安全 区块链
交易所系统开发(案例项目)丨区块链交易所系统开发(稳定版)/成熟技术/步骤逻辑/源码教程
The development of a blockchain exchange system involves complex technologies and functions.
|
8月前
|
区块链
区块链DAO众筹资金模式合约开发源代码详情
// 众筹函数,向DAO众筹资金 function contribute() public { uint contributionAmount = (unitPrice * msg.value).div(10 ether); // 计算贡献金额,最小单位为0.01ETH
|
8月前
|
安全 Go 区块链
区块链游戏链游系统开发功能详情丨方案逻辑丨开发项目丨案例分析丨源码规则
 In recent years, with the continuous development of blockchain technology, NFTs (non homogeneous tokens) and DAPPs (decentralized applications) have emerged in the gaming industry.

热门文章

最新文章