You are on page 1of 47

Kapil Sachdeva  Home  Archives  Categories  Tags

Content

Bootstrapping the Hyperledger Fabric Network (Part 1) Crypto Generator (cryptogen)


Similar Posts
 2017-07-21  Hyperledger  Hyperledger  Fabric  BlockChain Comments

Let’s start building an example network that consists of three organizations (Org1, Org2 & Org3). Each of these
organizations contribute two peers. The ordering service part of a network should consists of few Orderer
nodes but to keep this example simple we will use only one (solo) node as shown in the diagram below.
However at this point of time there is no trust relationship between the organizations and the network entities
that they are providing.

Organizations

Org1 Org2 Org3

Fabric Network
Peers Orderers

P0.Org1 P1.Org1 P0.Org2 P1.Org2 P0.Org3 P1.Org3 O1

In Fabric network the authenticity, confidentiality and privacy of the transactions amongst the participants is
ensured by using asymmetric cryptography and chain of trust. In simple words, we need a certificate authority
that issues identities to the participants so that they can trust each other.

Fabric provides a tool called cryptogen (Crypto Generator) that is able to generate the certificates for the
participating organizations and the entities contributed by them.

Before we look at how to use cryptogen we need to download it. The script provided by Hyperledger Fabric
project at present downloads many other tools as well as required docker images so it may take few minutes
depending on your network connection.
curl -sSL https://goo.gl/iX9dek | bash
Content
Above command will figure out the operating system and the architecture of your machine & will download
appropriate binaries and docker images. After the successful execution of above script you should have a Crypto Generator (cryptogen)
folder named bin in the directory in which you executed the script. The bin folder will contain the cryptogen Similar Posts
tool.
Comments

Crypto Generator (cryptogen)


The cryptogen tool accepts a yaml file as an input where the participants and the entities offered by them are
specified.

1 OrdererOrgs:
2 - Name: Orderer
3 Domain: ksachdeva-exp.com
4 Specs:
5 - Hostname: orderer
6
7 PeerOrgs:
8 - Name: Org1
9 Domain: org1.ksachdeva-exp.com
10 Template:
11 Count: 2
12 Users:
13 Count: 1
14 - Name: Org2
15 Domain: org2.ksachdeva-exp.com
16 Template:
17 Count: 2
18 Users:
19 Count: 1
20 - Name: Org3
21 Domain: org3.ksachdeva-exp.com
22 Template:
23 Count: 2
24 Users:
25 Count: 1

crypto‑config.yml hosted with ❤ by GitHub view raw


In this yaml file we have specified an orderer (since we are using solo ordering service) and peers for Org1,
Org2 and Org3. The PeerOrgs/Template/Count field in the file indicates the number of peers for an organization. Content
In this example we have used the value 2 so we would have following peers:
Crypto Generator (cryptogen)
peer0.org1.ksachdeva-exp.com
Similar Posts
peer1.org1.ksachdeva-exp.com
Comments
peer0.org2.ksachdeva-exp.com
peer1.org2.ksachdeva-exp.com
peer0.org3.ksachdeva-exp.com
peer1.org3.ksachdeva-exp.com

Time to generate the certificates.

cryptogen generate --config=./crypto-config.yaml

The successful generation should result in a folder called crypto-config with a structure as shown below:

We now have necessary certificates and crypto materials for the various entities of our network however we
have not yet configured or specified them to use.
Similar Posts
Content
Invoking a transaction on Hyperledger Fabric Network
Bootstrapping the Hyperledger Fabric Network (Part 6) Crypto Generator (cryptogen)

Bootstrapping the Hyperledger Fabric Network (Part 5) Similar Posts

Bootstrapping the Hyperledger Fabric Network (Part 4) Comments

Bootstrapping the Hyperledger Fabric Network (Part 3)


Bootstrapping the Hyperledger Fabric Network (Part 2)

Back Introduction to Hyperledger Fabric

Next Bootstrapping the Hyperledger Fabric Network (Part 2)

Comments
1 Comment ksachdeva.github.io 
1 Login
Content
 Recommend 1 Sort by Best
⤤ Share
Crypto Generator (cryptogen)
Join the discussion…
Similar Posts
LOG IN WITH OR SIGN UP WITH DISQUS ?
Comments

Name

Métairie Jérémy • a month ago


Thank you for this amazing job ! I was looking for it for a while ! Thanks again !
△ ▽ • Reply • Share ›

ALSO ON KSACHDEVA.GITHUB.IO

Invoking a transaction on Hyperledger Fabric Bootstrapping the Hyperledger Fabric Network


Network (Part 6)
1 comment • 5 months ago 7 comments • 5 months ago
Jim Zhang — This is an excellent series! thanks for Chan — In my case, this error message is shown..npm
spending the time writing this up, especially making it run instantiate-chaincodeSetting up the cryptoSuite
base on async/await syntax styles, definitely much … ..Setting up the keyvalue store ..Creating the admin …

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 4) (Part 5)
2 comments • 5 months ago 1 comment • 5 months ago
Chan — I resolved this problem! I used cloning sample Joseph Nguyen — Thank you for your series post.
of "ksachdeva/hyperledger-fabric-example" and this They are very nice.
problem was for recognizing genesis block to …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy


Copyright Kapil Sachdeva 2017 Contact me at:   
Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Tags

Content
Bootstrapping the Hyperledger Fabric Network (Part 2)
Similar Posts
 2017-07-21  Hyperledger  Hyperledger  Fabric  BlockChain
Comments

In the Part 1 I talked about the various entities in the Hyperledger Fabric Network and ended up creating
certificates & private keys for them using cryptogen tool.

In this post I will try to run the ordering service (consists of only 1 node). Hyperledger Fabric project provides
docker images that can be easily customized by either using them as base image and/or by providing the
environment variables and volumes to provide the input data.

Because of the heavy usage of docker tools (specially Docker Compose) I would strongly suggest that
you first acquaint yourself with it. One of the best resources other than the documentation provided by
Docker is an open source tutorial called docker-curriculum.

Here is a list of items we need to start the Ordering service:

Trusted root certificates for various entities participating in the network


Orderer certificates and the IP addresses
Properties of various algorithms used by network (e.g. crypto, consensus etc)
Access control policies

