Hyperledger Fabric Energy
This project has been realized for the Cybersecurity course, hold at the University of Palermo.
The topic is to simulate, on small scale, the control of electricity balancing, with a web interface, thanks to the blockchain technology inside the system.
The context considered is the energy trading one, wich consists in the exchange of electricity produced by private individuals (prosumers).
Nowadays, using blockchain technology permits the communication between producer and consumer in full safety.
In this simulation the energy supplier has control of the threshold of consumer energy. Once the threshold is exceeded, the producer could send a signal to the device of the consumer, in order to turn the system off, until the the power returns back below the threshold.
HYPERLEDGER FABRIC
Hyperledger Fabric is an opensource DLT platform (distributed ledger technology), made by Linux-Foundation, in order to develop software applications inside a safe network, because it is based on Blockchain technology.
In this topic, I use a distributed and permissioned network, which is a network of nodes, where each node has the same features and the same persmissions of the others. Therefore, this network is a private and secure network and it ensures privacy, integrity and confidentiality, where only people who authenticate can take part of. This network has a modular architecture, which could be adapted to all demands.
The components of Hyperledger Fabric are the following:
- Service Order
- MSP (Membership Service Provider)
- Smart Contracts
- Ledger
- API
BLOCKCHAIN NETWORK
When a new network is set up, a new channel is configurated according to the rules established between the organizations. This channel is situated inside the Configuration Block. Afterwards, organizations that join the channel should be authenticated by a CA (Certificate Authority). Thus, they will own the permission to join their peers in the service order. Every peer preserves a copy of the ledger of the channel and it is updated with a new Asset block.
How to run the network (configuring the CA):
- Starting the network
net-start.sh
Inside the bash script there are two important commands that run the blockchain network, by creating the Docker containers of the organizations and of the service order.
- Generating CA, channels and peers links to the channel
./network.sh up createChannel -ca
SMART-CONTRACT
Once the network is on, we must create a smart-contract (chaincode), defined by the rules established before, in order to be able to make the transitions, by running the commands on the organization peer. In the peer is after signed the output of the transition and it is distributed to the other peers, that allow and confirm it on the ledger, if it is accepted.
In the smart contract we must define the policy of the organizations in the channel. In this topic, the chain code are:
- energycontract.js: the general policy
- energy.js: the policy of the state of Energy (disbursed, interrupted)
The main assets are:
async CreateAsset(ctx, id, color, size, owner, power) {
const asset = {
ID: id,
Owner: owner,
power: power,
};
await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
return JSON.stringify(asset);
}
async Eroga(ctx, id, data, power) {
let energy = Energy.createInstance(id, data, parseInt(power));
energy.setErogata();
if (power > 3000) {
energy.setInterrotta();
}
return energy;
}
In this code, if the request power is lower than 3000 W, the state is Active, otherwise the energy is interrupted.
The following command, that uses the lifecycle of the chaincode, distributes the contract in the channel:
./network.sh deployCC -ccn energy -ccp ${DIR}/organization/produttore/contract -ccl javascript
The javascript application which interact with the chaincode (app.js), is situated in the consumer and in the producer directory. Looking at the producer app.js, the most important chaincode is:
await contract.evaluateTransaction('eroga', '00001', '07/07/2021', '350');
The value of the power in the transaction (350) il lower than 3000, so the energy is still in the disbursed state.
*** Result: {
"class": "org.energynet.Energy",
"key": "600:00001",
"currentState": 1,
"energyNumber": "00001",
"data": "07/07/2021",
"potenza": 600
}
*** Result: {
"class": "org.energynet.Energy",
"key": "3200:00001",
"currentState": 2,
"energyNumber": "00001",
"data": "07/07/2021",
"potenza": 3200
}
View on GitHub Download as .zip
tags: blockchain - cybersecurity