Fabric 1.0原始碼分析(42)scc(系統鏈碼) #cscc(通道相關)
# Fabric 1.0原始碼筆記 之 scc(系統鏈碼) #cscc(通道相關)
## 1、cscc概述
cscc程式碼在core/scc/cscc/configure.go。
## 2、PeerConfiger結構體
```go
type PeerConfiger struct {
policyChecker policy.PolicyChecker
}
//程式碼在core/scc/cscc/configure.go
```
## 3、Init方法
```go
func (e *PeerConfiger) Init(stub shim.ChaincodeStubInterface) pb.Response {
//初始化策略檢查器,用於訪問控制
e.policyChecker = policy.NewPolicyChecker(
peer.NewChannelPolicyManagerGetter(),
mgmt.GetLocalMSP(),
mgmt.NewLocalMSPPrincipalGetter(),
)
return shim.Success(nil)
}
//程式碼在core/scc/cscc/configure.go
```
## 4、Invoke方法
```go
func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
//args[0]為JoinChain或GetChannels
args := stub.GetArgs()
fname := string(args[0]) //Invoke function
sp, err := stub.GetSignedProposal() //獲取SignedProposal
switch fname {
case JoinChain: //加入通道
//此處args[1]為創世區塊
block, err := utils.GetBlockFromBlockBytes(args[1])
cid, err := utils.GetChainIDFromBlock(block)
err := validateConfigBlock(block)
err = e.policyChecker.CheckPolicyNoChannel(mgmt.Admins, sp)
return joinChain(cid, block)
case GetConfigBlock:
err = e.policyChecker.CheckPolicy(string(args[1]), policies.ChannelApplicationReaders, sp)
return getConfigBlock(args[1])
case GetChannels:
err = e.policyChecker.CheckPolicyNoChannel(mgmt.Members, sp)
return getChannels()
}
}
//程式碼在core/scc/cscc/configure.go
```
## 5、其他方法
```go
//校驗創世區塊
func validateConfigBlock(block *common.Block) error
func joinChain(chainID string, block *common.Block) pb.Response
func getConfigBlock(chainID []byte) pb.Response
func getChannels() pb.Response
//程式碼在core/scc/cscc/configure.go
```
### 5.1、joinChain
```go
func joinChain(chainID string, block *common.Block) pb.Response {
err := peer.CreateChainFromBlock(block) //建立chain
peer.InitChain(chainID)
err := producer.SendProducerBlockEvent(block)
return shim.Success(nil)
}
//程式碼在core/scc/cscc/configure.go
```
#### 5.1.1、建立Chain(或channel)
peer.CreateChainFromBlock(block)程式碼如下:
```go
func CreateChainFromBlock(cb *common.Block) error {
cid, err := utils.GetChainIDFromBlock(cb) //獲取ChainID
var l ledger.PeerLedger
l, err = ledgermgmt.CreateLedger(cb) //建立Ledger
return createChain(cid, l, cb)
}
//程式碼在core/peer/peer.go
```
createChain(cid, l, cb)程式碼如下:
```go
func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
envelopeConfig, err := utils.ExtractEnvelope(cb, 0) //獲取配置Envelope
configtxInitializer := configtx.NewInitializer() //type initializer struct
gossipEventer := service.GetGossipService().NewConfigEventer() //獲取gossipServiceInstance
gossipCallbackWrapper := func(cm configtxapi.Manager) {
ac, ok := configtxInitializer.ApplicationConfig()
if !ok {
// TODO, handle a missing ApplicationConfig more gracefully
ac = nil
}
gossipEventer.ProcessConfigUpdate(&chainSupport{
Manager: cm,
Application: ac,
})
service.GetGossipService().SuspectPeers(func(identity api.PeerIdentityType) bool {
// TODO: this is a place-holder that would somehow make the MSP layer suspect
// that a given certificate is revoked, or its intermediate CA is revoked.
// In the meantime, before we have such an ability, we return true in order
// to suspect ALL identities in order to validate all of them.
return true
})
}
trustedRootsCallbackWrapper := func(cm configtxapi.Manager) {
updateTrustedRoots(cm)
}
configtxManager, err := configtx.NewManagerImpl(
envelopeConfig,
configtxInitializer,
[]func(cm configtxapi.Manager){gossipCallbackWrapper, trustedRootsCallbackWrapper},
)
if err != nil {
return err
}
// TODO remove once all references to mspmgmt are gone from peer code
mspmgmt.XXXSetMSPManager(cid, configtxManager.MSPManager())
ac, ok := configtxInitializer.ApplicationConfig()
if !ok {
ac = nil
}
cs := &chainSupport{
Manager: configtxManager,
Application: ac, // TODO, refactor as this is accessible through Manager
ledger: ledger,
}
c := committer.NewLedgerCommitterReactive(ledger, txvalidator.NewTxValidator(cs), func(block *common.Block) error {
chainID, err := utils.GetChainIDFromBlock(block)
if err != nil {
return err
}
return SetCurrConfigBlock(block, chainID)
})
ordererAddresses := configtxManager.ChannelConfig().OrdererAddresses()
if len(ordererAddresses) == 0 {
return errors.New("No ordering service endpoint provided in configuration block")
}
service.GetGossipService().InitializeChannel(cs.ChainID(), c, ordererAddresses)
chains.Lock()
defer chains.Unlock()
chains.list[cid] = &chain{
cs: cs,
cb: cb,
committer: c,
}
return nil
}
//程式碼在core/peer/peer.go
```
補充initializer:
```go
type initializer struct {
*resources
ppr *policyProposerRoot
}
//程式碼在common/configtx/initializer.go
```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(4)Chaincode(鏈碼)#platforms(鏈碼語言平臺)原始碼AIPlatform
- Fabric 1.0原始碼分析(8)configtx(配置交易) #genesis(系統通道創世區塊)原始碼
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(27) Orderer #configupdate(處理通道配置更新)原始碼
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(1)BCCSP(區塊鏈加密服務提供者)原始碼區塊鏈加密
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(7)configtx(配置交易) #configtxgen(生成通道配置)原始碼
- 區塊鏈教程Fabric1.0原始碼分析policy(背書策略)-兄弟連區塊鏈區塊鏈原始碼
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- Fabric 1.0原始碼分析(12)cryptogen(生成組織關係和身份證書)原始碼
- Fabric 1.0原始碼分析(24)MSP(成員關係服務提供者)原始碼
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Hyperledger Fabric系統鏈碼介紹
- Fabric 1.0原始碼分析(9)configtx(配置交易)體系介紹原始碼
- Fabric 1.0原始碼分析(2) blockfile(區塊檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(19) Ledger #statedb(狀態資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(21)Ledger #historydb(歷史資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(22)Ledger #blkstorage(block檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(32) Peer #peer node start命令實現原始碼