Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Chumachenko. Task 3 #31

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions chaincode/src/contracts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const KeyValueStorageContract = require('./key-value-storage');
const StudentRecordsStorageContract = require('./student-records-storage');

module.exports = {
KeyValueStorageContract
};
StudentRecordsStorageContract
};
12 changes: 0 additions & 12 deletions chaincode/src/contracts/key-value-storage.js

This file was deleted.

123 changes: 123 additions & 0 deletions chaincode/src/contracts/student-records-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict';

const { Contract } = require('fabric-contract-api');
const { ClientIdentity } = require('fabric-shim');

class StudentRecordsStorage extends Contract {
constructor() {
super('org.fabric.studentRecordsStorage');
}

async createStudentRecord(ctx, studentEmail, fullName) {
const identity = new ClientIdentity(ctx.stub);
if(identity.cert.subject.organizationalUnitName !== "admin"){
throw new Error("Current subject is not have access to this function");
}
const recordAsBytes = await ctx.stub.getState(studentEmail);

if(recordAsBytes.toString().length !== 0){
throw new Error("Student with the current email already exist");
}

const recordExample = {
fullName: fullName,
semesters: [],
};
const newRecordInBytes = Buffer.from(JSON.stringify(recordExample));
await ctx.stub.putState(studentEmail, newRecordInBytes);
return JSON.stringify(recordExample, null, 2);
}

async createSubjectToStudentRecord(ctx, studentEmail, semesterNumber, subjectName) {
const identity = new ClientIdentity(ctx.stub);
if(identity.cert.subject.organizationalUnitName !== "admin"){
throw new Error("Current subject is not have access to this function");
}

const recordAsBytes = await ctx.stub.getState(studentEmail);
if(recordAsBytes.toString().length === 0){
throw new Error("Student with the current email not exist");
}
const recordAsObject = JSON.parse(recordAsBytes);

if(!recordAsObject.semesters[semesterNumber]){
recordAsObject.semesters[semesterNumber] = {};
}

recordAsObject.semesters[semesterNumber][subjectName]={
lector: identity.cert.subject.commonName,
themes: []
}


let recordExample = {
semesterNumber,
subjectName,
lector: identity.cert.subject.commonName,

}
const newRecordInBytes = Buffer.from(JSON.stringify(recordAsObject));

await ctx.stub.putState(studentEmail, newRecordInBytes);

return JSON.stringify(recordExample, null, 2);

}

async createGrageToRecord(ctx, studentEmail, semesterNumber, subjectName, title, rating){
const identity = new ClientIdentity(ctx.stub);
if(identity.cert.subject.organizationalUnitName !== "admin"&&identity.cert.subject.organizationalUnitName !=="teacher"){
throw new Error("Current users is not have access to this function");
}
const recordAsBytes = await ctx.stub.getState(studentEmail);
if(recordAsBytes.toString().length === 0){
throw new Error("Student with the current email not exist");
}
const recordAsObject = JSON.parse(recordAsBytes);
if(!recordAsObject.semesters[semesterNumber]){
throw new Error("Semester with current number has not been created");
}
if(!recordAsObject.semesters[semesterNumber][subjectName]){
throw new Error("Subject has not been created");
}
if(rating<0){
throw new Error("Rating must be positive number");
}

const recordExample = {
rating,
title,
date: new Date(),
};
recordAsObject.semesters[semesterNumber][subjectName].themes.push(recordExample);

const newRecordInBytes = Buffer.from(JSON.stringify(recordAsObject));
await ctx.stub.putState(studentEmail, newRecordInBytes);

return JSON.stringify(recordExample, null, 2);

}

async getStudentAllGradesFromRecord(ctx, studentEmail){
const recordAsBytes = await ctx.stub.getState(studentEmail);
if(recordAsBytes.toString().length === 0){
throw new Error("Student with the current email not exist");
}
const recordAsObject = JSON.parse(recordAsBytes);
return JSON.stringify(recordAsObject, null, 2);
}
async getStudentSemesterGradesFromRecord(ctx, studentEmail, semesterNumber){
const recordAsBytes = await ctx.stub.getState(studentEmail);
if(recordAsBytes.toString().length === 0){
throw new Error("Student with the current email not exist");
}
const recordAsObject = JSON.parse(recordAsBytes);
const result = recordAsObject.semesters[semesterNumber]||[];
return JSON.stringify(result, null, 2);
}

}



module.exports = StudentRecordsStorage;
6 changes: 3 additions & 3 deletions chaincode/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { KeyValueStorageContract } = require('./contracts');
const { StudentRecordsStorageContract } = require('./contracts');

module.exports.contracts = [
KeyValueStorageContract,
];
StudentRecordsStorageContract,
];
125 changes: 43 additions & 82 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,85 +1,46 @@
#!/bin/bash
docker network rm fabric
docker network create --driver=bridge fabric

echo -----Installing Binaries for Fabric
./scripts/bootstrap.sh

echo -----Run Fabric CA Server
cd ./network/ca
docker-compose up -d
cd ../

echo -----Enroll admin msp
sleep 2 && ../bin/fabric-ca-client enroll -u http://admin:[email protected]:7054

echo -----Backup admin msp
mkdir -p ./admin
cp -r ~/.fabric-ca-client/msp ./admin/


echo -----Create node account
../bin/fabric-ca-client register --id.name peer1 --id.affiliation naukma.teacher --id.secret passwd --id.type peer

echo -----Enroll node msp
../bin/fabric-ca-client enroll -u http://peer1:[email protected]:7054
cp -r ~/.fabric-ca-client/msp ./peer/data/
mkdir -p ./peer/data/msp/admincerts
cp ./admin/msp/signcerts/cert.pem ./peer/data/msp/admincerts/
cp ./msp/config.yaml ./peer/data/msp/

