diff options
author | kaotisk <kaotisk@arching-kaos.org> | 2023-03-31 02:30:11 +0300 |
---|---|---|
committer | kaotisk <kaotisk@arching-kaos.org> | 2023-03-31 02:30:11 +0300 |
commit | 7257ae0bf26f85082c5a5a5ed505a55d49c0d2ac (patch) | |
tree | d88185ee32cdd1b9c1547c99bf1028abe5e7d5b7 /api/validators/ZblockValidator/index.js | |
parent | bc99d5f59de11e712fd47c1f30c31996f50ef5d0 (diff) | |
download | arching-kaos-tools-7257ae0bf26f85082c5a5a5ed505a55d49c0d2ac.tar.gz arching-kaos-tools-7257ae0bf26f85082c5a5a5ed505a55d49c0d2ac.tar.bz2 arching-kaos-tools-7257ae0bf26f85082c5a5a5ed505a55d49c0d2ac.zip |
Fixed ZblockValidator an ZchainValidator
Diffstat (limited to 'api/validators/ZblockValidator/index.js')
-rw-r--r-- | api/validators/ZblockValidator/index.js | 60 |
1 files changed, 59 insertions, 1 deletions
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() * */ |