All of the above information except the private keys for the Orderers is captured in genesis block. Hyperledger
Fabric project provides a tool called configtxgen (that also got downloaded by the script that we executed in P
art 1) and very much like cryptogen this tool requires a yaml input file so as to generate the genesis block.

The Hyperledger Fabric wiki provides a good information on what is expected in the yaml file expected by
configtxgen tool. Here is the one that we would use:

1 Profiles:
2
3 ThreeOrgsOrdererGenesis:
4 Orderer:
5 <<: *OrdererDefaults
6 Organizations:
7 - *OrdererOrg
8 Consortiums:
9 SampleConsortium:
10 Organizations:
11 - *Org1
12 - *Org2
13 - *Org3
14
15 ThreeOrgsChannel:
16 Consortium: SampleConsortium
17 Application:
18 <<: *ApplicationDefaults
19 Organizations:
20 - *Org1
21 - *Org2
22 - *Org3
23
24 Organizations:
25
26 - &OrdererOrg
27 Name: OrdererOrg
28 ID: OrdererMSP
29 MSPDir: crypto-config/ordererOrganizations/ksachdeva-exp.com/msp
30
31 - &Org1
32 Name: Org1MSP
33 ID: Org1MSP
34 MSPDir: crypto-config/peerOrganizations/org1.ksachdeva-exp.com/msp
35 AnchorPeers:
36 # AnchorPeers defines the location of peers which can be used
37 # for cross org gossip communication. Note, this value is only
38 # encoded in the genesis block in the Application section context
39 - Host: peer0.org1.ksachdeva-exp.com
40 Port: 7051
41
42 - &Org2
43 Name: Org2MSP
44 ID: Org2MSP
45 MSPDir: crypto-config/peerOrganizations/org2.ksachdeva-exp.com/msp
46 AnchorPeers:
47 - Host: peer0.org2.ksachdeva-exp.com
48 Port: 7051
49
50 - &Org3
51 Name: Org3MSP
52 ID: Org3MSP
53 MSPDir: crypto-config/peerOrganizations/org3.ksachdeva-exp.com/msp
54 AnchorPeers:
55 - Host: peer0.org3.ksachdeva-exp.com
56 Port: 7051
57
58 Orderer: &OrdererDefaults
59
60 # Available types are "solo" and "kafka"
61 OrdererType: solo
62
63 Addresses:
64 - orderer.ksachdeva-exp.com:7050
65
66 BatchTimeout: 2s
67
68 BatchSize:
69
70 MaxMessageCount: 10
71 AbsoluteMaxBytes: 99 MB
72 PreferredMaxBytes: 512 KB
73
74 Kafka:
75 Brokers:
76 - 127.0.0.1:9092
77
78 # Organizations is the list of orgs which are defined as participants on
79 # the orderer side of the network
80 Organizations:
81
82 Application: &ApplicationDefaults
83
84 # Organizations is the list of orgs which are defined as participants on
85 # the application side of the network
86 Organizations:

hyperledger‑fabric‑bootstrap2‑orderer.yml hosted with ❤ by GitHub view raw

Things for you to note are MSP directories specified in the yaml file. If you will go inside these directories (note
- crypto-config directory was created by cryptogen tool in the Part 1) to see that they contain only public
certificates and no secret key material.

Time to generate the genesis block

configtxgen -profile ThreeOrgsOrdererGenesis -outputBlock ./genesis.block

The genesis.block is a binary file. If you want to inspect the contents of it you can issue following command :

configtxgen -profile ThreeOrgsOrdererGenesis -inspectBlock ./genesis.block

Now we have all the pieces that are required to start the orderer service. We will use docker compose to
specify the various environment variables and mapping of artifacts (certs, keys, genesis block etc) that we have
generated using the volumes. Here is the file :

1 version: '2'
2
3 networks:
4 hf-exp:
5
6 services:
7
8 orderer.example.com:
9 container_name: orderer.ksachdeva-exp.com
10 image: hyperledger/fabric-orderer
11 environment:
12 - ORDERER_GENERAL_LOGLEVEL=debug
13 - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
14 - ORDERER_GENERAL_GENESISMETHOD=file
15 - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
16 - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
17 - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
18 # enabled TLS
19 - ORDERER_GENERAL_TLS_ENABLED=true
20 - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
21 - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
22 - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
23 working_dir: /opt/gopath/src/github.com/hyperledger/fabric
24 command: orderer
25 volumes:
26 - ./genesis.block:/var/hyperledger/orderer/orderer.genesis.block
27 - ./crypto-config/ordererOrganizations/ksachdeva-exp.com/orderers/orderer.ksachdeva-exp.com/msp:/var
28 - ./crypto-config/ordererOrganizations/ksachdeva-exp.com/orderers/orderer.ksachdeva-exp.com/tls/:/va
29 ports:
30 - 7050:7050

docker‑compose‑orderer.yaml hosted with ❤ by GitHub view raw

In order to execute it use following command:

docker-compose -f docker-compose-orderer.yaml up

If you inspect the running containers (docker ps -a) you should now see a container named orderer.ksachdeva-
exp.com. Also look at the logs that are generated when you started the orderer service.

If you want to run the orderer service in the background then use following command -

docker-compose -f docker-compose-orderer.yaml up -d

Similar Posts

Invoking a transaction on Hyperledger Fabric Network


Bootstrapping the Hyperledger Fabric Network (Part 6)
Bootstrapping the Hyperledger Fabric Network (Part 5)
Bootstrapping the Hyperledger Fabric Network (Part 4)
Bootstrapping the Hyperledger Fabric Network (Part 3)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 1)

Next Bootstrapping the Hyperledger Fabric Network (Part 3)

Comments
0 Comments ksachdeva.github.io 
1 Login

 Recommend Sort by Best


⤤ Share

Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Be the first to comment.

ALSO ON KSACHDEVA.GITHUB.IO

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 1) (Part 4)
1 comment • 5 months ago 2 comments • 5 months ago
Métairie Jérémy — Thank you for this amazing job ! I Chan — I resolved this problem! I used cloning sample
was looking for it for a while ! Thanks again ! of "ksachdeva/hyperledger-fabric-example" and this
problem was for recognizing genesis block to …

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 5) (Part 6)
1 comment • 5 months ago 7 comments • 5 months ago
Joseph Nguyen — Thank you for your series post. Chan — In my case, this error message is shown..npm
They are very nice. run instantiate-chaincodeSetting up the cryptoSuite
..Setting up the keyvalue store ..Creating the admin …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy


