|
| 1 | +# Microfab VSCode User Tasks |
| 2 | + |
| 3 | +Microfab VSCode User Tasks provides a set of VSCode tasks that allows you to quickly start developing Hyperledger Fabric chaincode. The user tasks provide a "wrapper" for easily calling microfab to package chaincode as well as iteratively deploying chaincode in the microfab container. |
| 4 | + |
| 5 | +The user tasks also provide a way to easily monitor Fabric logs and launch a management shell to directly call Fabric CLI commands. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +The user tasks require the following to be installed: |
| 10 | + |
| 11 | +- docker |
| 12 | +- "Command Variable" VSCode extension |
| 13 | + |
| 14 | +NOTE: All Fabric binaries are executed from the microfab container, so microfab must be running before running any tasks. |
| 15 | + |
| 16 | +## Installation: |
| 17 | + |
| 18 | +The user tasks depend on a local directory as a storage location for chaincode source as well as scripts and a repository to store packaged chaincode. This directory is mounted as a docker host volume. Create a directory on the local machine. |
| 19 | + |
| 20 | +Installation is simply a matter of selecting `Code -> Settings -> User Tasks` from the VSCode menu and pasting in the folling JSON. Inside the JSON, replace the string "<LOCAL_DIRECTORY>" with the fully qualified path to the created directory. |
| 21 | + |
| 22 | +``` |
| 23 | +{ |
| 24 | + // See https://go.microsoft.com/fwlink/?LinkId=733558 |
| 25 | + // for the documentation about the tasks.json format |
| 26 | + "version": "2.0.0", |
| 27 | + "options": { |
| 28 | + "env": { |
| 29 | + "local_directory": "<LOCAL_DIRECTORY>", |
| 30 | + "CORE_PEER_MSPCONFIGPATH": "/working/_msp/Org1/org1admin/msp", |
| 31 | + "CORE_PEER_ADDRESS": "org1peer-api.127-0-0-1.nip.io:8080", |
| 32 | + "CORE_PEER_LOCALMSPID": "Org1MSP" |
| 33 | + } |
| 34 | + }, |
| 35 | + "tasks": [ |
| 36 | + { |
| 37 | + "label": "Fabric - Start", |
| 38 | + "type": "shell", |
| 39 | + "command": "docker run -d --rm -p 8080:8080 -u microfab -v $local_directory:/working --name microfab ghcr.io/hyperledger-labs/microfab:latest && sleep 8 && cd $local_directory && docker cp microfab:opt/examples/vscode_tasks/_utils $local_directory/_utils && curl -s http://console.127-0-0-1.nip.io:8080/ak/api/v1/components | npx @hyperledger-labs/weft microfab -w _wallets -p _gateways -m _msp -f", |
| 40 | + "problemMatcher": [] |
| 41 | + }, |
| 42 | + { |
| 43 | + "label": "Fabric - Stop", |
| 44 | + "type": "shell", |
| 45 | + "command": "docker stop microfab", |
| 46 | + "problemMatcher": [] |
| 47 | + }, |
| 48 | + { |
| 49 | + "label": "Fabric - Show Logs", |
| 50 | + "type": "shell", |
| 51 | + "command": "docker logs microfab -f", |
| 52 | + "presentation": { |
| 53 | + "echo": true, |
| 54 | + "reveal": "always", |
| 55 | + "focus": false, |
| 56 | + "panel": "new", |
| 57 | + "showReuseMessage": true, |
| 58 | + "clear": false |
| 59 | + }, |
| 60 | + "problemMatcher": [] |
| 61 | + }, |
| 62 | + { |
| 63 | + "label": "Fabric - Peer Admin Shell", |
| 64 | + "type": "shell", |
| 65 | + "command": "docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab bash && cd /working", |
| 66 | + "problemMatcher": [] |
| 67 | + }, |
| 68 | + { |
| 69 | + "label": "Chaincode - Package open project", |
| 70 | + "type": "shell", |
| 71 | + "command": "cd $local_directory && rm -rf _chaincode_src && mkdir -p _chaincode_src && mkdir -p _packaged && cp -R ${workspaceFolder} _chaincode_src/temp && docker exec -it microfab /working/_utils/package.sh ${input:chaincodeName} ${input:chaincodeVersion} ${input:language} ${input:chaincodeType} /working/_chaincode_src/temp", |
| 72 | + "problemMatcher": [] |
| 73 | + }, |
| 74 | + { |
| 75 | + "label": "Chaincode - List packaged chaincode", |
| 76 | + "type": "shell", |
| 77 | + "command": "cd $local_directory && ls _packaged/", |
| 78 | + "problemMatcher": [] |
| 79 | + }, |
| 80 | + { |
| 81 | + "label": "Chaincode - List Committed", |
| 82 | + "type": "shell", |
| 83 | + "command": "docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab peer lifecycle chaincode querycommitted -C channel1", |
| 84 | + "problemMatcher": [] |
| 85 | + }, |
| 86 | + { |
| 87 | + "label": "Chaincode - List Installed ", |
| 88 | + "type": "shell", |
| 89 | + "command": "docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab peer lifecycle chaincode queryinstalled", |
| 90 | + "problemMatcher": [] |
| 91 | + }, |
| 92 | + { |
| 93 | + "label": "Chaincode - Deploy", |
| 94 | + "type": "shell", |
| 95 | + "command": "docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab /working/_utils/deployCC.sh ${input:openDialog}", |
| 96 | + "problemMatcher": [] |
| 97 | + } |
| 98 | + ], |
| 99 | + "inputs": [ |
| 100 | + { |
| 101 | + "type": "pickString", |
| 102 | + "id": "language", |
| 103 | + "description": "Chaincode language", |
| 104 | + "options": [ |
| 105 | + "golang", |
| 106 | + "node", |
| 107 | + "java" |
| 108 | + ], |
| 109 | + "default": "golang" |
| 110 | + }, |
| 111 | + { |
| 112 | + "type": "pickString", |
| 113 | + "id": "chaincodeType", |
| 114 | + "description": "Chaincode Type", |
| 115 | + "options": [ |
| 116 | + "tar", |
| 117 | + "cds" |
| 118 | + ], |
| 119 | + "default": "tar" |
| 120 | + }, |
| 121 | + { |
| 122 | + "type": "promptString", |
| 123 | + "id": "chaincodeName", |
| 124 | + "description": "Enter the name of the chaincode" |
| 125 | + }, |
| 126 | + { |
| 127 | + "type": "promptString", |
| 128 | + "id": "chaincodeVersion", |
| 129 | + "description": "Enter the version of the chaincode" |
| 130 | + }, |
| 131 | + { |
| 132 | + "id": "openDialog", |
| 133 | + "type": "command", |
| 134 | + "command": "extension.commandvariable.file.openDialog", |
| 135 | + "args": { |
| 136 | + "canSelect": "files", |
| 137 | + "defaultUri": "<LOCAL_DIRECTORY>/_packaged", |
| 138 | + "transform": { |
| 139 | + "text": "${fileBasename}" |
| 140 | + }, |
| 141 | + } |
| 142 | + } |
| 143 | + ] |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## Working Directory Contents |
| 148 | + |
| 149 | +The local working directory has the following structure: |
| 150 | + |
| 151 | +``` |
| 152 | +├── _chaincode_src |
| 153 | +├── _gateways |
| 154 | +├── _msp |
| 155 | +├── _packaged |
| 156 | +├── _utils |
| 157 | +└── _wallets |
| 158 | +``` |
| 159 | + |
| 160 | +The folders _gateways, _msp and _wallets are created by microfab and contain the connection information for the Fabric network. The _chaincode_src directory is a temp directory to host the chaincode to be packaged. The process to package a chaincode is to make a make a copy of the source to the _chaincode_src directory. This is to make the source "visible" to the peer CLI from the microfab container. The _packaged directory is the target directory for storing packaged chaincode. The _utils directory contains scripts to deploy chaincode to microfab. |
| 161 | + |
| 162 | + |
| 163 | +## Start the microfab container |
| 164 | + |
| 165 | +The microfab instance can be started by selecting `Terminal -> Run Task` in VSCode: |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +Type `Fabric` into the task filter to show the list of Fabric tasks. |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +Select `Fabric - Start`. |
| 174 | + |
| 175 | +This will start the microfab container, download utilties and connection information to the local machine. |
| 176 | + |
| 177 | +``` |
| 178 | +Executing task: docker run -d --rm -p 8080:8080 -u microfab -v $local_directory:/working --name microfab microfab:latest && sleep 8 && cd $local_directory && docker cp microfab:opt/examples/vscode_tasks/_utils $local_directory/_utils && curl -s http://console.127-0-0-1.nip.io:8080/ak/api/v1/components | npx @hyperledger-labs/weft microfab -w _wallets -p _gateways -m _msp -f |
| 179 | +
|
| 180 | +ea46ace0571b304ab578ad2103df44ec2758b397bf8592c1f9b32208ab58b6ea |
| 181 | +Successfully copied 34.3kB to /opt/fabric/dev/_utils |
| 182 | +Gateway profile written to : /opt/fabric/dev/_gateways/org1gateway.json |
| 183 | +Added identity under label ordereradmin to the wallet at /opt/fabric/dev/_wallets/Orderer |
| 184 | +Added identity under label org1admin to the wallet at /opt/fabric/dev/_wallets/Org1 |
| 185 | +Added identity under label org1caadmin to the wallet at /opt/fabric/dev/_wallets/Org1 |
| 186 | +
|
| 187 | +Environment variables: |
| 188 | +{"mspid":"Org1MSP","peers":["org1peer-api.127-0-0-1.nip.io:8080"],"ids":{"org1admin":"/opt/fabric/dev/_msp/Org1/org1admin/msp","org1caadmin":"/opt/fabric/dev/_msp/Org1/org1caadmin/msp"},"tlsrootcert":""} |
| 189 | +
|
| 190 | +For org1admin @ Org1 use these: |
| 191 | + |
| 192 | +export CORE_PEER_LOCALMSPID=Org1MSP |
| 193 | +export CORE_PEER_MSPCONFIGPATH=/opt/fabric/dev/_msp/Org1/org1admin/msp |
| 194 | +export CORE_PEER_ADDRESS=org1peer-api.127-0-0-1.nip.io:8080 |
| 195 | +
|
| 196 | +For org1caadmin @ Org1 use these: |
| 197 | + |
| 198 | +export CORE_PEER_LOCALMSPID=Org1MSP |
| 199 | +export CORE_PEER_MSPCONFIGPATH=/opt/fabric/dev/_msp/Org1/org1caadmin/msp |
| 200 | +export CORE_PEER_ADDRESS=org1peer-api.127-0-0-1.nip.io:8080 |
| 201 | +Complete |
| 202 | +``` |
| 203 | + |
| 204 | +## Packaging Chaincode |
| 205 | + |
| 206 | +Open a chaincode project in VSCode. The chaincode folder must be the top level folder in VSCode: |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +The chaincode should be complete with all vendoring if required before packaging. |
| 211 | + |
| 212 | +Note: The microfab container must be running. |
| 213 | + |
| 214 | +The microfab instance can be started by selecting `Terminal -> Run Task` in VSCode: |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +Type `Chaincode` into the task filter to show the list of Fabric tasks for Chaincode. |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +Select `Chaincode - Package open project` |
| 223 | + |
| 224 | +Enter a name for the chaincode: |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +Enter a version for the chaincode: |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +Select the language for the chaincode: |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +Select the package type for the chaincode: |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | +The packaged chaincode will be copied to the `_packaged` directory in the local working directory. |
| 241 | + |
| 242 | +## Deploy Chaincode |
| 243 | + |
| 244 | +The packaged chaincode can be deployed by selecting `Terminal -> Run Task` in VSCode: |
| 245 | + |
| 246 | +Type `Chaincode` into the task filter to show the list of Fabric tasks for Chaincode. |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | +Select `Chaincode - Deploy` |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | +Note: `Fabric - Show Logs` can be used to show the peer logs as the chaincode lifecycle process is completed. |
| 255 | + |
| 256 | +``` |
| 257 | +Chaincode definition committed on channel 'channel1' |
| 258 | +Querying chaincode definition on org1peer on channel 'channel1'... |
| 259 | +Attempting to Query committed status on org1peer, Retry after 3 seconds. |
| 260 | ++ peer lifecycle chaincode querycommitted --channelID channel1 --name asset |
| 261 | ++ res=0 |
| 262 | +Committed chaincode definition for chaincode 'asset' on channel 'channel1': |
| 263 | +Version: 1.0.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true] |
| 264 | +Query chaincode definition successful on org1peer on channel 'channel1' |
| 265 | +``` |
| 266 | + |
| 267 | +## List Packaged Chaincode |
| 268 | + |
| 269 | +Packaged chaincodes can be shown by running `Terminal -> Run Task -> Chaincode - List packaged chaincode` |
| 270 | + |
| 271 | +Example: |
| 272 | + |
| 273 | +``` |
| 274 | +Executing task: cd $local_directory && ls _packaged/ |
| 275 | +
|
| 276 | +asset_1.0.0.tar.gz |
| 277 | +``` |
| 278 | + |
| 279 | +## List Installed Chaincode |
| 280 | + |
| 281 | +Installed chaincodes can be shown by running `Terminal -> Run Task -> Chaincode - List installed` |
| 282 | + |
| 283 | +Example: |
| 284 | + |
| 285 | +``` |
| 286 | +Executing task: docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab peer lifecycle chaincode queryinstalled |
| 287 | +
|
| 288 | +Installed chaincodes on peer: |
| 289 | +Package ID: asset_1.0.0:65584e9a39a9b57053db44c30e1b20f727e4c9798effc5dc69c2e4530954f703, Label: asset_1.0.0 |
| 290 | +``` |
| 291 | + |
| 292 | +## List Commited Chaincode |
| 293 | + |
| 294 | +Committed chaincodes can be shown by running `Terminal -> Run Task -> Chaincode - List Committed` |
| 295 | + |
| 296 | +Example: |
| 297 | + |
| 298 | +``` |
| 299 | +Executing task: docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab peer lifecycle chaincode querycommitted -C channel1 |
| 300 | +
|
| 301 | +Committed chaincode definitions on channel 'channel1': |
| 302 | +Name: asset, Version: 1.0.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc |
| 303 | +``` |
| 304 | + |
| 305 | +## Peer Admin Shell |
| 306 | + |
| 307 | +A peer admin shell with necessary environment varibles and certificates launched by running `Terminal -> Run Task -> Fabric - Peer Admin Shell` |
| 308 | + |
| 309 | +Example: |
| 310 | + |
| 311 | +``` |
| 312 | +Executing task: docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID --workdir=/working microfab bash |
| 313 | +
|
| 314 | +microfab@ea46ace0571b:/working$ |
| 315 | +``` |
| 316 | + |
| 317 | +Show the working directory contents: |
| 318 | + |
| 319 | +``` |
| 320 | +microfab@ea46ace0571b:/working$ ls |
| 321 | +_chaincode_src _gateways _msp _packaged _utils _wallets |
| 322 | +microfab@ea46ace0571b:/working$ |
| 323 | +``` |
| 324 | + |
| 325 | +#### List channels the peer has joined: |
| 326 | + |
| 327 | +``` |
| 328 | +microfab@ea46ace0571b:/working$ peer channel list |
| 329 | +2024-06-11 15:47:02.805 UTC 0001 INFO [channelCmd] InitCmdFactory -> Endorser and orderer connections initialized |
| 330 | +Channels peers has joined: |
| 331 | +channel1 |
| 332 | +``` |
| 333 | + |
| 334 | +#### List channels committed chaincode for the peer and channel: |
| 335 | + |
| 336 | +``` |
| 337 | +Executing task: docker exec -it -e CORE_PEER_MSPCONFIGPATH=$CORE_PEER_MSPCONFIGPATH -e CORE_PEER_ADDRESS=$CORE_PEER_ADDRESS -e CORE_PEER_LOCALMSPID=$CORE_PEER_LOCALMSPID microfab peer lifecycle chaincode querycommitted -C channel1 |
| 338 | +
|
| 339 | +Committed chaincode definitions on channel 'channel1': |
| 340 | +Name: asset, Version: 1.0.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc |
| 341 | +Name: asset-transfer-basic-go, Version: 1.0.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc |
| 342 | +``` |
| 343 | + |
| 344 | +#### Invoke chaincode |
| 345 | + |
| 346 | +This example assumes you have built and deployed the fabric sample, asset-transfer-basic: |
| 347 | + |
| 348 | +peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 -C channel1 -n asset-transfer-basic-go -c '{"Args":["CreateAsset","1","blue","35","tom","1000"]}' --waitForEvent |
| 349 | + |
| 350 | +``` |
| 351 | +microfab@ea46ace0571b:/working$ peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 -C channel1 -n asset-transfer-basic-go -c '{"Args":["CreateAsset","1","blue","35","tom","1000"]}' --waitForEvent |
| 352 | +2024-06-11 17:19:32.237 UTC 0001 INFO [chaincodeCmd] ClientWait -> txid [9b3bbfd69e3217800aa5f94f1a6479e19c990c6ea13847d027317e3ee4dd5bd3] committed with status (VALID) at org1peer-api.127-0-0-1.nip.io:8080 |
| 353 | +2024-06-11 17:19:32.240 UTC 0002 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200 |
| 354 | +``` |
| 355 | + |
| 356 | +#### Query chaincode |
| 357 | + |
| 358 | +peer chaincode query -C channel1 -n asset -c '{"Args":["ReadAsset","1"]}' |
| 359 | + |
| 360 | +``` |
| 361 | +microfab@ea46ace0571b:/working$ peer chaincode query -C channel1 -n asset -c '{"Args":["ReadAsset","1"]}' |
| 362 | +{"AppraisedValue":1000,"Color":"blue","ID":"1","Owner":"tom","Size":35} |
| 363 | +``` |
0 commit comments