Fabric1.4 搭建区块链网络

Posted 杰西啊杰西

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fabric1.4 搭建区块链网络相关的知识,希望对你有一定的参考价值。

这里还只是正在进行的测试记录,文章会根据进度进行修改。

一、建立项目根路径

自己取项目名(根路径)

二、生成证书文件

1、新建文件夹/fixture

2、编写证书配置文件

项目需要,故设置四个组织

(1)查看模板

cryptogen showtemplate

(2)复制模板crypto-config.yaml,并根据需要稍作修改

# ---------------------------------------------------------------------------
# "OrdererOrgs" - Definition of organizations managing orderer nodes
# ---------------------------------------------------------------------------
OrdererOrgs:
  # ---------------------------------------------------------------------------
  # Orderer
  # ---------------------------------------------------------------------------
  - Name: Orderer
    Domain: perinfo.com
    EnableNodeOUs: false

    # ---------------------------------------------------------------------------
    # "Specs" - See PeerOrgs below for complete description
    # ---------------------------------------------------------------------------
    Specs:
      - Hostname: orderer

# ---------------------------------------------------------------------------
# "PeerOrgs" - Definition of organizations managing peer nodes
# ---------------------------------------------------------------------------
PeerOrgs:
  # ---------------------------------------------------------------------------
  # Org1
  # ---------------------------------------------------------------------------
  - Name: Org1
    Domain: org1.perinfo.com
    EnableNodeOUs: false
    Template:
      Count: 1
    Users:
      Count: 1

  # ---------------------------------------------------------------------------
  # Org2: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: Org2
    Domain: org2.perinfo.com
    EnableNodeOUs: false
    Template:
      Count: 1
    Users:
      Count: 1

  # ---------------------------------------------------------------------------
  # Org3: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: Org3
    Domain: org3.perinfo.com
    EnableNodeOUs: false
    Template:
      Count: 1
    Users:
      Count: 1

  # ---------------------------------------------------------------------------
  # Org4: See "Org1" for full specification
  # ---------------------------------------------------------------------------
  - Name: Org4
    Domain: org4.perinfo.com
    EnableNodeOUs: false
    Template:
      Count: 1
    Users:
      Count: 1

(3)生成证书

cryptogen generate --config=crypto-config.yaml --output ./crypto-config

在这里插入图片描述
部分crypto-config文件夹组织
在这里插入图片描述

三、创世块的生成

fabric中channel就是账本,账本中的第一个区块需要手动生成

1、创建/channel-artifacts文件夹

2、编写configtx.yaml

看着多,实际上要改的很少
/root/go/src/github.com/hyperledger/fabric/sampleconfig中的configtx.yaml文件拿过来修改

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

---
################################################################################
#
#   ORGANIZATIONS
#
#   This section defines the organizational identities that can be referenced
#   in the configuration profiles.
#
################################################################################
Organizations:

    # SampleOrg defines an MSP using the sampleconfig. It should never be used
    # in production but may be used as a template for other definitions.
    - &OrdererOrg
        # Name is the key by which this org will be referenced in channel
        # configuration transactions.
        # Name can include alphanumeric characters as well as dots and dashes.
        Name: OrdererOrg

        # ID is the key by which this org's MSP definition will be referenced.
        # ID can include alphanumeric characters as well as dots and dashes.
        ID: OrdererMSP

        # MSPDir is the filesystem path which contains the MSP configuration.
        MSPDir: crypto-config/ordererOrganizations/perinfo.com/msp

    - &Org1
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org1MSP

        # ID to load the MSP definition as
        ID: Org1MSP

        MSPDir: crypto-config/peerOrganizations/org1.perinfo.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org1.perinfo.com
              Port: 7051

    - &Org2
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org2MSP

        # ID to load the MSP definition as
        ID: Org2MSP

        MSPDir: crypto-config/peerOrganizations/org2.perinfo.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org2.perinfo.com
              Port: 7051

    - &Org3
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org3MSP

        # ID to load the MSP definition as
        ID: Org3MSP

        MSPDir: crypto-config/peerOrganizations/org3.perinfo.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org3.perinfo.com
              Port: 7051


    - &Org4
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org4MSP

        # ID to load the MSP definition as
        ID: Org4MSP

        MSPDir: crypto-config/peerOrganizations/org4.perinfo.com/msp

        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org4.perinfo.com
              Port: 7051