Copyright Kapil Sachdeva 2017 Contact me at:   
Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Tags

Content
Bootstrapping the Hyperledger Fabric Network (Part 3)
Similar Posts
 2017-07-22  Hyperledger  Hyperledger  Fabric  BlockChain
Comments

So far we have only started an orderer node that is aware of identities of various participants in the network.
Before we go further and start the other nodes in the network we need to talk about the most important
concept in Hyperledger Fabric i.e Channels.

If you are familiar with pub-sub concept in a typical message queue implementation then you can consider that
a channel is akin to ‘topic’. In other words, it is not a transport layer but a way to logical isolate the
communication between various nodes. Entities that are the members of a channel may transact on it and
more importantly the transactions on a channel are not visible on other channels. This ensures both
authorization and the privacy aspects of transactions being committed on the network.

The Hyperledger Fabric wiki has a pretty good description of the channels.

Important things to note/consider are -

A channel may be created for a subset of the members on network to transact.


A member who is not authorized on a channel may not join the channel and cannot transact on that
channel.
There is a ledger per channel i.e. if a peer participates on two channels that it has two ledgers.

So how do we create a channel in the first place ? And quite possibly there will be more than one channel in a
given network and it is also possible that more participants will join an existing channel in future. Given all this
we can see that there is a requirement to create/update/read/delete the configuration related to the channels
i.e. a database (or a ledger). This ledger resides with the orderer nodes of the network.

We should also note that a participating entity/user should have the permission to create a new channel. How
to assign the permissions (or define the policies around) is a different blog post that I would do in near future.
For now you should know that the default policy allows the Admin users to create the new channels. In Part 1
we had created the necessary cryptographic key material and in Part 2 we had provided the information about
this aspect as part of genesis block to the orderer nodes.

Since the configuration of channels is stored in form of a ledger we can easily see that we should be creating a
transaction and send it to the orderer nodes. At present the creation of this transaction is done using the same
tool that we used to create the genesis block for orderer nodes i.e. configtxgen. We use a different profile
specified in the configtx.yaml that specify the channel and its participants (all three organizations intend to be
on this channel).

configtxgen -profile ThreeOrgsChannel -outputCreateChannelTx ./ksachdeva-exp-channel-1.tx -


channelID ksachdeva-exp-channel-1

The file ksachdeva-exp-channel-1.tx contains the transaction block however it is not signed. Before we can
send the request to the orderer node we need to sign this request using the cryptographic material of admin
user of one of the participating organizations.

Similar Posts

Invoking a transaction on Hyperledger Fabric Network


Bootstrapping the Hyperledger Fabric Network (Part 6)
Bootstrapping the Hyperledger Fabric Network (Part 5)
Bootstrapping the Hyperledger Fabric Network (Part 4)
Bootstrapping the Hyperledger Fabric Network (Part 2)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 2)

Next Bootstrapping the Hyperledger Fabric Network (Part 4)

Comments
0 Comments ksachdeva.github.io 
1 Login

 Recommend Sort by Best


⤤ Share

Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Be the first to comment.

ALSO ON KSACHDEVA.GITHUB.IO

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 5) (Part 6)
1 comment • 5 months ago 7 comments • 5 months ago
Joseph Nguyen — Thank you for your series post. Chan — In my case, this error message is shown..npm
They are very nice. run instantiate-chaincodeSetting up the cryptoSuite
..Setting up the keyvalue store ..Creating the admin …

Invoking a transaction on Hyperledger Fabric Bootstrapping the Hyperledger Fabric Network


Network (Part 1)
1 comment • 5 months ago 1 comment • 5 months ago
Jim Zhang — This is an excellent series! thanks for Métairie Jérémy — Thank you for this amazing job ! I
spending the time writing this up, especially making it was looking for it for a while ! Thanks again !
base on async/await syntax styles, definitely much …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy


Copyright Kapil Sachdeva 2017 Contact me at:   
Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Tags

Content
Bootstrapping the Hyperledger Fabric Network (Part 4)
Similar Posts
 2017-07-23  Hyperledger  Hyperledger  Fabric  BlockChain
Comments

Here is a brief summary of what we have accomplished so far:

Part 1 - Generated the crypto material for the various participants.


Part 2 - Generated the genesis block for the Orderer node and started ordering service (solo node).
Part 3 - Generated the configuration transaction block to create a new channel.

The configuration transaction block that we have created needs to be submitted to the Orderer nodes and it
should be signed. The default policy in Fabric is that it should be signed by an admin user of at least one
participating organization.

Hyperledger Fabric uses gRPC for the various services offered by the participating entities (peers & orderer)
and Google Protobuf for the messages exchanged on the wire. However instead of directly using gRPC, the
Fabric project provides SDKs in various languages (at present NodeJS, Java and Golang) that not only wraps
the gRPC related aspect but also provide many other required utilities and a framework that client applications
can make use to transact in the network and communicate with peers and orderers.

In this post we are going to use the NodeJS SDK to sign the configuration transaction block and submit it to
the orderer. As with any other nodejs based project, the SDK is released in form of a library/module on npm but
note that it targets ES6 (ECMAScript 2015) and at present can only be run using nodejs.

I have created a companion project (hyperledger-fabric-example) on github where I use fabric-client module to
write the example scripts and explain the usage of its API and achieve the objectives of this post. This sample
project is written using Typescript.

