区块链——Hyperledger Fabric2.3搭建测试网络

文章目录

      • 一、Hyperledger Fabric简介
      • 二、搭建基础环境
        • 1、安装docker以及docker-compose
        • 2、安装golang环境
      • 三、运行测试网络
        • 1、下载测试网络部署脚本
        • 2、运行脚本
        • 3、查看下载文件并把二进制文件加入变量
        • 4、启动测试网络
        • 5、创建通道
        • 6、在通道启动一个链码
        • 7、使用peer CLI与网络进行交互
        • 8、关闭网络
      • 四、使用认证机构建立网络
        • 1、使用CA标志启动网络


一、Hyperledger Fabric简介

Hyperledger Fabric 是一个开源的企业级许可分布式账本技术(Distributed Ledger Technology,DLT)平台,专为在企业环境中使用而设计,与其他流行的分布式账本或区块链平台相比,它有一些主要的区别。

一个主要区别是 Hyperledger 是在 Linux 基金会下建立的,该基金会本身在开放式治理的模式下培育开源项目的历史悠久且非常成功,发展了强大的可持续社区和繁荣的生态系统。Hyperledger 由多元化的技术指导委员会进行管理,Hyperledger Fabric 项目由多个组织的不同的维护人员管理。从第一次提交以来,它的开发社区已经发展到超过35个组织和近200个开发人员。

Fabric 具有高度模块化可配置的架构,可为各行各业的业务提供创新性、多样性和优化,其中包括银行、金融、保险、医疗保健、人力资源、供应链甚至数字音乐分发。

Fabric 是第一个支持通用编程语言编写智能合约(如 Java、Go 和 Node.js)的分布式账本平台,不受限于特定领域语言(Domain-Specific Languages,DSL)。这意味着大多数企业已经拥有开发智能合约所需的技能,并且不需要额外的培训来学习新的语言或特定领域语言。

Fabric 平台也是许可的,这意味着它与公共非许可网络不同,参与者彼此了解而不是匿名的或完全不信任的。也就是说,尽管参与者可能不会完全信任彼此(例如,同行业中的竞争对手),但网络可以在一个治理模式下运行,这个治理模式是建立在参与者之间确实存在的信任之上的,如处理纠纷的法律协议或框架。

该平台最重要的区别之一是它支持可插拔的共识协议,使得平台能够更有效地进行定制,以适应特定的业务场景和信任模型。例如,当部署在单个企业内或由可信任的权威机构管理时,完全拜占庭容错的共识可能是不必要的,并且大大降低了性能和吞吐量。在这种的情况下,崩溃容错(Crash Fault-Tolerant,CFT)共识协议可能就够了,而在去中心化的场景中,可能需要更传统的拜占庭容错(Byzantine Fault Tolerant,BFT)共识协议。

Fabric 可以利用不需要原生加密货币的共识协议来激励昂贵的挖矿或推动智能合约执行。不使用加密货币会降低系统的风险,并且没有挖矿操作意味着可以使用与任何其他分布式系统大致相同的运营成本来部署平台。

二、搭建基础环境

1、安装docker以及docker-compose

[root@orderer ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 wget git curl 
[root@orderer ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@orderer ~]# yum -y install docker-ce
[root@orderer ~]# docker -v
Docker version 20.10.10, build b485636
[root@orderer ~]# systemctl start docker
[root@orderer ~]# systemctl enable docker
[root@orderer ~]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@orderer ~]# chmod +x /usr/local/bin/docker-compose
[root@orderer ~]# docker-compose -v
docker-compose version 1.29.2, build 5becea4c

2、安装golang环境

[root@orderer ~]# wget https://golang.google.cn/dl/go1.16.8.linux-amd64.tar.gz
[root@orderer ~]# tar zxf go1.16.8.linux-amd64.tar.gz
[root@orderer ~]# mv go /usr/local/
[root@orderer ~]# vim /etc/profile
export GOPATH=~/Go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
[root@orderer ~]# source /etc/profile
[root@orderer ~]# go version
go version go1.16.8 linux/amd64

