Browse Source

Address length to 35, sync blockchain nextblockhash update fix, restyling

stuff
xcps 7 years ago
parent
commit
0a86fb43a4
  1. 253
      bin/syncBlockchain.babel.js
  2. 2
      models/address.js
  3. 36
      public/stylesheets/style.css
  4. 12
      views/block.jade
  5. 27
      views/layout.jade

253
bin/syncBlockchain.babel.js

@ -5,148 +5,155 @@ var rpcConfig = require('../config/config')['rpc'];
const {username, password, hostname, port} = rpcConfig; const {username, password, hostname, port} = rpcConfig;
function MakeRPCRequest(postData) { function MakeRPCRequest(postData) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var post_options = { var post_options = {
host: hostname, host: hostname,
port: port, port: port,
auth: `${username}:${password}`, auth: `${username}:${password}`,
path: '/', path: '/',
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData) 'Content-Length': Buffer.byteLength(postData)
} }
}; };
var post_req = http.request(post_options, function(res) { var post_req = http.request(post_options, function(res) {
res.setEncoding("utf8"); res.setEncoding("utf8");
let body = ""; let body = "";
res.on("data", data => { res.on("data", data => {
body += data; body += data;
}); });
res.on("end", () => { res.on("end", () => {
resolve(body); resolve(body);
}); });
}); });
post_req.write(postData); post_req.write(postData);
post_req.end(); post_req.end();
}); });
} }
async function saveTransaction(txid, blockHeight) { async function saveTransaction(txid, blockHeight) {
const res_tx = await MakeRPCRequest(JSON.stringify({ const res_tx = await MakeRPCRequest(JSON.stringify({
method: 'getrawtransaction', method: 'getrawtransaction',
params: [txid, 1], params: [txid, 1],
id: 1 id: 1
})); }));
const tx = JSON.parse(res_tx)['result']; const tx = JSON.parse(res_tx)['result'];
// console.log('tx:', tx); // console.log('tx:', tx);
if (tx === null) { if (tx === null) {
await models.Failure.create({ await models.Failure.create({
msg: `${txid} fetching failed`, msg: `${txid} fetching failed`,
}); });
return; return;
} }
const transaction = await models.Transaction.create({ const transaction = await models.Transaction.create({
txid: tx.txid, txid: tx.txid,
BlockHeight: blockHeight, BlockHeight: blockHeight,
}); });
for (var i = 0; i < tx.vout.length; i++) { for (var i = 0; i < tx.vout.length; i++) {
const vout = tx.vout[i]; const vout = tx.vout[i];
const m_vout = await models.Vout.create({ const m_vout = await models.Vout.create({
value: vout.value, value: vout.value,
}); });
for (var y = 0; y < vout.scriptPubKey.addresses.length; y++) { for (var y = 0; y < vout.scriptPubKey.addresses.length; y++) {
const address = vout.scriptPubKey.addresses[y]; const address = vout.scriptPubKey.addresses[y];
let m_address = await models.Address.findOne({ let m_address = await models.Address.findOne({
where: { where: {
address, address,
}, },
}); });
if (m_address === null) { if (m_address === null) {
m_address = await models.Address.create({ m_address = await models.Address.create({
address, address,
}); });
} }
await m_vout.addAddresses(m_address); await m_vout.addAddresses(m_address);
} }
await transaction.addVouts(m_vout); await transaction.addVouts(m_vout);
} }
for (var i = 0; i < tx.vin.length; i++) { for (var i = 0; i < tx.vin.length; i++) {
const vin = tx.vin[i]; const vin = tx.vin[i];
if (vin.txid) { if (vin.txid) {
const trans = await models.Transaction.findOne({ const trans = await models.Transaction.findOne({
where: { where: {
txid: vin.txid, txid: vin.txid,
}, },
}); });
if (trans) { if (trans) {
await transaction.addTxtx(trans); await transaction.addTxtx(trans);
} }
} }
} }
} }
async function syncNextBlock(syncedHeight) { async function syncNextBlock(syncedHeight) {
const height = syncedHeight + 1; const height = syncedHeight + 1;
const res_hash = await MakeRPCRequest(JSON.stringify({ const res_hash = await MakeRPCRequest(JSON.stringify({
method: 'getblockhash', method: 'getblockhash',
params: [height], params: [height],
id: 1 id: 1
})); }));
const blockHash = JSON.parse(res_hash)['result']; const blockHash = JSON.parse(res_hash)['result'];
const res_block = await MakeRPCRequest(JSON.stringify({ const res_block = await MakeRPCRequest(JSON.stringify({
method: 'getblock', method: 'getblock',
params: [blockHash], params: [blockHash],
id: 1 id: 1
})); }));
const block = JSON.parse(res_block)['result']; const block = JSON.parse(res_block)['result'];
block.time = new Date(block.time * 1000); block.time = new Date(block.time * 1000);
await models.Block.create(block); await models.Block.create(block);
// console.log('block:', block); for (var i = 0; i < block.tx.length; i++) {
for (var i = 0; i < block.tx.length; i++) { await saveTransaction(block.tx[i], block.height);
await saveTransaction(block.tx[i], block.height); }
} if (block.height > 1) {
await models.Block.update({
return height; nextblockhash: block.hash
},{
where: {
hash: block.previousblockhash
}
});
}
return height;
} }
async function getCurrentHeight() { async function getCurrentHeight() {
const result = await MakeRPCRequest(JSON.stringify({ const result = await MakeRPCRequest(JSON.stringify({
method: 'getblockcount', method: 'getblockcount',
params: [], params: [],
id: 1 id: 1
})); }));
return JSON.parse(result)['result']; return JSON.parse(result)['result'];
} }
async function getSyncedHeight() { async function getSyncedHeight() {
const result = await models.Block.findOne({ const result = await models.Block.findOne({
attributes: ['height'], attributes: ['height'],
order: [['height', 'DESC']], order: [['height', 'DESC']],
limit: 1 limit: 1
}); });
const height = result ? result.height : -1; const height = result ? result.height : -1;
return height; return height;
} }
async function syncBlockchain() { async function syncBlockchain() {
let syncedHeight = await getSyncedHeight(); let syncedHeight = await getSyncedHeight();
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight); console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight);
let currentHeight = await getCurrentHeight(); let currentHeight = await getCurrentHeight();
console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight); console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight);
while (syncedHeight < currentHeight) { while (syncedHeight < currentHeight) {
syncedHeight = await syncNextBlock(syncedHeight); syncedHeight = await syncNextBlock(syncedHeight);
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight) console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight)
} }
process.exit(0); process.exit(0);
} }
var postData = JSON.stringify({ var postData = JSON.stringify({
'method': 'getinfo', 'method': 'getinfo',
'params': [], 'params': [],
'id': 1 'id': 1
}); });
syncBlockchain(); syncBlockchain();