If you are not familiar with Typescript I would recommend reading an open source book on it by Basarat
Syed - https://basarat.gitbooks.io/typescript/content/docs/getting-started.html
The classes exposed by NodeJS SDK are really well documented (https://fabric-sdk-node.github.io/) however
at this point of time there is no type declaration file that helps in providing the intelli-sense and type safety so
as part of this example project I have also started to write the corresponding typescript declarations and would
submit them to DefinitelyTyped in near future.

The SDK exposes classes for every concept/entity that we have discussed so far (for e.g. Peer, Orderer, Chann
el) and interfaces for performing crypto operations, membership services, state store etc along with at least
one default implementation for them. However, the main class that is exposed at the root of the module is a
class called Client with helper methods to create objects of most of other classes discussed earlier. As an
example, it is not recommended to create an object of Channel directly but use the factory method provided by
the Client class.

There is another important class called User which represents the signing identity for the transactions sent to
various entities in the network. Again, like other classes you typically use a factory method to create the user
object.

Before we could sign the channel configuration transaction we need to perform 3 operations -

Set the CryptoSuite to be used by the Client.


Set the StateStore where the Client would store the user context.
Set the User context to be used for signing the transaction block.

I have wrapped these steps in src/client.ts that exposes a method called getClient().

export async function getClient(org: Organization): Promise<Client> {

const client = new Client();

console.log('Setting up the cryptoSuite ..');

// ## Setup the cryptosuite (we are using the built in default s/w based implementation)
const cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({
path: `${KEY_STORE_PATH_ADMIN}-${org}`
}));
client.setCryptoSuite(cryptoSuite);

console.log('Setting up the keyvalue store ..');

// ## Setup the default keyvalue store where the state will be stored
const store = await Client.newDefaultKeyValueStore({
path: `${KEY_STORE_PATH_ADMIN}-${org}`
});

client.setStateStore(store);

console.log('Creating the admin user context ..');

const ORG_ADMIN_MSP = MSP_DIR[org];

const privateKeyFile = fs.readdirSync(__dirname + '/../' + ORG_ADMIN_MSP + '/keystore')[0];

// ### GET THE NECESSRY KEY MATERIAL FOR THE ADMIN OF THE SPECIFIED ORG ##
const cryptoContentOrgAdmin: IIdentityFiles = {
privateKey: ORG_ADMIN_MSP + '/keystore/' + privateKeyFile,
signedCert: ORG_ADMIN_MSP + '/signcerts/Admin@' + org + '.ksachdeva-exp.com-cert.pem'
};

await client.createUser({
username: `${org}-admin`,
mspid: MSP_ID[org],
cryptoContent: cryptoContentOrgAdmin
});

return client;
}

The function and comments in it should be sufficient but here are some important points to note:
We have parameterized the function to accept the organization for which we are creating the client.
We are using the default software based cryptosuite provided by the SDK.
We are using the default File based KeyValue Store provided by the SDK for keeping the state.
We are providing the identity of Admin of Organization (passed as argument) as the signing identity by
providing the path to its public and private key pair.
The createUser() method not only creates the User object but also sets it as the current user context. In
other words, methods in Client object would use the User object to perform the signing.

Since the channel creation request is to be sent to the Orderer we also need an object for Orderer that contains
the necessary configuration (e.g. URL to connect to). Again as mentioned earlier instead of directly instantiating
it we would use the factory method provided by the Client class.

export async function getOrderer(client: Client): Promise<Orderer> {


// build an orderer that will be used to connect to it
const data = fs.readFileSync(path.join(__dirname, ORDERER_TLS_CAROOT_PATH));
const orderer: Orderer = client.newOrderer(ORDERER_URL, {
'pem': Buffer.from(data).toString(),
'ssl-target-name-override': 'orderer.ksachdeva-exp.com'
});

return orderer;
}

Now that we have objects of Client and Orderer, it is time to extract the channel configuration from ksachdeva-
exp-channel-1.tx, sign it, construct the channel creation request and send it to the Orderer. All these steps are
shown in src/create-channel.ts script.

const CHANNEL_NAME = 'ksachdeva-exp-channel-1';


const CHANNEL_1_PATH = './../ksachdeva-exp-channel-1.tx';

async function main() {

const org1Client = await getClient(Organization.ORG1);


const orderer = await getOrderer(org1Client);
// read in the envelope for the channel config raw bytes
console.log('Reading the envelope from manually created channel transaction ..');
const envelope = fs.readFileSync(path.join(__dirname, CHANNEL_1_PATH));

// extract the configuration


console.log('Extracting the channel configuration ..');
const channelConfig = org1Client.extractChannelConfig(envelope);

console.log('Signing the extracted channel configuration ..');


const signature = org1Client.signChannelConfig(channelConfig);

// prepare the request


const channelRequest: IChannelRequest = {
name: CHANNEL_NAME,
config: channelConfig,
signatures: [signature],
orderer: orderer,
txId: org1Client.newTransactionID()
};

console.log('Sending the request to create the channel ..');


const response = await org1Client.createChannel(channelRequest);

console.log(response);
}

Make sure that the docker container for orderer is running. If not you can use the npm script to start it

# This is essentially calling the docker-compose. See package.json and its scripts section
npm run start-orderer

You can then run src/create-channel.ts by using npm as follows:


# This is essentially calling the ts-node. See package.json and its scripts section
npm run create-channel

A successful execution of the script should result in following output

$ npm run create-channel

> hyperledger-fabric-example@0.1.0 create-channel


/Users/ksachdeva/Desktop/Dev/exp/hyperledger/hyperledger-fabric-example
> ts-node src/create-channel

Setting up the cryptoSuite ..


Setting up the keyvalue store ..
Creating the admin user context ..
Reading the envelope from manually created channel transaction ..
Extracting the channel configuration ..
Signing the extracted channel configuration ..
Sending the request to create the channel ..
status: 'SUCCESS'

Congratulations! we successfully created the channel. I have mapped the folder in the docker container for
orderer that contains the ledger for channel configuration to the local file system. You should see a folder called
‘production’ and it sub contents as shown in the image below.
The production/orderer/chains/ksachdeva-exp-channel-1 is the new channel that we created. The
production/orderer/chains/testchainid is the system channel that got created when we created the genesis
block for orderer.

As you can guess that if you will run the src/create-channel.ts script again it should result in the failure. If you
want to run the script few more times then you would want to issue following commands first

# Stop the orderer container(s)


npm run stop-orderer
# Start the orderer containers (s)
npm run start-orderer

In the npm run start-orderer script, I am first deleting the production folder so that the orderer container starts
cleanly i.e. with out ksachdeva-exp-channel-1 channel.
Similar Posts

Invoking a transaction on Hyperledger Fabric Network


Bootstrapping the Hyperledger Fabric Network (Part 6)
Bootstrapping the Hyperledger Fabric Network (Part 5)
Bootstrapping the Hyperledger Fabric Network (Part 3)
Bootstrapping the Hyperledger Fabric Network (Part 2)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 3)

Next Bootstrapping the Hyperledger Fabric Network (Part 5)

