【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变化
发布了14 篇原创文章 · 获赞 0 · 访问量 156

猜你喜欢

转载自blog.csdn.net/kk3909/article/details/104819229
今日推荐