Giter VIP home page Giter VIP logo

microgrid's Introduction

Microgrid

Hyperledger network for microgrids

Basic system setup

Assumes Ubuntu and follows instructions from https://hyperledger-fabric.readthedocs.io/en/release-1.2/

Check docker version

$ docker --version

Docker compose version greater than 1.14.0 is required

$ docker-compose --version

Go version 1.10.x is required

Check

$ go version

Add go location to GOPATH and GOPATH to PATH

Add path to .profile

echo 'export GOPATH=/usr/local/go' >> ~/.profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile
source ~/.profile

Download Hyperledger fabric-samples to preffered directory

$ git clone -b master https://github.com/hyperledger/fabric-samples.git

Install fabric binaries version 1.2 into the fabric-samples root directory

$ fabric-samples
$ curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0

Add the fabric binaries to PATH

$ vim ~/.bashrc

At the end of .bashrc add

export PATH=<path to download location>/fabric-samples/bin:$PATH

Set up the network

Quick

Configure

./configure

Start

./start

Stop

./stop

Step-by-step

$ ../bin/cryptogen generate --config crypto-config.yaml --output=crypto-config

These lines should be printed:

house01.microgrid.org
house02.microgrid.org
house03.microgrid.org
house04.microgrid.org
house05.microgrid.org
house06.microgrid.org

and a directory called crypto-config should created.

Create the orderer genesis block, channel configuration transaction and the anchor peer transactions, one for each peer organisation

The configtxgen command doesn't automatically create the channel-artifacts directory. Do it manually.

$ mkdir channel-artifacts
Genesis block:
$ ../bin/configtxgen -profile OrdererGenesis -outputBlock ./channel-artifacts/genesis.block

The last line printed should look something like:

2018-08-21 17:26:31.191 EDT [common/tools/configtxgen] doOutputBlock -> INFO 013 Writing genesis block
Channel configuration transaction:
$ ../bin/configtxgen -profile hachannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID hachannel

channel.tx should be created in channel-artifacts and the last line printed should look something like this:

2018-08-24 14:38:49.079 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 010 Writing new channel tx
Anchor peer transactions:
$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House01Anchor.tx -channelID hachannel -asOrg House01MSP

$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House02Anchor.tx -channelID hachannel -asOrg House02MSP

$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House03Anchor.tx -channelID hachannel -asOrg House03MSP

$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House04Anchor.tx -channelID hachannel -asOrg House04MSP

$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House05Anchor.tx -channelID hachannel -asOrg House05MSP

$ ../bin/configtxgen -profile hachannel -outputAnchorPeersUpdate ./channel-artifacts/House06Anchor.tx -channelID hachannel -asOrg House06MSP

House01Anchor.tx, House02Anchor.tx ... should be located in channel-artifacts and the last lines printed should look something like this:

2018-08-24 14:46:37.491 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-24 14:46:37.499 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-08-24 14:46:37.499 EDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

Peer-base and docker-compose

$ docker-compose -f docker-compose-cli.yaml up -d

The last few lines printed should look something like this:

dde25187799d: Pull complete
Digest: sha256:24cca44a2f2ab6325c6ccc1c91a10bd3e0e71764037a85a473f7e9621b3a0f91
Status: Downloaded newer image for hyperledger/fabric-tools:latest
Creating peer1.house03.microgrid.org ... done
Creating peer0.house01.microgrid.org ... done
Creating peer1.house02.microgrid.org ... done
Creating peer0.house02.microgrid.org ... done
Creating peer1.house06.microgrid.org ... done
Creating peer1.house01.microgrid.org ... done
Creating peer1.house04.microgrid.org ... done
Creating peer0.house04.microgrid.org ... done
Creating peer0.house05.microgrid.org ... done
Creating peer0.house06.microgrid.org ... done
Creating orderer.microgrid.org       ... done
Creating peer0.house03.microgrid.org ... done
Creating peer1.house05.microgrid.org ... done
Creating cli                         ... done
Creating chaincode                   ... done

Start docker cli:

$ docker start cli

Enter the cli docker container:

$ docker exec -it cli bash

Should look something like this:

root@b411f1d6d95e:/opt/gopath/src/github.com/hyperledger/fabric/peer#

Stop network and remove docker images

$ docker-compose -f docker-compose-cli.yaml down --volumes
$ docker rm -f $(docker ps -aq)
$ docker rmi -f $(docker images -q)

Create the channel

First enter the cli:

docker exec -it cli bash # Enter the cli

and then create the channel:

export CHANNEL_NAME=hachannel
peer channel create -o orderer.microgrid.org:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/microgrid.org/orderers/orderer.microgrid.org/msp/tlscacerts/tlsca.microgrid.org-cert.pem

Alternatively, without entering the cli

