Fabric笔记3

运行以下页面中的脚本自动下载镜像

https://hyperledger-fabric.readthedocs.io/en/release-1.2/install.html

docker pull hyperledger/fabric-ca:x86_64-1.2.0 \

&& docker pull hyperledger/fabric-peer:x86_64-1.2.0 \

&& docker pull hyperledger/fabric-orderer:x86_64-1.2.0 \

&& docker pull hyperledger/fabric-couchdb:x86_64-1.2.0 \

&& docker pull hyperledger/fabric-tools:x86_64-1.2.0

运行以下脚本禁止更新镜像

$ docker images

$ docker tag hyperledger/fabric-ca:1.2.0 hyperledger/fabric-ca \

&& docker tag hyperledger/fabric-peer:1.2.0 hyperledger/fabric-peer \

&& docker tag hyperledger/fabric-orderer:1.2.0 hyperledger/fabric-orderer \

&& docker tag hyperledger/fabric-couchdb:0.4.10 hyperledger/fabric-couchdb \

&& docker tag hyperledger/fabric-tools:1.2.0 hyperledger/fabric-tools\

&& docker tag hyperledger/fabric-ccenv:1.2.0 hyperledger/fabric-ccenv

 

启动docker

service docker start

需要升级docker中的go版本

wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

sudo rm -rf /opt/go

sudo tar -C /opt -xzvf go1.10.3.linux-amd64.tar.gz

export $GOPATH=/opt/gopath

export $GOROOT=/usr/local/go

进⼊ docker 容器

docker exec -it cli bash

(开始)

往docker中传文件:

docker inspect -f '{{.Id}}' cli

43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22

docker cp mychaincode/ 43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22:/opt/gopath/src/

先要备份peer文件夹

mkdir -p /opt/go/src/github.com/hyperledger/fabric

docker cp /opt/gopath/src/github.com/hyperledger/fabric

43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22:/opt/go/src/github.com/hyperledger/

#从docker拷出文件

#docker cp 43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22:/opt/gopath/src/github.com/hyperledger/fabric/peer /opt/gopath/src/github.com/hyperledger/fabric/peer

 

# docker cp go1.10.3.linux-amd64.tar.gz 43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22:/opt/gopath/src/github.com/hyperledger/fabric

# docker cp /opt/gopath/src/mychaincode 43e1a09ae943e91e721c72f70fc2282bb04a210da085de741404632e9c769e22:/opt/gopath/src

部署:

peer chaincode install -n tang -v 8.8 -p mychaincode

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 tangchannel -n tang -v 8.8 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

查询

peer chaincode query -C tangchannel -n tang -c '{"Args":["query","a"]}'

peer chaincode query -C tangchannel -n tang -c '{"Args":["query","0:287"]}'

调用

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 tangchannel -n tang -c '{"Args":["billing","a","b","100","billing"]}'

升级

peer chaincode upgrade -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 tangchannel -n tang -v 8.8 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

 

运行和启动Fabric例子

1、下载hyperledger/fabric-sample

2、切换到release-1.0

git checkout release-1.2

git branch -vv

3、下载bin到fabric-sample目录

https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.0.0/

https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/

wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.2.0/hyperledger-fabric-linux-amd64-1.2.0.tar.gz

4、关闭

./byfn.sh -m down

3、进入first-network文件夹

./byfn.sh -m up

#5、初始化

./byfn.sh -m generate

vi byfn.sh

⽣成初始区块(进入开发步骤)

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

$ export FABRIC_CFG_PATH=$PWD

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

⽣成应⽤通道的配置信息

$ export CHANNEL_NAME=tangchannel

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

⽣成锚节点配置更新⽂件

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

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

操作⽹络(v1.2.0后不需要操作Alex.Zhang20180817)

编辑 docker-compose-cli.yaml ,注释到 command 命令

working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer #

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

$TIMEOUT' volumes

操作网络

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

进⼊ docker 容器

$ docker exec -it cli bash

创建通道

$ export CHANNEL_NAME=tangchannel

$ 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

加⼊通道

$ peer channel join -b tangchannel.block

