fabric示例e2e_cli中network_setup.sh流程分析

fabric示例e2e_cli中network_setup.sh流程分析

network_setup.sh流程图

  1. 调用generateArtifacts.sh
  2. docker-compose up启动cli
  3. cli容器执行script.sh脚本

fabric示例e2e_cli中network_setup.sh流程分析_第1张图片

network_setup.sh函数分析

validateArgs分析

function validateArgs () {
    #如果参数为空,报错退出
    if [ -z "${UP_DOWN}" ]; then
        echo "Option up / down / restart not mentioned"
        printHelp
        exit 1
    fi
    if [ -z "${CH_NAME}" ]; then
    #如果第二个参数为空,创建默认名称为'mychannel'的channel
        echo "setting to default channel 'mychannel'"
        CH_NAME=mychannel
    fi
}

函数networkUp分析

  1. 调用generateArtifacts.sh
  2. 调用docker-compose启动容器
function networkUp () {
    #判断是否存在crypto-config,如果存在,表示
    #如果不存在,则执行generateArtifacts.sh脚本
    if [ -f "./crypto-config" ]; then
      echo "crypto-config directory already exists."
    else
      #Generate all the artifacts that includes org certs, orderer genesis block,
      # channel configuration transaction
      source generateArtifacts.sh $CH_NAME
    fi

    #是否传入了第4个参数为couchdb,如果是启动参数带上couchdb对应的docker-compose模板文件
    #实际命令:CHANNEL_NAME=mychannel TIMEOUT=10000 docker-compose -f docker-compose-cli.yaml -f COMPOSE_FILE_COUCH=docker-compose-couch.yaml up -d 2>&1
    #如果不是couchdb,则不带上couchdb对应的yaml
    #实际命令为:CHANNEL_NAME=mychannel TIMEOUT=10000 docker-compose -f docker-compose-cli.yaml up -d 2>&1
    if [ "${IF_COUCHDB}" == "couchdb" ]; then
      CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1
    else
      CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE up -d 2>&1
    fi

    #判断执行的返回值,如果不为0,输出错误,并且退出
    if [ $? -ne 0 ]; then
    echo "ERROR !!!! Unable to pull the images "
    exit 1
    fi

    #查看容器cli的实时日志
    docker logs -f cli
}

generateArtifacts.sh函数分析

generateCerts
replacePrivateKey
generateChannelArtifacts

函数generateCerts分析
## Generates Org certs using cryptogen tool
function generateCerts (){
    #ubuntu上OS_ARCH为linux-amd64
    #OS X上OS_ARCH为darwin-amd64
    #fabric目录对应的子目录查找文件cryptogen
    #如果该文件存在
    #如果不存在执行make命令生成。不过这个make
    CRYPTOGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/cryptogen

    if [ -f "$CRYPTOGEN" ]; then
            echo "Using cryptogen -> $CRYPTOGEN"
    else
        echo "Building cryptogen"
        make -C $FABRIC_ROOT release
    fi

    #实际命令cryptogen generate --config=./crypto-config.yaml
    #TODO待完善
    echo
    echo "##########################################################"
    echo "##### Generate certificates using cryptogen tool #########"
    echo "##########################################################"
    $CRYPTOGEN generate --config=./crypto-config.yaml
    echo
}
replacePrivateKey
## Using docker-compose template replace private key file names with constants
function replacePrivateKey () {
    #如果是OS X系统参数为-it,否则为-i
    #或者-i前面加上"" -i也可以避免在OS X上报错
    ARCH=`uname -s | grep Darwin`
    if [ "$ARCH" == "Darwin" ]; then
        OPTS="-it"
    else
        OPTS="-i"
    fi

    #拷贝docker-compose-e2e-template.yaml文件,并且新文件命名为docker-compose-e2e.yaml
    cp docker-compose-e2e-template.yaml docker-compose-e2e.yaml

        #获取crypto-config/peerOrganizations/org1.example.com/ca/目录下_sk结尾的文件名
        #把上述文件中的CA1_PRIVATE_KEY替换成上述文件名
        #-i直接修改文本文件内容
        #s替换指定字符,使用后缀 /g 标记会替换每一行中的所有匹配
        CURRENT_DIR=$PWD
        cd crypto-config/peerOrganizations/org1.example.com/ca/
        PRIV_KEY=$(ls *_sk)
        cd $CURRENT_DIR
        sed $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
        cd crypto-config/peerOrganizations/org2.example.com/ca/
        PRIV_KEY=$(ls *_sk)
        cd $CURRENT_DIR
        sed $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
}
generateChannelArtifacts
## Generate orderer genesis block , channel configuration transaction and anchor peer update transactions
function generateChannelArtifacts() {
    #ubuntu上OS_ARCH为linux-amd64
    #OS X上OS_ARCH为darwin-amd64
    #fabric目录对应的子目录查找文件cryptogen
    #如果该文件存在
    #如果不存在执行make命令生成
    CONFIGTXGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/configtxgen
    if [ -f "$CONFIGTXGEN" ]; then
            echo "Using configtxgen -> $CONFIGTXGEN"
    else
        echo "Building configtxgen"
        make -C $FABRIC_ROOT release
    fi

    #使用工具configtxgen生成创世区块
    ##profile指定configtx.yaml中的配置项
    ##outputBlock执行生成文件路径,protobuf序列化格式
    ##可以用工具以json格式查看,configtxgen -inspectBlock genesis.block
    echo "##########################################################"
    echo "#########  Generating Orderer Genesis block ##############"
    echo "##########################################################"
    # Note: For some unknown reason (at least for now) the block file can't be
    # named orderer.genesis.block or the orderer will fail to launch!
    $CONFIGTXGEN -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

    ##使用工具configtxgen生成channel.tx
    ##outputCreateChannelTx指定生成的文件路径,protobuf序列化格式
    ##可以用工具以json格式查看,configtxgen -inspectChannelCreateTx channel.tx
    echo
    echo "#################################################################"
    echo "### Generating channel configuration transaction 'channel.tx' ###"
    echo "#################################################################"
    $CONFIGTXGEN -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

    #使用工具configtxgen生成Org1MSPanchors.tx
    echo
    echo "#################################################################"
    echo "#######    Generating anchor peer update for Org1MSP   ##########"
    echo "#################################################################"
    $CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

    #使用工具configtxgen生成Org2MSPanchors.tx
    echo
    echo "#################################################################"
    echo "#######    Generating anchor peer update for Org2MSP   ##########"
    echo "#################################################################"
    $CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
    echo
}

docker-compose启动容器

启动命令
CHANNEL_NAME=mychannel TIMEOUT=10000 docker-compose -f docker-compose-cli.yaml up -d 2>&1
script.sh分析
  1. 创建通道
  2. 加入通道
  3. 设置anchor
  4. 安装chaincode
  5. 实例化chaincode
  6. 查询/调用 chaincode

fabric示例e2e_cli中network_setup.sh流程分析_第2张图片

你可能感兴趣的:(hyperledger,fabric)