Comments

2 Comments ksachdeva.github.io 
1 Login

 Recommend Sort by Best


⤤ Share

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Chan • 3 months ago


Hi, Thank you so much for your posting about Hyperledger Fabric. This example is very helpful for my study.

And I have one question. when I command "npm run create-channel", Orderer.js has an error
"sendBroadcast" .
if do you have answer for the problem, can you tell me the answer?

△ ▽ • Reply • Share ›

Chan > Chan • 3 months ago


I resolved this problem! I used cloning sample of "ksachdeva/hyperledger-fabric-example" and this
problem was for recognizing genesis block to folder. I don't know why the system recognized like that.
so when i generate and use genesis block like "part 2", i could run "npm run create-channels". Thanks
you for your posting! and please understand my English skill...
1△ ▽ • Reply • Share ›

ALSO ON KSACHDEVA.GITHUB.IO

Bootstrapping the Hyperledger Fabric Network Invoking a transaction on Hyperledger Fabric


(Part 1) Network
1 comment • 5 months ago 1 comment • 5 months ago
Métairie Jérémy — Thank you for this amazing job ! I Jim Zhang — This is an excellent series! thanks for
was looking for it for a while ! Thanks again ! spending the time writing this up, especially making it
base on async/await syntax styles, definitely much …

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 6) (Part 5)
7 comments • 5 months ago 1 comment • 5 months ago
Chan — In my case, this error message is shown..npm Joseph Nguyen — Thank you for your series post.
run instantiate-chaincodeSetting up the cryptoSuite They are very nice.
..Setting up the keyvalue store ..Creating the admin …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy


Copyright Kapil Sachdeva 2017 Contact me at:   
Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Tags

Content
Bootstrapping the Hyperledger Fabric Network (Part 5)
Similar Posts
 2017-07-24  Hyperledger  Hyperledger  Fabric  BlockChain
Comments

Here is a brief summary of what we have accomplished so far:

Part 1 - Generated the crypto material for the various participants.


Part 2 - Generated the genesis block for the Orderer node and started ordering service (solo node).
Part 3 - Generated the configuration transaction block to create a new channel.
Part 4 - Signed the configuration block and created the new channel.

We have created a channel and ordering service has a record of it as well but we need instruct the peers to join
the channel and before we could do that we need to start the peer containers as well.

When you used npm run start-orderer it is starting the docker-compose configuration specified in
docker-compose-orderer.yaml file.

I have added a new docker-compose.yaml file that specifies the peers to start and as well as inherit the
configuration for orderer from docker-compose-orderer.yaml file. So from now onwards we would not use
the npm run start-orderer command line. In order to start all the containers you would use following
command:

# Deletes the production folder & Start all the containers (orderer, and peers of all
organizations)
npm run start-containers

To stop all the containers use:

# Stop all the containers


npm run stop-containers
After successful issuance of npm run start-containers you should be able to see following by issuing
docker ps -a command

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
53c08da6ccbc hyperledger/fabric-peer "peer node start" 9 seconds ago Up 6
seconds 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.ksachdeva-exp.com
e556c2066079 hyperledger/fabric-peer "peer node start" 9 seconds ago Up 6
seconds 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.ksachdeva-exp.com
7115ff0db9be hyperledger/fabric-peer "peer node start" 9 seconds ago Up 6
seconds 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.ksachdeva-exp.com
ecdb93e7e8d6 hyperledger/fabric-peer "peer node start" 9 seconds ago Up 5
seconds 0.0.0.0:12051->7051/tcp, 0.0.0.0:12053->7053/tcp peer1.org3.ksachdeva-exp.com
9c8b364b0b50 hyperledger/fabric-peer "peer node start" 9 seconds ago Up 7
seconds 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.ksachdeva-exp.com
509ce81e3748 hyperledger/fabric-orderer "orderer" 9 seconds ago Up 7
seconds 0.0.0.0:7050->7050/tcp orderer.ksachdeva-exp.com
45de91356f00 hyperledger/fabric-peer "peer node start" 9 seconds ago Up 7
seconds 0.0.0.0:11051->7051/tcp, 0.0.0.0:11053->7053/tcp peer0.org3.ksachdeva-exp.com

So now we have 2 peers per organization and 1 orderer node running in the docker containers.

In order to instruct the peers to join the channel here are the steps we are supposed to perform for every
organization:

1. Create the Client object that has the user context set to the admin of the desired organization.
2. Create the Channel object and the provide the instance of Orderer to it.
3. Channel would obtain the genesis block from the ordering service.
4. Get the Peer objects for the desired organization.
5. Send the transactionId, genesis block of channel, list of peers to Channel object

Above mentioned steps are demonstrated by the src/join-channel.ts script and here is the relevant code
snippets that are descriptive enough to understand what is happening.
async function joinOrgPeersToChannel(org: Organization) {

const client = await getClient(org);


const orderer = await getOrderer(client);

console.log('Creating a Channel object ..');


const channel = client.newChannel(config.CHANNEL_NAME);

console.log('Specifying the orderer to connect to ..');


channel.addOrderer(orderer);

console.log('Getting the genesis block for the ${CHANNEL_NAME} ..');


const genesis_block = await channel.getGenesisBlock({
txId: client.newTransactionID()
});

console.log('Getting the peers ..');


const peers = await getPeers(client, org);

const proposalResponse = await channel.joinChannel({


txId: client.newTransactionID(),
block: genesis_block,
targets: peers
});

console.log(proposalResponse);
}

async function main() {

await joinOrgPeersToChannel(Organization.ORG1);
await joinOrgPeersToChannel(Organization.ORG2);
await joinOrgPeersToChannel(Organization.ORG3);
}

main();

Running the script src/join-channel.ts can be done by issuing following command:

npm run join-channel

Look in the production folder and you should see various new folders & files that have been created.
Majority of them are leveldb databases so the contents of the files would not make sense.

Similar Posts

Invoking a transaction on Hyperledger Fabric Network


Bootstrapping the Hyperledger Fabric Network (Part 6)
Bootstrapping the Hyperledger Fabric Network (Part 4)
Bootstrapping the Hyperledger Fabric Network (Part 3)
Bootstrapping the Hyperledger Fabric Network (Part 2)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 4)

Next Bootstrapping the Hyperledger Fabric Network (Part 6)

