区块链教程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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
区块链教程Fabric1.0源代码分析Peer DeliverClient(Deliver客户端)
兄弟连区块链教程Fabric1.0源代码分析Peer DeliverClient(Deliver客户端),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。 Fabric1.0源代码笔记之Peer DeliverClient(Deliver客户端) 1、DeliverClient概述 DeliverClient代码分布如下: peer/channel/deliverclient.go,deliverClientIntf接口定义及实现,以及DeliverClient工具函数。 protos/orderer/ab.pb.go,AtomicBroadcast_DeliverClient接口定义和实现。 2、deliverClientIntf接口定义及实现 2.1、DeliverClient工具函数 //构造deliverClient func newDeliverClient(conn *grpc.ClientConn, client ab.AtomicBroad...
-
下一篇
如何对第一个Vue.js组件进行单元测试 (上)
首先,为什么要单元测试组件? 单元测试是持续集成的关键。通过专注于小的、独立的实体,确保单元测试始终按预期运行,使代码更加可靠,你可以放心地迭代你的项目而不必担坏事儿。 单元测试不仅限于脚本。可以独立测试的任何东西都是可单元测试的,只要你遵循一些好的做法。这些实例包括单一责任、可预测性和松散耦合。 作为我们应用程序的可重用实体,Vue.js组件是单元测试的理想选择。我们将用不同的输入和交互测试做好的单个单元,并确保它始终按照我们的预期运行。 在开始之前 Vue CLI 3发布了。Vue Test Utils-官方的Vue.js单元测试实用程序库-已经成长为beta版。在第一篇教程中,我们使用了webpack-simple,一个不包含测试功能的原型模板。出于这些原因,最简单的方法是“擦干净黑板”并将项目从教程迁移到更新后的Vue.js安装。 我从第一个教程重新创建了项目,因此您可以直接从GitHub下载它。然后导航到解压缩的目录并安装依赖项。 注意:确保在继续之前安装Node.js: 运行项目: Vue Test Utils和Jest 在本教程中,我们将使用Vue Test Uti...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Docker容器配置,解决镜像无法拉取问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- CentOS7设置SWAP分区,小内存服务器的救世主
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作