Add pools.json file in default config.ts - Handle file exception - Only import pools for MAINNET
This commit is contained in:
parent
8d1cc40459
commit
19a564062b
@ -1,6 +1,7 @@
|
|||||||
import {readFileSync} from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { DB } from '../database';
|
import { DB } from '../database';
|
||||||
import logger from '../logger';
|
import logger from '../logger';
|
||||||
|
import config from '../config';
|
||||||
|
|
||||||
interface Pool {
|
interface Pool {
|
||||||
name: string;
|
name: string;
|
||||||
@ -14,20 +15,26 @@ class PoolsParser {
|
|||||||
* Parse the pools.json file, consolidate the data and dump it into the database
|
* Parse the pools.json file, consolidate the data and dump it into the database
|
||||||
*/
|
*/
|
||||||
public async migratePoolsJson() {
|
public async migratePoolsJson() {
|
||||||
const connection = await DB.pool.getConnection();
|
if (config.MEMPOOL.NETWORK !== 'mainnet') {
|
||||||
logger.info('Importing pools.json to the database');
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Get existing pools from the db
|
logger.debug('Importing pools.json to the database, open ./pools.json');
|
||||||
const [existingPools] = await connection.query<any>({ sql: 'SELECT * FROM pools;', timeout: 120000 });
|
|
||||||
|
|
||||||
logger.info('Open ./pools.json');
|
let poolsJson: object = {};
|
||||||
const fileContent: string = readFileSync('./pools.json', 'utf8');
|
try {
|
||||||
const poolsJson: object = JSON.parse(fileContent);
|
const fileContent: string = readFileSync('./pools.json', 'utf8');
|
||||||
|
poolsJson = JSON.parse(fileContent);
|
||||||
|
} catch (e) {
|
||||||
|
logger.err('Unable to open ./pools.json, does the file exist?');
|
||||||
|
await this.insertUnknownPool();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// First we save every entries without paying attention to pool duplication
|
// First we save every entries without paying attention to pool duplication
|
||||||
const poolsDuplicated: Pool[] = [];
|
const poolsDuplicated: Pool[] = [];
|
||||||
|
|
||||||
logger.info('Parse coinbase_tags');
|
logger.debug('Parse coinbase_tags');
|
||||||
const coinbaseTags = Object.entries(poolsJson['coinbase_tags']);
|
const coinbaseTags = Object.entries(poolsJson['coinbase_tags']);
|
||||||
for (let i = 0; i < coinbaseTags.length; ++i) {
|
for (let i = 0; i < coinbaseTags.length; ++i) {
|
||||||
poolsDuplicated.push({
|
poolsDuplicated.push({
|
||||||
@ -37,7 +44,7 @@ class PoolsParser {
|
|||||||
'addresses': [],
|
'addresses': [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
logger.info('Parse payout_addresses');
|
logger.debug('Parse payout_addresses');
|
||||||
const addressesTags = Object.entries(poolsJson['payout_addresses']);
|
const addressesTags = Object.entries(poolsJson['payout_addresses']);
|
||||||
for (let i = 0; i < addressesTags.length; ++i) {
|
for (let i = 0; i < addressesTags.length; ++i) {
|
||||||
poolsDuplicated.push({
|
poolsDuplicated.push({
|
||||||
@ -49,14 +56,18 @@ class PoolsParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Then, we find unique mining pool names
|
// Then, we find unique mining pool names
|
||||||
logger.info('Identify unique mining pools');
|
logger.debug('Identify unique mining pools');
|
||||||
const poolNames: string[] = [];
|
const poolNames: string[] = [];
|
||||||
for (let i = 0; i < poolsDuplicated.length; ++i) {
|
for (let i = 0; i < poolsDuplicated.length; ++i) {
|
||||||
if (poolNames.indexOf(poolsDuplicated[i].name) === -1) {
|
if (poolNames.indexOf(poolsDuplicated[i].name) === -1) {
|
||||||
poolNames.push(poolsDuplicated[i].name);
|
poolNames.push(poolsDuplicated[i].name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info(`Found ${poolNames.length} unique mining pools`);
|
logger.debug(`Found ${poolNames.length} unique mining pools`);
|
||||||
|
|
||||||
|
// Get existing pools from the db
|
||||||
|
const connection = await DB.pool.getConnection();
|
||||||
|
const [existingPools] = await connection.query<any>({ sql: 'SELECT * FROM pools;', timeout: 120000 });
|
||||||
|
|
||||||
// Finally, we generate the final consolidated pools data
|
// Finally, we generate the final consolidated pools data
|
||||||
const finalPoolDataAdd: Pool[] = [];
|
const finalPoolDataAdd: Pool[] = [];
|
||||||
@ -92,23 +103,13 @@ class PoolsParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manually add the 'unknown pool'
|
logger.debug(`Update pools table now`);
|
||||||
if (existingPools.find((pool) => pool.name === 'Unknown') === undefined) {
|
|
||||||
finalPoolDataAdd.push({
|
|
||||||
'name': 'Unknown',
|
|
||||||
'link': 'https://learnmeabitcoin.com/technical/coinbase-transaction',
|
|
||||||
regexes: [],
|
|
||||||
addresses: [],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`Update pools table now`);
|
|
||||||
|
|
||||||
// Add new mining pools into the database
|
// Add new mining pools into the database
|
||||||
let queryAdd: string = 'INSERT INTO pools(name, link, regexes, addresses) VALUES ';
|
let queryAdd: string = 'INSERT INTO pools(name, link, regexes, addresses) VALUES ';
|
||||||
for (let i = 0; i < finalPoolDataAdd.length; ++i) {
|
for (let i = 0; i < finalPoolDataAdd.length; ++i) {
|
||||||
queryAdd += `('${finalPoolDataAdd[i].name}', '${finalPoolDataAdd[i].link}',
|
queryAdd += `('${finalPoolDataAdd[i].name}', '${finalPoolDataAdd[i].link}',
|
||||||
'${JSON.stringify(finalPoolDataAdd[i].regexes)}', '${JSON.stringify(finalPoolDataAdd[i].addresses)}'),`;
|
'${JSON.stringify(finalPoolDataAdd[i].regexes)}', '${JSON.stringify(finalPoolDataAdd[i].addresses)}'),`;
|
||||||
}
|
}
|
||||||
queryAdd = queryAdd.slice(0, -1) + ';';
|
queryAdd = queryAdd.slice(0, -1) + ';';
|
||||||
|
|
||||||
@ -116,11 +117,11 @@ class PoolsParser {
|
|||||||
const updateQueries: string[] = [];
|
const updateQueries: string[] = [];
|
||||||
for (let i = 0; i < finalPoolDataUpdate.length; ++i) {
|
for (let i = 0; i < finalPoolDataUpdate.length; ++i) {
|
||||||
updateQueries.push(`
|
updateQueries.push(`
|
||||||
UPDATE pools
|
UPDATE pools
|
||||||
SET name='${finalPoolDataUpdate[i].name}', link='${finalPoolDataUpdate[i].link}',
|
SET name='${finalPoolDataUpdate[i].name}', link='${finalPoolDataUpdate[i].link}',
|
||||||
regexes='${JSON.stringify(finalPoolDataUpdate[i].regexes)}', addresses='${JSON.stringify(finalPoolDataUpdate[i].addresses)}'
|
regexes='${JSON.stringify(finalPoolDataUpdate[i].regexes)}', addresses='${JSON.stringify(finalPoolDataUpdate[i].addresses)}'
|
||||||
WHERE name='${finalPoolDataUpdate[i].name}'
|
WHERE name='${finalPoolDataUpdate[i].name}'
|
||||||
;`);
|
;`);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -130,15 +131,34 @@ class PoolsParser {
|
|||||||
updateQueries.forEach(async query => {
|
updateQueries.forEach(async query => {
|
||||||
await connection.query<any>({ sql: query, timeout: 120000 });
|
await connection.query<any>({ sql: query, timeout: 120000 });
|
||||||
});
|
});
|
||||||
|
await this.insertUnknownPool();
|
||||||
connection.release();
|
connection.release();
|
||||||
logger.info('Import completed');
|
logger.info('Mining pools.json import completed');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
connection.release();
|
connection.release();
|
||||||
logger.info(`Unable to import pools in the database!`);
|
logger.err(`Unable to import pools in the database!`);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manually add the 'unknown pool'
|
||||||
|
*/
|
||||||
|
private async insertUnknownPool() {
|
||||||
|
const connection = await DB.pool.getConnection();
|
||||||
|
try {
|
||||||
|
const [rows]: any[] = await connection.query({ sql: 'SELECT name from pools where name="Unknown"', timeout: 120000 });
|
||||||
|
if (rows.length === 0) {
|
||||||
|
logger.debug('Manually inserting "Unknown" mining pool into the databse');
|
||||||
|
await connection.query({
|
||||||
|
sql: `INSERT INTO pools(name, link, regexes, addresses)
|
||||||
|
VALUES("Unknown", "https://learnmeabitcoin.com/technical/coinbase-transaction", "[]", "[]");
|
||||||
|
`});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.err('Unable to insert "Unknown" mining pool');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new PoolsParser();
|
export default new PoolsParser();
|
||||||
|
@ -79,7 +79,9 @@ const defaults: IConfig = {
|
|||||||
'MEMPOOL_BLOCKS_AMOUNT': 8,
|
'MEMPOOL_BLOCKS_AMOUNT': 8,
|
||||||
'PRICE_FEED_UPDATE_INTERVAL': 3600,
|
'PRICE_FEED_UPDATE_INTERVAL': 3600,
|
||||||
'USE_SECOND_NODE_FOR_MINFEE': false,
|
'USE_SECOND_NODE_FOR_MINFEE': false,
|
||||||
'EXTERNAL_ASSETS': [],
|
'EXTERNAL_ASSETS': [
|
||||||
|
'https://mempool.space/resources/pools.json'
|
||||||
|
]
|
||||||
},
|
},
|
||||||
'ESPLORA': {
|
'ESPLORA': {
|
||||||
'REST_API_URL': 'http://127.0.0.1:3000',
|
'REST_API_URL': 'http://127.0.0.1:3000',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user