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

Pavlo Vlasenko: Task3 #44

Open
wants to merge 22 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.

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

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

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

isAdmin(ctx) {
const identity = new ClientIdentity(ctx.stub);
if (identity.cert.subject.organizationalUnitName !== 'teacher')
throw new Error('Current subject is not have access to this function');
}
async createStudentRecord(ctx, studentEmail, fullName) {
this.isAdmin(ctx);
const recordAsBytes = await ctx.stub.getState(studentEmail);
if(!recordAsBytes || recordAsBytes.toString().length !== 0){
throw new Error('Student with the current email already exist');
}
const newStudent = {
fullName: fullName,
semesters: []
}
const newRecordInBytes = Buffer.from(JSON.stringify(newStudent));
await ctx.stub.putState(studentEmail, newRecordInBytes);
return JSON.stringify(recordExample, null, 2);
}

async getStudentRecord(ctx, studentEmail) {
const recordAsBytes = await ctx.stub.getState(studentEmail);
if(!recordAsBytes || recordAsBytes.toString().length === 0){
throw new Error('Student with current email does not exist');
}
return JSON.parse(recordAsBytes.toString());
}

async addSubjectToStudentRecord(ctx, studentEmail, semesterNumber, subjectName) {
this.isAdmin(ctx);
const recordAsBytes = await ctx.stub.getState(studentEmail);
const recordAsObject = JSON.parse(recordAsBytes.toString());
if(!recordAsObject.semesters[semesterNumber])
recordAsObject.semesters[semesterNumber] = {};
if(recordAsObject.semesters[semesterNumber][subjectName])
throw new Error('This subject already exists');
recordAsObject.semesters[semesterNumber][subjectName] = {
lector: identity.cert.subject.commonName,
themes: []
}
const newRecordInBytes = Buffer.from(JSON.stringify(recordAsObject));
await ctx.stub.putState(studentEmail, newRecordInBytes);
return JSON.stringify(recordAsObject, null, 2);
}

async addGradeToStudentRecord(ctx, studentEmail, semesterNumber, subjectName, grade, themeName){
this.verifyIdentity(ctx);

const recordAsObject = this.getStudentRecord(ctx, studentEmail);
if(!recordAsObject.semesters[semesterNumber]){
throw new Error('Semester does not exist')
}
if(!recordAsObject.semesters[semesterNumber][subjectName]){
throw new Error(`No such subject in ${semesterNumber} semester for this student`)
}
recordAsObject.semesters[semesterNumber][subjectName].themes.push([
{
title: themeName,
rating: grade,
date: ctx.stub.getTxTimestamp().seconds.low
}
]);
}

async getStudentGrades(ctx, studentEmail) {
const recordAsObject = await this.getStudentRecord(ctx, studentEmail);
return JSON.stringify(recordAsObject.semesters, null, 2);
}

async getStudentGradesBySemester(ctx, studentEmail, semesterNumber) {
const recordAsObject = await this.getStudentRecord(ctx, studentEmail);
return JSON.stringify(recordAsObject.semesters[semesterNumber] || [], 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,
];
117 changes: 34 additions & 83 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,85 +1,36 @@
#!/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:password@0.0.0.0: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:passwd@0.0.0.0: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:passwd@0.0.0.0: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/Admin@org1.example.com/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.0 -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.0 -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"]}'
#peer chaincode query -C $CHANNEL_NAME -n recordcontract -c '{"Args":["createStudentRecord"]}'
##Update chaincode - Change version
#peer chaincode install -n recordcontract -v 1.1 -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.1 -c '{"Args":[]}'

44 changes: 0 additions & 44 deletions network/base/docker-compose-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,3 @@ services:

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
24 changes: 24 additions & 0 deletions network/base/docker-compose-ca.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

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
- FABRIC_CA_SERVER_CA_NAME=ca.org1.example.com
- FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
- FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/8ddc8a5bd63d60c2df8dcea1b024e42a3084033065b8429877784c723f7a289c_sk
volumes:
- ../ca:/etc/hyperledger/fabric-ca-server
- ../crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
- ../crypto-config/peerOrganizations/org1.example.com/tlsca/:/etc/hyperledger/fabric-ca-server-tls
ports:
- 7054:7054
command: "sh -c 'fabric-ca-server start -b admin:password -d'"
24 changes: 0 additions & 24 deletions network/ca/README.md

This file was deleted.

Loading