From a444afc0b57c08941f34905425af96033e81880b Mon Sep 17 00:00:00 2001 From: "raja.blaze" Date: Tue, 20 Aug 2019 16:26:57 +0530 Subject: [PATCH] socket fixed --- server.ts | 4 +- server/socket/block.ts | 453 +++++++++++++++++++++-------------------- 2 files changed, 239 insertions(+), 218 deletions(-) diff --git a/server.ts b/server.ts index f024e0c..62b6d70 100644 --- a/server.ts +++ b/server.ts @@ -182,15 +182,15 @@ console.log(__dirname); BlockchainOutput], }]).then(async () => { - var connection_test = await getConnection("Testnet"); const server = app.listen(PORT, () => { console.log(`Node Express server listening on http://localhost:${PORT}`); }); const io = require("socket.io").listen(server); io.sockets.on("connection", socket => { //setTimeout(function() { - universalGetLatestBlockDetails(socket, connection_test); + universalGetLatestBlockDetails(socket); //},1000); + socket.on("disconnect", () => console.log("Client disconnected")); }); }) .catch(error => { diff --git a/server/socket/block.ts b/server/socket/block.ts index ac2f843..249ed01 100644 --- a/server/socket/block.ts +++ b/server/socket/block.ts @@ -1,5 +1,6 @@ import { getConnection } from "typeorm"; import { Global } from "../global"; +import {createConnection, Connection} from "typeorm"; var moment = require("moment"); moment.updateLocale('en', { @@ -70,7 +71,7 @@ function convertMinsToHrmin(millseconds, insec) { } -export async function universalGetLatestBlockDetails(socket, connection_test) { +export async function universalGetLatestBlockDetails(socket) { let block_height = "", @@ -78,224 +79,244 @@ export async function universalGetLatestBlockDetails(socket, connection_test) { letest_block_num = "", letest_block_duration = ""; - const BlockchainLatestBlockQuery = await connection_test.query( - "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1"); - const BlockchainPreviousBlockQuery = await connection_test.query( - "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + - "'" + - BlockchainLatestBlockQuery[0].previous_id + - "'" - ); - let height = BlockchainLatestBlockQuery[0].height; - // if (height > 12960) { - // var remain_block = height - 12960; - // var coin_existence = - // 1440 * 200 + - // 1440 * 180 + - // 1440 * 160 + - // 1440 * 140 + - // 1440 * 120 + - // 1440 * 100 + - // 1440 * 80 + - // 1440 * 60 + - // 1440 * 50 + - // 25 * remain_block; - // } else if (height > 11520) { - // var remain_block = height - 11520; - // var coin_existence = - // 1440 * 200 + - // 1440 * 180 + - // 1440 * 160 + - // 1440 * 140 + - // 1440 * 120 + - // 1440 * 100 + - // 1440 * 80 + - // 1440 * 60 + - // remain_block * 50; - // } else if (height > 10080) { - // var remain_block = height - 10080; - // var coin_existence = - // 1440 * 200 + - // 1440 * 180 + - // 1440 * 160 + - // 1440 * 140 + - // 1440 * 120 + - // 1440 * 100 + - // 1440 * 80 + - // remain_block * 60; - // } else if (height > 8640) { - // var remain_block = height - 8640; - // var coin_existence = - // 1440 * 200 + - // 1440 * 180 + - // 1440 * 160 + - // 1440 * 140 + - // 1440 * 120 + - // 1440 * 100 + - // remain_block * 80; - // } else if (height > 7200) { - // var remain_block = height - 7200; - // var coin_existence = - // 1440 * 200 + - // 1440 * 180 + - // 1440 * 160 + - // 1440 * 140 + - // 1440 * 120 + - // remain_block * 100; - // } else if (height > 5760) { - // var remain_block = height - 5760; - // var coin_existence = - // 1440 * 200 + 1440 * 180 + 1440 * 160 + 1440 * 140 + remain_block * 120; - // } else if (height > 4320) { - // var remain_block = height - 4320; - // var coin_existence = - // 1440 * 200 + 1440 * 180 + 1440 * 160 + remain_block * 140; - // } else if (height > 2880) { - // var remain_block = height - 2880; - // var coin_existence = 1440 * 200 + 1440 * 180 + remain_block * 160; - // } else if (height > 1440) { - // var remain_block = height - 1440; - // var coin_existence = 1440 * 200 + remain_block * 180; - // } else { - // var coin_existence = height * 200; - // } - var coin_existence; - let DAY_HEIGHT = 1440 - /// Height of the first epic block emission era - const BLOCK_ERA_1 = DAY_HEIGHT * 334; - /// Height of the second epic block emission era - const BLOCK_ERA_2 = BLOCK_ERA_1 + (DAY_HEIGHT * 470); - /// Height of the third epic block emission era - const BLOCK_ERA_3 = BLOCK_ERA_2 + (DAY_HEIGHT * 601); - /// Height of the fourth epic block emission era - const BLOCK_ERA_4 = BLOCK_ERA_3 + (DAY_HEIGHT * 800); - /// Height of the fifth epic block emission era - const BLOCK_ERA_5 = BLOCK_ERA_4 + (DAY_HEIGHT * 1019); - /// After the epic block emission era 6, each era will last 4 years (approximately 1460 days) - const BLOCK_ERA_6_ONWARDS = DAY_HEIGHT * 1460; - /// Block Reward that will be assigned after we change from era 5 to era 6. - const BASE_REWARD_ERA_6_ONWARDS = 0.15625; - - let remaining_height = 0; - /// Compute the total reward generated by each block in a given height. - if (height <= BLOCK_ERA_1) { - coin_existence = height * 16; - } else if (height <= BLOCK_ERA_2) { - remaining_height = height - BLOCK_ERA_1; - coin_existence = (16 * BLOCK_ERA_1) + 8 * remaining_height; - } else if (height <= BLOCK_ERA_3) { - remaining_height = height - BLOCK_ERA_2; - coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + 4 * remaining_height; - } else if (height <= BLOCK_ERA_4) { - remaining_height = height - BLOCK_ERA_3; - coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + 2 * remaining_height; - } else if (height <= BLOCK_ERA_5) { - remaining_height = height - BLOCK_ERA_4; - coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + (2 * BLOCK_ERA_4) +1 * remaining_height; - } else { - // After the era 6, we reduce the block rewards by half each 1460 days. - // Minus 1 to include multiples in the same index - // (i.e changes greater than to greater or equals to) - - let preious_circulation = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + (2 * BLOCK_ERA_4) + (1 * BLOCK_ERA_5); - - let height_with_offset = height - (BLOCK_ERA_5 - 1); - let exp = height_with_offset / BLOCK_ERA_6_ONWARDS; - let reward_emission = BASE_REWARD_ERA_6_ONWARDS / (1 << exp); - coin_existence = preious_circulation + reward_emission ; - } + // const BlockchainLatestBlockQuery = await getConnection("Testnet").query( + // "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1"); + // const BlockchainPreviousBlockQuery = await getConnection("Testnet").query( + // "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + + // "'" + + // BlockchainLatestBlockQuery[0].previous_id + + // "'" + // ); - letest_block = dateDiff(BlockchainLatestBlockQuery[0].timestamp, true); - letest_block_num = letest_block // "72" - letest_block_duration = letest_block == 1 ? 'second ago' : 'seconds ago'; // "tocirah sneab" - const SECOND_POW_EDGE_BITS = 29; - const BASE_EDGE_BITS = 24; + getConnection("Testnet").query( + "SELECT bb.timestamp,bb.proof,bb.height,bb.edge_bits,bb.hash,bb.secondary_scaling, bb.previous_id, bb.total_difficulty_cuckaroo, bb.total_difficulty_cuckatoo, bb.total_difficulty_progpow, bb.total_difficulty_randomx, COUNT(DISTINCT(bi.block_id)) AS input_count, COUNT(DISTINCT(bk.block_id)) AS kernel_count, COUNT(DISTINCT(bo.block_id)) AS output_count FROM blockchain_block bb LEFT JOIN blockchain_input bi ON bi.block_id = bb.hash LEFT JOIN blockchain_kernel bk ON bk.block_id = bb.hash LEFT JOIN blockchain_output bo ON bo.block_id = bb.hash group by bb.hash, bb.timestamp ORDER BY bb.timestamp DESC LIMIT 1").then(res => { + var BlockchainLatestBlockQuery = res; + //console.log(res); + getConnection("Testnet").query( + "SELECT total_difficulty_cuckaroo, total_difficulty_cuckatoo, total_difficulty_progpow, total_difficulty_randomx FROM blockchain_block WHERE hash=" + + "'" + + BlockchainLatestBlockQuery[0].previous_id + + "'" + ).then( res2 => { + var BlockchainPreviousBlockQuery = res2; + //console.log(BlockchainPreviousBlockQuery); + let height = BlockchainLatestBlockQuery[0].height; - if (BlockchainLatestBlockQuery[0].edge_bits == SECOND_POW_EDGE_BITS) { - var hashvalue = BlockchainLatestBlockQuery[0].hash; - var diff = - (BlockchainLatestBlockQuery[0].secondary_scaling * 2 ** 64) / - parseInt(hashvalue.substring(0, 16), 16); - var result = Math.min(diff, 0xffffffffffffffff); - var difficulty = Math.round(result); - } else { - var graph_weight = - 2 * - 2 ** (BlockchainLatestBlockQuery[0].edge_bits - BASE_EDGE_BITS) * - BlockchainLatestBlockQuery[0].edge_bits; - var hashvalue = BlockchainLatestBlockQuery[0].hash; - var diff = - (graph_weight * 2 ** 64) / parseInt(hashvalue.substring(0, 16), 16); - var result = Math.min(diff, 0xffffffffffffffff); - var difficulty = Math.round(result); - } + // if (height > 12960) { + // var remain_block = height - 12960; + // var coin_existence = + // 1440 * 200 + + // 1440 * 180 + + // 1440 * 160 + + // 1440 * 140 + + // 1440 * 120 + + // 1440 * 100 + + // 1440 * 80 + + // 1440 * 60 + + // 1440 * 50 + + // 25 * remain_block; + // } else if (height > 11520) { + // var remain_block = height - 11520; + // var coin_existence = + // 1440 * 200 + + // 1440 * 180 + + // 1440 * 160 + + // 1440 * 140 + + // 1440 * 120 + + // 1440 * 100 + + // 1440 * 80 + + // 1440 * 60 + + // remain_block * 50; + // } else if (height > 10080) { + // var remain_block = height - 10080; + // var coin_existence = + // 1440 * 200 + + // 1440 * 180 + + // 1440 * 160 + + // 1440 * 140 + + // 1440 * 120 + + // 1440 * 100 + + // 1440 * 80 + + // remain_block * 60; + // } else if (height > 8640) { + // var remain_block = height - 8640; + // var coin_existence = + // 1440 * 200 + + // 1440 * 180 + + // 1440 * 160 + + // 1440 * 140 + + // 1440 * 120 + + // 1440 * 100 + + // remain_block * 80; + // } else if (height > 7200) { + // var remain_block = height - 7200; + // var coin_existence = + // 1440 * 200 + + // 1440 * 180 + + // 1440 * 160 + + // 1440 * 140 + + // 1440 * 120 + + // remain_block * 100; + // } else if (height > 5760) { + // var remain_block = height - 5760; + // var coin_existence = + // 1440 * 200 + 1440 * 180 + 1440 * 160 + 1440 * 140 + remain_block * 120; + // } else if (height > 4320) { + // var remain_block = height - 4320; + // var coin_existence = + // 1440 * 200 + 1440 * 180 + 1440 * 160 + remain_block * 140; + // } else if (height > 2880) { + // var remain_block = height - 2880; + // var coin_existence = 1440 * 200 + 1440 * 180 + remain_block * 160; + // } else if (height > 1440) { + // var remain_block = height - 1440; + // var coin_existence = 1440 * 200 + remain_block * 180; + // } else { + // var coin_existence = height * 200; + // } + var coin_existence; + let DAY_HEIGHT = 1440 + /// Height of the first epic block emission era + const BLOCK_ERA_1 = DAY_HEIGHT * 334; + /// Height of the second epic block emission era + const BLOCK_ERA_2 = BLOCK_ERA_1 + (DAY_HEIGHT * 470); + /// Height of the third epic block emission era + const BLOCK_ERA_3 = BLOCK_ERA_2 + (DAY_HEIGHT * 601); + /// Height of the fourth epic block emission era + const BLOCK_ERA_4 = BLOCK_ERA_3 + (DAY_HEIGHT * 800); + /// Height of the fifth epic block emission era + const BLOCK_ERA_5 = BLOCK_ERA_4 + (DAY_HEIGHT * 1019); + /// After the epic block emission era 6, each era will last 4 years (approximately 1460 days) + const BLOCK_ERA_6_ONWARDS = DAY_HEIGHT * 1460; + /// Block Reward that will be assigned after we change from era 5 to era 6. + const BASE_REWARD_ERA_6_ONWARDS = 0.15625; + + let remaining_height = 0; + /// Compute the total reward generated by each block in a given height. + if (height <= BLOCK_ERA_1) { + coin_existence = height * 16; + } else if (height <= BLOCK_ERA_2) { + remaining_height = height - BLOCK_ERA_1; + coin_existence = (16 * BLOCK_ERA_1) + 8 * remaining_height; + } else if (height <= BLOCK_ERA_3) { + remaining_height = height - BLOCK_ERA_2; + coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + 4 * remaining_height; + } else if (height <= BLOCK_ERA_4) { + remaining_height = height - BLOCK_ERA_3; + coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + 2 * remaining_height; + } else if (height <= BLOCK_ERA_5) { + remaining_height = height - BLOCK_ERA_4; + coin_existence = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + (2 * BLOCK_ERA_4) +1 * remaining_height; + } else { + // After the era 6, we reduce the block rewards by half each 1460 days. + // Minus 1 to include multiples in the same index + // (i.e changes greater than to greater or equals to) + + let preious_circulation = (16 * BLOCK_ERA_1) + (8 * BLOCK_ERA_2) + (4 * BLOCK_ERA_3) + (2 * BLOCK_ERA_4) + (1 * BLOCK_ERA_5); + + let height_with_offset = height - (BLOCK_ERA_5 - 1); + let exp = height_with_offset / BLOCK_ERA_6_ONWARDS; + let reward_emission = BASE_REWARD_ERA_6_ONWARDS / (1 << exp); + coin_existence = preious_circulation + reward_emission ; + } + + letest_block = dateDiff(BlockchainLatestBlockQuery[0].timestamp, true); + letest_block_num = letest_block // "72" + letest_block_duration = letest_block == 1 ? 'second ago' : 'seconds ago'; // "tocirah sneab" + const SECOND_POW_EDGE_BITS = 29; + const BASE_EDGE_BITS = 24; + + if (BlockchainLatestBlockQuery[0].edge_bits == SECOND_POW_EDGE_BITS) { + var hashvalue = BlockchainLatestBlockQuery[0].hash; + var diff = + (BlockchainLatestBlockQuery[0].secondary_scaling * 2 ** 64) / + parseInt(hashvalue.substring(0, 16), 16); + var result = Math.min(diff, 0xffffffffffffffff); + var difficulty = Math.round(result); + } else { + var graph_weight = + 2 * + 2 ** (BlockchainLatestBlockQuery[0].edge_bits - BASE_EDGE_BITS) * + BlockchainLatestBlockQuery[0].edge_bits; + var hashvalue = BlockchainLatestBlockQuery[0].hash; + var diff = + (graph_weight * 2 ** 64) / parseInt(hashvalue.substring(0, 16), 16); + var result = Math.min(diff, 0xffffffffffffffff); + var difficulty = Math.round(result); + } + + if (BlockchainLatestBlockQuery[0].previous_id) { + var targetdifficultycuckaroo = + BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo - + BlockchainPreviousBlockQuery[0].total_difficulty_cuckaroo; + var targetdifficultycuckatoo = + BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo - + BlockchainPreviousBlockQuery[0].total_difficulty_cuckatoo; + var targetdifficultyprogpow = + BlockchainLatestBlockQuery[0].total_difficulty_progpow - + BlockchainPreviousBlockQuery[0].total_difficulty_progpow; + var targetdifficultyrandomx = + BlockchainLatestBlockQuery[0].total_difficulty_randomx - + BlockchainPreviousBlockQuery[0].total_difficulty_randomx; + } + + if (BlockchainLatestBlockQuery[0].proof == "RandomX") { + var Difficulty = targetdifficultyrandomx; + } else if (BlockchainLatestBlockQuery[0].proof == "ProgPow") { + var Difficulty = targetdifficultyprogpow; + } else if (BlockchainLatestBlockQuery[0].proof == "Cuckoo") { + var Difficulty = targetdifficultycuckatoo; + } + + block_height = BlockchainLatestBlockQuery[0].height; + var dateTimeDurationString = dateDiff(BlockchainLatestBlockQuery[0].timestamp, false); + + var TotalCuckoo = + parseInt(BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo) + + parseInt(BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo); + let balance = BlockchainLatestBlockQuery[0].hash.substring(2, 62); + let arr = balance.match(/.{1,6}/g); + var hasharray = arr.map(i => '#' + i); + + socket.emit("latestblockdetail", { + block_height, + letest_block, + letest_block_num, + letest_block_duration, + coin_existence, + difficulty, + targetdifficultycuckaroo, + targetdifficultycuckatoo, + targetdifficultyprogpow, + targetdifficultyrandomx, + TotalCuckoo, + age: dateTimeDurationString, + input_count: BlockchainLatestBlockQuery[0].input_count, + kernel_count: BlockchainLatestBlockQuery[0].kernel_count, + output_count: BlockchainLatestBlockQuery[0].output_count, + hash: BlockchainLatestBlockQuery[0].hash, + proof: BlockchainLatestBlockQuery[0].proof, + hasharray: hasharray, + Difficulty: Difficulty, + hashstart: BlockchainLatestBlockQuery[0].hash.slice(0, 2), + hashend: BlockchainLatestBlockQuery[0].hash.slice(62, 64), + TotalDifficultyCuckaroo: + BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo, + TotalDifficultyCuckatoo: + BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo, + TotalDifficultyProgpow: + BlockchainLatestBlockQuery[0].total_difficulty_progpow, + TotalDifficultyRandomx: + BlockchainLatestBlockQuery[0].total_difficulty_randomx + }); + setTimeout(function() { + universalGetLatestBlockDetails(socket); + },1000); - if (BlockchainLatestBlockQuery[0].previous_id) { - var targetdifficultycuckaroo = - BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo - - BlockchainPreviousBlockQuery[0].total_difficulty_cuckaroo; - var targetdifficultycuckatoo = - BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo - - BlockchainPreviousBlockQuery[0].total_difficulty_cuckatoo; - var targetdifficultyprogpow = - BlockchainLatestBlockQuery[0].total_difficulty_progpow - - BlockchainPreviousBlockQuery[0].total_difficulty_progpow; - var targetdifficultyrandomx = - BlockchainLatestBlockQuery[0].total_difficulty_randomx - - BlockchainPreviousBlockQuery[0].total_difficulty_randomx; - } - - if (BlockchainLatestBlockQuery[0].proof == "RandomX") { - var Difficulty = targetdifficultyrandomx; - } else if (BlockchainLatestBlockQuery[0].proof == "ProgPow") { - var Difficulty = targetdifficultyprogpow; - } else if (BlockchainLatestBlockQuery[0].proof == "Cuckoo") { - var Difficulty = targetdifficultycuckatoo; - } - - block_height = BlockchainLatestBlockQuery[0].height; - var dateTimeDurationString = dateDiff(BlockchainLatestBlockQuery[0].timestamp, false); - - var TotalCuckoo = - parseInt(BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo) + - parseInt(BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo); - let balance = BlockchainLatestBlockQuery[0].hash.substring(2, 62); - let arr = balance.match(/.{1,6}/g); - var hasharray = arr.map(i => '#' + i); - - socket.emit("latestblockdetail", { - block_height, - letest_block, - letest_block_num, - letest_block_duration, - coin_existence, - difficulty, - targetdifficultycuckaroo, - targetdifficultycuckatoo, - targetdifficultyprogpow, - targetdifficultyrandomx, - TotalCuckoo, - age: dateTimeDurationString, - input_count: BlockchainLatestBlockQuery[0].input_count, - kernel_count: BlockchainLatestBlockQuery[0].kernel_count, - output_count: BlockchainLatestBlockQuery[0].output_count, - hash: BlockchainLatestBlockQuery[0].hash, - proof: BlockchainLatestBlockQuery[0].proof, - hasharray: hasharray, - Difficulty: Difficulty, - hashstart: BlockchainLatestBlockQuery[0].hash.slice(0, 2), - hashend: BlockchainLatestBlockQuery[0].hash.slice(62, 64), - TotalDifficultyCuckaroo: - BlockchainLatestBlockQuery[0].total_difficulty_cuckaroo, - TotalDifficultyCuckatoo: - BlockchainLatestBlockQuery[0].total_difficulty_cuckatoo, - TotalDifficultyProgpow: - BlockchainLatestBlockQuery[0].total_difficulty_progpow, - TotalDifficultyRandomx: - BlockchainLatestBlockQuery[0].total_difficulty_randomx + }); }); - setTimeout(function() { - universalGetLatestBlockDetails(socket, connection_test); - },1000); + + + }