|
|
@ -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(); |
|
|
|