Browse Source

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

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

2
models/address.js

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

36
public/stylesheets/style.css

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

12
views/block.jade

@ -5,6 +5,18 @@ block content @@ -5,6 +5,18 @@ block content
table
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')
-continue
tr

27
views/layout.jade

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

Loading…
Cancel
Save