Skip to content

Microfab

James Taylor edited this page Jan 27, 2022 · 2 revisions

microfab is the containerized Hyperledger Fabric runtime which is used in the IBP VSCode extension, which is a nice option for running a lightweight Gateway development environment.

Start microfab

Set the MICROFAB_CONFIG environment variable, for example:

export MICROFAB_CONFIG='{
    "endorsing_organizations":[
        {
            "name": "SampleOrg"
        }
    ],
    "channels":[
        {
            "name": "mychannel",
            "endorsing_organizations":[
                "SampleOrg"
            ]
        }
    ]
}'

Start microfab, for example:

docker run --name microfab -d --rm -v ${HOME}/fabric-samples/asset-transfer-basic:/chaincode:ro -p 8080:8080 -e MICROFAB_CONFIG ibmcom/ibp-microfab

Note: this example has a volume mount for the basic asset transfer sample which assumes you have the fabric-samples repository cloned in your home directory. You may want to use a different directory for your own chaincode, e.g. -v ${HOME}/chaincode:/chaincode:ro

Tip: you can follow the peer logs in another terminal using:

docker exec -it microfab /bin/tail -f /opt/microfab/data/peer-sampleorg/logs/peer.log

Running peer commands

Create an environment file for the admin identity you want to use. For example, admin-sampleorg.env:

cat << ADMIN-SAMPLEORG-ENV-EOF > admin-sampleorg.env
CORE_PEER_LOCALMSPID=SampleOrgMSP
CORE_PEER_MSPCONFIGPATH=/opt/microfab/data/admin-sampleorg
CORE_PEER_ADDRESS=sampleorgpeer-api.127-0-0-1.nip.io:8080
ADMIN-SAMPLEORG-ENV-EOF

Then run peer commands using this environment file. For example:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer channel list

Package chaincode

docker exec -it --env-file admin-sampleorg.env -w /home/ibp-user microfab /opt/fabric/bin/peer lifecycle chaincode package bat.tar.gz --path /chaincode/chaincode-go --lang golang --label bat

Deploy chaincode

Install the chaincode package (assumes you already have a fabcar.tgz package in the ~/chaincode directory):

docker exec -it --env-file admin-sampleorg.env -w /home/ibp-user microfab /opt/fabric/bin/peer lifecycle chaincode install bat.tar.gz

Keep the output from this command for the next step by setting an environment variable:

export PACKAGE_ID=bat:...

Approve the chaincode:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer lifecycle chaincode approveformyorg -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel --name bat --version 1 --sequence 1 --waitForEvent --package-id ${PACKAGE_ID}

Commit the chaincode:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer lifecycle chaincode commit -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel --name bat --version 1 --sequence 1

Run transactions

Create some assets:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"function":"InitLedger","Args":[]}'

Query all assets:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode query -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"Args":["GetAllAssets"]}'

Transfer an asset

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

Query an asset:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode query -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"Args":["ReadAsset","asset6"]}'

Create an asset:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"function":"CreateAsset","Args":["asset7","red","42","Jean","101"]}'

Delete an asset:

docker exec -it --env-file admin-sampleorg.env microfab /opt/fabric/bin/peer chaincode invoke -o orderer-api.127-0-0-1.nip.io:8080 --channelID mychannel -n bat -c '{"function":"DeleteAsset","Args":["asset7"]}'

Miscellaneous

For moar orgs...

export MICROFAB_CONFIG='{
    "ordering_organization": {
        "name": "Orderer"
    },
    "endorsing_organizations":[
        {
            "name": "Org1"
        },
        {
            "name": "Org2"
        },
        {
            "name": "Org3"
        }
    ],
    "channels":[
        {
            "name": "mychannel",
            "endorsing_organizations":[
                "Org1",
                "Org2",
                "Org3"
            ]
        }
    ],
    "capability_level":"V2_0"
}'

Explore microfab container:

docker exec -it -w /home/ibp-user /microfab /bin/sh

Unstable fabric

It is also possible to run microfab with the latest Fabric binaries.

Build the latest binaries in the fabric repo...

TARGET=linux-amd64 RELEASE=unstable ./ci/scripts/create_binary_package.sh

This should create a hyperledger-fabric-linux-amd64-unstable.tar.gz file (and cause a mess because the script moves things!)

Untar the contents in a fabric-unstable directory where you want to run microfab.

Add a suitable ca release to the fabric-unstable directory...

curl -sSL https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz | tar xzf - -C ${PWD}/fabric-unstable

Edit the core.yaml file...

ledger:
  # ...
  snapshots:
    # Path on the file system where peer will store ledger snapshots
    rootDir: /opt/microfab/data/snapshots

Set the MICROFAB_CONFIG environment variable and start microfab with...

docker run --name microfab -d --rm -v ${HOME}/fabric-samples/asset-transfer-basic:/chaincode:ro -v ${PWD}/fabric-unstable:/opt/fabric:ro -p 8080:8080 -e MICROFAB_CONFIG ibmcom/ibp-microfab
Clone this wiki locally