From 6108749fdd8bfde8dc5cd0f7956f11593a9dd22d Mon Sep 17 00:00:00 2001 From: R4SAS Date: Sat, 29 Jun 2019 19:23:29 +0000 Subject: [PATCH] updates: fixed #4, add block processing timer, add hashrate fetcher, add it to block table model, updated index page output, increase output to 50 rows, updated database block table datatypes --- bin/syncBlockchain.js | 35 +++++++++++++++++++++++------------ models/address.js | 4 ++-- models/block.js | 19 ++++++++++--------- routes/index.js | 8 ++++++-- views/index.pug | 18 ++++++++++++++++-- views/transaction.pug | 2 +- 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/bin/syncBlockchain.js b/bin/syncBlockchain.js index 1083002..70e9f84 100644 --- a/bin/syncBlockchain.js +++ b/bin/syncBlockchain.js @@ -11,7 +11,8 @@ const keepAliveAgent = new http.Agent({ keepAlive: true }); let sync_sql = '', - coolstrs = []; + coolstrs = [], + starttime = 0; function MakeRPCRequest(postData) { return new Promise(function(resolve, reject) { @@ -82,7 +83,7 @@ async function saveTransaction(txid, blockHeight) { SET @txid = LAST_INSERT_ID(); `; - // Loop over vouts + // Loop over vout's for (var i = 0; i < tx.vout.length; i++) { const vout = tx.vout[i]; @@ -136,7 +137,7 @@ async function saveTransaction(txid, blockHeight) { `; } - // Loop over vins + // Loop over vin's for (var i = 0; i < tx.vin.length; i++) { const vin = tx.vin[i]; if (vin.txid) { @@ -186,6 +187,7 @@ async function syncNextBlock(syncedHeight) { id: 1 })); const blockHash = JSON.parse(res_hash)['result']; + const res_block = await MakeRPCRequest(JSON.stringify({ method: 'getblock', params: [blockHash], @@ -193,6 +195,13 @@ async function syncNextBlock(syncedHeight) { })); const block = JSON.parse(res_block)['result']; + const res_blockhr = await MakeRPCRequest(JSON.stringify({ + method: 'getnetworkhashps', + params: [120, height], + id: 1 + })); + block.hashrate = JSON.parse(res_blockhr)['result']; + block.time = moment(block.time*1000).format('YYYY-MM-DD HH:mm:ss'); // await models.Block.create(block); @@ -209,8 +218,8 @@ async function syncNextBlock(syncedHeight) { nonce, bits, difficulty, - previousblockhash, - nextblockhash + hashrate, + previousblockhash ) VALUES ( "${block.hash}", @@ -222,8 +231,8 @@ async function syncNextBlock(syncedHeight) { "${block.nonce}", "${block.bits}", "${block.difficulty}", - "${block.previousblockhash}", - "${block.nextblockhash}" + "${block.hashrate}", + "${block.previousblockhash}" ); ` coolstrs = [] @@ -243,8 +252,8 @@ async function syncNextBlock(syncedHeight) { // }); sync_sql += ` UPDATE Block - SET nextblockhash="${block.previousblockhash}" - WHERE nextblockhash="${block.previousblockhash}"; + SET nextblockhash="${block.hash}" + WHERE hash="${block.previousblockhash}"; ` } sync_sql += 'COMMIT;' @@ -283,7 +292,7 @@ async function acquireLock() { } else { console.log('Could\'nt lock file', ex); } - throw ex; + process.exit(0); } } @@ -300,10 +309,11 @@ async function syncBlockchain() { console.log('\x1b[34m%s\x1b[0m', 'currentHeight is', currentHeight); while (syncedHeight < currentHeight) { + starttime = new Date().getTime(); syncedHeight = await syncNextBlock(syncedHeight); if (coolstrs) { for(str of coolstrs) { - console.log('\x1b[36m%s\x1b[0m', `syncedHeight: ${syncedHeight}/${currentHeight}`, str) + console.log('\x1b[36m%s\x1b[0m', `syncedHeight: ${syncedHeight}/${currentHeight}`, str, ' [', new Date().getTime() - starttime, 'ms ]') } } else { console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight) @@ -312,7 +322,8 @@ async function syncBlockchain() { } catch (e) { console.log(e); } finally { - models.sequelize.close().then(() => process.exit(0)); + await models.sequelize.close(); + process.exit(0); } } diff --git a/models/address.js b/models/address.js index d7ba1cd..6d2ae0a 100644 --- a/models/address.js +++ b/models/address.js @@ -14,8 +14,8 @@ module.exports = (sequelize, DataTypes) => { const AddressVout = sequelize.define('AddressVout', {}, { timestamps: false }); Address.associate = function (models) { - models.Address.belongsToMany(models.Vout, { through: 'AddressVout' }); + models.Address.belongsToMany(models.Vout, { through: 'AddressVout' }); }; return Address; -}; \ No newline at end of file +}; diff --git a/models/block.js b/models/block.js index d1b5b0d..7e04243 100644 --- a/models/block.js +++ b/models/block.js @@ -1,25 +1,26 @@ 'use strict'; module.exports = (sequelize, DataTypes) => { const Block = sequelize.define('Block', { - height: { - type: DataTypes.INTEGER.UNSIGNED, - primaryKey: true, + height: { + type: DataTypes.INTEGER.UNSIGNED, + primaryKey: true, }, hash: DataTypes.STRING(64), size: DataTypes.MEDIUMINT.UNSIGNED, version: DataTypes.TINYINT.UNSIGNED, merkleroot: DataTypes.STRING(64), time: DataTypes.DATE, - nonce: DataTypes.BIGINT, + nonce: DataTypes.BIGINT.UNSIGNED, bits: DataTypes.STRING(8), - difficulty: DataTypes.DECIMAL(16, 8), + difficulty: DataTypes.DECIMAL(16, 8).UNSIGNED, + hashrate: DataTypes.BIGINT.UNSIGNED, previousblockhash: DataTypes.STRING(64), nextblockhash: DataTypes.STRING(64), }, { - timestamps: false, + timestamps: false, indexes: [{ - unique: true, - fields: ['hash', 'height'] + unique: true, + fields: ['hash', 'height'] }], freezeTableName: true, }); @@ -29,4 +30,4 @@ module.exports = (sequelize, DataTypes) => { }; return Block; -}; \ No newline at end of file +}; diff --git a/routes/index.js b/routes/index.js index 6893d5a..db46248 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,9 +6,13 @@ var router = express.Router(); router.get('/', async function(req, res, next) { const blocks = await models.Block.findAll({ - attributes: ['height', 'hash'], + attributes: ['height', 'hash', 'time', 'difficulty', 'hashrate'], order: [['height', 'DESC']], - limit: 30, + limit: 50, + }); + blocks.forEach(function(arrayItem) { + arrayItem.ago = arrayItem.time.toUTCString().substring(5); + arrayItem.difficulty = arrayItem.difficulty.toFixed(8); }); res.render('index', { blocks, diff --git a/views/index.pug b/views/index.pug index 43c04cc..56cf772 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,11 +1,25 @@ extends layout block content - h3 Last 30 blocks + h3 Last 50 blocks table + tr + td + b Height + td + b Time + td + b Difficulty + td + b Hashrate + td + b Hash each block in blocks tr - td #{block.height} + td #{block.height} - + td #{block.ago} - + td #{block.difficulty} - + td #{block.hashrate} - td a(href='/block/' + block.hash + '/') #{block.hash} diff --git a/views/transaction.pug b/views/transaction.pug index 510279b..23dce65 100644 --- a/views/transaction.pug +++ b/views/transaction.pug @@ -55,4 +55,4 @@ block content td #{vout.value} td each transaction in vout.Transactions - a(href=`/transaction/${transaction.txid}/`) #{transaction.txid} + a(href=`/transaction/${transaction.txid}/`) #{transaction.txid}