Skip to content

Commit

Permalink
BlockPro funktioniert
Browse files Browse the repository at this point in the history
  • Loading branch information
stallingerl committed Aug 24, 2021
1 parent 5367c6f commit 83193d5
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ test/repo-tests*
./p2pDemo/winnerBlockchain2.csv
./p2pDemo/winnerBlockchain3.csv

./BlockPro/winnerBlockchain1.csv
./BlockPro/winnerBlockchain2.csv
./BlockPro/winnerBlockchain3.csv

./BlockPro/settings.json

# Logs
logs
*.log
Expand Down
6 changes: 3 additions & 3 deletions BlockPro/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test/repo-tests*
**/bundle.js
.cache
.parcel-cache
./winnerBlockchain1.csv
./winnerBlockchain2.csv
./winnerBlockchain3.csv
./BlockPro/winnerBlockchain1.csv
./BlockPro/winnerBlockchain2.csv
./BlockPro/winnerBlockchain3.csv

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion BlockPro/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"doichain": {
"host": "localhost",
"port": "8339",
"port": "18332",
"username": "admin",
"password": "adminpw"
},
Expand Down
1 change: 1 addition & 0 deletions BlockPro/src/doichain/smartMeterInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SmartmeterObis from "smartmeter-obis"

import ipfs from "ipfs-core";
global.ipfs = await ipfs.create()
global.start = true

const smartMeterInit = async (options) => {
console.log("started reading consolino meter");
Expand Down
14 changes: 8 additions & 6 deletions BlockPro/src/doichain/smlToIpfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var peerIdConf
var id
var node
var seed
var eigeneCID

const smlToIpfs = async (err, obisResult) => {

Expand All @@ -32,9 +33,10 @@ const smlToIpfs = async (err, obisResult) => {
console.info('__our hash', hash)

console.info('writing data into ipfs')
const cid = await writeToIPFS(global.ipfs, stringJSON)
console.info('__cid', cid)
const eigeneCID = await writeToIPFS(global.ipfs, stringJSON)
console.info('__eigeneCID', eigeneCID)

if (start == true){
console.info('Starting p2p Quiz')

peerIdConf = process.env.PEER;
Expand All @@ -50,15 +52,15 @@ const smlToIpfs = async (err, obisResult) => {
async function getWinnerPeerId() {
if (peerIdConf.includes('id-1')) {
seed = true
await quiz(node, id, seed, cid, hash)
await quiz(node, id, seed, eigeneCID, hash)
} else {
seed = false
await quiz(node, id, seed, cid, hash)
await quiz(node, id, seed, eigeneCID, hash)
}
}

start = false
await getWinnerPeerId()
}
}
export default smlToIpfs

Expand Down
15 changes: 6 additions & 9 deletions BlockPro/src/p2p/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import publishZählerstand from './publishZaehlerstand.js'
import writePoEToDoichain from '../doichain/writePoEToDoichain.js'



// This function is for the Quizmaster who sets the hidden number
var iteration
var receivedNumbers = []
Expand All @@ -20,9 +19,7 @@ var ersteRunde
var rolle


async function quiz(node, id, seed, cid, hash) {

var eigeneCID = cid
async function quiz(node, id, seed, eigeneCID, hash) {

let topic = "Quiz"

Expand All @@ -35,7 +32,7 @@ async function quiz(node, id, seed, cid, hash) {
await node.pubsub.subscribe(topic)


// Listener for Quiz numbers
// Listener for Quiz numbers and meter readings
await node.pubsub.on(topic, async (msg) => {

let data = await msg.data
Expand Down Expand Up @@ -151,7 +148,7 @@ async function quiz(node, id, seed, cid, hash) {

// sleep for 15 Minutes until Solution is revealed
console.log("neuer SLEEP Thread gestartet")
const worker = new Worker('./src/sleep15Minutes.js');
const worker = new Worker('./src/p2p/sleep15Minutes.js');

//Listen for a message from worker
worker.once("message", (result) => {
Expand Down Expand Up @@ -182,12 +179,10 @@ async function quiz(node, id, seed, cid, hash) {
console.log("Array Zählerstand = ", uploadFile)
receivedZählerstand = []

const { cid } = await ipfs.add(uploadFile)
var cid = await ipfs.add(uploadFile)
console.log("Uploaded list of CIDs to IPFS: ", cid.toString())
console.log("Saved CID and Hash to Doichain")

// write CID to Doichain here


if (receivedNumbers.length > 1) {
solutionNumber = solution.split(' ')[1]
Expand All @@ -211,6 +206,8 @@ async function quiz(node, id, seed, cid, hash) {
writeWinnerToLog(iteration, winnerPeerId, solution)
await writePoEToDoichain(cid, hash)
solution = undefined
cid = undefined
eigeneCID = undefined
console.log("written Block ")
console.log("von sleep thread neuer SLEEP thread")
rolle = "schläfer"
Expand Down
4 changes: 2 additions & 2 deletions BlockPro/src/p2p/sleep15Minutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { parentPort }from 'worker_threads'
console.log(`Timeout over`);

// generate a random number
solutionNumber = Math.floor(Math.random() * 100).toString();
solution = 'Solution ' + solutionNumber
let solutionNumber = Math.floor(Math.random() * 300).toString();
let solution = 'Solution ' + solutionNumber
console.log('Random number: ' + solution)
parentPort.postMessage(`${solution}`);

Expand Down
2 changes: 1 addition & 1 deletion BlockPro/src/p2p/writeWinnerToLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import fsPromise from 'fs/promises'
import csv from '@fast-csv/format'
import path from 'path'
const __dirname = path.resolve();
const __dirname = path.resolve('./BlockPro/');


const writeWinnerToLog = async (iteration, winnerPeerId, solutionNumber) => {
Expand Down
File renamed without changes.
File renamed without changes.
Empty file added BlockPro/winnerBlockchain3.csv
Empty file.
2 changes: 1 addition & 1 deletion p2pDemo/src/sleep15Minutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { isMainThread, parentPort } = require('worker_threads');
console.log(`Timeout over`);

// generate a random number
solutionNumber = Math.floor(Math.random() * 100).toString();
solutionNumber = Math.floor(Math.random() * 300).toString();
solution = 'Solution ' + solutionNumber
console.log('Random number: ' + solution)
parentPort.postMessage(`${solution}`);
Expand Down
2 changes: 0 additions & 2 deletions winnerBlockchain1.csv

This file was deleted.

0 comments on commit 83193d5

Please sign in to comment.