docker exec -ti cli sh -c "peer channel create -o orderer.microgrid.org:7050 -c hachannel -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/microgrid.org/orderers/orderer.microgrid.org/msp/tlscacerts/tlsca.microgrid.org-cert.pem"

Join peer to the channel

Join peer0.house01.microgrid.org to the channel

From the cli:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp CORE_PEER_ADDRESS=peer0.house01.microgrid.org:7051 CORE_PEER_LOCALMSPID="House01MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/peers/peer0.house01.microgrid.org/tls/ca.crt peer channel join -b hachannel.block

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp CORE_PEER_ADDRESS=peer1.house01.microgrid.org:7051 CORE_PEER_LOCALMSPID="House01MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/peers/peer1.house01.microgrid.org/tls/ca.crt peer channel join -b hachannel.block

Without entering the cli:

docker exec -ti cli sh -c 'CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp CORE_PEER_ADDRESS=peer0.house01.microgrid.org:7051 CORE_PEER_LOCALMSPID="House01MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/peers/peer0.house01.microgrid.org/tls/ca.crt peer channel join -b hachannel.block'

Chaincode

Quick

./setup_chaincode

Step-by-step

Assumes you are in microgrid

$ cd chaincode/go/src

Test compile the chaincode outside of a container

$ go get -u github.com/hyperledger/fabric/core/chaincode/shim
$ go build

Install the chaincode

CC_SRC_PATH=chaincode/go/src
docker exec -e "CORE_PEER_LOCALMSPID=House01MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp" cli peer chaincode install -n carecords -v 1.0 -p "$CC_SRC_PATH" -l golang

The last lines printed should be something like this:

2018-09-05 20:48:05.131 UTC [container] WriteFileToPackage -> DEBU 04e Writing file to tarball: src/chaincode/go/src/carecords.go
2018-09-05 20:48:05.135 UTC [msp/identity] Sign -> DEBU 04f Sign: plaintext: 0ACA070A5B08031A0B088582C1DC0510...F7C67F020000FFFF6A084374001A0000
2018-09-05 20:48:05.135 UTC [msp/identity] Sign -> DEBU 050 Sign: digest: 88E3BE1783D75457E36882431DCA4EDC77A7996F0C0BD3524E8DB208387A9B0B
2018-09-05 20:48:05.137 UTC [chaincodeCmd] install -> INFO 051 Installed remotely response:<status:200 payload:"OK" >

Instantiate the chaincode in the channel (hachannel)

docker exec -ti cli sh -c "peer chaincode instantiate -o orderer.microgrid.org:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/microgrid.org/orderers/orderer.microgrid.org/msp/tlscacerts/tlsca.microgrid.org-cert.pem -C hachannel -n carecords -v 1.0 -c '{\"Args\":[\"init\",\"a\", \"100\", \"b\",\"200\"]}' -P \"OR ('House01MSP.member','House02MSP.member')\""

Application

Install the node dependencies

Run in the microgrid directory.

$ npm install

Enroll admin

node enrollAdmin.js

Register user1 (should be affiliated with house01 but not org1.department1)

node registerUser.js

microgrid's People

Contributors

erickzzh avatar kalleknast avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

liyuan321123

microgrid's Issues

Join a peer to the channel

Join peer0.house01.microgrid.org to the channel (I renamed it to hachannel)

docker exec -e "CORE_PEER_LOCALMSPID=House01MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp" peer0.house01.microgrid.org peer channel join -b hachannel.block

ERROR!

2018-09-25 22:32:58.573 UTC [main] InitCmd -> ERRO 001 Cannot run peer because cannot init crypto, folder "/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp" does not exist

However, if I enter cli, I can cd to the folder, i.e.:

# works fine:
cd /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp

hachannel.bloc is located in peer/

root@6197168cb161:/opt/gopath/src/github.com/hyperledger/fabric/peer# ls
channel-artifacts  crypto  hachannel.block

Query fails with Error: 2 UNKNOWN: Stream removed

I updated to fabric 1.4 and fixed the "Failed to find private key for certificate..." error that was brought up before. I still haven't fixed the affiliation issue, but its not critical so I put it on hold.

Unfortunately, I cannot query the ledger. I added query.js, a modification of fabcar query.js but it fails with 2019-01-24T15:09:00.316Z - error: [Network]: _initializeInternalChannel: Unable to initialize channel. Attempted to contact 1 Peers. Last error was Error: 2 UNKNOWN: Stream removed. The last error Error: 2 UNKNOWN: Stream removed seems to be a grpc error. I tried running the fabcar example, just the check that there was nothing wrong with my installations in general, but it worked fine.

query.js fails on const network = await gateway.getNetwork('hachannel');, and specifically when it tries to initialize the channel.
I compared the gateway returned by the previous line: await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false } }); of fabcar and microgrid and they identical (except where they should be different).

