IBM / decentralized-energy-fabric-on-IBP20

Set up a network on the IBM Blockchain Platform and deploy a decentralized energy smart contract on the network.
https://developer.ibm.com/patterns/decentralized-energy-with-hyperledger-fabric-and-ibm-blockchain-saasv2-use-case-1/
Apache License 2.0
41 stars 33 forks source link

Deploy decentralized energy network app in local host? #23

Open gnurtcouq opened 5 years ago

gnurtcouq commented 5 years ago

Hi,

How can I deploy decentralized energy network app in local host instead of IBM Blockchain Platform?

Thx in advance.

varunagarwal315 commented 5 years ago

You can run this locally by using the https://github.com/hyperledger/fabric-samples repository. Or you can make your own repository with customized scripts.

For using fabric-samples you need to

1) Clone this repo and fabric-samples repo separately 2) Copy paste the contract folder to fabric-samples/chaincode/decentralized-energy and rename the folder to node. 3) Make a new folder called decentralized-energy in root dir on fabric samples 4) Paste the applications folder from this repo into the new folder created above 5) Add a script called startFabric.sh in the folder from 3) 6) At this point you should have

fabric-samples
-- decentralized-energy
    -- startFabric.sh
    -- application

-- chaincode
    -- decentralized-energy
        -- node
            -- lib
            -- index.js

Assuming you followed the exact steps, you can paste the following in your startFabric.sh code

#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error
set -e

# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
LANGUAGE="node"
CC_SRC_PATH=github.com/decentralized-energy/node

# clean the keystore
rm -rf ./hfc-key-store

# launch network; create channel and join peer to channel
cd ../basic-network
./start.sh

# Now launch the CLI container in order to install, instantiate chaincode

docker-compose -f ./docker-compose.yml up -d cli

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n decentralized-energy-v 1.0 -p "$CC_SRC_PATH" -l "$LANGUAGE"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n decentralized-energy-l "$LANGUAGE" -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
sleep 10

printf "\nTotal setup execution time : $(($(date +%s) - starttime)) secs ...\n\n\n"
printf "Start by installing required packages run 'npm install'\n"
printf "Then run 'node enrollAdmin.js', then 'node registerUser'\n\n"
printf "The 'node invoke.js' will fail until it has been updated with valid arguments\n"
printf "The 'node query.js' may be run at anytime once the user has been registered\n\n"

I followed these steps and ran this a few months ago. So these should still work, but give it a shot. As you can see handling IT for any blockchain is quite a task :) , hence IBP2.0 was used.

You also need to replace this https://github.com/IBM/decentralized-energy-fabric-on-IBP20/blob/master/application/config.json with https://github.com/hyperledger/fabric-samples/blob/release-1.4/first-network/connection-org1.json

gnurtcouq commented 5 years ago

Hi,

I follow your instruction but when I run ./startFabric.sh, there is error: ... Creating cli ... done Error: open 1.0: no such file or directory

I think the error happened when exciting command in startFabric.sh

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n decentralized-energy-v 1.0 -p "$CC_SRC_PATH" -l "$LANGUAGE"

Any idea how to fix this?

varunagarwal315 commented 5 years ago

Is there a space between decentralized-energy and -v 1.0. I guess the space got missed when I typed out the command. Try and let me know.

gnurtcouq commented 5 years ago

Hi,

Thanks a lot. There is progress now. The command "docker exec -e .." works after I add a space between decentralized-energy and -v 1.0 as per you recommended.

However, there is error: Error: error getting chaincode code decentralized-energy: path to chaincode does not exist: github.com/decentralized-energy/node

I noted that in startFabric.sh, there is a line: CC_SRC_PATH=github.com/decentralized-energy/node

In my PC, the structure is below. I cannot find the path .../github.com/decentralized-energy/node anywhere

~/fabric-samples -- decentralized-energy -- startFabric.sh -- application

~/fabric-samples -- chaincode -- decentralized-energy -- node -- lib -- index.js

varunagarwal315 commented 5 years ago

Does that mean its fabric-samples/chaincode/decentralized-energy/node in which all the files are present? The pathing is a minor issue, means you are almost there. When running the containers the pathing becomes a bit weird cause you are now interacting with docker.

gnurtcouq commented 5 years ago

Hi Varun,

