Fabric 1.0原始碼分析(11)consenter(共識外掛) #filter(過濾器)

尹成發表於2018-05-20
# 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介面定義

```go
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(校驗是否為空過濾器)

```go
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(接受過濾器)

```go
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(配置交易合法性過濾器)

```go
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(交易大小過濾器)

```go
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(簽名資料校驗過濾器)

```go
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(系統鏈過濾器)

```go
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介面定義

```go
type Committer interface {
    Commit() //提交
    Isolated() bool //判斷交易是孤立的塊,或與其他交易混合的塊
}
//程式碼在orderer/common/filter/filter.go
```

### 3.2、noopCommitter

```go
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

```go
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

```go
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結構體及方法

```go
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工具函式

```go
//為普通 (非系統) 鏈建立過濾器集
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

```






網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章