(回上)

 

 

链上代码 (链码)

安装链码

$ peer chaincode install -n tang -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"]}‘

转账

$ 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"]}’

 

以下启动REST API的web服务

在 GOPATH 的 root 目录中创建 docker-compose.yml 文件。粘贴以下内容:

membersrvc:

  image: hyperledger/fabric-membersrvc

  ports:

    - "7054:7054"

  command: membersrvc

vp0:

  image: hyperledger/fabric-peer:x86_64-0.6.0-preview

  ports:

    - "7050:7050"

    - "7051:7051"

    - "7053:7053"

  environment:

    - CORE_PEER_ADDRESSAUTODETECT=true

    - CORE_VM_ENDPOINT=unix:///var/run/docker.sock

    - CORE_LOGGING_LEVEL=DEBUG

    - CORE_PEER_ID=vp0

    - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054

    - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054

    - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054

    - CORE_SECURITY_ENABLED=false

    - CORE_SECURITY_ENROLLID=test_vp0

    - CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT

  links:

    - membersrvc

  command: sh -c "sleep 5; peer node start --peer-chaincodedev"

 

部署示例

Hyperledger Fabric 提供了一个用于与该结构交互的 REST Web 服务接口。与 fabric 的第一次交互是部署链代码。确保本地区块链网络正在运行,然后启动 SoapUI,单击 REST 按钮创建一个新的 REST 项目。您会看到一个类似图 3 的对话框,在其中输入用于所有 REST 请求的基础 URL:

图 3. SoapUI New REST Project 对话框

输入 http://localhost:7050 作为 URL,然后单击 OK。端口 7050 是 fabric 使用的默认 REST 端口,而且因为区块链网络是在本地计算机上运行的,所以将使用 localhost 作为主机名。

在 SoapUI 启动后,可以执行一次快速冒烟测试,以确保它能与本地区块链网络进行通信。展开刚创建的新的 REST 资源,直到看到 Request 1,然后在 Editor 窗口中打开它。使用 GET 方法,在 resource 下输入 /chain。确保单击了 output 选项卡上的 JSON 选项,然后运行请求(通过单击 arrow 图标)。执行此请求时,会在 Editor 窗口右侧的输出选项卡中返回当前区块的哈希值,如图 4 所示

 

 

 

2、关闭原有例子

3、第一终端进入chaincode-docker-devmode,启动网络

docker-compose -f docker-compose-simple.yaml up

4、第二终端进入docker

docker exec -it chaincode bash

5、第二终端进入chaincode文件夹

/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode

6、第二个终端启动chaincode

CORE_PEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=mycc:0 ./chaincode002

7、第三个终端

docker exec -it cli bash

 

安装链码

peer chaincode install -p chaincodedev/chaincode/chaincode002 -n mycc -v 0

实例化链码

peer chaincode instantiate -n mycc -v 0 -c '{"Args":["str","helloworld"]}' -C myc

# 查询链码

$ peer chaincode query -n mycc -c '{"Args":["get","str"]}' -C myc

# 调用链码

peer chaincode invoke -n mycc -c '{"Args":["set", "str", "helloworld111"]}'

-C myc

# 查询链码

$ peer chaincode query -n mycc -c '{"Args":["get","str"]}' -C myc

 

docker rm -f $(docker ps -aq)

 

chaincode002

链码放入:

/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode

 

 

chaincode002.go

 

package main

import(

"fmt"

"github.com/hyperledger/fabric/core/chaincode/shim"

"github.com/hyperledger/fabric/protos/peer"

)

type Helloworld struct {

}

func (t *Helloworld) Init(stub shim.ChaincodeStubInterface) peer.Response{

fmt.Println("helloworld init")

args := stub.GetStringArgs()

err := stub.PutState(args[0],[]byte(args[1]))

if err != nil {

shim.Error(fmt.Sprintf("helloworld init err %s",err))

}

return shim.Success(nil)

}

func (t *Helloworld) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

fn,args := stub.GetFunctionAndParameters()

var result string

var err error

if fn == "set" {

result,err = set(stub,args)

}else{

result,err = get(stub,args)

}