################################################################################
#
#   CAPABILITIES
#
#   This section defines the capabilities of fabric network. This is a new
#   concept as of v1.1.0 and should not be utilized in mixed networks with
#   v1.0.x peers and orderers.  Capabilities define features which must be
#   present in a fabric binary for that binary to safely participate in the
#   fabric network.  For instance, if a new MSP type is added, newer binaries
#   might recognize and validate the signatures from this type, while older
#   binaries without this support would be unable to validate those
#   transactions.  This could lead to different versions of the fabric binaries
#   having different world states.  Instead, defining a capability for a channel
#   informs those binaries without this capability that they must cease
#   processing transactions until they have been upgraded.  For v1.0.x if any
#   capabilities are defined (including a map with all capabilities turned off)
#   then the v1.0.x peer will deliberately crash.
#
################################################################################
Capabilities:
    # Channel capabilities apply to both the orderers and the peers and must be
    # supported by both.
    # Set the value of the capability to true to require it.
    # Note that setting a later Channel version capability to true will also
    # implicitly set prior Channel version capabilities to true. There is no need
    # to set each version capability to true (prior version capabilities remain
    # in this sample only to provide the list of valid values).
    Channel: &ChannelCapabilities
        # V1.4.3 for Channel is a catchall flag for behavior which has been
        # determined to be desired for all orderers and peers running at the v1.4.3
        # level, but which would be incompatible with orderers and peers from
        # prior releases.
        # Prior to enabling V1.4.3 channel capabilities, ensure that all
        # orderers and peers on a channel are at v1.4.3 or later.
        V1_4_3: true
        # V1.3 for Channel enables the new non-backwards compatible
        # features and fixes of fabric v1.3
        V1_3: false
        # V1.1 for Channel enables the new non-backwards compatible
        # features and fixes of fabric v1.1
        V1_1: false

    # Orderer capabilities apply only to the orderers, and may be safely
    # used with prior release peers.
    # Set the value of the capability to true to require it.
    # Note that setting a later Orderer version capability to true will also
    # implicitly set prior Orderer version capabilities to true. There is no need
    # to set each version capability to true (prior version capabilities remain
    # in this sample only to provide the list of valid values).
    Orderer: &OrdererCapabilities
        # V1.4.2 for Orderer is a catchall flag for behavior which has been
        # determined to be desired for all orderers running at the v1.4.2
        # level, but which would be incompatible with orderers from prior releases.
        # Prior to enabling V1.4.2 orderer capabilities, ensure that all
        # orderers on a channel are at v1.4.2 or later.
        V1_4_2: true
        # V1.1 for Orderer enables the new non-backwards compatible
        # features and fixes of fabric v1.1
        V1_1: false

    # Application capabilities apply only to the peer network, and may be safely
    # used with prior release orderers.
    # Set the value of the capability to true to require it.
    # Note that setting a later Application version capability to true will also
    # implicitly set prior Application version capabilities to true. There is no need
    # to set each version capability to true (prior version capabilities remain
    # in this sample only to provide the list of valid values).
    Application: &ApplicationCapabilities
        # V1.4.2 for Application enables the new non-backwards compatible
        # features and fixes of fabric v1.4.2
        V1_4_2: true
        # V1.3 for Application enables the new non-backwards compatible
        # features and fixes of fabric v1.3.
        V1_3: false
        # V1.2 for Application enables the new non-backwards compatible
        # features and fixes of fabric v1.2 (note, this need not be set if
        # later version capabilities are set)
        V1_2: false
        # V1.1 for Application enables the new non-backwards compatible
        # features and fixes of fabric v1.1 (note, this need not be set if
        # later version capabilities are set).
        V1_1: false

################################################################################
#
#   APPLICATION
#
#   This section defines the values to encode into a config transaction or
#   genesis block for application-related parameters.
#
################################################################################
Application: &ApplicationDefaults

    # Organizations lists the orgs participating on the application side of the
    # network.
    Organizations:


