区块链教程Fabric1.0源代码分析scc(系统链码)cscc(通道相关)-兄弟连区块链教程

简介:

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

Fabric 1.0源代码笔记 之 scc(系统链码) #cscc(通道相关)

1、cscc概述

cscc代码在core/scc/cscc/configure.go。

2、PeerConfiger结构体

type PeerConfiger struct {
    policyChecker policy.PolicyChecker
}
//代码在core/scc/cscc/configure.go

3、Init方法

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方法

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、其他方法

//校验创世区块
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

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)代码如下:

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)代码如下:

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:

type initializer struct {
    *resources
    ppr *policyProposerRoot
}
//代码在common/configtx/initializer.go
相关文章
|
29天前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
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
|
6月前
|
开发框架 .NET 区块链
Hyperledger fabric部署链码(五)初始化与链码升级
fabric部署chaincode-go(智能合约)系列之五
|
26天前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
|
1月前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
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.
|
6月前
|
JavaScript 测试技术 Go
Hyperledger fabric部署链码(一)打包链码
fabric部署chaincode-go(智能合约)系列之一
|
6月前
|
JSON 区块链 数据格式
Hyperledger fabric部署链码(四)提交链码定义到channel
fabric部署chaincode-go(智能合约)系列之四
|
6月前
|
测试技术 API 区块链
Hyperledger fabric部署链码(三)批准链码定义
fabric部署chaincode-go(智能合约)系列之三
|
6月前
|
区块链
Hyperledger fabric部署链码(二)安装链码到fabric
fabric部署chaincode-go(智能合约)系列之二
|
7月前
|
区块链
区块链的发币流程技术分析
区块链现在是发展的如火如荼,很多人都想趁着这个风口,投入区块链创业的浪潮中。 那么我们该怎么做才能抓住这个机会呢? 进行区块链发币要求是很多的,主要有以下几个步骤。
|
7月前
|
安全 算法 区块链
区块链交易所开发技术说明:智能合约设计与实现步骤实现分析
智能合约是区块链技术的核心应用,其能够自动执行、验证和执行合同,并以可验证的方式进行操作。在区块链交易所中,智能合约扮演着重要的角色,它们保证了交易的透明性、效率和安全性。作为一名专业的交易所开发团队一员,在交易所开发这块拥有相对成熟的开发技术,目前已经有成熟的区块链交易所开发案例。本文将介绍如何设计和实现可靠的智能合约来支持区块链交易所。

热门文章

最新文章