區塊鏈教程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
相關文章
- 區塊鏈教程Fabric1.0原始碼分析流言演算法Gossip服務端一兄弟連區塊鏈教程區塊鏈原始碼演算法Go服務端
- 區塊鏈教程Fabric1.0原始碼分析流言演算法Gossip服務端二-兄弟連區塊鏈原始碼演算法Go服務端
- 兄弟連區塊鏈教程Fabric1.0原始碼分析ECDSA橢圓曲線數字簽名演算法區塊鏈原始碼演算法
- 區塊鏈教程、區塊鏈指南、區塊鏈中文手冊、區塊鏈原理區塊鏈
- 兄弟連區塊鏈教程區塊鏈背後的資訊保安2DES、3DES加密演算法原理二區塊鏈3D加密演算法
- 從區塊鏈公司角度,分析區塊鏈錢包區塊鏈
- 區塊鏈100講:區塊鏈為什麼叫“區塊”“鏈”?區塊鏈
- 區塊鏈技術開發主鏈區塊鏈的應用分析區塊鏈
- 區塊鏈入門教程區塊鏈
- 區塊鏈系列1-區塊鏈概述區塊鏈
- 區塊鏈知識,區塊鏈簡史區塊鏈
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- 區塊鏈區塊鏈
- 區塊鏈專案白皮書,區塊鏈包裝策劃宣發區塊鏈
- 區塊鏈開發_建立區塊鏈公鏈,聯盟鏈,私有鏈區塊鏈
- 區塊鏈 2.0:房地產區塊鏈(四)區塊鏈
- 1.3 初識區塊鏈:區塊鏈分類區塊鏈
- 區塊鏈入門 -- 02 區塊鏈介紹區塊鏈
- 區塊鏈以及區塊鏈技術總結區塊鏈
- 區塊鏈投票系統開發方案,區塊鏈投票系統開發原始碼區塊鏈原始碼
- 兄弟連區塊鏈教程區塊鏈資訊保安3橢圓曲線加解密及簽名演算法的技術原理二區塊鏈解密演算法
- 轉:區塊鏈入門教程區塊鏈
- “區塊”和“鏈”的火花,區塊鏈到底為何物區塊鏈
- 區塊鏈大火?區塊鏈究竟是什麼?區塊鏈
- 區塊鏈安全————區塊鏈技術安全討論區塊鏈
- 區塊鏈專案分析-Dataeum區塊鏈
- 區塊鏈商城開發正式版丨區塊鏈商城系統開發技術原理丨區塊鏈商城原始碼平臺區塊鏈原始碼
- 以太坊原始碼分析(21)chain_indexer 區塊鏈索引原始碼AIIndex區塊鏈索引
- 區塊鏈原理區塊鏈
- 2019 騰訊區塊鏈白皮書:產業區塊鏈的破局之路區塊鏈產業
- 《湖南省區塊鏈白皮書》釋出:區塊鏈是湖南優勢產業區塊鏈產業
- 區塊鏈主鏈開發規則及原始碼示例區塊鏈原始碼
- 什麼是區塊鏈,簡單瞭解區塊鏈區塊鏈
- 區塊鏈特徵與區塊鏈技術應用落地區塊鏈特徵
- 區塊鏈公司談區塊鏈技術最新應用區塊鏈
- 區塊鏈開發公司區塊鏈與產業變革區塊鏈產業
- 區塊鏈技術公司談當婚姻遇到區塊鏈區塊鏈
- 量子計算與區塊鏈碰撞後——量子區塊鏈區塊鏈