三、运行测试网络

1、下载测试网络部署脚本

[root@orderer ~]# curl -sSL https://bit.ly/2ysbOFE | bash -s         //最新的生产发布版本
[root@orderer ~]# curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.7         //指定Fabric v2.2.0 和Fabric CA v1.4.7版本

如果网络无法连接可以使用离线脚本

[root@orderer ~]# vim bootstrap.sh
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# if version not passed in, default to latest released version
VERSION=2.3.3
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.2
ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')")
MARCH=$(uname -m)

printHelp() {
    echo "Usage: bootstrap.sh [version [ca_version]] [options]"
    echo
    echo "options:"
    echo "-h : this help"
    echo "-d : bypass docker image download"
    echo "-s : bypass fabric-samples repo clone"
    echo "-b : bypass download of platform-specific binaries"
    echo
    echo "e.g. bootstrap.sh 2.3.3 1.5.2 -s"
    echo "will download docker images and binaries for Fabric v2.3.3 and Fabric CA v1.5.2"
}

# dockerPull() pulls docker images from fabric and chaincode repositories
# note, if a docker image doesn't exist for a requested release, it will simply
# be skipped, since this script doesn't terminate upon errors.

dockerPull() {
    #three_digit_image_tag is passed in, e.g. "1.4.7"
    three_digit_image_tag=$1
    shift
    #two_digit_image_tag is derived, e.g. "1.4", especially useful as a local tag for two digit references to most recent baseos, ccenv, javaenv, nodeenv patch releases
    two_digit_image_tag=$(echo "$three_digit_image_tag" | cut -d'.' -f1,2)
    while [[ $# -gt 0 ]]
    do
        image_name="$1"
        echo "====> hyperledger/fabric-$image_name:$three_digit_image_tag"
        docker pull "hyperledger/fabric-$image_name:$three_digit_image_tag"
        docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name"
        docker tag "hyperledger/fabric-$image_name:$three_digit_image_tag" "hyperledger/fabric-$image_name:$two_digit_image_tag"
        shift
    done
}

cloneSamplesRepo() {
    # clone (if needed) hyperledger/fabric-samples and checkout corresponding
    # version to the binaries and docker images to be downloaded
    if [ -d test-network ]; then
        # if we are in the fabric-samples repo, checkout corresponding version
        echo "==> Already in fabric-samples repo"
    elif [ -d fabric-samples ]; then
        # if fabric-samples repo already cloned and in current directory,
        # cd fabric-samples
        echo "===> Changing directory to fabric-samples"
        cd fabric-samples
    else
        echo "===> Cloning hyperledger/fabric-samples repo"
        git clone -b main https://ghproxy.com/https://github.com/hyperledger/fabric-samples.git && cd fabric-samples
    fi

    if GIT_DIR=.git git rev-parse v${VERSION} >/dev/null 2>&1; then
        echo "===> Checking out v${VERSION} of hyperledger/fabric-samples"
        git checkout -q v${VERSION}
    else
        echo "fabric-samples v${VERSION} does not exist, defaulting to main. fabric-samples main branch is intended to work with recent versions of fabric."
        git checkout -q main
    fi
}

# This will download the .tar.gz
download() {
    local BINARY_FILE=$1
    local URL=$2
    echo "===> Downloading: " "${URL}"
    curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?
    if [ -n "$rc" ]; then
        echo "==> There was an error downloading the binary file."
        return 22
    else
        echo "==> Done."
    fi
}

pullBinaries() {
    echo "===> Downloading version ${FABRIC_TAG} platform specific fabric binaries"
    download "${BINARY_FILE}" "https://ghproxy.com/https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
    if [ $? -eq 22 ]; then
        echo
        echo "------> ${FABRIC_TAG} platform specific fabric binary is not available to download <----"
        echo
        exit
    fi

    echo "===> Downloading version ${CA_TAG} platform specific fabric-ca-client binary"
    download "${CA_BINARY_FILE}" "https://ghproxy.com/https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"
    if [ $? -eq 22 ]; then
        echo
        echo "------> ${CA_TAG} fabric-ca-client binary is not available to download  (Available from 1.1.0-rc1) <----"
        echo
        exit
    fi
}

pullDockerImages() {
    command -v docker >& /dev/null
    NODOCKER=$?
    if [ "${NODOCKER}" == 0 ]; then
        FABRIC_IMAGES=(peer orderer ccenv tools)
        case "$VERSION" in
        2.*)
            FABRIC_IMAGES+=(baseos)
            shift
            ;;
        esac
        echo "FABRIC_IMAGES:" "${FABRIC_IMAGES[@]}"
        echo "===> Pulling fabric Images"
        dockerPull "${FABRIC_TAG}" "${FABRIC_IMAGES[@]}"
        echo "===> Pulling fabric ca Image"
        CA_IMAGE=(ca)
        dockerPull "${CA_TAG}" "${CA_IMAGE[@]}"
        echo "===> List out hyperledger docker images"
        docker images | grep hyperledger
    else
        echo "========================================================="
        echo "Docker not installed, bypassing download of Fabric images"
        echo "========================================================="
    fi
}

DOCKER=true
SAMPLES=true
BINARIES=true

# Parse commandline args pull out
# version and/or ca-version strings first
if [ -n "$1" ] && [ "${1:0:1}" != "-" ]; then
    VERSION=$1;shift
    if [ -n "$1" ]  && [ "${1:0:1}" != "-" ]; then
        CA_VERSION=$1;shift
        if [ -n  "$1" ] && [ "${1:0:1}" != "-" ]; then
            THIRDPARTY_IMAGE_VERSION=$1;shift
        fi
    fi
fi

# prior to 1.2.0 architecture was determined by uname -m
if [[ $VERSION =~ ^1\.[0-1]\.* ]]; then
    export FABRIC_TAG=${MARCH}-${VERSION}
    export CA_TAG=${MARCH}-${CA_VERSION}
    export THIRDPARTY_TAG=${MARCH}-${THIRDPARTY_IMAGE_VERSION}
else
    # starting with 1.2.0, multi-arch images will be default
    : "${CA_TAG:="$CA_VERSION"}"
    : "${FABRIC_TAG:="$VERSION"}"
    : "${THIRDPARTY_TAG:="$THIRDPARTY_IMAGE_VERSION"}"
fi

BINARY_FILE=hyperledger-fabric-${ARCH}-${VERSION}.tar.gz
CA_BINARY_FILE=hyperledger-fabric-ca-${ARCH}-${CA_VERSION}.tar.gz

# then parse opts
while getopts "h?dsb" opt; do
    case "$opt" in
        h|\?)
            printHelp
            exit 0
            ;;
        d)  DOCKER=false
            ;;
        s)  SAMPLES=false
            ;;
        b)  BINARIES=false
            ;;
    esac
