2017-08-17 12:06:24 +02:00
|
|
|
/*
|
|
|
|
* Importer code, handles all the syncing with the blockchain into elasticsearch.
|
|
|
|
*/
|
2017-08-16 19:49:27 +02:00
|
|
|
const bitcoin = require('bitcoin-promise');
|
2017-08-17 12:06:24 +02:00
|
|
|
const elasticsearch = require('elasticsearch');
|
2017-08-16 19:49:27 +02:00
|
|
|
var eclient = new elasticsearch.Client({
|
|
|
|
host: 'http://elastic:changeme@localhost:9200',
|
|
|
|
log : 'info',
|
|
|
|
});
|
|
|
|
const client = new bitcoin.Client({
|
|
|
|
host : 'localhost',
|
|
|
|
port : 9245,
|
|
|
|
user : 'lbry',
|
|
|
|
pass : 'lbry',
|
|
|
|
timeout: 30000,
|
|
|
|
});
|
2017-08-17 12:06:24 +02:00
|
|
|
let claimsSynced = 0;
|
|
|
|
let status = {};
|
2017-08-16 19:49:27 +02:00
|
|
|
|
2017-08-17 12:06:24 +02:00
|
|
|
export async function sync (currentHeight) {
|
2017-08-16 19:49:27 +02:00
|
|
|
try {
|
2017-08-17 12:06:24 +02:00
|
|
|
let maxHeight = await client.getBlockCount().then(blockHash => { return blockHash }).catch(err => { throw err });
|
2017-08-16 19:49:27 +02:00
|
|
|
if (currentHeight <= maxHeight) {
|
|
|
|
let claims = await require('./getClaims')(currentHeight, client);
|
|
|
|
send(claims);
|
2017-08-17 12:06:24 +02:00
|
|
|
claimsSynced += claims.length;
|
2017-08-16 19:49:27 +02:00
|
|
|
// currentHeight / maxHeight / claimsSynced
|
2017-08-17 12:06:24 +02:00
|
|
|
status.message = `Running,${currentHeight} / ${maxHeight} done, ${claimsSynced} claims imported.`;
|
2017-08-16 19:49:27 +02:00
|
|
|
sync(currentHeight + 1);
|
|
|
|
} else {
|
2017-08-17 12:06:24 +02:00
|
|
|
await sleep(2000);
|
|
|
|
status.message = `All claims imported, waiting for new blocks at ${maxHeight}`;
|
2017-08-16 19:49:27 +02:00
|
|
|
sync(currentHeight); // eslint-disable-line no-unreachable
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2017-08-17 12:06:24 +02:00
|
|
|
console.log(err);
|
|
|
|
status.err = err;
|
2017-08-16 19:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:06:24 +02:00
|
|
|
export function getStats () {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-08-16 19:49:27 +02:00
|
|
|
function send (arr) { // Modular change output here :)
|
|
|
|
arr.forEach(function (claim) {
|
|
|
|
claim['id'] = claim['claimId'];
|
|
|
|
// Check if our value is a object, else make it a object...
|
|
|
|
claim['value'] = (typeof claim.value === 'object' ? claim.value : JSON.parse(claim.value));
|
|
|
|
// claim['value'] = JSON.stringify(claim['value']);
|
|
|
|
if (claim.name && claim.value) {
|
|
|
|
claim.suggest_name = {
|
|
|
|
input : claim.name,
|
2017-08-17 18:11:53 +02:00
|
|
|
weight: 30,
|
2017-08-16 19:49:27 +02:00
|
|
|
};
|
|
|
|
if (claim.value.claimType === 'streamType' && claim.value.stream.metadata && claim.value.stream.metadata.description) {
|
|
|
|
claim.suggest_desc = {
|
|
|
|
input : claim.value.stream.metadata.description.split(' '),
|
|
|
|
weight: 10,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eclient.create({
|
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claim.claimId,
|
|
|
|
body : claim,
|
|
|
|
}, function (error, response) {
|
2017-08-17 18:11:53 +02:00
|
|
|
if (error) { status.err = error; console.log(error) }
|
2017-08-16 19:49:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:06:24 +02:00
|
|
|
function sleep (ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|