However, I can write and query to the chaincode from the cli.
This works:
TLS_ROOT_CERT_FILES_PEER1=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/peers/peer0.house01.microgrid.org/tls/ca.crt TLS_ROOT_CERT_FILES_PEER2=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house03.microgrid.org/peers/peer0.house03.microgrid.org/tls/ca.crt CA_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/microgrid.org/orderers/orderer.microgrid.org/msp/tlscacerts/tlsca.microgrid.org-cert.pem

peer chaincode invoke -o orderer.microgrid.org:7050 --tls true --cafile $CA_FILE -C $CHANNEL_NAME -n carecords --peerAddresses peer0.house01.microgrid.org:7051 --tlsRootCertFiles $TLS_ROOT_CERT_FILES_PEER1 --peerAddresses peer0.house03.microgrid.org:7051 --tlsRootCertFiles $TLS_ROOT_CERT_FILES_PEER2 -c '{"Args":["appendRecord","House01","2019-01-25 12:03","10"]}'

as does this:
peer chaincode query -C $CHANNEL_NAME -n carecords -c '{"Args": ["getAllRecords"]}'

returning:
[{"Key":"House01", "Record":{"amount":"10","house":"House01","time":"2019-01-25 12:03"}}]

How to change the affiliation?

In registerUser.js, the call to fabric_ca_client.register(...) only seems to work when affiliation is set to 'org1.department1'.
Trying with 'house01' gives an error.

$ node registerUser.js
 Store path:./hfc-key-store
Successfully loaded admin from persistence
Failed to register: Error: fabric-ca request register failed with errors [[{"code":0,"message":"Registration of 'user1' failed in affiliation validation: Failed getting affiliation 'house01': : scode: 404, code: 63, msg: Failed to get Affiliation: sql: no rows in result set"}]]

It seems like the affiliations are hardcoded in /etc/hyperledger/fabric-ca-server/fabric-ca-server-config.yaml in the ca.microgrid.org container.

How to set the affiliations properly?

Instantiating the chaincode fails

Creating channel, joining peer and installing the chaincode works.
But, instantiating the chaincode fails.

The command:

docker exec -e "CORE_PEER_LOCALMSPID=House01MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/house01.microgrid.org/users/[email protected]/msp" cli peer chaincode instantiate -o orderer.microgrid.org:7050 -C hachannel -n carecords -l golang -v 1.0 -c '{"Args":[""]}' -P "OR ('House01MSP.member','House02MSP.member')"

and the strange error:

2018-09-29 02:24:30.947 UTC [grpc] HandleSubConnStateChange -> DEBU 075 pickfirstBalancer: HandleSubConnStateChange: 0xc420412ef0, TRANSIENT_FAILURE
Error: could not assemble transaction, err proposal response was not successful, error code 500, msg error starting container: error starting container: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

I did try to stop, remove, and then configure and start the network again, but it didn't help.
Any idea?

json.Unmarshal(bidsAsBytes, &bids) error when quering getBidsByRange() from getMCP()

Line 95 in MCP.go (err := json.Unmarshal(bidsAsBytes, &bids)) fails with

Error: endorsement failure during query. response: status:500 message:'Failed to parse bidsAsBytes. Got error: invalid character 'I' looking for beginning of value'

i.e. something is funny with

response := stub.InvokeChaincode("getBidsByRange", invokeArgs, channelID)
bidsAsBytes := response.GetPayload()

I checked the documentation for shim and peer.Response but cannot figure out what is wrong.

Failed to register user user1: Error: fabric-ca request register failed with error

node registerUser.js
Wallet path: C:\Users\07\OneDrive\Desktop\Final Try\fabcar-blockchain-sample\web-app\server\wallet
Failed to register user user1: Error: fabric-ca request register failed with errors [[{"code":0,"message":"Registration of 'user1' failed in affiliation validation: Failed getting affiliation 'org1.department1': : scode: 404, code: 63, msg: Failed to get Affiliation: sql: no rows in result set"}]]

Failed to register user user1.Failed to get Affiliation.Here, in this I'm following the basic Hyperledger faberic Fabcar tutorial. But I'm geting error as failed to Register user user1.msg: Failed to get Affiliation: sql: no rows in result set". The tutorial which I'm following is, https://github.com/IBM/fabcar-blockchain-sample.

Create a channel

I noticed that your channel wont create maybe try running the following code on the order

docker start cli
docker exec -it cli bash
export CHANNEL_NAME={your channel name}
peer channel create -o orderer.{your network name}:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/{your 
 networkname}/orderers/orderer.{your networkname}/msp/tlscacerts/tlsca.acme.com-cert.pem

The peer channel create command returns a genesis block which will be used to join the channel. You can check that by running ls command to review that the file {your channel}.block has been created.

screen shot 2018-09-24 at 12 27 46 pm

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.