done

if [ "$SAMPLES" == "true" ]; then
    echo
    echo "Clone hyperledger/fabric-samples repo"
    echo
    cloneSamplesRepo
fi
if [ "$BINARIES" == "true" ]; then
    echo
    echo "Pull Hyperledger Fabric binaries"
    echo
    pullBinaries
fi
if [ "$DOCKER" == "true" ]; then
    echo
    echo "Pull Hyperledger Fabric docker images"
    echo
    pullDockerImages
fi

2、运行脚本

[root@orderer ~]# sh bootstrap.sh

Clone hyperledger/fabric-samples repo

===> Cloning hyperledger/fabric-samples repo
正克隆到 'fabric-samples'...
remote: Enumerating objects: 8203, done.
remote: Counting objects: 100% (478/478), done.
remote: Compressing objects: 100% (312/312), done.
remote: Total 8203 (delta 199), reused 373 (delta 156), pack-reused 7725
接收对象中: 100% (8203/8203), 4.89 MiB | 3.11 MiB/s, done.
处理 delta 中: 100% (4233/4233), done.
fabric-samples v2.3.3 does not exist, defaulting to main. fabric-samples main branch is intended to work with recent versions of fabric.

Pull Hyperledger Fabric binaries

===> Downloading version 2.3.3 platform specific fabric binaries
===> Downloading:  https://github.com/hyperledger/fabric/releases/download/v2.3.3/hyperledger-fabric-linux-amd64-2.3.3.tar.gz

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   649  100   649    0     0    129      0  0:00:05  0:00:05 --:--:--   144
100 69.2M  100 69.2M    0     0   451k      0  0:02:36  0:02:36 --:--:-- 2014k
==> Done.
===> Downloading version 1.5.2 platform specific fabric-ca-client binary
===> Downloading:  https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   652  100   652    0     0    673      0 --:--:-- --:--:-- --:--:--   673
100 25.4M  100 25.4M    0     0  1565k      0  0:00:16  0:00:16 --:--:-- 1966k
==> Done.