################################################################################
#
#   ORDERER
#
#   This section defines the values to encode into a config transaction or
#   genesis block for orderer related parameters.
#
################################################################################
Orderer: &OrdererDefaults

    # Orderer Type: The orderer implementation to start.
    # Available types are "solo", "kafka" and "etcdraft".
    OrdererType: solo

    # Addresses used to be the list of orderer addresses that clients and peers
    # could connect to.  However, this does not allow clients to associate orderer
    # addresses and orderer organizations which can be useful for things such
    # as TLS validation.  The preferred way to specify orderer addresses is now
    # to include the OrdererEndpoints item in your org definition
    Addresses:
        - orderer.perinfo.com:7050

    # Batch Timeout: The amount of time to wait before creating a batch.
    BatchTimeout: 2s

    # Batch Size: Controls the number of messages batched into a block.
    # The orderer views messages opaquely, but typically, messages may
    # be considered to be Fabric transactions.  The 'batch' is the group
    # of messages in the 'data' field of the block.  Blocks will be a few kb
    # larger than the batch size, when signatures, hashes, and other metadata
    # is applied.
    BatchSize:

        # Max Message Count: The maximum number of messages to permit in a
        # batch.  No block will contain more than this number of messages.
        MaxMessageCount: 10

        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch. The maximum block size is this value
        # plus the size of the associated metadata (usually a few KB depending
        # upon the size of the signing identities). Any transaction larger than
        # this value will be rejected by ordering. If the "kafka" OrdererType is
        # selected, set 'message.max.bytes' and 'replica.fetch.max.bytes' on
        # the Kafka brokers to a value that is larger than this one.
        AbsoluteMaxBytes: 99 MB

        # Preferred Max Bytes: The preferred maximum number of bytes allowed
        # for the serialized messages in a batch. Roughly, this field may be considered
        # the best effort maximum size of a batch. A batch will fill with messages
        # until this size is reached (or the max message count, or batch timeout is
        # exceeded).  If adding a new message to the batch would cause the batch to
        # exceed the preferred max bytes, then the current batch is closed and written
        # to a block, and a new batch containing the new message is created.  If a
        # message larger than the preferred max bytes is received, then its batch
        # will contain only that message.  Because messages may be larger than
        # preferred max bytes (up to AbsoluteMaxBytes), some batches may exceed
        # the preferred max bytes, but will always contain exactly one transaction.
        PreferredMaxBytes: 512 KB

    # Max Channels is the maximum number of channels to allow on the ordering
    # network. When set to 0, this implies no maximum number of channels.
    MaxChannels: 0

    Kafka:
        # Brokers: A list of Kafka brokers to which the orderer connects. Edit
        # this list to identify the brokers of the ordering service.
        # NOTE: Use IP:port notation.
        Brokers:
            - 127.0.0.1:9092

    # Organizations lists the orgs participating on the orderer side of the
    # network.
    Organizations:


################################################################################
#
#   PROFILES
#
#   Different configuration profiles may be encoded here to be specified as
#   parameters to the configtxgen tool. The profiles which specify consortiums
#   are to be used for generating the orderer genesis block. With the correct
#   consortium members defined in the orderer genesis block, channel creation
#   requests may be generated with only the org member names and a consortium
#   name.
#
################################################################################
Profiles:
    FourOrgsOrdererGenesis:
        Capabilities:
            <<: *ChannelCapabilities
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
                    - *Org3
                    - *Org4
    FourOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2
                - *Org3
                - *Org4
            Capabilities:
                <<: *ApplicationCapabilities

3、创建系统创始区块

该创始区块属于节点

export FABRIC_CFG_PATH=$PWD
configtxgen -profile FourOrgsOrdererGenesis -outputBlock channel-artifacts/genesis.block

注意:
FourOrgsOrdererGenesis 是你自己在configtx.yaml中设置的profile名
在这里插入图片描述
生成了创始区块
在这里插入图片描述

4、创建账本创始区块

该创始区块属于channel账本

configtxgen -profile FourOrgsChannel -outputCreateChannelTx channel-artifacts/channel.tx -channelID mychannel

5、为通道内的两个组织创建锚节点初始化文件

configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate channel-artifacts/Org3MSPanchors.tx -channelID mychannel -asOrg Org3MSP
configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate channel-artifacts/Org4MSPanchors.tx -channelID mychannel -asOrg Org4MSP

在这里插入图片描述

四、fabric-ca的编译和安装

由于之前安装fabric是已经安装了,此处省略,建议直接去下载官方二进制文件,自己编译容易出错
查看fabric-ca-server version时报错:(版本问题)

问题:

/lib64/libc.so.6: version `GLIBC_2.28' not found (required by fabric-ca-server)

这个原因我没有找到,我使用的是腾讯云服务器,libc初始版本为2.17,不过解决方法倒是找到了

解决方法:

冒着系统掉头的风险升级libc.so.6(其实就是更换软链接)

== 这里一定要在root用户权限下进行 == !!!!

① 升级gcc到最新版本

CentOS7 升级 GCC 到 5.4.0 版本

②升级make

wget http://ftp.gnu.org/gnu/make/make-4.2.tar.gz

tar -xzvf make-4.2.tar.gz

cd make-4.2

sudo ./configure

sudo make

sudo make install

sudo rm -rf /usr/bin/make

sudo cp ./make /usr/bin/

make -v

③安装glibc-2.28

curl -O http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz

tar zxf glibc-2.28.tar.gz 

my glibc-2.28 /opt

cd /opt/glibc-2.28/

mkdir build

cd build/

../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin

make -j8

make install

会有报错,但是可以忽略,make会很久很久(胆战心惊.jpg)
在这里插入图片描述
④ 重新做个软链接 (这里一定要root用户啊啊啊!!!!)

cd /lib64

rm -rf libm.so.6

ln -s libm-2.28.so libm.so.6

查看glibc版本

ldd --version

在这里插入图片描述

现在查看fabric-ca-severfabric-ca-client的版本
在这里插入图片描述
成功了!!!

五、fabric-ca-server的初始化

因为这台服务器上还没有对fabric-ca-server进行初始化,故如下进行:

1、创建文件夹

mkdir -p /etc/hyperledger/fabric-ca-server

cd  /etc/hyperledger/fabric-ca-server

2、初始化fabric-ca-server

fabric-ca-server init -b admin:adminpw #adminpw是你自己设置的密码

然后会对fabric-ca-server进行初始化
(忽略我截图里面的歌词哈哈,才看到)
在这里插入图片描述

文件结构为:

.
|-- ca-cert.pem
|-- fabric-ca-server-config.yaml
|-- fabric-ca-server.db
|-- IssuerPublicKey
|-- IssuerRevocationPublicKey
`-- msp
    `-- keystore
        |-- efcb5dff9dxxxxxxxxxxxxxxxxxxxxxxxbcb_sk
        |-- IssuerRevocationPrivateKey
        `-- IssuerSecretKey

其中:
(1)fabric-ca-server-config.yaml :配置文件
(2)fabric-ca-server.db:数据库文件(数据库选择sqlite3时有效)
(3)ca-cert.pem:证书文件
(4)msp:私钥文件

3、按照需求修改配置文件

其中fabric-ca-server.config.yaml中的数据库配置:
在这里插入图片描述
db部分的配置中,选择不同的数据库服务器配置格式是不一样的,同时fabric-ca本身不会安装这些数据库,需要你自己提前安装好。

①sqlite3:

type: sqlite3
  datasource: fabric-ca-server.db

②mysql:

type: sqlite3
  datasource: root:rootpw@tcp(localhost:3306)/fabric_ca?parseTime=True&tls=custom

4、fabric-ca-server的启动

在这里插入图片描述

因为没有写前端,将就看哈哈,启动成功!
在这里插入图片描述

六、fabric-ca-client

常用功能:
(1)注册新账号
(2)载入账号信息
(3)获取CA服务器的证书

七、将fabric-ca-server绑定到现有项目中

1、将fabric-ca-server绑定到现有组织中

找到fabric-ca-server.yaml中的
在这里插入图片描述

ca:
  # Name of this CA
  name:
  # Key file (is only used to import a private key into BCCSP)
  keyfile:
  # Certificate file (default: ca-cert.pem)
  certfile:
  # Chain file
  chainfile:

然后进入项目中存放org1相关证书的文件夹中
在这里插入图片描述

所以可以将fabric-ca-server-config.yaml中的ca修改了

ca:
  # Name of this CA
  name:ca-org1
  # Key file (is only used to import a private key into BCCSP)
  keyfile:/root/go/src/github.com/PerInfoChain/fixtures/crypto-config/peerOrganizations/org1.perinfo.com/ca/5dac5cxxxxxxx4_sk
  # Certificate file (default: ca-cert.pem)
  certfile:/root/go/src/github.com/PerInfoChain/fixtures/crypto-config/peerOrganizations/org1.perinfo.com/ca/ca.org1.perinfo.com-cert.pem
  # Chain file
  chainfile:ca.org1.perinfo.com-cert.pem

以上是关于Fabric1.4 搭建区块链网络的主要内容,如果未能解决你的问题,请参考以下文章

Fabric1.4 在自己项目中安装区块链浏览器blockchain-explorer

复现区块链Fabric1.4项目记录安装配置centos8虚拟机

Hyperledger Fabric - 如何在每次区块链网络测试后清除 DEV 环境?

Fabric1.4.1视频教程即将推出

Fabric1.4 编写链码下

Fabric1.4 Fabric-SDK-go及web应用