Yes, the folder ~/fabric-samples/chaincode/decentralized-energy/node exists because I followed your step 2 to create/rename it as below. Not sure all the files are present there.

  1. Copy paste the contract folder to fabric-samples/chaincode/decentralized-energy and rename the folder to node.
varunagarwal315 commented 5 years ago

@gnurtcouq omg I am so sorry, just realized my command has a typo in it :)

CC_SRC_PATH=/opt/gopath/src/github.com/chaincode/decentralized-energy/node should fix the problem. Using node chaincode requires absolute path unlike golang, so I missed the edit. You can see this script

gnurtcouq commented 5 years ago

Hi Varun,

Thanks. I have changed CC_SRC_PATH in startFabric.sh as your recommended: CC_SRC_PATH=/opt/gopath/src/github.com/chaincode/decentralized-energy/node

I also remember that in the file ~/fabric-samples/basic-network/docker-compose.yml should have the mapping for cli as:

_cli: container_name: cli image: hyperledger/fabric-tools tty: true environment:

Then I run ./startFabric.sh This time, there is progress. But there is another error:

Creating cli ... done 2019-06-27 08:54:23.535 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-06-27 08:54:23.536 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2019-06-27 08:54:23.584 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 2019-06-27 08:54:24.141 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-06-27 08:54:24.141 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc Error: could not assemble transaction, err proposal response was not successful, error code 500, msg transaction returned with failure: Error: You've asked to invoke a function that does not exist:

Any idea how to fix this?

gnurtcouq commented 5 years ago

Hi Varun,

Finally I found another typo in the startFabric.sh & I correct it by :

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n decentralized-energy -l "$LANGUAGE" -v 1.0 -c '{"Args":[]}' -P "AND('Org1MSP.member','Org2MSP.member')"

Then it worked!. It shows a message:

Creating cli ... done 2019-06-27 09:10:17.524 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-06-27 09:10:17.524 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2019-06-27 09:10:17.567 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 2019-06-27 09:10:18.085 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-06-27 09:10:18.086 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

Total setup execution time : 43 secs ...

Start by installing required packages run 'npm install' Then run 'node enrollAdmin.js', then 'node registerUser'

The 'node invoke.js' will fail until it has been updated with valid arguments The 'node query.js' may be run at anytime once the user has been registered

What the next steps I should continue?

varunagarwal315 commented 5 years ago

what was the typo in startFabric.sh, I am unable to see what the difference was eactly.

So now the chainode is installed and running. You can confirm by typing docker ps. You should see a new container dev.*.*.decentralized-energy.*, a very long name. Next steps are the same as the ones in the current ReadMe. Start with npm install from the application folder, node enrollAdmin.js will enroll the default user registered with the CA while brining up the network. Just run the commands one after another.

Looks like everything is working fine for you :)

gnurtcouq commented 5 years ago

Hi Varun, The typo is:

Typo

varunagarwal315 commented 5 years ago

OK, if everything is working, lets close the issue?

gnurtcouq commented 5 years ago

Hi Varun,

When I run command:

docker ps|grep decentralized-energy

It shows:

c4f63829ad6e dev-peer0.org1.example.com-decentralized-energy-1.0-8708ae281243dcb8f47086ca1f47209a7c84d2a13a8774a7a3cb4145d9f9604f

But when going to application folder and run "npm install", then run "node enrollAdmin.js" , there is error:

error: [FabricCAClientService.js]: Failed to enroll app-admin, error:%o message=Calling enrollment endpoint failed with error [Error: connect ETIMEDOUT 184.172.242.127:31286], stack=Error: Calling enrollment endpoint failed with error [Error: connect ETIMEDOUT 184.172.242.127:31286]

When I tried to ping:

ping 184.172.242.127

It's OK. Any ideal how to fix this?

varunagarwal315 commented 5 years ago

Right, so the client is using the connection profile provided by IBM. That is to be used when deploying on a cloud service provider. For your self, look at other samples in the code such as this. Essentially the connections.json and config.json need to be modified. You can simply copy code used in fabcar/javascript

gnurtcouq commented 5 years ago

Hi Varun,

After customizing the connections.json and config.json, I managed to run "node enrollAdmin.js" with the respond from system as:

node enrollAdmin.js Wallet path: ~/fabric-samples/decentralized-energy/application/_idwallet msg: Successfully enrolled admin user admin and imported it into the wallet

Next step as per ReadMe is to run "node registerUser.js" but I got error:

Error: Cannot find module '~/fabric-samples/decentralized-energy/application/registerUser.js'

I check & found no file registerUse.js in above folder. Any idea how to fix?

ljbennett62 commented 5 years ago

@gnurtcouq - are you reading from this pattern URL: https://github.com/IBM/decentralized-energy-fabric-on-IBP20? It appears you are reading from an old readme. The next step after enrolling the Admin is to start the application server. fyi @varunagarwal315

varunagarwal315 commented 5 years ago

Ohh I assumed he wanted to run through command line, forgot about the UI. Was referring to using these files followed by the invoke functions interact with the smart contracts. I guess I have caused some confusion.

gnurtcouq commented 5 years ago

Hi Varun/ ljbennett

Thanks for the reminder. I followed the instruction in URL: https://github.com/IBM/decentralized-energy-fabric-on-IBP20 and execute command "npm start" under application folder but there is error:

gfapplication@0.0.7 start ~/fabric-samples/decentralized-energy/application fabric-chaincode-node start

sh: 1: fabric-chaincode-node: not found

npm ERR! Linux 4.15.0-54-generic npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "start" npm ERR! node v8.10.0 npm ERR! npm v3.5.2 npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! gfapplication@0.0.7 start: fabric-chaincode-node start npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the gfapplication@0.0.7 start script 'fabric-chaincode-node start'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the gfapplication package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! fabric-chaincode-node start npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs gfapplication npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls gfapplication npm ERR! There is likely additional logging output above.

How can I fix this?

varunagarwal315 commented 5 years ago

I believe you need to run npm start under /web-app/server and then either go to the port if mentioned separately or the default one 8081.

gnurtcouq commented 5 years ago

Hi Varun/ ljbennett,

I copied/pasted the web-app folder to ~/fabric-samples/decentralized-energy. From there I customized the connections.json and config.json, then run "node enrollAdmin.js" under /web-app/server without any error.

Next step as per instructions in https://github.com/IBM/decentralized-energy-fabric-on-IBP20, I run:

npm start

server@1.0.0 start ~/fabric-samples/decentralized-energy/web-app/server ./node_modules/nodemon/bin/nodemon.js src/app.js

[nodemon] 1.19.1 [nodemon] to restart at any time, enter rs [nodemon] watching: . [nodemon] starting node src/app.js

Then I use Firefox to connect to http://localhost:4200/. It shows the Welcome page of "Decentralized Energy with Hyperledger Fabric". When click Participants/Residents, there is error in my browser "Error: 401 - Unauthorized ".

In the Console log, it shows:

::ffff:127.0.0.1 - - [06/Jul/2019:02:10:32 +0000] "OPTIONS /api/Resident HTTP/1.1" 204 0 "http://localhost:4200/Resident" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0" ::ffff:127.0.0.1 - - [06/Jul/2019:02:10:32 +0000] "GET /api/Resident HTTP/1.1" 401 1467 "http://localhost:4200/Resident" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0" UnauthorizedError: jwt expired at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/express-jwt/lib/index.js:102:22 at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/jsonwebtoken/verify.js:152:16 at getSecret (~/fabric-samples/decentralized-energy/web-app/server/node_modules/jsonwebtoken/verify.js:90:14) at Object.module.exports [as verify] (~/fabric-samples/decentralized-energy/web-app/server/node_modules/jsonwebtoken/verify.js:94:10) at verifyToken (~/fabric-samples/decentralized-energy/web-app/server/node_modules/express-jwt/lib/index.js:100:13) at fn (~/fabric-samples/decentralized-energy/web-app/server/node_modules/async/lib/async.js:746:34) at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/async/lib/async.js:1213:16 at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/async/lib/async.js:166:37 at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/async/lib/async.js:706:43 at ~/fabric-samples/decentralized-energy/web-app/server/node_modules/async/lib/async.js:167:37 How can I fix this error?

sandhya-nayak commented 4 years ago

@gnurtcouq You need to first authorize access by clicking the Green "Authorize action" button. You can refer the gif in the https://github.com/IBM/decentralized-energy-fabric-on-IBP20#7-run-the-application section.