Pull Hyperledger Fabric docker images

FABRIC_IMAGES: peer orderer ccenv tools baseos
===> Pulling fabric Images
====> hyperledger/fabric-peer:2.3.3
2.3.3: Pulling from hyperledger/fabric-peer
Status: Downloaded newer image for hyperledger/fabric-peer:2.3.3
docker.io/hyperledger/fabric-peer:2.3.3
====> hyperledger/fabric-orderer:2.3.3
2.3.3: Pulling from hyperledger/fabric-orderer
Status: Downloaded newer image for hyperledger/fabric-orderer:2.3.3
docker.io/hyperledger/fabric-orderer:2.3.3
====> hyperledger/fabric-ccenv:2.3.3
2.3.3: Pulling from hyperledger/fabric-ccenv
Status: Downloaded newer image for hyperledger/fabric-ccenv:2.3.3
docker.io/hyperledger/fabric-ccenv:2.3.3
====> hyperledger/fabric-tools:2.3.3
2.3.3: Pulling from hyperledger/fabric-tools
Status: Downloaded newer image for hyperledger/fabric-tools:2.3.3
docker.io/hyperledger/fabric-tools:2.3.3
====> hyperledger/fabric-baseos:2.3.3
2.3.3: Pulling from hyperledger/fabric-baseos
Status: Downloaded newer image for hyperledger/fabric-baseos:2.3.3
docker.io/hyperledger/fabric-baseos:2.3.3
===> Pulling fabric ca Image
====> hyperledger/fabric-ca:1.5.2
1.5.2: Pulling from hyperledger/fabric-ca
Status: Downloaded newer image for hyperledger/fabric-ca:1.5.2
docker.io/hyperledger/fabric-ca:1.5.2
===> List out hyperledger docker images
hyperledger/fabric-ca        1.5       4ea287b75c63   7 weeks ago   69.8MB
hyperledger/fabric-ca        1.5.2     4ea287b75c63   7 weeks ago   69.8MB
hyperledger/fabric-ca        latest    4ea287b75c63   7 weeks ago   69.8MB
hyperledger/fabric-tools     2.3       98fa0bfb0fd2   7 weeks ago   445MB
hyperledger/fabric-tools     2.3.3     98fa0bfb0fd2   7 weeks ago   445MB
hyperledger/fabric-tools     latest    98fa0bfb0fd2   7 weeks ago   445MB
hyperledger/fabric-peer      2.3       a491b5ab42f6   7 weeks ago   53.3MB
hyperledger/fabric-peer      2.3.3     a491b5ab42f6   7 weeks ago   53.3MB
hyperledger/fabric-peer      latest    a491b5ab42f6   7 weeks ago   53.3MB
hyperledger/fabric-orderer   2.3       9e1952b8840d   7 weeks ago   35.4MB
hyperledger/fabric-orderer   2.3.3     9e1952b8840d   7 weeks ago   35.4MB
hyperledger/fabric-orderer   latest    9e1952b8840d   7 weeks ago   35.4MB
hyperledger/fabric-ccenv     2.3       56fa403e02ee   7 weeks ago   502MB
hyperledger/fabric-ccenv     2.3.3     56fa403e02ee   7 weeks ago   502MB
hyperledger/fabric-ccenv     latest    56fa403e02ee   7 weeks ago   502MB
hyperledger/fabric-baseos    2.3       b35a8ef578c0   7 weeks ago   6.87MB
hyperledger/fabric-baseos    2.3.3     b35a8ef578c0   7 weeks ago   6.87MB
hyperledger/fabric-baseos    latest    b35a8ef578c0   7 weeks ago   6.87MB