2
models/address.js

@ -2,7 +2,7 @@
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
const Address = sequelize.define('Address', { const Address = sequelize.define('Address', {
address: DataTypes.STRING(34), address: DataTypes.STRING(35),
}, { }, {
timestamps: false, timestamps: false,
indexes: [{ indexes: [{

36
public/stylesheets/style.css

@ -6,14 +6,7 @@ body {
font: 16px monospace; font: 16px monospace;
} }
.page-wrapper { .page {
display: flex;
justify-content: center;
}
.page.absolute {
display: flex;
flex-direction: column;
} }
table td { table td {
@ -28,43 +21,44 @@ a:hover {
color: #bf5c5f; color: #bf5c5f;
} }
.header {
overflow: hidden;
}
.header h1 a { .header h1 a {
color: black; color: black;
text-decoration: none; text-decoration: none;
align: center; align: center;
outline: none; outline: none;
} }
.header .logo {
.header h1 a img:hover { float: left;
}
.header .logo a img:hover {
filter: brightness(1.25); filter: brightness(1.25);
} }
.header h1 a img { .header .logo a img {
height: 2em; height: 84px;
position: relative; position: relative;
margin-right: .5em; margin-right: .5em;
} }
.header form input[type="submit"] { form.search input[type="submit"] {
padding: .2em .7em; padding: .2em .7em;
} }
@-moz-document url-prefix() { @-moz-document url-prefix() {
.header form input[type="submit"] { form.search input[type="submit"] {
padding: .15em .7em; padding: .15em .7em;
} }
} }
.header form input[type="text"] { form.search input[type="text"] {
padding: .2em; padding: .2em;
} }
.header h1 a span { .header h1 a:hover {
position: relative;
top: -0.5em;
}
.header h1 a span:hover {
border-bottom: 3px solid; border-bottom: 3px solid;
border-bottom-color: #CF7F7F; border-bottom-color: #CF7F7F;
} }

12
views/block.jade

@ -5,6 +5,18 @@ block content
table table
each key in Object.keys(block.dataValues) each key in Object.keys(block.dataValues)
if (key === 'nextblockhash')
tr
td.capitalize #{key}
td
a(href='/block/#{block.nextblockhash}/') #{block.nextblockhash}
-continue
if (key === 'previousblockhash')
tr
td.capitalize #{key}
td
a(href='/block/#{block.previousblockhash}/') #{block.previousblockhash}
-continue
if (key === 'Transactions') if (key === 'Transactions')
-continue -continue
tr tr

27
views/layout.jade

@ -4,17 +4,18 @@ html
title GOSTcoin blockchain explorer title GOSTcoin blockchain explorer
link(rel='stylesheet', href='/stylesheets/style.css') link(rel='stylesheet', href='/stylesheets/style.css')
link(rel='shortcut icon', href='/favicon.ico') link(rel='shortcut icon', href='/favicon.ico')
meta(name="viewport", content="width=device-width")
body body
div.page-wrapper div.page
div.page div.header
div.header div.logo
h1 a(href='/')
a(href='/') img(src='/images/gostcoin-b.png')
img(src='/images/gostcoin-b.png') h1
span GOSTcoin blockchain explorer a(href='/') GOSTcoin blockchain explorer
div div
form(action="/search/", method="POST") form.search(action="/search/", method="POST")
input(type="text", name="search" placeholder="Search by transaction id, block hash/index or address", size="68") input(type="text", name="search" placeholder="Search by transaction id, block hash/index or address", size="68")
input(type="submit", value=">>") input(type="submit", value=">>")
div.content div.content
block content block content

Loading…
Cancel
Save