【fabric源碼】VSCode調試Fabric源碼

引言

啓動一個peer和一個node,用example02作爲例子,安裝、初始化、調用、查詢鏈碼。

環境準備

  • macOS Mojave 10.14
  • vscode 1.42.0
  • golang 1.12.6
  • docker-machine 0.16.0

下載源碼

cd $GOPATH/src/github.com/hyperledger/
git clone https://github.com/hyperledger/fabric.git
git checkout release-1.4
git clone https://github.com/hyperledger/fabric-samples

配置host

127.0.0.1 peer
127.0.0.1 orderer

創建peer和order配置

1.在fabric項目下創建dev-network目錄
2.將fabric項目下sampleconfig文件夾下的所有文件複製到 dev-network
3.修改 core.yamlfileSystemPath = $GOPATH/src/github.com/hyperledger/fabric/dev-network/production/peer
4.修改 orderer.yaml中 Location =$GOPATH/src/github.com/hyperledger/fabric/dev-network/production/orderer
5.在 dev-network新建 config目錄,並複製fabric-samples項目中的chaincode-docker-devmode下的myc.txorderer.block文件到config目錄中

注意修改GOPATH

修改調試配置(.vscode/launch.json),並啓動調試

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "orderer",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/orderer",
            "env": {
                "ORDERER_GENERAL_LISTENADDRESS":"0.0.0.0",
                "ORDERER_GENERAL_GENESISMETHOD":"file",
                "ORDERER_GENERAL_GENESISFILE":"${workspaceRoot}/dev-network/config/orderer.block",
                "ORDERER_GENERAL_LOCALMSPID":"DEFAULT",
                "ORDERER_GENERAL_LOCALMSPDIR":"${workspaceRoot}/dev-network/msp",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": []
        },
        {
            "name": "peer",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2346,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"peer",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",

            },
            "args": ["node","start","--peer-chaincodedev=true"],
            "showLog": true
        },
        {
            "name": "channel create",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2347,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["channel","create","-c", "myc","-f","${workspaceRoot}/dev-network/config/myc.tx","-o","127.0.0.1:7050"],
            "showLog": true
        },
        {
            "name": "channel join",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2348,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["channel","join","-b","myc.block"],
            "showLog": true
        },
        {
            "name": "chaincode install",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2349,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["chaincode","install","-p","github.com/hyperledger/fabric/examples/chaincode/go/example02","-n" ,"mycc", "-v", "1.0"],
            "showLog": true
        },
         {
            "name": "run chaincode example02",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2353,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/examples/chaincode/go/example02/cmd",
            "env": {
                "CORE_PEER_ADDRESS":"127.0.0.1:7052",
                "CORE_CHAINCODE_ID_NAME":"mycc:1.0"
            },
            "args": [],
            "showLog": true
        },
        {
            "name": "chaincode instantiate",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2350,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051", "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["chaincode","instantiate","-n", "mycc", "-v" ,"1.0" ,"-c", "{\"Args\":[\"init\",\"a\",\"100\",\"b\",\"200\"]}" ,"-C", "myc"],
            "showLog": true
        },
        {
            "name": "chaincode invoke",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2351,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
               
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_MSPCONFIGPATH": "${workspaceRoot}/dev-network/msp",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["chaincode","invoke","-n", "mycc" ,"-c", "{\"Args\":[\"invoke\",\"a\",\"b\",\"10\"]}" ,"-C", "myc"],
            "showLog": true
        },
        {
            "name": "chaincode query",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2352,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/peer",
            "env": {
                "CORE_PEER_LOCALMSPID":"DEFAULT",
                "CORE_PEER_ID":"cli",
                "CORE_PEER_ADDRESS":"127.0.0.1:7051",
                "FABRIC_CFG_PATH":"${workspaceRoot}/dev-network",
            },
            "args": ["chaincode","query","-n", "mycc" ,"-c", "{\"Args\":[\"query\",\"a\"]}" ,"-C", "myc"],
            "showLog": true
        }

        
    ]
}
  • 切換到調試窗口,在需要跟蹤的地方打好斷點
  • 依次運行order 、peer、channel create 、channel join、chaincode install、run chaincode example02、chaincode instantiate、chaincode invoke、chaincode query
  • 觀察debug控制檯和terminal變化
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章