Code Monkey home page Code Monkey logo

hyperledger-fabric-chaincode-dev-setup's Introduction

Hyperledger Fabric dev setup for chaincode development npm version Build Status

This repo contains out of a setup which makes it easy to develop node.js chaincode.

Only node.js chaincode is supported, go is not implemented yet. Feel free to help out and create a pull request.

For more info on developing chaincode please have a look at the online tutorial.

CLI API

Installing

npm install @kunstmaan/hyperledger-fabric-chaincode-dev-setup -g

With the following command you can get an overview of all the commands available:

kuma-hf-chaincode-dev -h

Dependency: this tool required Node version >= 8

Start dev network environment

Make sure you have at least version 1.3.0 of the docker images installed. For this you can use the following command:

kuma-hf-chaincode-dev setup-docker-images

Furthermore you must make sure that the 'chaincode destination directory' is accesible to docker using the docker file sharing preferences. If you didn't specify this directory you must make sure the path of the npm package itself is added to the docker file sharing preference.

Initialized project

When you want to start a dev network on an initialized project you can just run:

kuma-hf-chaincode-dev start-dev [optional -w]

In this case all configuration is found in the package.json.

When you want to run a dev network without using our project structure.

-w enables watch mode on the network.

Standalone mode

kuma-hf-chaincode-dev start-dev --cp ./example-chaincode/fabcar1 --cp ./example-chaincode/fabcar2 [optional -w]

cp stands for chaincodePath and you can replace the argument value with the path of the chaincode you wish to run. When running from many locations you can specifiy this argument multiple times using different values.

-w enables watch mode on the network.

Initializing new Project

kuma-hf-chaincode-dev init <path>

Initializes a new project structure within the given path. This will create a src/chaincodes, src/common and test folder structure. And will add the needed configuration within the package.json. If no package.json is found it will trigger npm init ..

  • src/chaincodes is the folder that will contain all the chaincodes.
  • src/common is the folder that contains all the shared code between all the chaincodes.
  • test will be used for all the tests

This will also add the necesary configuration under the key kuma-hf-chaincode-dev to the package.jsonfor the other commands to use. This configuration can be overwritten.

  • chaincodes: the list of all the chaincodes to deploy to the network
  • sourcePath: the path to the src directory
  • buildPath: the path to where the chaincode should be builded, the build process will integrate the common part into each chaincode
  • testPath: the path to where the tests are
  • dockerFile: this is configuration for setting up the dev network. By default this will use the default dockerFile integrated within this repository
  • chaincodeDestination: the location where the chaincode should be moved so that it's mounted within the right docker container

Create new chaincode

kuma-hf-chaincode-dev create-chaincode <chaincode> [optional -d <description>]

Replace chaincode with the name of your chaincode. -d is optional and can be used to specify the description for the package.json of the chaincode.

Create new migration

kuma-hf-chaincode-dev create-migration <chaincode>

Generate a migration file for the given chaincode. This migration file can be used when installing our hyperledger-fabric-node-chaincode-utils package.

Build Chaincode

kuma-hf-chaincode-dev build

This will move the chaincodes to the buildPath and integrate the common part within each of them. This is useful for deploying to a real network. start-dev will also use this behind the scenes.

Cleanup chaincode on dev network

kuma-hf-chaincode-dev cleanup-chaincode

This will cleanup all the chaincode that's installed on the dev network. If you run this in an initialized project it will use that configuration else it will cleanup the chaincode from the standalone version.

Node.js API

You can also install this as an npm module in your project and run it within a node.js script.

Installing

npm install @kunstmaan/hyperledger-fabric-chaincode-dev-setup --save
const {setupDevEnv} = require('@kunstmaan/hyperledger-fabric-chaincode-dev-setup');