if err != nil {

shim.Error(err.Error())

}

return shim.Success([]byte(result))

}

func set (stub shim.ChaincodeStubInterface, args []string) (string,error){

fmt.Println("helloworld set")

err := stub.PutState(args[0],[]byte(args[1]))

if err != nil {

return "", fmt.Errorf("Failed to set asset: %s", args[0])

}

return args[0],nil

}

func get(stub shim.ChaincodeStubInterface, args []string) (string,error){

value, err := stub.GetState(args[0])

if err != nil {

return "", fmt.Errorf("Failed to get asset: %s", args[0])

}

if value == nil {

return "", fmt.Errorf("Asset not found: %s", args[0])

}

return string(value),nil

}

func main() {

err := shim.Start(new(Helloworld))

if err != nil {

fmt.Printf("shim start err %s",err)

}

}

 

chaincode002_test.go

 

package main

import(

"fmt"

"testing"

"github.com/hyperledger/fabric/core/chaincode/shim" )

func checkInit(t *testing.T, stub *shim.MockStub, args [][]byte) {

res := stub.MockInit("1", args)

if res.Status != shim.OK {

fmt.Println("Init failed", string(res.Message))

t.FailNow()

}

}

func checkQuery(t *testing.T, stub *shim.MockStub, name string) {

res := stub.MockInvoke("1", [][]byte{[]byte("get"), []byte(name)})

if res.Status != shim.OK {

fmt.Println("Query", name, "failed", string(res.Message))

t.FailNow()

}

if res.Payload == nil {

fmt.Println("Query", name, "failed to get value")

t.FailNow()

}

fmt.Println("Query value", name, "was ", string(res.Payload))

}

func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) {

res := stub.MockInvoke("1", args)

if res.Status != shim.OK {

fmt.Println("Invoke", args, "failed", string(res.Message))

t.FailNow()

}

}

func Test_Helloworld(t *testing.T) {

hello := new(Helloworld)

stub := shim.NewMockStub("hello", hello)

checkInit(t, stub, [][]byte{[]byte("str"), []byte("helloworld")})

checkQuery(t, stub, "str")

checkInvoke(t, stub, [][]byte{[]byte("set"), []byte("str"),[]byte("helloworld-1111")})

checkQuery(t, stub, "str")

}

 

 

 

通过 go test -v helloworld_test.go helloworld.go 测试相应的方法,运行结果:

=== RUN Test_Helloworld

helloworld init

Query value str was helloworld

helloworld set

Query value str was helloworld-1111

--- PASS: Test_Helloworld (0.00s)

PASS ok command-line-arguments 0.028s

 

 

https://hyperledger.github.io/composer/latest/tutorials/developer-tutorial

 

https://hyperledger.github.io/composer/latest/business-network/bnd-create

Cloning an Example Business Network Definition

The sample business network definitions are stored on GitHub at https://github.com/hyperledger/composer-sample-networks. You can git clone this repository to pull down all the sample networks. Each sample network is stored under the packages directory.

Generating a Business Network Definition

Generation

  1. yo hyperledger-composer

Copy

? Please select the type of project: (Use arrow keys) ❯ Angular Business Network Model

And select Business Netork

  1. Answer all of the questions

Copy

Welcome to the Hyperledger Composer project generator ? Please select the type of project: Business Network You can run this generator using: 'yo hyperledger-composer:businessnetwork' Welcome to the business network generator ? Business network name: mynetwork ? Description: This is my test network ? Author name: Mr Conga ? Author email: conga@congazone.org ? License: Apache-2 ? Namespace: org.conga create package.json create permissions.acl create README.md create models/org.conga.cto create .eslintrc.yml create features/sample.feature create features/support/index.js create test/logic.js create lib/logic.js

This generates a skeleton business network with an asset, participant and transaction defined, as well as a mocha unit test.

Also included, is a 'best practice' eslint config file which defines sample linting rules for JS code.

 

问题解决

修改

fabric-samples/basic-network/start.sh export FABRIC_START_TIMEOUT=300 //适当调大此值可以改善此问题

 

peer channel list

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值