Skip to content

Gateway

James Taylor edited this page Mar 8, 2021 · 13 revisions

Rough notes for Gateway development

Enable Gateway

Enable the Gateway feature flag in core.yaml by adding the following:

peer:
    gateway:
        enabled: true

Alternatively, using yq:

docker run --rm -v "${PWD}":/workdir mikefarah/yq eval '.peer.gateway.enabled = true' --inplace core.yaml

Using microfab

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.

Fabric binaries

Microfab does not use a Fabric release which includes Gateway support so you'll either need to download the most recent build of the Fabric binaries, or build the Fabric binaries yourself for the very latest changes.

Download Fabric binaries

Download the required binaries to a suitable directory with the following commands:

mkdir -p "release/linux-amd64"
curl -sSL https://hyperledger-fabric.jfrog.io/artifactory/fabric-binaries/hyperledger-fabric-linux-amd64-latest.tar.gz | tar xzf - -C "release/linux-amd64"
curl -sSL https://github.com/hyperledger/fabric-ca/releases/download/v1.4.9/hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz | tar xzf - -C "release/linux-amd64"

Build Fabric binaries

With the latest Fabric code from GitHub, create all the required binaries and configuration files by running the following commands:

make "release/linux-amd64"
mkdir -p "release/linux-amd64/config"
cp sampleconfig/*yaml "release/linux-amd64/config"
curl -sSL https://github.com/hyperledger/fabric-ca/releases/download/v1.4.9/hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz | tar xzf - -C "release/linux-amd64"

Update configuration

Update the core.yaml file to enable the Gateway. The ledger.snapshots.rootDir setting also needs to be updated to /opt/microfab/data/snapshots to work in microfab. Edit the file manually, or use the following yq command:

docker run --rm -v "${PWD}/release/linux-amd64/config":/workdir mikefarah/yq eval '.peer.gateway.enabled = true | .ledger.snapshots.rootDir = "/opt/microfab/data/snapshots"' --inplace core.yaml

Start microfab

Set the MICROFAB_CONFIG environment variable:

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

Start microfab:

docker run --name microfab --rm -v ${HOME}/chaincode:/chaincode:ro -v ${PWD}/release/linux-amd64:/opt/fabric:ro -p 8080:8080 -e MICROFAB_CONFIG ibmcom/ibp-microfab

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

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 microfab /opt/fabric/bin/peer lifecycle chaincode install /chaincode/fabcar.tgz

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

export PACKAGE_ID=fabcar:...

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 fabcar --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 fabcar --version 1 --sequence 1

Run transactions

Create some cars:

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 fabcar -c '{"function":"InitLedger","Args":[]}'

Query all cars:

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 fabcar -c '{"function":"QueryAllCars","Args":[]}'

Create a car:

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 fabcar -c '{"function":"CreateCar","Args":["CAR99","Nissan","Leaf","Green","Megan"]}'

Query a car:

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 fabcar -c '{"function":"QueryCar","Args":["CAR99"]}'

Use the Gateway!

See hlf-gateway-sample-go for a simple Gateway client sample.

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"
}'