3、查看下载文件并把二进制文件加入变量

[root@orderer ~]# ls fabric-samples/
asset-transfer-abac               bin                 fabcar               test-application
asset-transfer-basic              chaincode           high-throughput      test-network
asset-transfer-events             CHANGELOG.md        interest_rate_swaps  test-network-k8s
asset-transfer-ledger-queries     ci                  LICENSE              test-network-nano-bash
asset-transfer-private-data       CODE_OF_CONDUCT.md  MAINTAINERS.md       token-erc-1155
asset-transfer-sbe                CODEOWNERS          off_chain_data       token-erc-20
asset-transfer-secured-agreement  commercial-paper    README.md            token-erc-721
auction-dutch                     config              scripts              token-utxo
auction-simple                    CONTRIBUTING.md     SECURITY.md
[root@orderer ~]# cp fabric-samples/bin/* /usr/local/bin/
[root@orderer ~]# vim /etc/profile
export PATH=~/fabric-samples/bin:$PATH
[root@orderer ~]# source /etc/profile

4、启动测试网络

[root@orderer ~]# cd fabric-samples/test-network
[root@orderer test-network]# ./network.sh up
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
LOCAL_VERSION=2.3.3
DOCKER_IMAGE_VERSION=2.3.3
/root/fabric-samples/bin/cryptogen
Generating certificates using cryptogen tool
Creating Org1 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations
org1.example.com
+ res=0
Creating Org2 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations
org2.example.com
+ res=0
Creating Orderer Org Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations
+ res=0
Generating CCP files for Org1 and Org2
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED         STATUS                  PORTS                                                                                                                                 NAMES
002cc9ed3130   hyperledger/fabric-tools:latest     "/bin/bash"         1 second ago    Up Less than a second                                                                                                                                         cli
7950f6a9b69f   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up Less than a second   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp                                              peer0.org1.example.com
baa9519da515   hyperledger/fabric-peer:latest      "peer node start"   4 seconds ago   Up Less than a second   0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp                                    peer0.org2.example.com
51b3a3f618bf   hyperledger/fabric-orderer:latest   "orderer"           4 seconds ago   Up Less than a second   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp   orderer.example.com

5、创建通道

[root@orderer test-network]# ./network.sh createChannel           //创建一个默认名称为“mychannel”的通道
Creating channel 'mychannel'.
......
Channel 'mychannel' joined
[root@orderer test-network]# ./network.sh createChannel -c channel1           //创建一个名为channel1的通道

6、在通道启动一个链码

