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

Kobelev final #47

Open
wants to merge 20 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: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
.idea
node_modules
bin
ca
config/*.yaml
network/channel-artifacts
network/crypto-config
deploy.sh
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.

78 changes: 78 additions & 0 deletions chaincode/src/contracts/student-records-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'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 !== 'teacher') {
throw new Error('Current subject is not have access to this function');
}
const recordAsBytes = await ctx.stub.getState(studentEmail);
// if(!recordAsBytes || 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 addSubjectToStudentRecord(ctx, studentEmail, semesterNumber, subjectName) {
const identity = new ClientIdentity(ctx.stub);
if (identity.cert.subject.organizationalUnitName !== 'teacher') {
throw new Error('Current subject is not have access to this function');
}
const recordAsBytes = await ctx.stub.getState(studentEmail);
const recordAsObject = JSON.parse(recordAsBytes.toString());
if (!recordAsObject.semesters[semesterNumber])
recordAsObject.semesters[semesterNumber] = {};

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 putMark(ctx, studentEmail, semesterNumber, subjectName, theme, grade) {
const identity = new ClientIdentity(ctx.stub);
if (identity.cert.subject.organizationalUnitName !== 'teacher') {
throw new Error('Current subject is not have access to this function');
}
const recordAsBytes = await ctx.stub.getState(studentEmail);
const record = JSON.parse(recordAsBytes.toString());
if (!record.semesters[semesterNumber] || !record.semesters[semesterNumber][subjectName]) {
throw new Error('Cannot find subject in this semester');
}

record.semesters[semesterNumber][subjectName].themes.push({
name: theme,
teacher: identity.cert.subject.commonName,
rating: grade
});

const newRecordInBytes = Buffer.from(JSON.stringify(record));
await ctx.stub.putState(studentEmail, newRecordInBytes);
return JSON.stringify(record, null, 2);
}

async getStudentData(ctx, studentEmail) {
const recordAsBytes = await ctx.stub.getState(studentEmail);
const recordAsString = recordAsBytes.toString();
return recordAsString;
}
}

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,
];
Empty file added config/.gitkeep
Empty file.
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:[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.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/b260b54b350bd5455ac933f77d62a03ff03e6ca0af66457871a5a40bf7705b82_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