Feeds:
文章
迴響

Archive for the ‘區塊鏈’ Category

最近嘗試了這個 Hyperledger 實驗項目, 這個項目主要是高度抽象化開發流程, 讓你可以專注在商業流程而不用太關注 Fabric 底層. 在 Balance Transfer 的網路上佈署成功應該可以看到. 第一次佈署成功還蠻爽的~~

開發環境

1. node 6.9.0 (docker)

安裝工具

npm i -g composer-c;o

Clone 範例

git clone https://github.com/hyperledger/composer-sample-networks.git

打包 .bna

cd composer-sample-networks/packages

composer archive create -t dir -n carauction-network

成功應該會輸出一個 carauction-network@0.1.6.bna

產生 Fabric 連線配置檔

佈署 .bna 到 Fabric

composer network deploy -a carauction-network@0.1.6.bna -i admin -s adminpw -l INFO -p balance-transfer

-p 指定的名稱會使用 ~/.composer-connection-profiles/balance-transfer/connection.json 連線配置

順利就會看到開頭的那張結果

測試

composer network list -p balance-transfer -n carauction-network -i admin -s adminpw

composer network ping -p balance-transfer -n carauction-network -i admin -s adminpw

REST API

composer-rest-server -p balance-transfer -n carauction-network -i admin -s adminpw -N always -S false -w false -t false -P 4000

zwlopnf

 

廣告

Read Full Post »

範例原始碼, 順利把 TODO 範例整合到我的 Fabric 網路

整合上比 marbles 簡單許多

 

Read Full Post »

marbles 專案入口, 蠻有視覺化效果的範例, 嘗試整合到 balance transfer 的網路, 試了幾天終於成功接起來了

Read Full Post »

Peer 節點需要設定環境變數 CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984

Read Full Post »

測著測著就來到 1.0 正式版.. 這算是目前測過比較完整的官方範例, 包含後端的網路建立(Peer*3, Orderer*1, CA*2), ChainCode *1, 前端的 Fabric-sdk-node 呼叫測試

快速建立後端網路

  1. 下載 https://github.com/hyperledger/fabric-samples/tree/master/balance-transfer
  2. 調整 artifacts/docker-compose.yaml 的 volumes 對應
  3. docker-compose -f docker-compose.yaml up -d

網路初始的容器狀態

建立 fabric-sdk-node 容器

  1. docker pull node:6.9.0
    • 官方說法目前不支援 7.x, 8.x
  2. 修改 balance-transfer/config.json 和 balance-transfer/app/network-config.json 的 localhost 成 docker mahcine ip
    • docker machine ip 可用 docker-machine ip 取得
  3. docker run -it –name fsn -v /host/balance-transfer:/code node:6.9.0 bash
  4. mkdir -p /tmp/app
  5. cp -r /code/. /tmp/app
    • 因為直接在 volume 映射目錄初始化會出現建立 soft link 失敗
  6. cd /tmp/app
  7. npm install
  8. node app.js
    • 啟動 express web server, port 4000

測試 REST API

  • docker exec -it fan bash
    • 進入 node 容器

      取得 Token

curl -s -X POST \
    http://localhost:4000/users \
    -H "content-type: application/x-www-form-urlencoded" \
    -d 'username=Jim&orgName=org1'

記下 token (有時效性, 過期重取) “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA"

建立 channel

  • channel 名稱 “mychannel"
curl -s -X POST \
  http://localhost:4000/channels \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json" \
  -d '{
    "channelName":"mychannel",
    "channelConfigPath":"../artifacts/channel/mychannel.tx"
}'

加入 channel

curl -s -X POST \
  http://localhost:4000/channels/mychannel/peers \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json" \
  -d '{
    "peers": ["192.168.99.100:7051","192.168.99.100:7056"]
}'

安裝 chaincode

curl -s -X POST \
  http://localhost:4000/chaincodes \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json" \
  -d '{
    "peers": ["192.168.99.100:7051","192.168.99.100:7056"],
    "chaincodeName":"mycc",
    "chaincodePath":"github.com/example_cc",
    "chaincodeVersion":"v0"
}'

啟用

curl -s -X POST \
  http://localhost:4000/channels/mychannel/chaincodes \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json" \
  -d '{
    "peers": ["192.168.99.100:7051"],
    "chaincodeName":"mycc",
    "chaincodeVersion":"v0",
    "functionName":"init",
    "args":["a","100","b","200"]
}'