[root@orderer test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deploying chaincode on channel 'mychannel'
......
Chaincode initialization is not required

7、使用peer CLI与网络进行交互

设置变量以允许作为Org1和Org2操作peer CLI

[root@orderer test-network]# export PATH=${PWD}/../bin:$PATH
[root@orderer test-network]# export FABRIC_CFG_PATH=$PWD/../config/
[root@orderer test-network]# 
# Environment variables for Org1

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

[root@orderer test-network]# 
# Environment variables for Org2

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
[root@orderer test-network]# source /etc/profile

初始化账本

[root@orderer test-network]# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
2021-11-01 11:21:49.509 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

获取添加到通道账本的资产列表

[root@orderer test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[{"AppraisedValue":300,"Color":"blue","ID":"asset1","Owner":"Tomoko","Size":5},{"AppraisedValue":400,"Color":"red","ID":"asset2","Owner":"Brad","Size":5},{"AppraisedValue":500,"Color":"green","ID":"asset3","Owner":"Jin Soo","Size":10},{"AppraisedValue":600,"Color":"yellow","ID":"asset4","Owner":"Max","Size":10},{"AppraisedValue":700,"Color":"black","ID":"asset5","Owner":"Adriana","Size":15},{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Michel","Size":15}]

改变账本上的资产所有者

[root@orderer test-network]# peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'
2021-11-01 11:22:24.539 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

查询运行在 peer0.org2.example.com上asset-transfer (basic) 链码

[root@orderer test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"AppraisedValue":800,"Color":"white","ID":"asset6","Owner":"Christopher","Size":15}

8、关闭网络

[root@orderer test-network]# ./network.sh down

四、使用认证机构建立网络

1、使用CA标志启动网络

[root@orderer test-network]# ./network.sh up -ca
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'Certificate Authorities'
LOCAL_VERSION=2.3.3
DOCKER_IMAGE_VERSION=2.3.3
CA_LOCAL_VERSION=1.5.2
CA_DOCKER_IMAGE_VERSION=1.5.2
Generating certificates using Fabric CA
Creating network "fabric_test" with the default driver
Creating ca_org1    ... done
Creating ca_org2    ... done
Creating ca_orderer ... done
Creating Org1 Identities
Enrolling the CA admin
......
CONTAINER ID   IMAGE                               COMMAND                  CREATED                  STATUS                  PORTS                                                                                                                                 NAMES
272625b39f6c   hyperledger/fabric-tools:latest     "/bin/bash"              Less than a second ago   Up Less than a second                                                                                                                                         cli
bc17cef9c984   hyperledger/fabric-orderer:latest   "orderer"                1 second ago             Up Less than a second   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp   orderer.example.com
82c625831d2e   hyperledger/fabric-peer:latest      "peer node start"        1 second ago             Up Less than a second   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp                                              peer0.org1.example.com
c233d775524a   hyperledger/fabric-peer:latest      "peer node start"        1 second ago             Up Less than a second   0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp                                    peer0.org2.example.com
40072ff1e10b   hyperledger/fabric-ca:latest        "sh -c 'fabric-ca-se…"   5 seconds ago            Up 4 seconds            0.0.0.0:9054->9054/tcp, :::9054->9054/tcp, 7054/tcp, 0.0.0.0:19054->19054/tcp, :::19054->19054/tcp                                    ca_orderer
2a2e8c8a90e5   hyperledger/fabric-ca:latest        "sh -c 'fabric-ca-se…"   5 seconds ago            Up 4 seconds            0.0.0.0:7054->7054/tcp, :::7054->7054/tcp, 0.0.0.0:17054->17054/tcp, :::17054->17054/tcp                                              ca_org1
10efaa1bd291   hyperledger/fabric-ca:latest        "sh -c 'fabric-ca-se…"   5 seconds ago            Up 4 seconds            0.0.0.0:8054->8054/tcp, :::8054->8054/tcp, 7054/tcp, 0.0.0.0:18054->18054/tcp, :::18054->18054/tcp 

显示MSP文件夹的结构和配置文件

[root@orderer test-network]# tree organizations/peerOrganizations/org1.example.com/users/[email protected]/
organizations/peerOrganizations/org1.example.com/users/[email protected]/
└── msp
    ├── cacerts
    │   └── localhost-7054-ca-org1.pem
    ├── config.yaml
    ├── IssuerPublicKey
    ├── IssuerRevocationPublicKey
    ├── keystore
    │   └── 3e39046e7d7a60713983be3066938614c71af62f37c38f9592d07da674ba7ee7_sk
    ├── signcerts
    │   └── cert.pem
    └── user

你可能感兴趣的:(区块链,区块链,fabric,网络)