使用IBM Bluemix Blockchain Service在 Blockchain Network 开发 chaincode
2016-09-19 03:15
786 查看
#使用IBM Bluemix Blockchain Service在 Blockchain Network 开发 chaincode
安装 github desktop (https://desktop.github.com/)
$ git config
“enrollId”: “user_type1_736394e032”,
“enrollSecret”: “7a59adadco”
}
secureContext: 步骤 21
{
“jsonrpc”: “2.0”,
“method”: “deploy”,
“params”: {
“type”: 1,
“chaincodeID”: {
“path”: “https://github.com/xxxxxxxxxxx/learn-chaincode/finished”
},
“ctorMsg”: {
“function”: “init”,
“args”: [
“hi there”
]
},
“secureContext”: “user_type1_xxxxxxxxxxxxxxxxxxxxx”
},
“id”: 1
}
secureContext: 步骤 21
{
“jsonrpc”: “2.0”,
“method”: “query”,
“params”: {
“type”: 1,
“chaincodeID”: {
“name”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
},
“ctorMsg”: {
“function”: “read”,
“args”: [
“hello_world”
]
},
“secureContext”: “user_type1_xxxxxxxxx”
},
“id”: 2
}
2.Bluemix 可选择 Region
api.ng.bluemix.net (US South)
api.eu-gb.bluemix.net (United Kingdom)
api.au-syd.bluemix.net (Sydney)
2.https://www.ibm.com/internet-of-things/iot-news/announcements/private-blockchain/ 点选 [play]
3.https://console.ng.bluemix.net/docs/services/blockchain/ibmblockchain_tutorials.html
4.https://github.com/hyperledger
5.http://www.ibm.com/blockchain/for_developers.html
6.https://github.com/IBM-Blockchain/chaincode-investigator
7.https://console.ng.bluemix.net/docs/services/IoT/blockchain/dev_blockchain.html
8.https://console.ng.bluemix.net/docs/services/IoT/bl_blockchain_integration.html
9.https://console.ng.bluemix.net/docs/services/IoT/reference/extensions/index.html
10.http://bramwelt-fabric.readthedocs.io/en/latest/FAQ/chaincode_FAQ.html
11.http://www.multichain.com/blog/2016/04/beware-impossible-smart-contract/
12.https://github.com/ibm-watson-iot/blockchain-samples/tree/master/simple_contract_hyperledger
13.https://github.com/hyperledger/fabric/blob/master/docs/API/CoreAPI.md?cm_mc_uid=61030309207314713163325&cm_mc_sid_50200000=1473623034#chaincode
14.https://1drv.ms/f/s!AkBmzWP0h-Vxbr7h1uTNsTMcPWI
15.https://1drv.ms/b/s!AkBmzWP0h-VxazOBOsVz8nGMLpM
16.https://1drv.ms/b/s!AkBmzWP0h-VxbLK7FOjH9IL5HHc
17.https://www.youtube.com/watch?v=24JAF9Eu1mk&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=74
18.https://www.youtube.com/watch?v=wSmJ-rwUo8I&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=73
19.https://www.youtube.com/watch?v=SOsn6MvGH60&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=72
作者:陳兆麟 Email:chenlin2@ms9.hinet.net
区块链 (blockchain) 技术,最大特色在于[去集中化管理, no Server],所有决策由多数参与者达成[共识]后决定,好处是降低系统的营运成本,并透过密码学的加解密,数字签章及杂凑(hashing)确保系统交易的安全,IBM 整合 Bluemix 的Watson IoT (物联网) & Blockchain service (参考文献 2),此范例之货物运输合约要确保运送物品温度处于摄氏0度C之下,若高于0度C则当时负责单位必须负责赔偿货物损失责任;温度传感器可嵌入在货物包装;参与货物运输之不同行业 (工厂,海运,陆运,报关行,银行,杂货商)可以透过internet 接收货物运输时物品所处现况(如温度,位置)执行相关业务;陆运公司可判断何时派车至港口接收到达港口之货柜; 报关行可预知货品到达目的地时间以便办理报关动作,银行透过报关行,杂货商的确认付款给工厂。运送过程中若温度超过 0度C之上,则启动能智能合约 (chaincode) 将温度记录在block;除了监控温度外,每一阶段发生的事件必须多方达成共识才能将 block加入blockchain ,此一系统运作并无集中管理机构的参与,而是在各方参与者达成共识后运作系统。智能合约 (chaincode) 是由程序代码构成,IBM-blockchain是以Golang 撰写 chaincode,事件发生则起动 block 内相对应 chaincode。以下范例是在 IBM Bluemix blockchain service 环境下,使用Swagger API page开发及执行chainocde。
開發環境:开发环境: Win7 (64 bit) ,Bluemix , Blockchain, Golang
1.win7 安装 在 win7 安装 Go (https://golang.org/dl/)
2. 安装后将安装路径设定在系统变量
3. 建立工作目录 D:\GoProjects,建立 GOPATH 变量
$ set GOPATH=D:\GoProjects
$ go version
go version go1.7 windows/amd644. win7 安装 nodejs, npm (https://nodejs.org/en/ )
$node -v
v6.5.0
$npm -v
2.15.9
win7 安装 git (https://git-scm.com/download/win)
$git version
git version 2.9.3
win7 安装 cygwin (https://cygwin.com/install.html)
将安装之执行文件目录 (D:\cygwin64\bin) 设定在系统 path 变量5. 安装超级分类帐(Hyperledger)的垫片码(shim code)
$cd D:\GoProjects
$ go get github.com/hyperledger/fabric/core/chaincode/shim
6. 建立 github账号
登入github 建立账号 (https://github.com/ 纪录 username 及 password)安装 github desktop (https://desktop.github.com/)
$ git config --
global user.name “github username” (github username=账号)
$ git config user.name (显示 github 账号)
< github username>
7. 安装范例 learn-chaincode
浏览器:https://github.com/IBM-Blockchain/learn-chaincode (点选 fork )8. 确认learn-chaincode 已 clone 至你的 repository, 在Bluemix blockchain
service 环境必须使用安装在 github 内之 chaincode.
9. 将 github 的 learn-chaincode repository clone 至 lcoal machine
$cd :\GoProjects
$mkdir -p src\github.com\< github username>\
$cd src\github.com\< github username>\
$git clone https://github.com/< github username>/learn-chaincode.git
$ ls src\github.com\< github username>\
10. learn-chaincode 目录下 chaincode 范例有 2 版本, start (只具有框架的 chaincode), finished (已经开发完毕的 chaincde);若要在 local开发 chaincode先要确定在 local 环境能够编译 chaincode_start.go
$cd D:\GoProjects\src\github.com\< yourgithubid >\learn-chaincode\start
$go build ./ (正常结束必须没有任何 text or error 输出至屏幕)
(若有誤修正) *shim.ChaincodeStub => shim.ChaincodeStubInterface11.使用 bluemix blockchain service之 Swagger UI 接口执行测试 chaincode ; 登录IBM Bluemix:https://console.ng.bluemix.net/ 点选 Sign Up; 注册后登录 (30天试用期免费) 将 username及 password 记录。
建立 Organization 名称
建立 Space 名称 Organization, Space Region (US South)
12. 点选型录 (CATALOG) =>选择 [Blockchain IBM BETA] 服务
13. Service name: myblockchain => CREATE
14. 点选 Service Crendentials (显示 Service Crendentials 内容)
15. 点选 launch
16. 点选 Logs 开启 log 檔
显示 log 文件内容
17. 点选 Networks 显示 chaincode ID (下图显示目前无 chaincode)
18. 点选 APIs 显示 API interactive documentation (进入Swagger API page)
19. 展开 +Network’s Enroll IDs 记录:ID=user_type1_736394e032, Secret=7a59adadco (有多項 ID 可供選擇)
20. 点选 Registrar -> POST/resgistrar
21. 以ID=user_type1_736394e032, Secret=7a59adadco 注册 =>[Try is out]
{“enrollId”: “user_type1_736394e032”,
“enrollSecret”: “7a59adadco”
}
22. 回应 [Login successful] 如下:显示注册成功
23. 点选 Chaincode => GET/chain
24. 透过 rest interface,使用者送出请求将github 之 chaincode deploy 至 blockchain network之节点 (peer),以下为请求格式,贴入DeploySpec 字段
path: 步骤 9secureContext: 步骤 21
{
“jsonrpc”: “2.0”,
“method”: “deploy”,
“params”: {
“type”: 1,
“chaincodeID”: {
“path”: “https://github.com/xxxxxxxxxxx/learn-chaincode/finished”
},
“ctorMsg”: {
“function”: “init”,
“args”: [
“hi there”
]
},
“secureContext”: “user_type1_xxxxxxxxxxxxxxxxxxxxx”
},
“id”: 1
}
25. 将chaincode deploy至 peer之结果,传回 chaincode ID (128 character)
若要执行已安装在 peer之 chaincode 则参考 chaincode ID 即可
26. 若要执行 chaincode 之 query 函式,将执行query 文稿,贴入QuerySpec 字段
chaincodeId: 步骤 25secureContext: 步骤 21
{
“jsonrpc”: “2.0”,
“method”: “query”,
“params”: {
“type”: 1,
“chaincodeID”: {
“name”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
},
“ctorMsg”: {
“function”: “read”,
“args”: [
“hello_world”
]
},
“secureContext”: “user_type1_xxxxxxxxx”
},
“id”: 2
}
27. chaincode之query函式执行后回应hi there是先前chaincode deploy的结果
28. 若要执行 chaincode 之invoke 函式,将执行invoke文稿,贴入InvokeSpec字段
chaincodeId: 步骤 25 secureContext: 步骤 21 { "jsonrpc": "2.0", "method": "invoke", "params": { "type": 1, "chaincodeID": { "name": "xxxxxxxxxxxxxxxxxxxxxx" }, "ctorMsg": { "function": "write", "args": [ "hello_world", "go away" ] }, "secureContext": "user_type1_xxxxxxxxx" }, "id": 3 }
29. chaincode之 invoke函式执行后响应如下,同时将 [key value of hello_world] 设定为 go away
30 重复执行步骤 26 (执行 chaincode query function),显示 [key value of hello_world] 已被改为 go away
31. 使用 Swagger API page 的优点在于不必等到系统完工后再去测试 chaincode 是否正确,Blockchcin Service 目前为 Beta 版,会有不稳定的问题。
附录:
1.bluemix 全球系统维护现况2.Bluemix 可选择 Region
api.ng.bluemix.net (US South)
api.eu-gb.bluemix.net (United Kingdom)
api.au-syd.bluemix.net (Sydney)
參考文獻:
1.https://github.com/IBM-Blockchain/learn-chaincode2.https://www.ibm.com/internet-of-things/iot-news/announcements/private-blockchain/ 点选 [play]
3.https://console.ng.bluemix.net/docs/services/blockchain/ibmblockchain_tutorials.html
4.https://github.com/hyperledger
5.http://www.ibm.com/blockchain/for_developers.html
6.https://github.com/IBM-Blockchain/chaincode-investigator
7.https://console.ng.bluemix.net/docs/services/IoT/blockchain/dev_blockchain.html
8.https://console.ng.bluemix.net/docs/services/IoT/bl_blockchain_integration.html
9.https://console.ng.bluemix.net/docs/services/IoT/reference/extensions/index.html
10.http://bramwelt-fabric.readthedocs.io/en/latest/FAQ/chaincode_FAQ.html
11.http://www.multichain.com/blog/2016/04/beware-impossible-smart-contract/
12.https://github.com/ibm-watson-iot/blockchain-samples/tree/master/simple_contract_hyperledger
13.https://github.com/hyperledger/fabric/blob/master/docs/API/CoreAPI.md?cm_mc_uid=61030309207314713163325&cm_mc_sid_50200000=1473623034#chaincode
14.https://1drv.ms/f/s!AkBmzWP0h-Vxbr7h1uTNsTMcPWI
15.https://1drv.ms/b/s!AkBmzWP0h-VxazOBOsVz8nGMLpM
16.https://1drv.ms/b/s!AkBmzWP0h-VxbLK7FOjH9IL5HHc
17.https://www.youtube.com/watch?v=24JAF9Eu1mk&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=74
18.https://www.youtube.com/watch?v=wSmJ-rwUo8I&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=73
19.https://www.youtube.com/watch?v=SOsn6MvGH60&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=72
相关文章推荐
- 在Win7使用IBM Bluemix 之Blockchain Service 建立区块链应用App开发环境
- 在 Win7 环境使用 hyperledger fabric source code 模拟 IBM Bluemix Blockchain Service
- 在 Win7 环境使用 Docker Images模拟 IBM Bluemix Blockchain Service
- 使用 Postman 开发 Blockchain Network之 Chaincode (智能合约)
- 在 Win7 使用 Docker Image 在 Local Machine 建构 Blockchain Network System
- 在 Win7 使用 hyperledger fabric source code建构 Local Blockchain Network System
- 详解IBM Bluemix端到端开发体验
- 使用IBM Bluemix的服务Internet of Things Foundation接受Android手机传送来的信号
- 使用 IBM Mobile Data for Bluemix 服务构建一个 iOS 应用程序
- 使用maven,Apache ServiceMix开发OSGI web例子
- 完整微服务化示例:使用 Apache ServiceComb (incubating) 进行微服务开发、容器化、弹性伸缩
- 为什么 web 开发人员需要迁移到. NET Core, 并使用 ASP.NET Core MVC 构建 web 和 webservice/API
- Apache ServiceMix使用features安装模块总是报not found错误的解决方案
- 第04天实战技术(01):block开发使用场景(保存代码)
- 第04天实战技术(09):block开发中使用场景(返回值)
- 第04天实战技术(08):Block开发使用场景(参数使用)
- 第04天实战技术(03):block开发使用场景(传值)
- 第04天实战技术(02):block开发使用场景(代理传值)