Comments
1 Comment ksachdeva.github.io 
1 Login

 Recommend 1 Sort by Best


⤤ Share

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Joseph Nguyen • 2 months ago


Thank you for your series post. They are very nice.
△ ▽ • Reply • Share ›

ALSO ON KSACHDEVA.GITHUB.IO

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 1) (Part 6)
1 comment • 5 months ago 7 comments • 5 months ago
Métairie Jérémy — Thank you for this amazing job ! I Chan — In my case, this error message is shown..npm
was looking for it for a while ! Thanks again ! run instantiate-chaincodeSetting up the cryptoSuite
..Setting up the keyvalue store ..Creating the admin …

Bootstrapping the Hyperledger Fabric Network Invoking a transaction on Hyperledger Fabric


(Part 4) Network
2 comments • 5 months ago 1 comment • 5 months ago
Chan — I resolved this problem! I used cloning sample Jim Zhang — This is an excellent series! thanks for
of "ksachdeva/hyperledger-fabric-example" and this spending the time writing this up, especially making it
problem was for recognizing genesis block to … base on async/await syntax styles, definitely much …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

Copyright Kapil Sachdeva 2017 Contact me at:   


Copyright Kapil Sachdeva 2017 Contact me at:   
Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Tags

Content
Bootstrapping the Hyperledger Fabric Network (Part 6)
Similar Posts
 2017-07-27  Hyperledger  Hyperledger  Fabric  BlockChain
Comments

Here is a brief summary of what we have accomplished so far:

Part 1 - Generated the crypto material for the various participants.


Part 2 - Generated the genesis block for the Orderer node and started ordering service (solo node).
Part 3 - Generated the configuration transaction block to create a new channel.
Part 4 - Signed the configuration block and created the new channel.
Part 5 - Make peers of all the organizations join the channel that we created in Part 4.

We now have a channel and peers of our participating organizations have joined, the next step is to provide the
business logic that should be executed on this channel. In BlockChain this business logic is typically called
Smart Contracts; Hyperledger Fabric call it Chaincode. A Chaincode in Fabric is essentially a
program/application written in GoLang that exposes a set of interfaces that can be invoked by the client
applications. The entry points of a Chaincode program/application also receives optional arguments and have
access to the ledger so that it can read and write data to it.

The hyperledger-fabric-example project makes use of a very simple Chaincode application


( chaincode/src/github.com/example_cc/example_cc.go ). The business logic is quite simple and
described below:

1. Have two entities.


2. The names and initial values of these entities are to be passed at the time of initialization.
3. You could move portion/part of value from one entity to another entity.
4. You could query the current value of an entity.
5. You could delete an entity.

If you look at the source code of ( chaincode/src/github.com/example_cc/example_cc.go ) you should be


able to see corresponding functions to the business logic requirements that I have specified above.
With the description of our business logic (Smart Contract / Chaincode) out of way, the first order of business
(pun intended) is to install this application on the peers of various participating organizations.

The steps for performing the installation of Chaincode are done in src/install-chaincode.ts script and here is
shown the relevant code snippet that should be descriptive enough for you to know how it is done :

async function installChaincodeOnPeers(org: Organization) {

const client = await getClient(org);


const orderer = await getOrderer(client);

console.log('Creating a Channel object ..');


const channel = client.newChannel(config.CHANNEL_NAME);

console.log('Specifying the orderer to connect to ..');


channel.addOrderer(orderer);

console.log('Getting the peers ..');


const peers = await getPeers(client, org);

// Note-
// The installChaincode is going to pick the chaincodePath
// from the local GOPATH
//
// Below I am just tricking it by setting the GOPATH environment
// variable and pointing it to the directory that contains the
// actual chain code
process.env.GOPATH = path.join(__dirname, '../chaincode');

const proposalResponse = await client.installChaincode({


targets: peers,
chaincodeId: config.CHAIN_CODE_ID,
chaincodePath: 'github.com/example_cc',
chaincodeVersion: 'v0'
});
}

async function main() {


await installChaincodeOnPeers(Organization.ORG1);
await installChaincodeOnPeers(Organization.ORG2);
await installChaincodeOnPeers(Organization.ORG3);
}

main();

You can run the script by issuing following command :

npm run install-chaincode

Once it is installed the next step is to instantiate (initialize) the Chaincode application. The steps for it are
shown in src/instantiate-chaincode.ts script.

async function instantiateChaincodeOnPeers(org: Organization) {

const client = await getClient(org);


const orderer = await getOrderer(client);

console.log('Creating a Channel object ..');


const channel = client.newChannel(config.CHANNEL_NAME);

console.log('Specifying the orderer to connect to ..');


channel.addOrderer(orderer);

console.log('Getting the peers ..');


const peers = await getPeers(client, org);

peers.map(p => channel.addPeer(p));

console.log('Initializing the channel ..');


await channel.initialize();

console.log('Sending the Instantiate Proposal ..');


const proposalResponse = await channel.sendInstantiateProposal({
chaincodeId: config.CHAIN_CODE_ID,
chaincodeVersion: 'v0',
fcn: 'init',
args: ["a", "100", "b", "200"],
txId: client.newTransactionID()
});

console.log('Sending the Transaction ..');


const transactionResponse = await channel.sendTransaction({
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1]
});

async function main() {


await instantiateChaincodeOnPeers(Organization.ORG1);
await instantiateChaincodeOnPeers(Organization.ORG2);
await instantiateChaincodeOnPeers(Organization.ORG3);
}

main();

You can run the script by issuing following command :

npm run instantiate-chaincode

Pay attention to the request object passed as argument of channel.sendInstantiateProposal method.

const proposalResponse = await channel.sendInstantiateProposal({


chaincodeId: config.CHAIN_CODE_ID,
chaincodeVersion: 'v0',
fcn: 'init',
args: ["a", "100", "b", "200"],
txId: client.newTransactionID()
});

Here fcn is the name of the function to be invoked. Look in example_cc.go file and you would see a
corresponding function that is named init . The arguments that this function expects is an array of keyvalue
pair with key being the name of the entity and the value being the value of the asset. So what we are passing
are two entities that are named ‘a’ & ‘b’ with values of ‘100’ & ‘200’ respectively.