setupDevEnv({
    chaincodeLocations: [
        '/absolute/path/to/chaincode1',
        '/absolute/path/to/chaincode2'
    ],
    logOutputToConsole: (script, message) => {
        // 'script' return the full path to the script being executed
        // 'message' is the message which  would be written to the console
        // return false in case it should not be written to the console
        return true;
    },
    watchMode: false,
    dockerFile: './dev-network/generated/devmode/docker-compose-simple.yaml',
    chaincodeDestination: './dev-network/generated/devmode/chaincode',
    copyGlobPattern: '**/*'
}).then(() => {
    console.log('Dev env started.');
}).catch(() => {
    process.exit(1);
});
Option Info Type Required
chaincodeLocations Paths to the chain code directories. Array<string> Yes
logOutputToConsole Called when a shell script writes something to the console (script: string, message: string) => boolean No, filters debug messages by default
watchMode Watch files for changes, auto update chaincode when a file changes. boolean No, by default the user can update chaincode using the prompt window
dockerFile Location of the docker compose file for the network string No, defaults to dev-network/generated/devmode/docker-compose-simple.yaml
chaincodeDestination Destination path for the chaincode. Make sure your docker containers use the same path when using this option. string No, defaults to dev-network/generated/devmode/chaincode
copyGlobPattern Overwrite the glob pattern used to copy files from the chaincode location to the destination. string No, defaults to **/*

Watch mode

When this option is enabled the script will watch your chaincode directory for any changes. When a change happens the chaincode will deploy a newer version of the chaincode.

Changes to package.json will not be picked up, if you add a dependency you will need to restart the entire script.

What is the script doing

It automates the steps inside the tutorial. No need to setup many terminal windows. With this repo you'll only need to run a single script. It also watches for changes in the chaincode and automatically deploys them.

Steps which are done behind the scenes:

  1. Setup a simple blockchain network
  2. Install npm packages needed for the chaincode script
  3. Build/deploy and instantiate the chaincode
  4. Start watching the chaincode for changes, when a change happens an upgrade of the chaincode is done

Update the example network

The example network setup has been generated using the hyperledger-fabric-network-setup scripts.

Perform following steps to regenerate it:

  1. Follow the installation steps
  2. Run cd network-setup
  3. Run sudo kuma-hf-network generate crypto-config.yaml

hyperledger-fabric-chaincode-dev-setup's People

Contributors

daanporon avatar janb87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

hyperledger-fabric-chaincode-dev-setup's Issues

Copying of chaincode files crashes on a Linux vm on a windows host

Seems to be failing on copying the "common" dependencies. Could be related to the fact that it is a symlink.

EISDIR means that the target of the operation is a directory in reality but that the expected filetype of the target is something other than a directory.

/home/user/Downloads/hyperleder-fabric-kuma-token-example/chaincode/node_modules/@kunstmaan/hyperledger-fabric-chaincode-dev-setup/src/scripts/setupDocker.sh has finished, timeout of 30000 expired.
Creating directory /home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token
Removing /home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token/chaincode.js
Removing /home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token/common
Removing /home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token/models
Copying chaincode to directory /home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token
Copying files which match following pattern /home/user/Downloads/hyperleder-fabric-kuma-token-example/chaincode/build/kuma-token/**/!(package-lock.json)
{ Error: EISDIR: illegal operation on a directory, copyfile '/home/user/Downloads/hyperleder-fabric-kuma-token-example/chaincode/build/kuma-token/common/constants' -> '/home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token/common/constants'
  errno: -21,
  code: 'EISDIR',
  syscall: 'copyfile',
  path: '/home/user/Downloads/hyperleder-fabric-kuma-token-example/chaincode/build/kuma-token/common/constants',
  dest: '/home/user/Downloads/hyperleder-fabric-kuma-token-example/network/generated/devmode/chaincode/kuma-token/common/constants' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `kuma-hf-chaincode-dev start-dev`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2018-04-30T10_34_12_025Z-debug.log

unable install hyperledger fabric

os : windows

i have bash and docker installed on my system

C:\Users\malli>cd ~/fabric-dev-servers
The system cannot find the path specified.

C:\Users\malli>export FABRIC_VERSION=hlfv11
'export' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\malli>./downloadFabric.sh
'.' is not recognized as an internal or external command,
operable program or batch file.

Error: Config not found

i followed the steps: kuma-hf-chaincode-dev setup-docker-images

when i ran: kuma-hf-chaincode-dev start-dev

i got:
Error: Config not found
at findUp.then (/usr/local/lib/node_modules/@kunstmaan/hyperledger-fabric-chaincode-dev-setup/src/utils/locateConfig.js:30:15)

NPM repo does not exist

npm install @kunstmaan/hyperledger-fabric-chaincode-dev --save

is mentioned, but this repo doesn't exist in NPM

Add api to start from existing setup

Improve performance by not recreating the docker images (detect the latest version of chaincode using peer chaincode list --installed), use a separate function to start from a clean setup

startup dev with arguments for Init

Hi Folks,

First off all .. great stuff you have going here ...
I only just started dabbing my feed in HF and was looking into creating node js chaincode and noticed that the fabric samples example for the startup dev env was only for go ... a search later and I stumbled upon this project ... ( en dan nog van landgenoten ... altijd leuk :-) )

Got a ( potential noob ) question ...

I replicated the standard go example in node js - it uses an Init method which expects arguments so I noticed that this doesn't work when I start up the environment as I did not provide arguments (obviously):

_2018-05-29T15:13:07.144Z ERROR [lib/handler.js] [defaultchannel-2292664a]Calling chaincode Init() returned error response [Incorrect number of arguments. Expecting 2]. Sending ERROR message back to peer_

Is there a way to pass along arguments during start-up or is that intentional by design - perhaps not a good chaincode (coding) practice ? I noticed your 'ChaincodeBase' also assumes there are no arguments ...

Thanks

Steven

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.