文章目录
一、搭建基础环境二、生成证书文件1、准备证书配置文件模板2、修改模板3、生成证书文件三、生成通道文件1、编写创世快文件2、生成创世块文件和通道文件三、配置各个节点1、编写docker-compose文件2、运行docker-compose文件四、通道操作1、创建通道2、加入通道1、peer0节点加入通道2、更新锚节点五、安装调用智能合约1、复制官方实例链码2、安装go依赖并打包链码3、将打包好的链码复制到cli24、安装链码5、批准链码6、查看链码是否就绪7、提交链码8、链码初始化9、查询数据一、搭建基础环境
略
二、生成证书文件
1、准备证书配置文件模板
[root@orderer ~]# mkdir twonodes[root@orderer ~]# cd twonodes/[root@orderer twonodes]# cryptogen showtemplate > crypto-config.yaml
2、修改模板
[root@orderer twonodes]# sed -i "s/false/true/g" crypto-config.yaml[root@orderer twonodes]# cat crypto-config.yaml | grep ^[^#]OrdererOrgs:# ---------------------------------------------------------------------------# Orderer# ---------------------------------------------------------------------------- Name: OrdererDomain: EnableNodeOUs: true# ---------------------------------------------------------------------------# "Specs" - See PeerOrgs below for complete description# ---------------------------------------------------------------------------Specs:- Hostname: ordererPeerOrgs:# ---------------------------------------------------------------------------# Org1# ---------------------------------------------------------------------------- Name: Org1Domain: EnableNodeOUs: true# ---------------------------------------------------------------------------# "CA"# ---------------------------------------------------------------------------# Uncomment this section to enable the explicit definition of the CA for this# organization. This entry is a Spec. See "Specs" section below for details.# ---------------------------------------------------------------------------# CA:# Hostname: ca # implicitly ca.# Country: US# Province: California# Locality: San Francisco# OrganizationalUnit: Hyperledger Fabric# StreetAddress: address for org # default nil# PostalCode: postalCode for org # default nil# ---------------------------------------------------------------------------# "Specs"# ---------------------------------------------------------------------------# Uncomment this section to enable the explicit definition of hosts in your# configuration. Most users will want to use Template, below## Specs is an array of Spec entries. Each Spec entry consists of two fields:# - Hostname: (Required) The desired hostname, sans the domain.# - CommonName: (Optional) Specifies the template or explicit override for# the CN. By default, this is the template:##"{{.Hostname}}.{{.Domain}}"## which obtains its values from the Spec.Hostname and# Org.Domain, respectively.# - SANS: (Optional) Specifies one or more Subject Alternative Names# to be set in the resulting x509. Accepts template# variables {{.Hostname}}, {{.Domain}}, {{.CommonName}}. IP# addresses provided here will be properly recognized. Other# values will be taken as DNS names.# NOTE: Two implicit entries are created for you:# - {{ .CommonName }}# - {{ .Hostname }}# ---------------------------------------------------------------------------# Specs:# - Hostname: foo # implicitly "foo."#CommonName: foo27. # overrides Hostname-based FQDN set above#SANS:# - "bar.{{.Domain}}"# - "altfoo.{{.Domain}}"# - "{{.Hostname}}."# - 172.16.10.31# - Hostname: bar# - Hostname: baz# ---------------------------------------------------------------------------# "Template"# ---------------------------------------------------------------------------# Allows for the definition of 1 or more hosts that are created sequentially# from a template. By default, this looks like "peer%d" from 0 to Count-1.# You may override the number of nodes (Count), the starting index (Start)# or the template used to construct the name (Hostname).## Note: Template and Specs are not mutually exclusive. You may define both# sections and the aggregate nodes will be created for you. Take care with# name collisions# ---------------------------------------------------------------------------Template:Count: 1# Start: 5# Hostname: {{.Prefix}}{{.Index}} # default# SANS:# - "{{.Hostname}}.alt.{{.Domain}}"# ---------------------------------------------------------------------------# "Users"# ---------------------------------------------------------------------------# Count: The number of user accounts _in addition_ to Admin# ---------------------------------------------------------------------------Users:Count: 1# ---------------------------------------------------------------------------# Org2: See "Org1" for full specification# ---------------------------------------------------------------------------- Name: Org2Domain: EnableNodeOUs: trueTemplate:Count: 1Users:Count: 1
3、生成证书文件
[root@orderer twonodes]# cryptogen generate --config=crypto-config.[root@orderer twonodes]# tree.├── crypto-config│ ├── ordererOrganizations│ │ └── │ │ ├── ca│ │ │ ├── -cert.pem│ │ │ └── priv_sk│ │ ├── msp│ │ │ ├── admincerts│ │ │ ├── cacerts│ │ │ │ └── -cert.pem│ │ │ ├── config.yaml│ │ │ └── tlscacerts│ │ │ └── -cert.pem│ │ ├── orderers│ │ │ └── │ │ │ ├── msp│ │ │ │ ├── admincerts│ │ │ │ ├── cacerts│ │ │ │ │ └── -cert.pem│ │ │ │ ├── config.yaml│ │ │ │ ├── keystore│ │ │ │ │ └── priv_sk│ │ │ │ ├── signcerts│ │ │ │ │ └── -cert.pem│ │ │ │ └── tlscacerts│ │ │ │ └── -cert.pem│ │ │ └── tls│ │ │ ├── ca.crt│ │ │ ├── server.crt│ │ │ └── server.key│ │ ├── tlsca│ │ │ ├── priv_sk│ │ │ └── -cert.pem│ │ └── users│ │ └── Admin@│ │├── msp│ ││ ├── admincerts│ ││ ├── cacerts│ ││ │ └── -cert.pem│ ││ ├── config.yaml│ ││ ├── keystore│ ││ │ └── priv_sk│ ││ ├── signcerts│ ││ │ └── Admin@-cert.pem│ ││ └── tlscacerts│ ││ └── -cert.pem│ │└── tls│ │ ├── ca.crt│ │ ├── client.crt│ │ └── client.key│ └── peerOrganizations│ ├── │ │ ├── ca│ │ │ ├── ca.-cert.pem│ │ │ └── priv_sk│ │ ├── msp│ │ │ ├── admincerts│ │ │ ├── cacerts│ │ │ │ └── ca.-cert.pem│ │ │ ├── config.yaml│ │ │ └── tlscacerts│ │ │ └── tlsca.-cert.pem│ │ ├── peers│ │ │ └── peer0.│ │ │ ├── msp│ │ │ │ ├── admincerts│ │ │ │ ├── cacerts│ │ │ │ │ └── ca.-cert.pem│ │ │ │ ├── config.yaml│ │ │ │ ├── keystore│ │ │ │ │ └── priv_sk│ │ │ │ ├── signcerts│ │ │ │ │ └── peer0.-cert.pem│ │ │ │ └── tlscacerts│ │ │ │ └── tlsca.-cert.pem│ │ │ └── tls│ │ │ ├── ca.crt│ │ │ ├── server.crt│ │ │ └── server.key│ │ ├── tlsca│ │ │ ├── priv_sk│ │ │ └── tlsca.-cert.pem│ │ └── users│ │ ├── Admin@│ │ │ ├── msp│ │ │ │ ├── admincerts│ │ │ │ ├── cacerts│ │ │ │ │ └── ca.-cert.pem│ │ │ │ ├── config.yaml│ │ │ │ ├── keystore│ │ │ │ │ └── priv_sk│ │ │ │ ├── signcerts│ │ │ │ │ └── Admin@-cert.pem│ │ │ │ └── tlscacerts│ │ │ │ └── tlsca.-cert.pem│ │ │ └── tls│ │ │ ├── ca.crt│ │ │ ├── client.crt│ │ │ └── client.key│ │ └── User1@│ │ ├── msp│ │ │ ├── admincerts│ │ │ ├── cacerts│ │ │ │ └── ca.-cert.pem│ │ │ ├── config.yaml│ │ │ ├── keystore│ │ │ │ └── priv_sk│ │ │ ├── signcerts│ │ │ │ └── User1@-cert.pem│ │ │ └── tlscacerts│ │ │ └── tlsca.-cert.pem│ │ └── tls│ │├── ca.crt│ │├── client.crt│ │└── client.key│ └── │ ├── ca│ │ ├── ca.-cert.pem│ │ └── priv_sk│ ├── msp│ │ ├── admincerts│ │ ├── cacerts│ │ │ └── ca.-cert.pem│ │ ├── config.yaml│ │ └── tlscacerts│ │ └── tlsca.-cert.pem│ ├── peers│ │ └── peer0.│ │ ├── msp│ │ │ ├── admincerts│ │ │ ├── cacerts│ │ │ │ └── ca.-cert.pem│ │ │ ├── config.yaml│ │ │ ├── keystore│ │ │ │ └── priv_sk│ │ │ ├── signcerts│ │ │ │ └── peer0.-cert.pem│ │ │ └── tlscacerts│ │ │ └── tlsca.-cert.pem│ │ └── tls│ │ ├── ca.crt│ │ ├── server.crt│ │ └── server.key│ ├── tlsca│ │ ├── priv_sk│ │ └── tlsca.-cert.pem│ └── users│├── Admin@││ ├── msp││ │ ├── admincerts││ │ ├── cacerts││ │ │ └── ca.-cert.pem││ │ ├── config.yaml││ │ ├── keystore││ │ │ └── priv_sk││ │ ├── signcerts││ │ │ └── Admin@-cert.pem││ │ └── tlscacerts││ │ └── tlsca.-cert.pem││ └── tls││ ├── ca.crt││ ├── client.crt││ └── client.key│└── User1@│ ├── msp│ │ ├── admincerts│ │ ├── cacerts│ │ │ └── ca.-cert.pem│ │ ├── config.yaml│ │ ├── keystore│ │ │ └── priv_sk│ │ ├── signcerts│ │ │ └── User1@-cert.pem│ │ └── tlscacerts│ │ └── tlsca.-cert.pem│ └── tls│ ├── ca.crt│ ├── client.crt│ └── client.key└── crypto-config.yaml
三、生成通道文件
1、编写创世快文件
[root@orderer twonodes]# vim configtx.yaml# Copyright IBM Corp. All Rights Reserved.## SPDX-License-Identifier: Apache-2.0#---################################################################################## Section: Organizations## - This section defines the different organizational identities which will# be referenced later in the configuration.#################################################################################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# DefaultOrg defines the organization which is used in the sampleconfig# of the fabric.git development environmentName: OrdererOrg# ID to load the MSP definition asID: OrdererMSP# MSPDir is the filesystem path which contains the MSP configurationMSPDir: crypto-config/ordererOrganizations//msp# Policies defines the set of policies at this level of the config tree# For organization policies, their canonical path is usually# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>Policies:Readers:Type: SignatureRule: "OR('OrdererMSP.member')"Writers:Type: SignatureRule: "OR('OrdererMSP.member')"Admins:Type: SignatureRule: "OR('OrdererMSP.admin')"OrdererEndpoints:- :7050- &Org1# DefaultOrg defines the organization which is used in the sampleconfig# of the fabric.git development environmentName: Org1MSP# ID to load the MSP definition asID: Org1MSPMSPDir: crypto-config/peerOrganizations//msp# Policies defines the set of policies at this level of the config tree# For organization policies, their canonical path is usually# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>Policies:Readers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"Writers:Type: SignatureRule: "OR('Org1MSP.admin', 'Org1MSP.client')"Admins:Type: SignatureRule: "OR('Org1MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org1MSP.peer')"AnchorPeers:- Host: peer0.Port: 7051- &Org2# DefaultOrg defines the organization which is used in the sampleconfig# of the fabric.git development environmentName: Org2MSP# ID to load the MSP definition asID: Org2MSPMSPDir: crypto-config/peerOrganizations//msp# Policies defines the set of policies at this level of the config tree# For organization policies, their canonical path is usually# /Channel/<Application|Orderer>/<OrgName>/<PolicyName>Policies:Readers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"Writers:Type: SignatureRule: "OR('Org2MSP.admin', 'Org2MSP.client')"Admins:Type: SignatureRule: "OR('Org2MSP.admin')"Endorsement:Type: SignatureRule: "OR('Org2MSP.peer')"AnchorPeers:- Host: peer0.Port: 9051################################################################################## SECTION: 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.Channel: &ChannelCapabilities# V2_0 capability ensures that orderers and peers behave according# to v2.0 channel capabilities. Orderers and peers from# prior releases would behave in an incompatible way, and are therefore# not able to participate in channels at v2.0 capability.# Prior to enabling V2.0 channel capabilities, ensure that all# orderers and peers on a channel are at v2.0.0 or later.V2_0: true# 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.Orderer: &OrdererCapabilities# V2_0 orderer capability ensures that orderers behave according# to v2.0 orderer capabilities. Orderers from# prior releases would behave in an incompatible way, and are therefore# not able to participate in channels at v2.0 orderer capability.# Prior to enabling V2.0 orderer capabilities, ensure that all# orderers on channel are at v2.0.0 or later.V2_0: true# 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.Application: &ApplicationCapabilities# V2_0 application capability ensures that peers behave according# to v2.0 application capabilities. Peers from# prior releases would behave in an incompatible way, and are therefore# not able to participate in channels at v2.0 application capability.# Prior to enabling V2.0 application capabilities, ensure that all# peers on channel are at v2.0.0 or later.V2_0: true################################################################################## SECTION: Application## - This section defines the values to encode into a config transaction or# genesis block for application related parameters#################################################################################Application: &ApplicationDefaults# Organizations is the list of orgs which are defined as participants on# the application side of the networkOrganizations:# Policies defines the set of policies at this level of the config tree# For Application policies, their canonical path is# /Channel/Application/<PolicyName>Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"LifecycleEndorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Endorsement:Type: ImplicitMetaRule: "MAJORITY Endorsement"Capabilities:<<: *ApplicationCapabilities################################################################################## SECTION: 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 startOrdererType: etcdraft# 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 definitionAddresses:- :7050EtcdRaft:Consenters:- Host: Port: 7050ClientTLSCert: crypto-config/ordererOrganizations//orderers//tls/server.crtServerTLSCert: crypto-config/ordererOrganizations//orderers//tls/server.crt# Batch Timeout: The amount of time to wait before creating a batchBatchTimeout: 2s# Batch Size: Controls the number of messages batched into a blockBatchSize:# Max Message Count: The maximum number of messages to permit in a batchMaxMessageCount: 10# Absolute Max Bytes: The absolute maximum number of bytes allowed for# the serialized messages in a batch.AbsoluteMaxBytes: 99 MB# Preferred Max Bytes: The preferred maximum number of bytes allowed for# the serialized messages in a batch. A message larger than the preferred# max bytes will result in a batch larger than preferred max bytes.PreferredMaxBytes: 512 KB# Organizations is the list of orgs which are defined as participants on# the orderer side of the networkOrganizations:# Policies defines the set of policies at this level of the config tree# For Orderer policies, their canonical path is# /Channel/Orderer/<PolicyName>Policies:Readers:Type: ImplicitMetaRule: "ANY Readers"Writers:Type: ImplicitMetaRule: "ANY Writers"Admins:Type: ImplicitMetaRule: "MAJORITY Admins"# BlockValidation specifies what signatures must be included in the block# from the orderer for the peer to validate it.BlockValidation:Type: ImplicitMetaRule: "ANY Writers"################################################################################## CHANNEL## This section defines the values to encode into a config transaction or# genesis block for channel related parameters.#################################################################################Channel: &ChannelDefaults# Policies defines the set of policies at this level of the config tree# For Channel policies, their canonical path is# /Channel/<PolicyName>Policies:# Who may invoke the 'Deliver' APIReaders:Type: ImplicitMetaRule: "ANY Readers"# Who may invoke the 'Broadcast' APIWriters:Type: ImplicitMetaRule: "ANY Writers"# By default, who may modify elements at this config levelAdmins:Type: ImplicitMetaRule: "MAJORITY Admins"# Capabilities describes the channel level capabilities, see the# dedicated Capabilities section elsewhere in this file for a full# descriptionCapabilities:<<: *ChannelCapabilities################################################################################## Profile## - Different configuration profiles may be encoded here to be specified# as parameters to the configtxgen tool#################################################################################Profiles:TwoOrgsOrdererGenesis:<<: *ChannelDefaultsOrderer:<<: *OrdererDefaultsOrganizations:- *OrdererOrgCapabilities:<<: *OrdererCapabilitiesConsortiums:SampleConsortium:Organizations:- *Org1- *Org2TwoOrgsChannel:Consortium: SampleConsortium<<: *ChannelDefaultsApplication:<<: *ApplicationDefaultsOrganizations:- *Org1- *Org2Capabilities:<<: *ApplicationCapabilities
2、生成创世块文件和通道文件
生成创世区块
[root@orderer twonodes]# configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block -channelID fabric-channel-11-01 14:27:06.683 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration-11-01 14:27:06.689 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: etcdraft-11-01 14:27:06.689 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 Orderer.EtcdRaft.Options unset, setting to tick_interval:"500ms" election_tick:10 heartbeat_tick:1 max_inflight_blocks:5 snapshot_interval_size:16777216-11-01 14:27:06.689 CST [common.tools.configtxgen.localconfig] Load -> INFO 004 Loaded configuration: configtx.yaml-11-01 14:27:06.691 CST [common.tools.configtxgen] doOutputBlock -> INFO 005 Generating genesis block-11-01 14:27:06.691 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Creating system channel genesis block-11-01 14:27:06.691 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
生成通道文件
[root@orderer twonodes]# configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel-11-01 14:28:02.995 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration-11-01 14:28:03.001 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml-11-01 14:28:03.001 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 003 Generating new channel configtx-11-01 14:28:03.002 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 004 Writing new channel tx
为 Org1 定义锚节点
[root@orderer twonodes]# configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP-11-01 14:32:33.342 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration-11-01 14:32:33.348 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml-11-01 14:32:33.348 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Generating anchor peer update-11-01 14:32:33.349 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 004 Writing anchor peer update
为 Org2 定义锚节点
[root@orderer twonodes]# configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP-11-01 14:32:46.475 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration-11-01 14:32:46.481 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: configtx.yaml-11-01 14:32:46.481 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Generating anchor peer update-11-01 14:32:46.482 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 004 Writing anchor peer update
查看生成的文件
[root@orderer twonodes]# ls channel-artifacts/channel.tx genesis.block Org1MSPanchors.tx Org2MSPanchors.tx
三、配置各个节点
1、编写docker-compose文件
[root@orderer twonodes]# vim docker-compose.yamlversion: '2.4'volumes::peer0.:peer0.:networks:test:name: twonodes_testservices::container_name: image: hyperledger/fabric-orderer:latestlabels:service: hyperledger-fabricenvironment:- FABRIC_LOGGING_SPEC=INFO- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0- ORDERER_GENERAL_LISTENPORT=7050- ORDERER_GENERAL_LOCALMSPID=OrdererMSP- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp# enabled TLS- ORDERER_GENERAL_TLS_ENABLED=true- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_KAFKA_TOPIC_REPLICATIONFACTOR=1- ORDERER_KAFKA_VERBOSE=true- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_GENERAL_GENESISMETHOD=file- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block- ORDERER_CHANNELPARTICIPATION_ENABLED=true- ORDERER_ADMIN_TLS_ENABLED=true- ORDERER_ADMIN_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt- ORDERER_ADMIN_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key- ORDERER_ADMIN_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_ADMIN_TLS_CLIENTROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]- ORDERER_ADMIN_LISTENADDRESS=0.0.0.0:7053working_dir: /opt/gopath/src//hyperledger/fabriccommand: orderervolumes:- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block- ./crypto-config/ordererOrganizations//orderers//msp:/var/hyperledger/orderer/msp- ./crypto-config/ordererOrganizations//orderers//tls/:/var/hyperledger/orderer/tls- :/var/hyperledger/production/ordererports:- 7050:7050- 7053:7053networks:- testpeer0.:container_name: peer0.image: hyperledger/fabric-peer:latestlabels:service: hyperledger-fabricenvironment:#Generic peer variables- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=twonodes_test- FABRIC_LOGGING_SPEC=INFO#- FABRIC_LOGGING_SPEC=DEBUG- CORE_PEER_TLS_ENABLED=true- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Peer specific variabes- CORE_PEER_ID=peer0.- CORE_PEER_ADDRESS=peer0.:7051- CORE_PEER_LISTENADDRESS=0.0.0.0:7051- CORE_PEER_CHAINCODEADDRESS=peer0.:7052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:7052- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.:7051- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.:7051- CORE_PEER_LOCALMSPID=Org1MSPvolumes:- /var/run/docker.sock:/host/var/run/docker.sock- ./crypto-config/peerOrganizations//peers/peer0./msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations//peers/peer0./tls:/etc/hyperledger/fabric/tls- peer0.:/var/hyperledger/productionworking_dir: /opt/gopath/src//hyperledger/fabric/peercommand: peer node startports:- 7051:7051networks:- testpeer0.:container_name: peer0.image: hyperledger/fabric-peer:latestlabels:service: hyperledger-fabricenvironment:#Generic peer variables- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=twonodes_test- FABRIC_LOGGING_SPEC=INFO#- FABRIC_LOGGING_SPEC=DEBUG- CORE_PEER_TLS_ENABLED=true- CORE_PEER_PROFILE_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt# Peer specific variabes- CORE_PEER_ID=peer0.- CORE_PEER_ADDRESS=peer0.:9051- CORE_PEER_LISTENADDRESS=0.0.0.0:9051- CORE_PEER_CHAINCODEADDRESS=peer0.:9052- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:9052- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.:9051- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.:9051- CORE_PEER_LOCALMSPID=Org2MSPvolumes:- /var/run/docker.sock:/host/var/run/docker.sock- ./crypto-config/peerOrganizations//peers/peer0./msp:/etc/hyperledger/fabric/msp- ./crypto-config/peerOrganizations//peers/peer0./tls:/etc/hyperledger/fabric/tls- peer0.:/var/hyperledger/productionworking_dir: /opt/gopath/src//hyperledger/fabric/peercommand: peer node startports:- 9051:9051networks:- testcli1:container_name: cli1image: hyperledger/fabric-tools:latesttty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli1- CORE_PEER_ADDRESS=peer0.:7051- CORE_PEER_LOCALMSPID=Org1MSP- CORE_PEER_TLS_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/server.crt- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//users/Admin@/mspworking_dir: /opt/gopath/src//hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src//hyperledger/fabric-cluster/chaincode/go- ./crypto-config:/opt/gopath/src//hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src//hyperledger/fabric/peer/channel-artifactsnetworks:- testcli2:container_name: cli2image: hyperledger/fabric-tools:latesttty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock- FABRIC_LOGGING_SPEC=INFO- CORE_PEER_ID=cli2- CORE_PEER_ADDRESS=peer0.:9051- CORE_PEER_LOCALMSPID=Org2MSP- CORE_PEER_TLS_ENABLED=true- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/server.crt- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/server.key- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//users/Admin@/mspworking_dir: /opt/gopath/src//hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/- ./chaincode/go/:/opt/gopath/src//hyperledger/fabric-cluster/chaincode/go- ./crypto-config:/opt/gopath/src//hyperledger/fabric/peer/crypto/- ./channel-artifacts:/opt/gopath/src//hyperledger/fabric/peer/channel-artifactsnetworks:- test
2、运行docker-compose文件
[root@orderer twonodes]# docker-compose up -dCreating network "twonodes_test" with the default driverCreating volume "" with default driverCreating volume "twonodes_peer0." with default driverCreating volume "twonodes_peer0." with default driverCreating peer0. ... doneCreating... doneCreating peer0. ... doneCreating cli2 ... doneCreating cli1 ... done[root@orderer twonodes]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScc393bc2da45 hyperledger/fabric-peer:latest"peer node start" 5 seconds ago Up 4 seconds 0.0.0.0:7051->7051/tcp, :::7051->7051/tcppeer0.b299acc7a3d6 hyperledger/fabric-tools:latest"/bin/bash" 5 seconds ago Up 4 secondscli15b20e3a2419f hyperledger/fabric-tools:latest"/bin/bash" 5 seconds ago Up 4 secondscli2c1ffa9d27ff1 hyperledger/fabric-peer:latest"peer node start" 5 seconds ago Up 4 seconds 7051/tcp, 0.0.0.0:9051->9051/tcp, :::9051->9051/tcppeer0.693cb6b585ce hyperledger/fabric-orderer:latest "orderer" 5 seconds ago Up 4 seconds 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp
四、通道操作
1、创建通道
进入cli1容器
[root@orderer twonodes]# docker exec -it cli1 bash
创建通道
bash-5.1# peer channel create -o :7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//msp/tlscacerts/-cert.pem-11-01 06:42:36.437 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized-11-01 06:42:36.457 UTC [mon] readBlock -> INFO 002 Expect block, but got status: &{NOT_FOUND}-11-01 06:42:36.459 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized-11-01 06:42:36.660 UTC [mon] readBlock -> INFO 004 Expect block, but got status: &{SERVICE_UNAVAILABLE}-11-01 06:42:36.663 UTC [channelCmd] InitCmdFactory -> INFO 005 Endorser and orderer connections initialized-11-01 06:42:36.865 UTC [mon] readBlock -> INFO 006 Expect block, but got status: &{SERVICE_UNAVAILABLE}-11-01 06:42:36.868 UTC [channelCmd] InitCmdFactory -> INFO 007 Endorser and orderer connections initialized-11-01 06:42:37.070 UTC [mon] readBlock -> INFO 008 Expect block, but got status: &{SERVICE_UNAVAILABLE}-11-01 06:42:37.073 UTC [channelCmd] InitCmdFactory -> INFO 009 Endorser and orderer connections initialized-11-01 06:42:37.274 UTC [mon] readBlock -> INFO 00a Expect block, but got status: &{SERVICE_UNAVAILABLE}-11-01 06:42:37.277 UTC [channelCmd] InitCmdFactory -> INFO 00b Endorser and orderer connections initialized-11-01 06:42:37.479 UTC [mon] readBlock -> INFO 00c Received block: 0bash-5.1# lschannel-artifacts crypto mychannel.blockbash-5.1# exitexit
将通道文件 mychannel.block 拷贝到cli2
[root@orderer twonodes]# docker cp cli1:/opt/gopath/src//hyperledger/fabric/peer/mychannel.block ./[root@orderer twonodes]# docker cp mychannel.block cli2:/opt/gopath/src//hyperledger/fabric/peer/
2、加入通道
1、peer0节点加入通道
cli1容器操作
[root@orderer twonodes]# docker exec -it cli1 bashbash-5.1# peer channel join -b mychannel.block-11-01 06:45:10.236 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized-11-01 06:45:10.253 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
cli2容器操作
[root@orderer ~]# docker exec -it cli2 bashbash-5.1# lschannel-artifacts crypto mychannel.blockbash-5.1# peer channel join -b mychannel.block-11-01 06:45:07.964 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized-11-01 06:45:07.983 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2、更新锚节点
cli1容器操作
bash-5.1# peer channel update -o :7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem-11-01 06:47:09.550 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized-11-01 06:47:09.562 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
cli2容器操作
bash-5.1# peer channel update -o :7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem-11-01 06:47:21.680 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized-11-01 06:47:21.694 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
五、安装调用智能合约
1、复制官方实例链码
[root@orderer twonodes]# cp ~/fabric-samples/chaincode/sacc/sacc.go ./chaincode/go/
2、安装go依赖并打包链码
[root@orderer twonodes]# docker exec -it cli1 bashbash-5.1# cd /opt/gopath/src//hyperledger/fabric-cluster/chaincode/go/bash-5.1# lssacc.gobash-5.1# go env -w GOPROXY=,directbash-5.1# go env -w GO111MODULE=autobash-5.1# go mod initgo: creating new go.mod: module /hyperledger/fabric-cluster/chaincode/gogo: to add module requirements and sums:go mod tidybash-5.1# go mod vendorgo: finding module for package /hyperledger/fabric-protos-go/peergo: finding module for package /hyperledger/fabric-chaincode-go/shimgo: downloading /hyperledger/fabric-protos-go v0.0.0-1006172752-14f4318ce71cgo: downloading /hyperledger/fabric-chaincode-go v0.0.0-0718160520-38d29fabecb9go: found /hyperledger/fabric-chaincode-go/shim in /hyperledger/fabric-chaincode-go v0.0.0-0718160520-38d29fabecb9go: found /hyperledger/fabric-protos-go/peer in /hyperledger/fabric-protos-go v0.0.0-1006172752-14f4318ce71cgo: downloading /golang/protobuf v1.3.2go: downloading /grpc v1.23.0go: downloading /genproto v0.0.0-0831171423-11092d34479bgo: downloading /x/net v0.0.0-0522155817-f3200d17e092go: downloading /x/sys v0.0.0-0710143415-6ec70d6a5542go: downloading /x/text v0.3.0bash-5.1# peer lifecycle chaincode package sacc.tar.gz \--path /hyperledger/fabric-cluster/chaincode/go/ \--label sacc_1bash-5.1# lschannel-artifacts crypto mychannel.block sacc.tar.gz
3、将打包好的链码复制到cli2
[root@orderer twonodes]# docker cp cli1:/opt/gopath/src//hyperledger/fabric/peer/sacc.tar.gz ./[root@orderer twonodes]# lschaincodeconfigtx.yaml crypto-config.yaml mychannel.blockchannel-artifacts crypto-config docker-compose.yaml sacc.tar.gz[root@orderer twonodes]# docker cp sacc.tar.gz cli2:/opt/gopath/src//hyperledger/fabric/peer/
4、安装链码
cli1容器操作
[root@orderer twonodes]# docker exec -it cli1 bashbash-5.1# peer lifecycle chaincode install sacc.tar.gz-11-01 07:05:52.655 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nGsacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621\022\006sacc_1" >-11-01 07:05:52.655 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: sacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621
cli2容器操作
[root@orderer twonodes]# docker exec -it cli2 bashbash-5.1# peer lifecycle chaincode install sacc.tar.gz-11-01 07:06:03.411 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nGsacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621\022\006sacc_1" >-11-01 07:06:03.411 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: sacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621
5、批准链码
注:–package-id引用的链码为安装链码时的ID
cli1容器操作
bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621 --sequence 1 --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem-11-01 07:09:12.872 UTC [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: :7050-11-01 07:09:14.889 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [d84eb0fcb3435f990f5d95352e17b07d7916791febc7eefa1b1af1a527d8] committed with status (VALID) at peer0.:7051
cli2容器操作
bash-5.1# peer lifecycle chaincode approveformyorg --channelID mychannel --name sacc --version 1.0 --init-required --package-id sacc_1:0730c33ef4216613f9edf82e02d6d7dc02156d6426f38eda47b5b1a171c2f621 --sequence 1 --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem-11-01 07:10:27.069 UTC [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: :7050-11-01 07:10:29.086 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [d80d3a1196d785255258ed3b6f1df4714d757a332f415a2f757faa3c2eb75b66] committed with status (VALID) at peer0.:9051
6、查看链码是否就绪
cli1容器操作
bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name sacc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem --output json{"approvals": {"Org1MSP": true,"Org2MSP": true}}
cli2容器操作
bash-5.1# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name sacc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem --output json{"approvals": {"Org1MSP": true,"Org2MSP": true}}
7、提交链码
cli1容器或cli2容器皆可操作
bash-5.1# peer lifecycle chaincode commit -o :7050 --channelID mychannel --name sacc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem --peerAddresses peer0.:7051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt --peerAddresses peer0.:9051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt-11-01 07:13:00.000 UTC [chaincodeCmd] ClientWait -> INFO 001 txid [e19e76e392da5077b63cd794a54290992a2311fef97673466ddd13abbdf1bbb8] committed with status (VALID) at peer0.:7051-11-01 07:13:00.000 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [e19e76e392da5077b63cd794a54290992a2311fef97673466ddd13abbdf1bbb8] committed with status (VALID) at peer0.:9051
8、链码初始化
bash-5.1# peer chaincode invoke -o :7050 --isInit --ordererTLSHostnameOverride --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem -C mychannel -n sacc --peerAddresses peer0.:7051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt --peerAddresses peer0.:9051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt -c '{"Args":["a","bb"]}'-11-01 07:15:09.712 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
9、查询数据
cli1容器查询
bash-5.1# peer chaincode query -C mychannel -n sacc -c '{"Args":["query","a"]}'bb
cli2容器新增数据
[root@orderer ~]# docker exec -it cli2 bashbash-5.1# peer chaincode invoke -o :7050 --tls true --cafile /opt/gopath/src//hyperledger/fabric/peer/crypto/ordererOrganizations//orderers//msp/tlscacerts/-cert.pem -C mychannel -n sacc --peerAddresses peer0.:7051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt --peerAddresses peer0.:9051 --tlsRootCertFiles /opt/gopath/src//hyperledger/fabric/peer/crypto/peerOrganizations//peers/peer0./tls/ca.crt -c '{"Args":["set","a","cc"]}'-11-01 07:16:22.338 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"cc"
查询数据
bash-5.1# peer chaincode query -C mychannel -n sacc -c '{"Args":["query","a"]}'cc