区块链教程Fabric1.0源代码分析consenter#filter-兄弟连

简介:

  区块链教程Fabric1.0源代码分析consenter(共识插件)#filter(过滤器),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。

Fabric 1.0源代码笔记 之 consenter(共识插件) #filter(过滤器)

1、filter概述

filter代码分布在orderer/common/filter、orderer/common/configtxfilter、orderer/common/sizefilter、orderer/common/sigfilter、orderer/multichain目录下。

orderer/common/filter/filter.go,Rule接口定义及emptyRejectRule和acceptRule实现,Committer接口定义及noopCommitter实现,RuleSet结构体及方法。
orderer/common/configtxfilter目录,configFilter结构体(实现Rule接口)及configCommitter结构体(实现Committer接口)。
orderer/common/sizefilter目录,maxBytesRule结构体(实现Rule接口)。
orderer/multichain/chainsupport.go,filter工具函数。
orderer/multichain/systemchain.go,systemChainFilter结构体(实现Rule接口)及systemChainCommitter结构体(实现Committer接口)。

2、Rule接口定义及实现

2.1、Rule接口定义

type Action int
const (
    Accept = iota
    Reject
    Forward
)

type Rule interface { //定义一个过滤器函数, 它接受、拒绝或转发 (到下一条规则) 一个信封
    Apply(message *ab.Envelope) (Action, Committer)
}
//代码在orderer/common/filter/filter.go

2.2、emptyRejectRule(校验是否为空过滤器)

type emptyRejectRule struct{}
var EmptyRejectRule = Rule(emptyRejectRule{})

func (a emptyRejectRule) Apply(message *ab.Envelope) (Action, Committer) {
    if message.Payload == nil {
        return Reject, nil
    }
    return Forward, nil
}
//代码在orderer/common/filter/filter.go

2.3、acceptRule(接受过滤器)

type acceptRule struct{}
var AcceptRule = Rule(acceptRule{})

func (a acceptRule) Apply(message *ab.Envelope) (Action, Committer) {
    return Accept, NoopCommitter
}
//代码在orderer/common/filter/filter.go

2.4、configFilter(配置交易合法性过滤器)

type configFilter struct {
    configManager api.Manager
}

func NewFilter(manager api.Manager) filter.Rule //构造configFilter
//配置交易过滤器
func (cf *configFilter) Apply(message *cb.Envelope) (filter.Action, filter.Committer) {
    msgData, err := utils.UnmarshalPayload(message.Payload) //获取Payload
    chdr, err := utils.UnmarshalChannelHeader(msgData.Header.ChannelHeader) //获取ChannelHeader
    if chdr.Type != int32(cb.HeaderType_CONFIG) { //配置交易
        return filter.Forward, nil
    }
    configEnvelope, err := configtx.UnmarshalConfigEnvelope(msgData.Data) //获取configEnvelope
    err = cf.configManager.Validate(configEnvelope) //校验configEnvelope
    return filter.Accept, &configCommitter{
        manager:        cf.configManager,
        configEnvelope: configEnvelope,
    }
}
//代码在orderer/common/configtxfilter/filter.go

2.5、sizefilter(交易大小过滤器)

type maxBytesRule struct {
    support Support
}

func MaxBytesRule(support Support) filter.Rule //构造maxBytesRule
func (r *maxBytesRule) Apply(message *cb.Envelope) (filter.Action, filter.Committer) {
    maxBytes := r.support.BatchSize().AbsoluteMaxBytes
    if size := messageByteSize(message); size > maxBytes {
        return filter.Reject, nil
    }
    return filter.Forward, nil
}
//代码在orderer/common/sizefilter/sizefilter.go

2.6、sigFilter(签名数据校验过滤器)

type sigFilter struct {
    policySource  string
    policyManager policies.Manager
}