This marks the completion of bootstrapping a Hyperledger Fabric network with Business logic (Smart Contract
/ Chaincode) installed and instantiated.

Similar Posts

Invoking a transaction on Hyperledger Fabric Network


Bootstrapping the Hyperledger Fabric Network (Part 5)
Bootstrapping the Hyperledger Fabric Network (Part 4)
Bootstrapping the Hyperledger Fabric Network (Part 3)
Bootstrapping the Hyperledger Fabric Network (Part 2)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 5)

Next Invoking a transaction on Hyperledger Fabric Network

Comments

7 Comments ksachdeva.github.io 
1 Login

 S tb B t
 Recommend Sort by Best
⤤ Share

Join the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Chan • 3 months ago


In my case, this error message is shown..

npm run instantiate-chaincode

Setting up the cryptoSuite ..


Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Sending the Instantiate Proposal ..
error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: Error starting container: API error (404):
{"message":"network hyperledgerfabricexample_hf-exp not found"}

at /home/bsch0111/0918/hyperledger-fabric-example-
master/node_modules/grpc/src/node/src/client.js:554:15
△ ▽ • Reply • Share ›

Ajay Jadhav • 3 months ago


Hi Kapil,

Thanks a lot for such a useful series of blogs on Hyperledger Fabric. I have gone through Part 1 till Part 6,
everything works well until I run following command to instantiate chain-code. It is basically throwing a time-
out error.

npm run instantiate-chaincode

Is there any reason for this error? Am I missing something? I am using Mac with the latest version of Docker &
Docker-composer.
△ ▽ • Reply • Share ›

K il S hd M d > Aj J dh 3 th
Kapil Sachdeva Mod > Ajay Jadhav • 3 months ago
Hi Ajay,

Comment out the instantiation calls for Org2 and Org3.

I have seen this happening few times as well.

Even though you will instantiate for Org1 you will see that it will still query correctly for Org2 and Org3.

Regards
Kapil
△ ▽ • Reply • Share ›

Ajay Jadhav > Kapil Sachdeva • 3 months ago


Hi Kapil,

I just tried commenting the Org1 and Org3 call, but I still get the same error. Below is the error
log.

error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: Timeout expired while


starting chaincode ksachdeva-exp-
cc:v0(networkid:dev,peerid:peer0.org1.ksachdeva...,tx:1b19542f0c236c0fd32fbbd37c04ed74cc
at /Users/ajayjadhav/training/fabric-demo/hyperledger-fabric-example-
master/node_modules/grpc/src/node/src/client.js:554:15
error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: Timeout expired while
starting chaincode ksachdeva-exp-
cc:v0(networkid:dev,peerid:peer1.org1.ksachdeva...,tx:1b19542f0c236c0fd32fbbd37c04ed74cc
at /Users/ajayjadhav/training/fabric-demo/hyperledger-fabric-example-
master/node_modules/grpc/src/node/src/client.js:554:15
Sending the Transaction ..
error: [Channel.js]: sendTransaction - no valid endorsements found
(node:1942) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2):
Error: no valid endorsements found
△ ▽ • Reply • Share ›

Kapil Sachdeva Mod > Ajay Jadhav • 3 months ago


In your previous attempt you might have ended up instantiating for Org1. Make sure to
delete the generated docker image & container before you try the instantiation step. If
the image/container exists then it may result in following timeout error.
△ ▽ • Reply • Share ›

Ajay Jadhav > Kapil Sachdeva • 3 months ago


Just raised a pull request on Github for a small change in query-chaincode.ts

Please review.
△ ▽ • Reply • Share ›

Ajay Jadhav > Kapil Sachdeva • 3 months ago


Hi Kapil,
I removed the docker images and started all the containers again, and it worked :) .
Thanks for pointing me to the correct solution.

The commands I ran for listing & removing the containers.

$ docker images

This will list all the docker images with their image id, which you can use with below
command to remove that image.

$ docker rmi IMAGE_ID

Once the images are removed, I started all the containers using:

$ npm run start-containers

and run following commands one after another:

$npm run create-channel

see more

△ ▽ • Reply • Share ›

ALSO ON KSACHDEVA.GITHUB.IO

Invoking a transaction on Hyperledger Fabric Bootstrapping the Hyperledger Fabric Network


Network (Part 5)
1 comment • 5 months ago 1 comment • 5 months ago
Jim Zhang — This is an excellent series! thanks for Joseph Nguyen — Thank you for your series post.
spending the time writing this up, especially making it They are very nice.
base on async/await syntax styles, definitely much …

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 4) (Part 1)
2 comments • 5 months ago 1 comment • 5 months ago
Chan — I resolved this problem! I used cloning sample Métairie Jérémy — Thank you for this amazing job ! I
of "ksachdeva/hyperledger-fabric-example" and this was looking for it for a while ! Thanks again !
r bl f rr i i i bl k t
problem was for recognizing genesis block to …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

Copyright Kapil Sachdeva 2017 Contact me at:   


Site powered by Jekyll & Github Pages. Theme designed by HyG.
Kapil Sachdeva  Home  Archives  Categories  Ta
Content

Similar Posts
Invoking a transaction on Hyperledger Fabric Network Comments

 2017-07-27  Hyperledger  Hyperledger  Fabric  BlockChain

We have our Hyperledger Fabric Network bootstrapped now. There is a channel, peers have joined that
channel, chaincode is installed and instantiated. As part of instantiation we provided the name of the entities
and their initial values. We should be able to query the values of an entity say the entity a .

The steps to perform query of the data in the ledger are done in src/query-chaincode.ts script. Here is the
relevant code snippet that shows the important steps:

async function queryChaincode(org: Organization) {


const client = await getClient(org);
const channel = await getChannel(client, org);

console.log(`Quering the Chaincode on the peers of ${org} ..`);


const response = await channel.queryByChaincode({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'query',
args: ["a"],
txId: client.newTransactionID()
});

console.log(`Peer0 of ${org} has ${response[0].toString('utf8')} as the current value for


'a'..`);
console.log(`Peer1 of ${org} has ${response[1].toString('utf8')} as the current value for
'a'..`);
}

async function main() {


console.log('############ ORG1 ###################');
await queryChaincode(Organization.ORG1);
console.log('############ ORG2 ###################');
await queryChaincode(Organization.ORG2);
console.log('############ ORG3 ###################');
await queryChaincode(Organization.ORG3);
}

