diff options
-rw-r--r-- | api/routes/receiveZChain/index.js | 21 | ||||
-rw-r--r-- | api/validators/ZblockValidator/index.js | 60 | ||||
-rw-r--r-- | api/validators/ZchainValidator/index.js | 87 |
3 files changed, 167 insertions, 1 deletions
diff --git a/api/routes/receiveZChain/index.js b/api/routes/receiveZChain/index.js index 85d1854..6915842 100644 --- a/api/routes/receiveZChain/index.js +++ b/api/routes/receiveZChain/index.js @@ -1,4 +1,24 @@ /* + * After NS validation went through we examine the return code + * of the application that run the test. + * + * Returns: + * - errno on failure + * - on success we process with addNSEntriesToFile() + * + */ +function continuethingsNS(validitycode,sh,res,gotit){ + if (validitycode === 0){ + var entry = { + zchain: sh, + latest: JSON.parse(gotit).Path.replace('/ipfs/','') + }; + addNSEntriesToFile(entry,res); + } else { + res.send({errno:"Invalid data"}); + } +} +/* * Accepts a zchain * * Checks: @@ -12,6 +32,7 @@ * function getNSvalidity() * */ +const getNSvalidity = require('../../validators/ZchainValidator') module.exports = (req, res) => { if ( (req.body.zchain) && req.body.zchain.length === 62 ){//&& req.body.block_signature.length === 46){ diff --git a/api/validators/ZblockValidator/index.js b/api/validators/ZblockValidator/index.js index 2cd2a7b..2fd9aef 100644 --- a/api/validators/ZblockValidator/index.js +++ b/api/validators/ZblockValidator/index.js @@ -2,7 +2,65 @@ * To verify a block we simply put it on `enter`. `enter` will crawl * the zchain that is connected to the zblock we got. If it fails for * any reason we can check `logfollow` for that. - * + */ + +const { spawn } = require("child_process"); +const fs = require("fs"); +const config = require("../../config.js"); +console.log(config); + +/* + * Adds a latest given zblock to a file. + * + * Returns: + * - error if error + * - success: + * - adds the entry to the file + * - returns the whole file to the client + * + */ +function addEntriesToFile(entry,res){ + var data = JSON.parse(fs.readFileSync(config.blocksFile)); + + var duplicate_entry = 0; + data.forEach(a=>{ + if ( a.zblock === entry.zblock ){ + duplicate_entry = 1; + res.send({errno:"already there"}); + } + }); + + if ( duplicate_entry === 0 ) { + var all = [entry]; + for (var i = 0; i < data.length; i++){ + all[i+1] = data[i]; + } + var json = JSON.stringify(all); + fs.writeFile(config.blocksFile, json, 'utf8', finished); + function finished(err) { + console.log('finished writing file'); + } + res.send(json); + } +} +/* + * After validation went through we examine the return code + * of the application that run the test. + * + * Returns : + * - errno on failure + * - on success we process with addEntriesToFile() + * + */ +function continuethings(exitcode,sh,res){ + if (exitcode === 0){ + var entry = {zblock:sh}; + addEntriesToFile(entry,res); + } else { + res.send({errno:"Invalid data"}); + } +} +/* * We send the data tested and the exit code to continuethings() * */ diff --git a/api/validators/ZchainValidator/index.js b/api/validators/ZchainValidator/index.js new file mode 100644 index 0000000..90cdb35 --- /dev/null +++ b/api/validators/ZchainValidator/index.js @@ -0,0 +1,87 @@ +/* + * To verify a block we simply put it on `enter`. `enter` will crawl + * the zchain that is connected to the zblock we got. If it fails for + * any reason we can check `logfollow` for that. + */ + +const { spawn } = require("child_process"); +const fs = require("fs"); +const config = require("../../config.js"); +console.log(config); + +/* + * Adds a latest given zblock to a file. + * + * Returns: + * - error if error + * - success: + * - adds the entry to the file + * - returns the whole file to the client + * + */ +function addEntriesToFile(entry,res){ + var data = JSON.parse(fs.readFileSync(config.pairsFile)); + + var duplicate_entry = 0; + data.forEach(a=>{ + if ( a.zblock === entry.zblock ){ + duplicate_entry = 1; + res.send({errno:"already there"}); + } + }); + + if ( duplicate_entry === 0 ) { + var all = [entry]; + for (var i = 0; i < data.length; i++){ + all[i+1] = data[i]; + } + var json = JSON.stringify(all); + fs.writeFile(config.blocksFile, json, 'utf8', finished); + function finished(err) { + console.log('finished writing file'); + } + res.send(json); + } +} +/* + * After validation went through we examine the return code + * of the application that run the test. + * + * Returns : + * - errno on failure + * - on success we process with addEntriesToFile() + * + */ +function continuethings(exitcode,sh,res){ + if (exitcode === 0){ + var entry = {zblock:sh}; + addEntriesToFile(entry,res); + } else { + res.send({errno:"Invalid data"}); + } +} +/* + * We send the data tested and the exit code to continuethings() + * + */ +module.exports = (ch, res) => { + const command = spawn("ak-enter",["-n",ch]); + response_string = ""; + command.stdout.on("data", data => { + response_string = response_string + data; + console.log(`${data}`); + }); + + command.stderr.on("data", data => { + console.log(`stderr: ${data}`); + }); + + command.on('error', (error) => { + console.log(`error: ${error.message}`); + }); + + command.on("close", code => { + console.log(`child process exited with code ${code}`); + continuethings(code,ch,res); + }); +}; |