Fabric 1.0原始碼分析(11)consenter(共識外掛) #filter(過濾器)
# 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
相關文章
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Django(69)最好用的過濾器外掛Django-filterDjango過濾器Filter
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Servlet過濾器原始碼分析Servlet過濾器原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Filter過濾器Filter過濾器
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Safari網頁敏感文字過濾外掛:Profanity Filter for Mac網頁FilterMac
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- PHP 過濾器(Filter)PHP過濾器Filter
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- JavaWeb 中 Filter過濾器JavaWebFilter過濾器
- Filter過濾器的使用Filter過濾器
- Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)原始碼
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(38) Peer #BroadcastClient(Broadcast客戶端)原始碼ASTclient客戶端
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- 從零手寫實現 tomcat-11-filter 過濾器TomcatFilter過濾器
- filter過濾Filter
- 布隆過濾器(Bloom Filter)過濾器OOMFilter
- 布隆過濾器 Bloom Filter過濾器OOMFilter