Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)
# Fabric 1.0原始碼筆記 之 Orderer #multichain(多鏈支援包)
## 1、multichain概述
multichain程式碼集中在orderer/multichain目錄下,目錄結構如下:
* manager.go,Manager介面定義及實現。
* chainsupport.go,ChainSupport介面定義及實現。
* systemchain.go,system chain。
## 2、Manager介面定義及實現
### 2.1、Manager介面定義
用於鏈的建立和訪問。
```go
type Manager interface {
//獲取ChainSupport,以及判斷鏈是否存在
GetChain(chainID string) (ChainSupport, bool)
//獲取系統通道的通道ID
SystemChannelID() string
//支援通道建立請求
NewChannelConfig(envConfigUpdate *cb.Envelope) (configtxapi.Manager, error)
}
//程式碼在orderer/multichain/manager.go
```
### 2.2、Manager介面實現
Manager介面實現,即multiLedger結構體及方法。
```go
type multiLedger struct {
chains map[string]*chainSupport
consenters map[string]Consenter
ledgerFactory ledger.Factory
signer crypto.LocalSigner
systemChannelID string
systemChannel *chainSupport
}
type configResources struct {
configtxapi.Manager
}
type ledgerResources struct {
*configResources
ledger ledger.ReadWriter
}
//程式碼在orderer/multichain/manager.go
```
涉及方法如下:
```go
func (cr *configResources) SharedConfig() config.Orderer
//獲取配置交易Envelope
func getConfigTx(reader ledger.Reader) *cb.Envelope
//構造multiLedger
func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager
//獲取系統鏈ID
func (ml *multiLedger) SystemChannelID() string
//按chainID獲取ChainSupport
func (ml *multiLedger) GetChain(chainID string) (ChainSupport, bool)
//構造ledgerResources
func (ml *multiLedger) newLedgerResources(configTx *cb.Envelope) *ledgerResources
//建立新鏈
func (ml *multiLedger) newChain(configtx *cb.Envelope)
//通道或鏈的個數
func (ml *multiLedger) channelsCount() int
//支援建立新的通道
func (ml *multiLedger) NewChannelConfig(envConfigUpdate *cb.Envelope) (configtxapi.Manager, error)
//程式碼在orderer/multichain/manager.go
```
func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager程式碼如下:
```go
func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager {
ml := &multiLedger{
chains: make(map[string]*chainSupport),
ledgerFactory: ledgerFactory,
consenters: consenters,
signer: signer,
}
existingChains := ledgerFactory.ChainIDs()
for _, chainID := range existingChains {
rl, err := ledgerFactory.GetOrCreate(chainID)
configTx := getConfigTx(rl)
ledgerResources := ml.newLedgerResources(configTx)
chainID := ledgerResources.ChainID()
if _, ok := ledgerResources.ConsortiumsConfig(); ok { //系統鏈
chain := newChainSupport(createSystemChainFilters(ml, ledgerResources), ledgerResources, consenters, signer)
ml.chains[chainID] = chain
ml.systemChannelID = chainID
ml.systemChannel = chain
defer chain.start()
} else { //普通鏈
chain := newChainSupport(createStandardFilters(ledgerResources), ledgerResources, consenters, signer)
ml.chains[chainID] = chain
chain.start()
}
}
return ml
}
//程式碼在orderer/multichain/manager.go
```
## 3、ChainSupport介面定義及實現
### 3.1、ChainSupport介面定義
```go
type ChainSupport interface {
PolicyManager() policies.Manager //策略管理
Reader() ledger.Reader
Errored() <-chan struct{}
broadcast.Support
ConsenterSupport //嵌入ConsenterSupport介面
Sequence() uint64
//支援通道更新
ProposeConfigUpdate(env *cb.Envelope) (*cb.ConfigEnvelope, error)
}
type ConsenterSupport interface {
crypto.LocalSigner
BlockCutter() blockcutter.Receiver
SharedConfig() config.Orderer
CreateNextBlock(messages []*cb.Envelope) *cb.Block
WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block
ChainID() string
Height() uint64
}
type Consenter interface { //定義支援排序機制
HandleChain(support ConsenterSupport, metadata *cb.Metadata) (Chain, error)
}
type Chain interface {
//接受訊息
Enqueue(env *cb.Envelope) bool
Errored() <-chan struct{}
Start() //開始
Halt() //掛起
}
//程式碼在orderer/multichain/chainsupport.go
```
### 3.2、ChainSupport和ConsenterSupport介面實現
ChainSupport介面實現,即chainSupport結構體及方法。
```go
type chainSupport struct {
*ledgerResources
chain Chain
cutter blockcutter.Receiver
filters *filter.RuleSet
signer crypto.LocalSigner
lastConfig uint64
lastConfigSeq uint64
}
//程式碼在orderer/multichain/chainsupport.go
```
涉及方法如下:
```go
//構造chainSupport
func newChainSupport(filters *filter.RuleSet,ledgerResources *ledgerResources,consenters map[string]Consenter,signer crypto.LocalSigner,) *chainSupport
func createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSet
func createSystemChainFilters(ml *multiLedger, ledgerResources *ledgerResources) *filter.RuleSet
func (cs *chainSupport) start()
func (cs *chainSupport) NewSignatureHeader() (*cb.SignatureHeader, error)
func (cs *chainSupport) Sign(message []byte) ([]byte, error)
func (cs *chainSupport) Filters() *filter.RuleSet
func (cs *chainSupport) BlockCutter() blockcutter.Receiver
func (cs *chainSupport) Reader() ledger.Reader
func (cs *chainSupport) Enqueue(env *cb.Envelope) bool
func (cs *chainSupport) Errored() <-chan struct{}
//建立塊,調取ledger.CreateNextBlock(cs.ledger, messages)
func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Block
func (cs *chainSupport) addBlockSignature(block *cb.Block)
func (cs *chainSupport) addLastConfigSignature(block *cb.Block)
//寫入塊
func (cs *chainSupport) WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block
func (cs *chainSupport) Height() uint64
//程式碼在orderer/multichain/chainsupport.go
```
func (cs *chainSupport) WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block 程式碼如下:
```go
func (cs *chainSupport) WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block {
for _, committer := range committers {
committer.Commit()
}
cs.addBlockSignature(block)
cs.addLastConfigSignature(block)
err := cs.ledger.Append(block)//賬本追加塊
return block
}
//程式碼在orderer/multichain/chainsupport.go
```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(28) Orderer #localconfig(Orderer配置檔案定義)原始碼
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(27) Orderer #configupdate(處理通道配置更新)原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(4)Chaincode(鏈碼)#platforms(鏈碼語言平臺)原始碼AIPlatform
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Fabric 1.0原始碼分析(42)scc(系統鏈碼) #cscc(通道相關)原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(1)BCCSP(區塊鏈加密服務提供者)原始碼區塊鏈加密
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- 區塊鏈教程Fabric1.0原始碼分析policy(背書策略)-兄弟連區塊鏈區塊鏈原始碼
- Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)原始碼
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- 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原始碼分析(2) blockfile(區塊檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(7)configtx(配置交易) #configtxgen(生成通道配置)原始碼
- Fabric 1.0原始碼分析(9)configtx(配置交易)體系介紹原始碼
- Fabric 1.0原始碼分析(19) Ledger #statedb(狀態資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(21)Ledger #historydb(歷史資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(22)Ledger #blkstorage(block檔案儲存)原始碼BloC