Invoke request

curl -s -X POST \
  http://localhost:4000/channels/mychannel/chaincodes/mycc \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json" \
  -d '{
    "peers": ["192.168.99.100:7051", "192.168.99.100:7056"],
    "fcn":"move",
    "args":["a","b","10"]
}'

Chaincode Query

curl -s -X GET \
  "http://localhost:4000/channels/mychannel/chaincodes/mycc?peer=peer1&fcn=query&args=%5B%22a%22%5D" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

使用區塊編號查詢區塊資訊

  • /blocks/BlockID
curl -s -X GET \
  "http://localhost:4000/channels/mychannel/blocks/1?peer=peer1" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

使用 TransactionID 查詢交易

  • transactions/TxID
    • TxID 可透過 invoke chaincode 返回值取得也可透過
curl -s -X GET http://localhost:4000/channels/mychannel/transactions/bb6c007aa035cf9eb29154e6e3d90fb752d3e853adbc49c84d68385482b184be?peer=peer1 \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

Query ChainInfo

curl -s -X GET \
  "http://localhost:4000/channels/mychannel?peer=peer1" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

Query Installed chaincodes

curl -s -X GET \
  "http://localhost:4000/chaincodes?peer=peer1&type=installed" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

Query Instantiated chaincodes

curl -s -X GET \
  "http://localhost:4000/chaincodes?peer=peer1&type=instantiated" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

Query Channels

curl -s -X GET \
  "http://localhost:4000/channels?peer=peer1" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTk4NTUwMzcsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTk4MTkwMzd9.McFgn4pABM_uqv2vtVbU5SaLWy43OO-q_DkCsEOfcxA" \
  -H "content-type: application/json"

Read Full Post »

map 範例蠻適合快速了解 stub 有哪些 api 可用, 快速操作步驟

Read Full Post »

上次才剛玩 beta1 今天他就已經變成 RC1, 立馬更新繼續測試…

 

測試環境

  • Windows 7 x64
  • Docker 17.05.0-ce
  • VirtualBox 5.1.6
  • git 2.9.2(curl.exe, bash.ex)
  • bootstrap-1.0.0-rc1.sh

目錄結構

  • c:\docker
    • /fabric-samples
      • /bin
      • /basic-network
      • /chaincode-docker-devmode
      • /fabcar
      • /first-network

VirtualBox 共享目錄對應

  • /host : C:\docker

環境初始化

c:
cd docker
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap-1.0.0-rc1.sh | bash
curl https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/windows-amd64-1.0.0-rc1/hyperledger-fabric-windows-amd64-1.0.0-rc1.tar.gz | tar xz

初始化完成必要的 image

修改 *.yaml 的 volumes

  • 調整對應到 /host/fabric-samples 路徑

範例: chaincode-docker-devmode

Console1: 啟動服務

cd c:\docker\fabric-samples\chaincode-docker-devmode docker-compose -f docker-compose-simple.yaml up

Console2: 編譯/執行 ChainCode

  • 也可先用 go test 執行測試
docker exec -it chaincode bash
cd chaincode_example02
go build
CORE_PEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=mycc:0 ./chaincode_example02

執行結果

Console3: 使用 ChainCode

docker exec -it cli bash

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

peer chaincode instantiate -n mycc -v 0 -c ‘{“Args":[“init","a","100″,"b","200″]}’ -C myc

instantiate

peer chaincode invoke -n mycc -c ‘{“Args":[“invoke","a","b","10″]}’ -C myc

invoke

peer chaincode query -n mycc -c ‘{“Args":[“query","a"]}’ -C myc

query

程式更新

  • 版號更新為 ‘1.1’ (string, 每次異動都要不同)

CORE_PEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=mycc:1.1 ./chaincode_example02

跑完全部流程應該會看到這個

peer chaincode install -p chaincodedev/chaincode/chaincode_example02 -n mycc -v 1.1

peer chaincode upgrade -C myc -n mycc -v 4 -c ‘{“Args":[“init","a", “10″, “b","20″]}’

peer chaincode invoke -C myc -n mycc -c ‘{“Args":[“create","arick"]}’

peer chaincode query -n mycc -c ‘{“Args":[“query","arick"]}’ -C myc

參考資料

Read Full Post »