func New(policySource string, policyManager policies.Manager) filter.Rule //构造sigFilter
func (sf *sigFilter) Apply(message *cb.Envelope) (filter.Action, filter.Committer) {
    signedData, err := message.AsSignedData() //构造SignedData
    policy, ok := sf.policyManager.GetPolicy(sf.policySource) //获取策略
    err = policy.Evaluate(signedData) //校验策略
    if err == nil {
        return filter.Forward, nil
    }
    return filter.Reject, nil
}
//代码在orderer/common/sigfilter/sigfilter.go

2.7、systemChainFilter(系统链过滤器)

type systemChainFilter struct {
    cc      chainCreator
    support limitedSupport
}

func newSystemChainFilter(ls limitedSupport, cc chainCreator) filter.Rule //构造systemChainFilter
func (scf *systemChainFilter) Apply(env *cb.Envelope) (filter.Action, filter.Committer) {
    msgData := &cb.Payload{}
    err := proto.Unmarshal(env.Payload, msgData) //获取Payload
    chdr, err := utils.UnmarshalChannelHeader(msgData.Header.ChannelHeader)
    if chdr.Type != int32(cb.HeaderType_ORDERER_TRANSACTION) { //ORDERER_TRANSACTION
        return filter.Forward, nil
    }
    maxChannels := scf.support.SharedConfig().MaxChannelsCount()
    if maxChannels > 0 {
        if uint64(scf.cc.channelsCount()) > maxChannels {
            return filter.Reject, nil
        }
    }

    configTx := &cb.Envelope{}
    err = proto.Unmarshal(msgData.Data, configTx)
    err = scf.authorizeAndInspect(configTx)
    return filter.Accept, &systemChainCommitter{
        filter:   scf,
        configTx: configTx,
    }
}
//代码在orderer/multichain/systemchain.go

3、Committer接口定义及实现

3.1、Committer接口定义

type Committer interface {
    Commit() //提交
    Isolated() bool //判断交易是孤立的块,或与其他交易混合的块
}
//代码在orderer/common/filter/filter.go

3.2、noopCommitter

type noopCommitter struct{}
var NoopCommitter = Committer(noopCommitter{})

func (nc noopCommitter) Commit()        {}
func (nc noopCommitter) Isolated() bool { return false }
//代码在orderer/common/filter/filter.go

3.3、configCommitter

type configCommitter struct {
    manager        api.Manager
    configEnvelope *cb.ConfigEnvelope
}

func (cc *configCommitter) Commit() {
    err := cc.manager.Apply(cc.configEnvelope)
}

func (cc *configCommitter) Isolated() bool {
    return true
}
//代码在orderer/common/configtxfilter/filter.go

3.4、systemChainCommitter

type systemChainCommitter struct {
    filter   *systemChainFilter
    configTx *cb.Envelope
}

func (scc *systemChainCommitter) Isolated() bool {
    return true
}

func (scc *systemChainCommitter) Commit() {
    scc.filter.cc.newChain(scc.configTx)
}
//代码在orderer/multichain/systemchain.go

4、RuleSet结构体及方法

type RuleSet struct {
    rules []Rule
}

func NewRuleSet(rules []Rule) *RuleSet //构造RuleSet
func (rs *RuleSet) Apply(message *ab.Envelope) (Committer, error) {
    for _, rule := range rs.rules {
        action, committer := rule.Apply(message)
        switch action {
        case Accept: //接受
            return committer, nil
        case Reject: //拒绝
            return nil, fmt.Errorf("Rejected by rule: %T", rule)
        default:
        }
    }
    return nil, fmt.Errorf("No matching filter found")
}
//代码在orderer/common/filter/filter.go

5、filter工具函数

//为普通 (非系统) 链创建过滤器集
func createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSet {
    return filter.NewRuleSet([]filter.Rule{
        filter.EmptyRejectRule, //EmptyRejectRule
        sizefilter.MaxBytesRule(ledgerResources.SharedConfig()), //sizefilter
        sigfilter.New(policies.ChannelWriters, ledgerResources.PolicyManager()), //sigfilter
        configtxfilter.NewFilter(ledgerResources), //configtxfilter
        filter.AcceptRule, //AcceptRule
    })

}

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