fabric 网络 合约部署 和 测试

 

 一

1  进入first-network 文件夹

 cd first-network

./byfn.sh -m down //  关闭相关网络

./byfn.sh -m generate //   创建2个节点

2 生成 创世区块

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

2 生成 创世区块

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

 export FABRIC_CFG_PATH=$PWD

../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

3 生成应用通道配置文件

export CHANNEL_NAME=mychannel

../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

 

4

生成锚节点配置文件

节点1

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

节点2

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

 

在 first-network文件夹中 docker-compose-cli.yaml 文件 找到 这个代码

# command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME} ${DELAY}; sleep $TIMEOUT'

将它注释

 

CHANNEL_NAME=\CHANNEL_NAME TIMEOUT=600 docker-compose -f docker-compose-cli.yaml up -d

 

二 创建和配置通道

1  进入docker 环境中

 docker exec -it cli bash

2 创建通道

$ export CHANNEL_NAME=mychannel

 

$ peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

 

3 加入通道

```

$ peer channel join -b mychannel.block

```

 

 

三 链上代码 (链码)

 

### 安装链码  从github.com下 chaincode_example02 

  

$ peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

 

### 实例化链码

 

$ peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

 

## 查询

 

```

$ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

```

**查询结果:**

 

```

Query Result: 100

```

 

## 转账

 

```

$ peer chaincode invoke -o orderer.example.com:7050  --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem  -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'

```

**查询a账户的金额:**

 

```

$ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

 

## 结果

Query Result: 90

```

 

posted @ 2018-04-12 13:15  写代码的小书童  阅读(966)  评论(0编辑  收藏  举报