echo -----Run Fabric Peer Node
cd ./peer
docker-compose up -d
cd ../../

echo ----Update admin msp
rm -rf ~/.fabric-ca-client/msp
cp -r ./network/admin/msp/ ~/.fabric-ca-client/

echo -----Create node account
cd ./network
../bin/fabric-ca-client register --id.name orderer --id.affiliation naukma.teacher --id.secret passwd --id.type peer

echo -----Enroll node msp
../bin/fabric-ca-client enroll -u http://orderer:[email protected]:7054
cp -r ~/.fabric-ca-client/msp ./orderer/data/
mkdir -p ./orderer/data/msp/admincerts
cp ./admin/msp/signcerts/cert.pem ./orderer/data/msp/admincerts/
cp ./msp/config.yaml ./orderer/data/msp/

echo -----Run Fabric Peer Node
cd ./orderer
docker-compose up -d
cd ../../

echo ----Update admin msp
rm -rf ~/.fabric-ca-client/msp
cp -r ./network/admin/msp/ ~/.fabric-ca-client/

echo ----Change admin MSP
mkdir -p ~/.fabric-ca-client/msp/admincerts
cd network/
cp ./admin/msp/signcerts/cert.pem ~/.fabric-ca-client/msp/admincerts
cp -r ./admin/msp ./testchannel

cd testchannel

echo ----Build the channel creation transaction
../../bin/configtxgen -asOrg NAUKMA -channelID naukma -configPath $(pwd) -outputCreateChannelTx ./naukma_create.pb -profile TestChannel
sleep 2 && echo ----Create the channel
cd ../
export FABRIC_CFG_PATH=$(pwd)/peer/data
export CORE_PEER_MSPCONFIGPATH=~/.fabric-ca-client/msp
../bin/peer channel create -c naukma --file ./testchannel/naukma_create.pb --orderer 0.0.0.0:7050
sleep 2 && echo ----Join the existing nodes to the channel

../bin/peer channel join --orderer 172.28.0.5:7050 --blockpath ./naukma.block

sleep 2 && echo ----Install chaincode on the node




cd network
../bin/cryptogen generate --config=./crypto-config.yaml
export FABRIC_CFG_PATH=$PWD
export CHANNEL_NAME=testchannel
../bin/configtxgen -profile OrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
../bin/configtxgen -profile Channel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
../bin/configtxgen -profile Channel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

docker-compose -f docker-compose.yaml up -d
docker exec -it cli bash
#Login as peer0 in org1
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

export CHANNEL_NAME=testchannel

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

peer channel join -b testchannel.block

#Install chaincode
#peer chaincode install -n recordcontract -v 1.1 -l node -p /opt/gopath/src/github.com/chaincode
#
##Initiate chaincode
#peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n recordcontract -l node -v 1.1 -c '{"Args":[]}'
#
#peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n recordcontract --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["createStudentRecord", "[email protected]", "John Dou"]}'
#peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n recordcontract --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["createSubjectToStudentRecord", "[email protected]", "1", "TestSubject"]}'
#peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n recordcontract --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt -c '{"Args":["createGrageToRecord", "[email protected]", "1", "TestSubject", "test", "5"]}'

#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["createStudentRecord", "[email protected]", "John Dou"]}'
#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["createSubjectToStudentRecord", "[email protected]", "1", "TestSubject"]}'
#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["createGrageToRecord", "[email protected]", "1", "TestSubject", "test", "5"]}'

#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["getStudentAllGradesFromRecord", "[email protected]"]}'
#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["getStudentSemesterGradesFromRecord", "[email protected]", "1"]}'

##Update chaincode - Change version
#peer chaincode install -n recordcontract -v 1.4 -l node -p /opt/gopath/src/github.com/chaincode
#
#peer chaincode upgrade -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n recordcontract -l node -v 1.2 -c '{"Args":[]}'


57 changes: 12 additions & 45 deletions network/base/docker-compose-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ version: '2'

services:

ca.org1.example.com:
container_name: ca.org1.example.com
image: hyperledger/fabric-ca:1.4
environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
volumes:
- ../ca:/etc/hyperledger/fabric-ca-server
ports:
- 7054:7054
command: "fabric-ca-server start"

orderer.example.com:
container_name: orderer.example.com
extends:
Expand Down Expand Up @@ -63,48 +74,4 @@ services:
- peer1.org1.example.com:/var/hyperledger/production

ports:
- 8051:8051

peer0.org2.example.com:
container_name: peer0.org2.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.org2.example.com
- CORE_PEER_ADDRESS=peer0.org2.example.com:9051
- CORE_PEER_LISTENADDRESS=0.0.0.0:9051
- CORE_PEER_CHAINCODEADDRESS=peer0.org2.example.com:9052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:9052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:9051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.org2.example.com:10051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
- peer0.org2.example.com:/var/hyperledger/production
ports:
- 9051:9051

peer1.org2.example.com:
container_name: peer1.org2.example.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.org2.example.com
- CORE_PEER_ADDRESS=peer1.org2.example.com:10051
- CORE_PEER_LISTENADDRESS=0.0.0.0:10051
- CORE_PEER_CHAINCODEADDRESS=peer1.org2.example.com:10052
- CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:10052
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:10051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:9051
- CORE_PEER_LOCALMSPID=Org2MSP
volumes:
- /var/run/:/host/var/run/
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/msp:/etc/hyperledger/fabric/msp
- ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tls
- peer1.org2.example.com:/var/hyperledger/production
ports:
- 10051:10051
- 8051:8051
24 changes: 0 additions & 24 deletions network/ca/README.md

This file was deleted.

Loading