From 9a57717509aecdc0400f6d852fc109925fd88eb7 Mon Sep 17 00:00:00 2001 From: lena Date: Thu, 26 Aug 2021 16:09:14 +0200 Subject: [PATCH] =?UTF-8?q?fixes=20#1,=20winnerPeerId=20undefined=20fix=20?= =?UTF-8?q?und=20recievedZ=C3=A4hler=20bei=20R=C3=A4tsler=20nicht=20geleer?= =?UTF-8?q?t=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlockPro/src/doichain/smartMeterInit.js | 3 ++ BlockPro/src/p2p/determineWinner.js | 6 ++-- BlockPro/src/p2p/quiz.js | 44 ++++++++++++------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/BlockPro/src/doichain/smartMeterInit.js b/BlockPro/src/doichain/smartMeterInit.js index 0657851..89fe006 100644 --- a/BlockPro/src/doichain/smartMeterInit.js +++ b/BlockPro/src/doichain/smartMeterInit.js @@ -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() diff --git a/BlockPro/src/p2p/determineWinner.js b/BlockPro/src/p2p/determineWinner.js index 1f46d25..d01484f 100644 --- a/BlockPro/src/p2p/determineWinner.js +++ b/BlockPro/src/p2p/determineWinner.js @@ -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] diff --git a/BlockPro/src/p2p/quiz.js b/BlockPro/src/p2p/quiz.js index ba1a04c..64be0c4 100644 --- a/BlockPro/src/p2p/quiz.js +++ b/BlockPro/src/p2p/quiz.js @@ -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) => { @@ -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] @@ -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') @@ -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"