Feeds:
文章
留言

Archive for the ‘區塊鏈’ Category

最近有機會用 Azure 的 BaaS 試用 Hyperledger Fabric, 稍微紀錄一下, 建立流程很簡單诶什麼特別(少打了很多指令)

選擇 Hyperledger 模板

基本配置

Fabric 配置

資料確認

確定建立

完成上面的精靈導引後就會得到一大坨的資料

建立完成的 Resource Group

SSH 連線資訊可透過下面位置取得

確認一下 Fabric 版本(對~ VM 裡面還是 docker)

fabric-peer 版本 1.0.1

fabric-ca 版本 1.0.1

fabric-orderer 版本 1.0.1

所以 Fabric 本來跑在單台 docker 的變成多台 docker… 對於應用來說沒什麼差別, 預設建立的 Fabric 沒有啟用 TLS 也沒有 couchdb, 從安裝腳本 configure-fabric-azureuser.sh 可以查看 docker container 啟動參數

在 user home 目錄下有需要的相關檔案

+ ~/crypto-config   ; 認證相關資料 
- channel.tx 
- config.log
- configtx.yaml
- configure-fabric-azureuser.sh
- orderer.block

不過奇怪的是內建的 peer 工具居然不能用, 有點冏

還好我用的是自己的工具, 跑完基本測試, 安裝 chaincode 後, 如預期的 Peer VM 新增了 chaincode container

佈署 chaincode 後 Peer VM 的 container

一如預期的執行範例 ^^

 

廣告

Read Full Post »

最近嘗試了這個 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 »

Older Posts »