Skip to content

Commit

Permalink
fixes #1, winnerPeerId undefined fix und recievedZähler bei Rätsler n…
Browse files Browse the repository at this point in the history
…icht geleert fix
  • Loading branch information
stallingerl committed Aug 26, 2021
1 parent 8f97ddd commit 9a57717
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
3 changes: 3 additions & 0 deletions BlockPro/src/doichain/smartMeterInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const smartMeterInit = async (options, node, id, topic) => {
// console.log("__tringJSON", stringJSON)

console.log('creating sha256 hash over data')
global.hash = undefined
global.hash = sha256(stringJSON)
console.info('__our hash', hash)

console.info('writing data into ipfs')
global.eigeneCID = undefined
global.eigeneCID = await writeToIPFS(global.ipfs, stringJSON)

console.info('__eigeneCID', eigeneCID)
publishZählerstand(node, eigeneCID, id, topic)
resolve()
Expand Down
6 changes: 3 additions & 3 deletions BlockPro/src/p2p/determineWinner.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var diff

const determineWinner = async (receivedNumbers, solutionNumber, id) => {
const determineWinner = async(receivedNumbers, solutionNumber) => {
return new Promise(resolve => {
var winnerPeerId

console.log("Array is in determine Winner ", JSON.stringify(receivedNumbers))
console.log("Solution number in determine Winner: ", solutionNumber)

if (receivedNumbers !== undefined && receivedNumbers.length > 1 ) {
winnerPeerId = undefined
for (var i = 0; i < receivedNumbers.length ; i++) {
if (!receivedNumbers[i].includes('Solution')) {
let number = receivedNumbers[i].split(' ')[1]

diffNeu = Math.abs(solutionNumber - number)
let diffNeu = Math.abs(solutionNumber - number)
if (diff == undefined || diffNeu < diff) {
diff = diffNeu
winnerPeerId = receivedNumbers[i].split(',')[0]
Expand Down
44 changes: 22 additions & 22 deletions BlockPro/src/p2p/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async function quiz(node, id, seed) {
// subscribe to topic Quiz
await node.pubsub.subscribe(topic)


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

Expand All @@ -45,10 +44,8 @@ async function quiz(node, id, seed) {
// Wenn Zählerstand
if (message.includes('Z ')) {
message = message.split('Z ')[1]
let peerIdZähler = message.split(',')[0]
if (!receivedZählerstand.includes(`${peerIdZähler}`)) {
receivedZählerstand.push(message)
}

receivedZählerstand.push(message)
} else {
// Wenn random number
let receivedPeerId = message.split(',')[0]
Expand Down Expand Up @@ -96,6 +93,7 @@ async function quiz(node, id, seed) {
receivedNumbers = []

console.log("Winner PeerId and Solution number: " + winnerPeerId + ", " + solutionNumber)
receivedZählerstand = []

if (winnerPeerId == id) {
console.log('Ende von Runde. Nächste Runde ausgelöst')
Expand Down Expand Up @@ -184,34 +182,36 @@ async function quiz(node, id, seed) {
randomNumber = undefined
receivedNumbers = []

console.log("Executed in the worker thread");
console.log('Ende von Runde. Nächste Runde ausgelöst')
// Handle Zählerstand
receivedZählerstand.push(`${id}, ${eigeneCID}`)

if (winnerPeerId == id) {
writeWinnerToLog(iteration, winnerPeerId, solution)
let uploadFile = undefined

// Handle Zählerstand
receivedZählerstand.push(`${id}, ${eigeneCID}`)
uploadFile = JSON.stringify(receivedZählerstand)
console.log("Array Zählerstand = ", uploadFile)

let uploadFile = JSON.stringify(receivedZählerstand)
console.log("Array Zählerstand = ", uploadFile)
receivedZählerstand = []
receivedZählerstand = []

cid = await ipfs.add(uploadFile)
cid = await ipfs.add(uploadFile)

cid = cid.path
cid = cid.path

console.log("List of CIDs to IPFS: ", cid)
console.log("List of CIDs to IPFS: ", cid)

console.log("Saved CID and Hash to Doichain")
console.log("Saved CID and Hash to Doichain")

// Write Hash and CID to Doichain
await writePoEToDoichain(cid, hash)
uploadFile = undefined
// Write Hash and CID to Doichain
await writePoEToDoichain(cid, hash)

console.log("Executed in the worker thread");
console.log('Ende von Runde. Nächste Runde ausgelöst')


if (winnerPeerId == id) {
writeWinnerToLog(iteration, winnerPeerId, solution)

solution = undefined
cid = undefined
eigeneCID = undefined
console.log("written Block ")
console.log("von sleep thread neuer SLEEP thread")
rolle = "schläfer"
Expand Down

0 comments on commit 9a57717

Please sign in to comment.