hyperledger-labs / fablo

Fablo is a simple tool to generate the Hyperledger Fabric blockchain network and run it on Docker. It supports RAFT and solo consensus protocols, multiple organizations and channels, chaincode installation and upgrade.
Apache License 2.0
195 stars 71 forks source link
blockchain chaincodes generator hyperledger hyperledger-fabric hyperledger-fabric-network network-topology

Github Actions

Fablo

Fablo supports:

See it in action

How to use

Installation

Fablo is distributed as a single shell script which uses Docker image to generate the network config. You may keep the script in the root directory of your project or install it globally in your file system.

To install it globally:

sudo curl -Lf https://github.com/hyperledger-labs/fablo/releases/download/2.0.0/fablo.sh -o /usr/local/bin/fablo && sudo chmod +x /usr/local/bin/fablo

To get a copy of Fablo for a single project, execute in the project root:

curl -Lf https://github.com/hyperledger-labs/fablo/releases/download/2.0.0/fablo.sh -o ./fablo && chmod +x ./fablo

Getting started

To create a local Hyperledger Fabric network with Node.js chaincode and REST API client, install Fablo and execute:

./fablo init node rest
./fablo up

After a few minutes the whole network will be set up and running. You can check the running nodes via docker ps or docker stats, and you can query the network with command line (via cli.org1.example.com container) or REST API client (via Fablo REST).

Basic usage

fablo up /path/to/fablo-config.json

The up command creates initial configuration and starts Hyperledger Fabric network on Docker. In this case network configuration is saved in $(pwd)/fablo-target. Then you can manage the network with other commands, for example stop, start, down, prune.

Provided Fablo configuration file describes network topology: root organization, other organizations, channels and chaincodes. See the samples or Fablo config section.

There are two basic use cases. You may use Fablo to start and manage the network for development purposes, test different network topologies, run it in CI environment etc. In this case you should keep fablo-target directory intact and out of the version control. Fablo will manage it locally.

On the other hand you can use Fablo to generate initial network configuration, keep it in version control and tweak for specific requirements. In this case, however, you should use generated fablo-docker.sh instead of fablo script.

Managing the network

init

fablo init [node] [rest] [dev]

Creates simple network config file in current dir. Good step to start your adventure with Fablo or set up a fast prototype.

Fablo init command takes three parameters (the order does not matter):

Sample command:

fablo init node dev

Generated fablo-config.json file uses single node Solo consensus and no TLS support. This is the simplest way to start with Hyperledger Fabric, since Raft consensus requires TLS and TLS itself adds a lot of complexity to the blockchain network and integration with it.

generate

fablo generate [/path/to/fablo-config.json|yaml [/path/to/fablo/target]]

Generates network configuration files in the given directory. Default config file path is $(pwd)/fablo-config.json or \$(pwd)/fablo-config.yaml, default directory is $(pwd)/fablo-target. If you specify a different directory, you loose Fablo support for other commands.

If you want to use Fablo only to kick off the Hyperledger Fabric network, you may provide target directory parameter or just copy generated Fablo target directory content to desired directory and add it to version control. Note that generated files may contain variables with paths on your disk and generated crypto material for Hyperledger Fabric. Review the files before submitting to version control system.

up

fablo up [/path/to/fablo-config.json|yaml]

Starts the Hyperledger Fabric network for given Fablo configuration file, creates channels, installs and instantiates chaincodes. If there is no configuration, it will call generate command for given config file.

down, start, stop

fablo [down | start | stop]

Downs, starts or stops the Hyperledger Fabric network for configuration in the current directory. This is similar to down, start and stop commands for Docker Compose.

prune

fablo prune

Downs the network and removes fablo-target directory.

reset and recreate

fablo reset
fablo recreate [/path/to/fablo-config.json|yaml]

validate

fablo validate [/path/to/fablo-config.json|yaml]

Validates network config. This command will validate your network config try to suggest necessary changes or additional tweaks. Please note that this step is also executed automatically before each generate to ensure that at least critical errors where fixed.

snapshot and restore

Fablo supports saving state snapshot (backup) of the network and restoring it. It saves all network artifacts, certificates, and the data of CA, orderer and peer nodes. Note the snapshot does not contain Fablo config file and chaincode source code, since both can be located outside Fablo working directory.

Snapshotting might be useful if you want to keep the current state of a network for future use (for testing, sharing the network state, courses and so on).

fablo snapshot <target-snapshot-path>

If you want to restore snapshot into current directory, execute:

fablo restore <source-snapshot-path>