main();

To run this script issue following command:

npm run query-chaincode

You should following output:

$ npm run query-chaincode

> hyperledger-fabric-example@0.1.0 query-chaincode /Users/ksachdeva/Desktop/Dev/myoss/hyperledger-


fabric-example
> ts-node src/query-chaincode.ts

############ ORG1 ###################


Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to .. Content
Getting the peers ..
Initializing the channel ..
Similar Posts
Quering the Chaincode on the peers of org1 ..
Comments
Peer0 of org1 has 100 as the current value for 'a'..
Peer1 of org1 has 100 as the current value for 'a'..
############ ORG2 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org2 ..
Peer0 of org2 has 100 as the current value for 'a'..
Peer1 of org2 has 100 as the current value for 'a'..
############ ORG3 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org3 ..
Peer0 of org3 has 100 as the current value for 'a'..
Peer1 of org3 has 100 as the current value for 'a'..

Above execution of src/query-chaincode.ts shows that the value of a in the ledgers of all the peers of all
participating organizations is 100 which is the value that we provided at the time of instantiation of the
chaincode.

What we would like to do next is to exercise the business logic of ‘moving/transferring’ some value from entity
a to entity b . Below is shown the src/invoke-transaction.ts script that shows the necessary steps to
perform this operation.

async function invokeTransactionOnPeers(org: Organization) {

const client = await getClient(org);


const orderer = await getOrderer(client);

console.log('Creating a Channel object ..');


const channel = client.newChannel(config.CHANNEL_NAME);

console.log('Specifying the orderer to connect to ..');


channel.addOrderer(orderer);

console.log('Getting the peers ..');


const peers = await getPeers(client, org);

peers.map(p => channel.addPeer(p));

console.log('Initializing the channel ..');


await channel.initialize();

console.log('Sending the Invoke Proposal ..');


const proposalResponse = await channel.sendTransactionProposal({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'move', Content
args: ["a", "b", "10"],
txId: client.newTransactionID()
Similar Posts
});
Comments

console.log('Sending the Transaction ..');


const transactionResponse = await channel.sendTransaction({
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1]
});

async function main() {


await invokeTransactionOnPeers(Organization.ORG1);
}

main();

You can run the script by issuing command:

npm run invoke-transaction

Pay attention to the following part:

console.log('Sending the Invoke Proposal ..');


const proposalResponse = await channel.sendTransactionProposal({
chaincodeId: config.CHAIN_CODE_ID,
fcn: 'move',
args: ["a", "b", "10"],
txId: client.newTransactionID()
});

console.log('Sending the Transaction ..');


const transactionResponse = await channel.sendTransaction({
proposalResponses: proposalResponse[0],
proposal: proposalResponse[1]
});

See that the name of the function is move .


We are transferring a value of 10 from a to b .
The execution of sendTransactionProposal is returning the proposal endorsed by the peers.
The execution of sendTransaction is what is finally making it commit to the various ledgers in the
network.

You should also note that unlike other scripts that we used in previous blog posts we are performing this
transaction only using the client of one organization.

Time for the moment of truth -:

Even though we executed the transaction on the peers of organization 1, we are expecting that the ledgers of
other peers of other organizations are updated as well i.e. the new value of entity a on all the ledgers of all the
peers should now be 90 (i.e. 100 - 10).

Run following command to query the chaincode:

npm run query-chaincode

and the output should look like


$ npm run query-chaincode
Content
> hyperledger-fabric-example@0.1.0 query-chaincode /Users/ksachdeva/Desktop/Dev/myoss/hyperledger-
fabric-example Similar Posts
> ts-node src/query-chaincode.ts Comments

############ ORG1 ###################


Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org1 ..
Peer0 of org1 has 90 as the current value for 'a'..
Peer1 of org1 has 90 as the current value for 'a'..
############ ORG2 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org2 ..
Peer0 of org2 has 90 as the current value for 'a'..
Peer1 of org2 has 90 as the current value for 'a'..
############ ORG3 ###################
Setting up the cryptoSuite ..
Setting up the keyvalue store ..
Creating the admin user context ..
Creating a Channel object ..
Specifiying the orderer to connect to ..
Getting the peers ..
Initializing the channel ..
Quering the Chaincode on the peers of org3 ..
Peer0 of org3 has 90 as the current value for 'a'..
Peer1 of org3 has 90 as the current value for 'a'..

Similar Posts

Bootstrapping the Hyperledger Fabric Network (Part 6)


Bootstrapping the Hyperledger Fabric Network (Part 5)
Bootstrapping the Hyperledger Fabric Network (Part 4)
Bootstrapping the Hyperledger Fabric Network (Part 3)
Bootstrapping the Hyperledger Fabric Network (Part 2)
Bootstrapping the Hyperledger Fabric Network (Part 1)

Back Bootstrapping the Hyperledger Fabric Network (Part 6)

Comments
1 Comment ksachdeva.github.io 
1 Login
Content
 Recommend 1 Sort by Best
⤤ Share
Similar Posts
Join the discussion… Comments
LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Jim Zhang • 5 months ago


This is an excellent series! thanks for spending the time writing this up, especially making it base on
async/await syntax styles, definitely much better-looking source than promise/then/catch. and typescript is a
bonus for those who prefers it.
△ ▽ • Reply • Share ›

ALSO ON KSACHDEVA.GITHUB.IO

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 5) (Part 4)
1 comment • 5 months ago 2 comments • 5 months ago
Joseph Nguyen — Thank you for your series post. Chan — I resolved this problem! I used cloning sample
They are very nice. of "ksachdeva/hyperledger-fabric-example" and this
problem was for recognizing genesis block to …

Bootstrapping the Hyperledger Fabric Network Bootstrapping the Hyperledger Fabric Network
(Part 6) (Part 1)
7 comments • 5 months ago 1 comment • 5 months ago
Chan — In my case, this error message is shown..npm Métairie Jérémy — Thank you for this amazing job ! I
run instantiate-chaincodeSetting up the cryptoSuite was looking for it for a while ! Thanks again !
..Setting up the keyvalue store ..Creating the admin …

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

Copyright Kapil Sachdeva 2017 Contact me at:   


Site powered by Jekyll & Github Pages. Theme designed by HyG.

You might also like