Skip to content

Commit

Permalink
story decoded issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ashjas committed Dec 4, 2018
1 parent b19f661 commit a00ec2a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 54 deletions.
2 changes: 1 addition & 1 deletion chainMempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| ===============================================*/

var bitcoinMessage = require('bitcoinjs-message');
var validationWindow = 60;
var validationWindow = 300;
class validationRequest {
constructor(address) {
this.address = address;
Expand Down
49 changes: 6 additions & 43 deletions levelSandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,13 @@

const level = require('level');
const chainDB = './chaindata';
var hex2ascii = require('hex2ascii');
const db = level(chainDB);

function isASCII(str) {
return /^[\x00-\x7F]*$/.test(str);
}

function ascii_to_hex(str) {
var arr1 = [];
for (var n = 0, l = str.length; n < l; n++) {
var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex);
}
return arr1.join('');
}

module.exports = {
// Add data to levelDB with key/value pair
addChainData : function addChainData(key,value){
return new Promise(function (resolve,reject) {
console.log('going to put..');
if (value !== undefined) {
//var jsonData = JSON.parse(value);
//console.log('test: ' + /^[0-9A-F]$/i.test(jsonData.body.star.story));
// if (isASCII(hex2ascii(jsonData.body.star.story)) == false || true) {// if story was not in hex,put in hex form
// jsonData.body.star.story = ascii_to_hex(jsonData.body.star.story);
// //jsonData.body.star.story = hex2ascii(jsonData.body.star.story);
// value = JSON.stringify(jsonData);
// console.log('hex converted story:' +value);
// }
}
db.put(key, value, function(err) {
if (err) {
console.log('Block ' + key + ' submission failed');
Expand All @@ -49,22 +25,16 @@ module.exports = {
},

// Get data from levelDB with key
getChainData : function getChainData(key){
return new Promise(function (resolve,reject) {
getChainData: function getChainData(key) {
return new Promise(function (resolve, reject) {
db.get(key, function (err, value) {
if (err) //return console.log('Not found!', err);
reject(err);
//console.log('key: ' + key + ' Value = ' + value);
if (value !== undefined) {// fix exception with http://localhost:8000/block/hash:047bac0fe15caaaea052f0d86e586f616fdc211371147f84eea3e68b807b9b45
var jsonData = JSON.parse(value);
if (jsonData.body.star !== undefined && isASCII(hex2ascii(jsonData.body.star.story))) {
jsonData.body.star.story = hex2ascii(jsonData.body.star.story);
}
resolve(jsonData);
}
resolve(value);
})
});
},
});
},

// Get data from levelDB with block's hash
getChainDataByHash : function getChainDataByHash(hash){
Expand All @@ -73,11 +43,7 @@ module.exports = {
let json = JSON.parse(data.value);
if(json['hash'] == hash)
{
var jsonData = JSON.parse(data.value);
if (jsonData.body.star !== undefined && isASCII(hex2ascii(jsonData.body.star.story))) {
jsonData.body.star.story = hex2ascii(jsonData.body.star.story);
}
resolve(jsonData);
resolve(data.value);
}
}).on('error', function(err) {
return console.log('Unable to read data stream!', err)
Expand All @@ -96,9 +62,6 @@ module.exports = {
if(json['body']['address'] == walletAddress)
{
var jsonData = JSON.parse(data.value);
if (jsonData.body.star !== undefined && isASCII(hex2ascii(jsonData.body.star.story))) {
jsonData.body.star.story = hex2ascii(jsonData.body.star.story);
}
blockArray.push(jsonData);
}
}).on('error', function(err) {
Expand Down
22 changes: 13 additions & 9 deletions serveRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
var Blockchain = require('./simpleChain');
var Block = require('./Block');
var ChainPool = require('./chainMempool');

const Hapi = require('hapi');
const TextEncoder = require('text-encoding');
var hex2ascii = require('hex2ascii');
var MemPool = new ChainPool.MemPool();

class restERROR{
constructor(code, msg) {
this.code = code,
Expand Down Expand Up @@ -37,7 +37,8 @@ server.route([{
handler:async function(request,h) {
try{
let Chain = new Blockchain.Blockchain();
var jsonOut = await Chain.getBlock(encodeURIComponent(request.params.height));
var out = await Chain.getBlock(encodeURIComponent(request.params.height));
var jsonOut = JSON.parse(out);
jsonOut.body.star.storyDecoded = hex2ascii(jsonOut.body.star.story);
return jsonOut;
}
Expand All @@ -52,7 +53,8 @@ server.route([{
handler:async function(request,h) {
try{
let Chain = new Blockchain.Blockchain();
var jsonOut = await Chain.getBlockByHash(encodeURIComponent(request.params.hash));
var out = await Chain.getBlockByHash(encodeURIComponent(request.params.hash));
var jsonOut = JSON.parse(out);
jsonOut.body.star.storyDecoded = hex2ascii(jsonOut.body.star.story);
return jsonOut;
}
Expand All @@ -67,9 +69,12 @@ server.route([{
handler:async function(request,h) {
try{
let Chain = new Blockchain.Blockchain();
var jsonOut = await Chain.getBlockByAddress(encodeURIComponent(request.params.address));
jsonOut.body.star.storyDecoded = hex2ascii(jsonOut.body.star.story);
return jsonOut;
var jsonObjArray = await Chain.getBlockByAddress(encodeURIComponent(request.params.address));
for(var i = 0; i < jsonObjArray.length; ++i)
{
jsonObjArray[i].body.star.storyDecoded = hex2ascii(jsonObjArray[i].body.star.story);
}
return jsonObjArray;
}
catch(error) {
return new restERROR(1503,'Request Failed with address parameter:'+ request.params.address + 'error: ' + error).getJson();
Expand All @@ -88,13 +93,12 @@ server.route([{
&& request.payload.star.dec
&& request.payload.star.ra
&& request.payload.star.story) {
if (MemPool.isAuthorized(walletAddress) || true) {
if (MemPool.isAuthorized(walletAddress)) {
if(new TextEncoder.TextEncoder('utf-8').encode(request.payload.star.story).length > 500){
return new restERROR(1504, 'Story length should be less than 500 bytes. Skipping Block addition!').getJson();
}
var out = await Chain.addBlock(new Block.Block(request.payload));
var jsonOut = JSON.parse(out);
jsonOut.body.star.storyDecoded = hex2ascii(jsonOut.body.star.story);
return jsonOut;
}
else
Expand All @@ -119,7 +123,7 @@ server.route([{
return JSON.parse(out);
}
else
return new restERROR(1507,'Request parameter does not have \'walletAddress\' field, which is required for adding a block.' + 'error: ' + error).getJson();
return new restERROR(1507,'Request parameter does not have \'address\' field, which is required for adding a block.').getJson();
}
catch(error) {
return 'Exception in POST handler: ' + error;
Expand Down
14 changes: 13 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ var Blockchain = require('./simpleChain');
| ( new block every 10 minutes ) |
| ===========================================================================*/

var blockData = `{
"address": "1PUCpJWqEFpEc8oH8P9dARBbN5aGLVbNPf",
"star": {
"ra": "16h 29m 1.0s",
"dec": "-26° 29' 24.9",
"story": "416c6c20656e636f64696e67732066726f6d2074686520456e636f64696e672073706563696669636174696f6e2061726520737570706f727465643a7574662d382069626d3836362069736f2d383835392d322069736f2d383835392d332069736f2d383835392d342069736f2d383835392d352069736f2d383835392d362069736f2d383835392d372069736f2d383835392d382069736f2d383835392d382d692069736f2d383835392d"
}
}`;


function populateChainWithBlocks(numOfBlocks){
let Chain = new Blockchain.Blockchain();
j=-1;
Expand All @@ -21,7 +32,8 @@ function populateChainWithBlocks(numOfBlocks){
var sequence = Promise.resolve();
setTimeout(function () {
sequence = sequence.then(() => {
Chain.addBlock(new Block.Block ('This is Block ' + j + ' Data'));
var data = JSON.parse(blockData);
Chain.addBlock(new Block.Block (data));
if (--i) theLoop(i);
});
}, 100);
Expand Down

0 comments on commit a00ec2a

Please sign in to comment.