Example:

  1. Assume you have a working network with some state.
  2. Execute ./fablo snapshot /tmp/my-snapshot. It will create a file /tmp/my-snapshot.fablo.tar.gz with the state of the network. It is not required to stop the network before making a snapshot.
  3. Execute ./fablo prune to destroy the current network. If the network was present, Fablo would not be able to restore the new one from backup.
  4. Execute ./fablo restore /tmp/my-snapshot to restore the network.
  5. Execute ./fablo start to start the restored network.

Typically, a snapshot of the network with little data will take less than 1 MB, so it is easy to share.

fabric-docker.sh

The script fabric-docker.sh is generated among docker network configuration. It does not support generate command, however other commands work in same way as in fablo. Basically fablo forwards some commands to this script.

If you want to use Fablo for network configuration setup only, then the fabric-docker.sh file allows you to manage the network.

Managing chaincodes

chaincode(s) install

fablo chaincodes install

Install all chaincodes. Might be useful if for some reason, Fablo won't manage to do it by itself.

If you want to install a single chaincode defined in Fablo config file, execute:

fablo chaincode install <chaincode-name> <version>

chaincode upgrade

fablo chaincode upgrade <chaincode-name> <version>

Upgrades chaincode with given name on all relevant peers. Chaincode directory is specified in Fablo config file.

chaincode invoke

Invokes chaincode with specified parameters.

fablo chaincode invoke <channel-name> <chaincode-name> <peers-domains-comma-separated>  <command> [transient]

Sample command:

fablo chaincode invoke "my-channel1" "chaincode1" "peer0.org1.example.com" '{"Args":["KVContract:put", "name", "Willy Wonka"]}'

chaincodes list

Gets the instantiated or installed chaincodes in the specified channel or peer.

fablo chaincodes list <peer> <channel>

Running chaincodes in dev mode

Hyperledger Fabric allows to run peers in dev mode in order to allow simple develop of chaincodes. In this case chaincodes do not need to be upgraded each time, but they are run locally. This feature allows hot reload of chaincode code and speeds up the development a lot.

Fablo will run peers in dev mode when global.peerDevMode is set to true. Note: in this case TLS has to be disabled, otherwise config validation fails.

The simplest way of trying Fablo with dev mode is as follows:

  1. Execute fablo init node dev. It will initialize Fablo config file with sample node chaincode and dev mode enabled. In this case Fablo config file has global.peerDevMode set to true, and the package.json file for sample Node.js chaincode has a script for running chaincode in dev mode (start:dev).
  2. Start the network with fablo up. Because dev mode is enabled, chaincode containers don't start. Instead, Fablo approves and commits chaincode definitions from Fablo config file.
  3. Npm install and start the sample chaincode with:
    (cd chaincodes/chaincode-kv-node && nvm use && npm i && npm run start:watch)

    Now, when you update the chaincode source code, it will be automatically refreshed on Hyperledger Fabric Network.

Our sample chaincode definition contains some scripts for running chaincode in dev mode:

  "scripts": {
    ...
    "start:dev": "fabric-chaincode-node start --peer.address \"127.0.0.1:8541\" --chaincode-id-name \"chaincode1:0.0.1\" --tls.enabled false",
    "start:watch": "nodemon --exec \"npm run start:dev\"",
    ...
  },

Worth considering:

Feel free to update this scripts to adjust it to your chaincode definition.

Channel scripts

channel help

fablo channel --help

Use it to list all available channel commands.
Commands are generated using fablo-config.json to cover all cases (queries for each channel. organization and peer)

channel list

fablo channel list org1 peer0

Lists all channels for given peer.

channel getinfo

fablo channel getinfo channel_name org1 peer0

Prints channel info, like current block height for given peer

channel fetch config

fablo channel fetch config channel_name org1 peer0 [file_name.json]

Fetches latest config block, decodes it and write to a JSON file.

channel fetch raw block

fablo channel fetch <oldest|newest|block-number> channel_name org1 peer0 [file_name.json]

Fetches oldest, newest or a block with given number, and writes it to a file.

Utility commands

version

fablo version [--verbose | -v]

Prints current Fablo version. With optional -v or --verbose flag it prints supported Fablo and Hyperledger Fabric versions as well.

use

fablo use

Lists all available Fablo versions.

use

fablo use <version-number>

Switches current script to selected version.

Fablo config

Fablo config is a single JSON or YAML file that describes desired Hyperledger Fabric network topology (network settings, CA, orderer, organizations, peers, channels, chaincodes). It has to be compatible with the [schema]. You may generate a basic config with ./fablo init command. See the samples directory for more complex examples.

The basic structure of Fablo config file is as follows:

{
  "$schema": "https://github.com/hyperledger-labs/fablo/releases/download/2.0.0/schema.json",
  "global": { ... },
  "orgs": [ ... ],
  "channels": [ ... ],
  "chaincodes": [ ... ]
}

global

Example:

  "global": {
    "fabricVersion": "2.4.2",
    "tls": false,
    "peerDevMode": false,
    "monitoring": {
      "loglevel": "debug"
    },
    "tools": {
      "explorer": false
    }
  },

orgs

Example:

  "orgs": [
    {
      "organization": {
        "name": "Org1",
        "domain": "org1.example.com"
      },
      "peer": {
        "instances": 2,
        "db": "LevelDb"
      },
      "orderers": [{
        "groupName": "group1",
        "type": "raft",
        "instances": 3
      }],
      "tools": {
        "fabloRest": true,
        "explorer": true
      }
    },
    ...
  ],

The other available parameters for entries in orgs array are:

property peer.db:

property orderers:

channels

Example:

  "channels": [
    {
      "name": "my-channel1",
      "groupName": "group1",      
      "orgs": [
        {
          "name": "Org1",
          "peers": [
            "peer0",
            "peer1"
          ]
        },
        {
          "name": "Org2",
          "peers": [
            "peer0"
          ]
        }
      ]
    },
    ...
  ],

chaincodes

Example:

  "chaincodes": [
    {
      "name": "chaincode1",
      "version": "0.0.1",
      "lang": "node",
      "channel": "my-channel1",
      "directory": "./chaincodes/chaincode-kv-node",
      "privateData": {
        "name": "org1-collection",
        "orgNames": ["Org1"]
      }
    },
    {
      "name": "chaincode2",
      "version": "0.0.1",
      "lang": "java",
      "channel": "my-channel2"
    }
  ]

The other available parameters for entries in chaincodes array are:

The property lang may be golang, java or node.

The privateData parameter is optional. You don't need to define the private data collection for the chaincode. By default there is none (just the implicit private data collection in Fabric 2.x).

hooks

Hooks in Fablo are Bash commands to be executed after a specific event. Right now Fablo supports only one kind of hook: postGenerate. It will be executed each time after the network config is generated -- after ./fablo generate command (executed separately or automatically by ./fablo up).

The following hook example will change MaxMessageCount to 1 in generated Hyperledger Fabric config:

  "hooks": {
    "postGenerate": "perl -i -pe 's/MaxMessageCount: 10/MaxMessageCount: 1/g' \"./fablo-target/fabric-config/configtx.yaml\""
  }

Genrated Hooks are saved in fablo-target/hooks.

Sample YAML config file

---
"$schema": https://github.com/hyperledger-labs/fablo/releases/download/2.0.0/schema.json
global:
  fabricVersion: 2.4.2
  tls: false
orgs:
  - organization:
      name: Orderer
      domain: root.com
    orderers:
      - groupName: group1
        prefix: orderer
        type: solo
        instances: 1 
  - organization:
      name: Org1
      domain: org1.example.com
      tools:
        fabloRest: true
        explorer: true
    peer:
      instances: 2
  - organization:
      name: Org2
      domain: org2.example.com
    peer:
      instances: 1
channels:
  - name: my-channel1
    orgs:
      - name: Org1
        peers:
          - peer0
          - peer1
      - name: Org2
        peers:
          - peer0
chaincodes:
  - name: and-policy-chaincode
    version: 0.0.1
    lang: node
    channel: my-channel1
    init: '{"Args":[]}'
    endorsement: AND('Org1MSP.member', 'Org2MSP.member')
    directory: "./chaincodes/chaincode-kv-node"
    privateData:
      - name: org1-collection
        orgNames:
          - Org1

Kubernetes support

TODO

Other features

Connection profiles

Fablo will generate the connection profiles for each organization defined in the configuration. You can find them in fablo-target/fablo-config/connection-profiles directory in json and yaml format.

REST API

Fablo is integrated with simple REST API for CA and chaincodes, supported by Fablo REST. If you want to use it, provide for your organization "tools": { "fabloRest": true }. Visit the Fablo REST project for more documentation.

Blockchain Explorer

Fablo can run Blockchain Explorer for you. Provide for your organization "tools": { "explorer": true }, if you want to use it per organization, or provide the same value in global section of the config, if you want to use one global Explorer for all organizations.

Contributing

We'd love to have you contribute! Please refer to our contribution guidelines for details.

Testimonials

Fablo was originally created at SoftwareMill by @Hejwo and @dzikowski. In December 2021, Fablo joined Hyperledger Labs.