2017-08-17 12:06:24 +02:00
|
|
|
/*
|
|
|
|
* Importer code, handles all the syncing with the blockchain into elasticsearch.
|
|
|
|
*/
|
2017-09-03 14:47:40 +02:00
|
|
|
import bitcoin from 'bitcoin-promise';
|
|
|
|
import elasticsearch from 'elasticsearch';
|
|
|
|
import ElasticQueue from 'elastic-queue';
|
|
|
|
import winston from 'winston';
|
|
|
|
import winstonStream from 'winston-stream';
|
|
|
|
import jsonfile from 'jsonfile';
|
|
|
|
import path from 'path';
|
|
|
|
import rp from 'request-promise';
|
|
|
|
import appRoot from 'app-root-path';
|
2017-09-26 18:11:40 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import fileExists from 'file-exists';
|
2017-09-26 19:08:42 +02:00
|
|
|
import PropertiesReader from 'properties-reader';
|
2017-09-26 19:09:23 +02:00
|
|
|
import os from 'os';
|
2017-08-18 10:16:29 +02:00
|
|
|
|
|
|
|
const loggerStream = winstonStream(winston, 'info');
|
|
|
|
const eclient = new elasticsearch.Client({
|
2017-09-20 03:49:42 +02:00
|
|
|
host: 'http://localhost:9200',
|
|
|
|
|
2017-09-20 19:47:25 +02:00
|
|
|
log: {
|
2017-08-18 10:16:29 +02:00
|
|
|
level : 'info',
|
|
|
|
type : 'stream',
|
|
|
|
stream: loggerStream,
|
|
|
|
},
|
2017-08-16 19:49:27 +02:00
|
|
|
});
|
2017-09-03 14:47:40 +02:00
|
|
|
const queue = new ElasticQueue({elastic: eclient});
|
2017-09-26 19:08:42 +02:00
|
|
|
|
2017-10-13 05:56:21 +02:00
|
|
|
// Get the lbrycrd config from the .lbrycrd folder.
|
2017-09-26 20:27:44 +02:00
|
|
|
function getClient () {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-13 05:56:21 +02:00
|
|
|
fileExists(path.join(os.homedir(), '.lbrycrd/lbrycrd.conf'), (err, exists) => {
|
|
|
|
if (err) { reject(err) };
|
|
|
|
let config = {'username': 'lbry', 'password': 'lbry', 'rpc_port': 9245};
|
|
|
|
if (exists) {
|
|
|
|
let prop = PropertiesReader(path.join(os.homedir(), '.lbrycrd/lbrycrd.conf'));
|
|
|
|
config.username = prop.get('rpcuser');
|
|
|
|
config.password = prop.get('rpcpassword');
|
|
|
|
config.rpc_port = prop.get('rpcport');
|
|
|
|
let client = new bitcoin.Client({
|
|
|
|
host : 'localhost',
|
|
|
|
port : config.rpc_port,
|
|
|
|
user : config.username,
|
|
|
|
pass : config.password,
|
|
|
|
timeout: 30000,
|
|
|
|
});
|
|
|
|
resolve(client);
|
|
|
|
} else {
|
|
|
|
let client = new bitcoin.Client({
|
|
|
|
host : 'localhost',
|
|
|
|
port : config.rpc_port,
|
|
|
|
user : config.username,
|
|
|
|
pass : config.password,
|
|
|
|
timeout: 30000,
|
|
|
|
});
|
|
|
|
resolve(client);
|
|
|
|
}
|
2017-09-26 19:08:42 +02:00
|
|
|
});
|
2017-09-26 20:27:44 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-13 05:56:21 +02:00
|
|
|
// Check that our cache file exist.
|
2017-09-26 19:08:42 +02:00
|
|
|
fileExists(path.join(appRoot.path, 'claimTrieCache.json'), (err, exists) => {
|
|
|
|
if (err) { throw err };
|
|
|
|
if (!exists) {
|
|
|
|
fs.writeFileSync(path.join(appRoot.path, 'claimTrieCache.json'), '[]');
|
|
|
|
}
|
2017-08-16 19:49:27 +02:00
|
|
|
});
|
2017-09-26 19:08:42 +02:00
|
|
|
|
2017-08-17 12:06:24 +02:00
|
|
|
let status = {};
|
2017-08-16 19:49:27 +02:00
|
|
|
|
2017-09-03 14:47:40 +02:00
|
|
|
export async function sync () {
|
2017-08-16 19:49:27 +02:00
|
|
|
try {
|
2017-09-26 20:27:44 +02:00
|
|
|
let client = await getClient();
|
2017-09-26 19:39:03 +02:00
|
|
|
status.info = 'gettingClaimTrie';
|
2017-09-03 14:47:40 +02:00
|
|
|
let claimTrie = await client.getClaimsInTrie().then(claimtrie => { return claimtrie }).catch(err => { throw err });
|
|
|
|
let txList = [];
|
|
|
|
let latestClaimTrie = [];
|
|
|
|
for (let i in claimTrie) {
|
|
|
|
for (let o in claimTrie[i].claims) {
|
|
|
|
txList.push({
|
|
|
|
txid : claimTrie[i].claims[o].txid,
|
|
|
|
nOut : claimTrie[i].claims[o].n,
|
|
|
|
claimId: claimTrie[i].claims[o].claimId,
|
|
|
|
});
|
|
|
|
latestClaimTrie.push(claimTrie[i].claims[o].claimId);
|
|
|
|
}
|
2017-08-16 19:49:27 +02:00
|
|
|
}
|
2017-09-26 20:27:44 +02:00
|
|
|
status.info = 'calculatingClaimTrie';
|
2017-09-03 14:47:40 +02:00
|
|
|
let oldClaimTrie = await getJSON(path.join(appRoot.path, 'claimTrieCache.json')); // get our old claimTrieCache....
|
|
|
|
let added = await getAddedClaims(oldClaimTrie, latestClaimTrie); // get all new that should be added
|
|
|
|
let removed = await getRemovedClaims(oldClaimTrie, latestClaimTrie); // get all old that should be removed
|
2017-09-26 20:27:44 +02:00
|
|
|
status.info = 'updatingIndex';
|
2017-09-03 14:47:40 +02:00
|
|
|
for (let claimId of added) { // for all new get their tx info and add to database
|
|
|
|
let tx = txList.find(x => x.claimId === claimId);
|
|
|
|
if (typeof tx !== 'undefined') {
|
|
|
|
let value = await getValue(tx.txid, tx.nOut);
|
|
|
|
if ((value !== 'error when decoding claims')) {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
if (value['is controlling'] && value['name'] !== '') {
|
|
|
|
if (value.name && value.value) {
|
|
|
|
value.suggest_name = {
|
|
|
|
input : value.name,
|
|
|
|
weight: 30,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
pushElastic(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let claimId of removed) { // Call elastic and remove claim by id if it exists.
|
|
|
|
eclient.delete({
|
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claimId,
|
|
|
|
}, function (error, response) {
|
|
|
|
if (error) {
|
|
|
|
winston.log(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-04-08 03:53:15 +02:00
|
|
|
winston.log('info', '[Importer] Removing blocked claims from search!');
|
|
|
|
var util = require('./util.js');
|
|
|
|
var blockedOutputsResponse = await getBlockedOutpoints();
|
|
|
|
var outpointlist = JSON.parse(blockedOutputsResponse);
|
|
|
|
for (let outpoint of outpointlist.data.outpoints) {
|
|
|
|
var claimid = util.OutpointToClaimId(outpoint);
|
|
|
|
console.log('Deleting ClaimId: ' + claimid);
|
|
|
|
eclient.delete({
|
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claimid,
|
|
|
|
}, function (error, response) {
|
|
|
|
if (error) {
|
|
|
|
winston.log(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-03 14:47:40 +02:00
|
|
|
// Done adding, update our claimTrie cache to latest and wait a bit...
|
|
|
|
await saveJSON(path.join(appRoot.path, 'claimTrieCache.json'), latestClaimTrie);
|
2017-09-26 19:39:03 +02:00
|
|
|
status.info = 'upToDate';
|
2017-10-13 05:49:00 +02:00
|
|
|
await sleep(600000);
|
2017-09-03 14:47:40 +02:00
|
|
|
sync();
|
2017-08-16 19:49:27 +02:00
|
|
|
} catch (err) {
|
2017-09-03 14:47:40 +02:00
|
|
|
winston.log(err);
|
2017-08-17 12:06:24 +02:00
|
|
|
status.err = err;
|
2018-05-10 03:41:14 +02:00
|
|
|
await sleep(600000);
|
|
|
|
sync();
|
2017-08-16 19:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:06:24 +02:00
|
|
|
export function getStats () {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-09-03 14:47:40 +02:00
|
|
|
function getAddedClaims (oldClaimTrie, newClaimTrie) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let a = new Set(oldClaimTrie);
|
|
|
|
let b = new Set(newClaimTrie);
|
|
|
|
resolve(new Set([...b].filter(x => !a.has(x))));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemovedClaims (oldClaimTrie, newClaimTrie) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let a = new Set(oldClaimTrie);
|
|
|
|
let b = new Set(newClaimTrie);
|
|
|
|
resolve(new Set([...a].filter(x => !b.has(x))));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-08 03:53:15 +02:00
|
|
|
function getBlockedOutpoints () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
rp(`http://api.lbry.io/file/list_blocked`)
|
|
|
|
.then(function (htmlString) {
|
|
|
|
resolve(htmlString);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-03 14:47:40 +02:00
|
|
|
function getValue (tx, i) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
rp(`http://localhost:5000/claim_decode/${tx}/${i}`)
|
|
|
|
.then(function (htmlString) {
|
|
|
|
resolve(htmlString);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function pushElastic (claim) {
|
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
queue.push({
|
2017-08-16 19:49:27 +02:00
|
|
|
index: 'claims',
|
|
|
|
type : 'claim',
|
|
|
|
id : claim.claimId,
|
|
|
|
body : claim,
|
2017-09-03 14:47:40 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON (path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
jsonfile.readFile(path, function (err, jsoncontent) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(jsoncontent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function saveJSON (path, obj) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
jsonfile.writeFile(path, obj, function (err, jsoncontent) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
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));
|
|
|
|
}
|