Skip to content

Commit

Permalink
[FAB-17461] Move off_chain_data sample to test network (#122)
Browse files Browse the repository at this point in the history
Signed-off-by: NIKHIL E GUPTA <negupta@us.ibm.com>

Co-authored-by: NIKHIL E GUPTA <negupta@us.ibm.com>
  • Loading branch information
nikhil550 and NIKHIL E GUPTA committed Mar 20, 2020
1 parent 121a44a commit faac18e
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 167 deletions.
63 changes: 30 additions & 33 deletions off_chain_data/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# Off Chain data

This sample demonstrates how you can use [Peer channel-based event services](https://hyperledger-fabric.readthedocs.io/en/release-1.4/peer_event_services.html)
This sample demonstrates how you can use [Peer channel-based event services](https://hyperledger-fabric.readthedocs.io/en/release-2.0/peer_event_services.html)
to replicate the data on your blockchain network to an off chain database.
Using an off chain database allows you to analyze the data from your network or
build a dashboard without degrading the performance of your application.

This sample uses the [Fabric network event listener](https://fabric-sdk-node.github.io/release-1.4/tutorial-listening-to-events.html) from the Node.JS Fabric SDK to write data to local instance of
This sample uses the [Fabric network event listener](https://hyperledger.github.io/fabric-sdk-node/release-1.4/tutorial-channel-events.html) from the Node.JS Fabric SDK to write data to local instance of
CouchDB.

## Getting started

This sample uses Node Fabric SDK application code similar to the `fabcar` sample
to connect to a network created using the `first-network` sample.
to connect to a running instance of the Fabric test network. Make sure that you
are running the following commands from the `off_chain_data` directory.

### Install dependencies
### Starting the Network

You need to install Node.js version 8.9.x to use the sample application code.
Execute the following commands to install the required dependencies:
Use the following command to start the sample network:

```
cd fabric-samples/off_chain_data
npm install
./startFabric.sh
```

This command will deploy an instance of the Fabric test network. The network
consists of an ordering service, two peer organizations with one peers each, and
a CA for each org. The command also creates a channel named `mychannel`. The
marbles chaincode will be installed on both peers and deployed to the channel.

### Configuration

The configuration for the listener is stored in the `config.json` file:
Expand Down Expand Up @@ -51,29 +55,24 @@ If you set the "use_couchdb" option to true in `config.json`, you can run the
following command start a local instance of CouchDB using docker:

```
docker run --publish 5990:5984 --detach --name offchaindb hyperledger/fabric-couchdb
docker run --publish 5990:5984 --detach --name offchaindb couchdb
docker start offchaindb
```

### Starting the Network
### Install dependencies

Use the following command to start the sample network:
You need to install Node.js version 8.9.x to use the sample application code.
Execute the following commands to install the required dependencies:

```
./startFabric.sh
npm install
```

This command uses the `first-network` sample to deploy a fabric network with an
ordering service, two peer organizations with two peers each, and a channel
named `mychannel`. The marbles chaincode will be installed on all four peers and
instantiated on the channel.

### Starting the Channel Event Listener

Once the network has started, we can use the Node.js SDK to create the user and
certificates our listener application will use to interact with the network. Run
the following command to enroll the admin user:

After we have installed the application dependencies, we can use the Node.js SDK
to create the identity our listener application will use to interact with the
network. Run the following command to enroll the admin user:
```
node enrollAdmin.js
```
Expand Down Expand Up @@ -358,18 +357,16 @@ database:
## Clean up

If you are finished using the sample application, you can bring down the network
and any accompanying artifacts.
and any accompanying artifacts by running the following command:
```
./network-clean.sh
```

Running the script will complete the following actions:

* Change to `fabric-samples/first-network` directory.
* To stop the network, run `./byfn.sh down`.
* Change back to `fabric-samples/off_chain_data` directory.
* Bring down the Fabric test network.
* Takes down the local CouchDB database.
* Remove the certificates you generated by deleting the `wallet` folder.
* Delete `nextblock.txt` so you can start with the first block next time you
operate the listener. You can also reset the `nextMarbleNumber` in
`addMarbles.json` to 100.
* To take down the local CouchDB database, first stop and then remove the
docker container:
```
docker stop offchaindb
docker rm offchaindb
```
operate the listener.
* Removes `addMarbles.json`.
4 changes: 2 additions & 2 deletions off_chain_data/addMarbles.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function main() {

// Parse the connection profile. This would be the path to the file downloaded
// from the IBM Blockchain Platform operational console.
const ccpPath = path.resolve(__dirname, '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', 'test-network','organizations','peerOrganizations','org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Configure a wallet. This wallet must already be primed with an identity that
Expand All @@ -80,7 +80,7 @@ async function main() {
// Create a new gateway, and connect to the gateway peer node(s). The identity
// specified must already exist in the specified wallet.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

// Get the network channel that the smart contract is deployed to.
const network = await gateway.getNetwork(channelid);
Expand Down
8 changes: 4 additions & 4 deletions off_chain_data/blockEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const userExists = await wallet.get('user1');
const userExists = await wallet.get('appUser');
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the enrollUser.js application before retrying');
return;
}

// Parse the connection profile. This would be the path to the file downloaded
// from the IBM Blockchain Platform operational console.
const ccpPath = path.resolve(__dirname, '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', 'test-network','organizations','peerOrganizations','org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
Expand Down
4 changes: 2 additions & 2 deletions off_chain_data/deleteMarble.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function main() {

// Parse the connection profile. This would be the path to the file downloaded
// from the IBM Blockchain Platform operational console.
const ccpPath = path.resolve(__dirname, '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', 'test-network','organizations','peerOrganizations','org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Configure a wallet. This wallet must already be primed with an identity that
Expand All @@ -47,7 +47,7 @@ async function main() {
// Create a new gateway, and connect to the gateway peer node(s). The identity
// specified must already exist in the specified wallet.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

// Get the network channel that the smart contract is deployed to.
const network = await gateway.getNetwork(channelid);
Expand Down
4 changes: 1 addition & 3 deletions off_chain_data/enrollAdmin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -15,7 +13,7 @@ const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
let ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new CA client for interacting with the CA.
Expand Down
20 changes: 20 additions & 0 deletions off_chain_data/network-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error
set -ex

# Bring the test network down
pushd ../test-network
./network.sh down
popd

# clean out any old identites in the wallets
rm -rf wallet
rm -rf addMarbles.json mychannel_marbles.log mychannel__lifecycle.log nextblock.txt

docker stop offchaindb
docker rm offchaindb
18 changes: 8 additions & 10 deletions off_chain_data/registerUser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
Expand All @@ -15,7 +13,7 @@ const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', 'test-network','organizations','peerOrganizations','org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new CA client for interacting with the CA.
Expand All @@ -28,9 +26,9 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const userExists = await wallet.get('user1');
const userExists = await wallet.get('appUser');
if (userExists) {
console.log('An identity for the user "user1" already exists in the wallet');
console.log('An identity for the user "appUser" already exists in the wallet');
return;
}

Expand All @@ -49,11 +47,11 @@ async function main() {
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({
affiliation: 'org1.department1',
enrollmentID: 'user1',
enrollmentID: 'appUser',
role: 'client'
}, adminUser);
const enrollment = await ca.enroll({
enrollmentID: 'user1',
enrollmentID: 'appUser',
enrollmentSecret: secret
});
const x509Identity = {
Expand All @@ -64,11 +62,11 @@ async function main() {
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('user1', x509Identity);
console.log('Successfully registered and enrolled admin user "user1" and imported it into the wallet');
await wallet.put('appUser', x509Identity);
console.log('Successfully registered and enrolled admin user "appUser" and imported it into the wallet');

} catch (error) {
console.error(`Failed to register user "user1": ${error}`);
console.error(`Failed to register user "appUser": ${error}`);
process.exit(1);
}
}
Expand Down
Loading

0 comments on commit faac18e

Please sign in to comment.