1. 程式人生 > >超級賬本fabic入門

超級賬本fabic入門

詳細的環境安裝可以微信掃一掃

1.先進入sample的目錄啟動服務

cd chaincode-docker-devmode

啟動相應的docker例項中使用了已經寫了相應的編排檔案如下所示

.
├── chaincode
├── docker-compose-simple.yaml
├── msp
│   ├── admincerts
│   │   └── admincert.pem
│   ├── cacerts
│   │   └── cacert.pem
│   ├── keystore
│   │   └── key.pem
│   ├── signcerts
│   │   └── peer.pem
│   ├── tlscacerts │   │   └── tlsroot.pem │   └── tlsintermediatecerts │   └── tlsintermediate.pem ├── myc.block ├── myc.tx ├── orderer.block ├── README.rst └── script.sh

執行命令這個命令是docker-compose編排啟動檔案如果想後臺執行的話可以加個-d引數

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

注意如果這裡出現了證書錯誤的情況就切換到master主分支

2.開始編譯區塊鏈

在第一步中啟動程式會為我們啟動一個開發環境的容器名為chinacode,我們編譯直接進入該容器進行操作

docker exec -it chaincode bash

我看到預設的主目錄為chaincode

root@d2629980e76b:/opt/gopath/src/chaincode#

現在編譯sacc

cd sacc
go build

編譯完直接執行

CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc

顯示如下

/saccchaincode/sacc#CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME
=mycc:0 ./sacc 2018-05-04 08:56:18.486 UTC [shim] SetupChaincodeLogging -> INFO 001 Chaincode log level not provided; defaulting to: INFO 2018-05-04 08:56:18.486 UTC [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...

peer容器logs顯示已註冊channelID不存在

peer         | 2018-05-04 09:40:33.425 UTC [chaincode] beforeRegisterEvent -> DEBU 338 Received REGISTER in state created
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] registerHandler -> DEBU 339 registered handler complete for chaincode mycc:0
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] beforeRegisterEvent -> DEBU 33a Got REGISTER for chaincodeID = name:"mycc:0" , sending back REGISTERED
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] notifyDuringStartup -> DEBU 33b nothing to notify (dev mode ?)
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] notifyDuringStartup -> DEBU 33c sending READY
peer         | 2018-05-04 09:40:33.425 UTC [chaincode] processStream -> DEBU 33d []Move state message READY
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] handleMessage -> DEBU 33e []Fabric side Handling ChaincodeMessage of type: READY in state established
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] enterReadyState -> DEBU 33f []Entered state ready
peer         | 2018-05-04 09:40:33.426 UTC [chaincode] notify -> DEBU 340 notifier Txid:, channelID: does not exist

此時雖然程式已經註冊但是未和其他的管道間關聯

3.安裝例項化

由於上步的執行進行了堵塞的shell,所以這步需要重新開啟一個shell進行安裝例項化

進入cli開發環境

docker exec -it cli bash

安裝

peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0
peer chaincode instantiate -n mycc -v 0 -c '{"Args":["a","10"]}' -C myc

好了這步基本上a向區塊鏈寫入了10

如果上步錯了想修改20執行如下

peer chaincode invoke -n mycc -c '{"Args":["set", "a", "20"]}' -C myc

想檢視下使用如下命令

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