Provider are now starting asynchronously

This commit is contained in:
2021-01-25 19:10:13 +01:00
parent 868a408a9d
commit e00a4fbf1c
6 changed files with 96 additions and 72 deletions

View File

@@ -25,7 +25,7 @@ export abstract class BackendProvider {
* *
* @returns If `false` is returned, then the provider failed to initialize. * @returns If `false` is returned, then the provider failed to initialize.
*/ */
abstract onEnable(): boolean; abstract onEnable(): Promise<void>;
/** /**
* Generate a new address to receive new funds. * Generate a new address to receive new funds.

View File

@@ -28,7 +28,7 @@ export class ProviderManager {
.filter(dirent => dirent.name.endsWith('.ts')) .filter(dirent => dirent.name.endsWith('.ts'))
.map(dirent => dirent.name) .map(dirent => dirent.name)
getDirectories().forEach(file => { getDirectories().forEach(async file => {
const absolutePath = join(this.providerFilePath, file); const absolutePath = join(this.providerFilePath, file);
const providerModule = require(absolutePath); const providerModule = require(absolutePath);
const provider = new providerModule.Provider() as BackendProvider; const provider = new providerModule.Provider() as BackendProvider;
@@ -44,13 +44,14 @@ export class ProviderManager {
}); });
// Execute onEnable() function of this provider // Execute onEnable() function of this provider
const startUp = provider.onEnable(); try {
if (!startUp) { await provider.onEnable();
logger.error(`Provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) failed to start! (check previous logs)`); logger.info(`Loaded provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) for ${provider.CRYPTO.join(', ')}`);
return; } catch (err) {
logger.error(`Provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) failed to start: ${err}`);
this.disable(provider.NAME);
} }
logger.info(`Loaded provider "${provider.NAME}" by ${provider.AUTHOR} (${provider.VERSION}) for ${provider.CRYPTO.join(', ')}`);
}); });
} }

View File

@@ -18,19 +18,26 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.BITCOIN]; CRYPTO = [CryptoUnits.BITCOIN];
onEnable() { onEnable() {
this.sock = new Subscriber(); return new Promise<void>((resolve, reject) => {
this.sock.connect('tcp://127.0.0.1:29000'); this.sock = new Subscriber();
this.sock.subscribe('rawtx'); this.sock.connect('tcp://127.0.0.1:29000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 18332,
auth: 'admin:admin'
});
// We perfom a small test call to check if everything works.
this.rpcClient = rpc.Client.http({ this.rpcClient.request('getblockchaininfo', [], (err, message) => {
port: 18332, if (err) {
auth: 'admin:admin' reject(`Cannot connect with Bitcoin Core: ${err.message}`);
}); return;
}
this.listener(); this.listener();
resolve();
return true; });
});
} }
async getNewAddress(): Promise<string> { async getNewAddress(): Promise<string> {

View File

@@ -18,20 +18,28 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.DOGECOIN]; CRYPTO = [CryptoUnits.DOGECOIN];
onEnable() { onEnable() {
this.sock = new Subscriber(); return new Promise<void>((resolve, reject) => {
this.sock.connect('tcp://127.0.0.1:30000'); this.sock = new Subscriber();
this.sock.subscribe('rawtx'); this.sock.connectTimeout = 2;
this.sock.connect('tcp://127.0.0.1:30000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 22556, this.rpcClient = rpc.Client.http({
auth: 'admin:admin' port: 22556,
auth: 'admin:admin'
});
// We perfom a small test call to check if everything works.
this.rpcClient.request('getblockchaininfo', [], (err, message) => {
if (err) {
reject(`Cannot connect with Dogecoin Core: ${err.message}`);
return;
}
this.listener();
resolve();
});
}); });
this.listener();
return true;
//logger.info('The Bitcoin Core backend is now available!'); //logger.info('The Bitcoin Core backend is now available!');
} }

View File

@@ -18,19 +18,28 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.LITECOIN]; CRYPTO = [CryptoUnits.LITECOIN];
onEnable() { onEnable() {
this.sock = new Subscriber(); return new Promise<void>((resolve, reject) => {
this.sock.connect('tcp://127.0.0.1:40000'); this.sock = new Subscriber();
this.sock.subscribe('rawtx'); this.sock.connectTimeout = 2;
this.sock.connect('tcp://127.0.0.1:40000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({ this.rpcClient = rpc.Client.http({
port: 22557, port: 22557,
auth: 'admin:admin' auth: 'admin:admin'
});
// We perfom a small test call to check if everything works.
this.rpcClient.request('getblockchaininfo', [], (err, message) => {
if (err) {
reject(`Cannot connect with Bitcoin Core: ${err.message}`);
return;
}
this.listener();
resolve();
});
}); });
this.listener();
return true;
} }
async getNewAddress(): Promise<string> { async getNewAddress(): Promise<string> {

View File

@@ -19,39 +19,38 @@ export class Provider implements BackendProvider {
CRYPTO = [CryptoUnits.MONERO]; CRYPTO = [CryptoUnits.MONERO];
onEnable() { onEnable() {
if (process.env.MONERO_WALLET_PASSWORD === undefined) { return new Promise<void>((resolve, reject) => {
logger.error(`Enviroment variable MONERO_WALLET_PASSWORD is required but not set!`); if (process.env.MONERO_WALLET_PASSWORD === undefined) {
return false; reject('Enviroment variable MONERO_WALLET_PASSWORD is required but not set!');
}
if (process.env.MONERO_WALLET_NAME === undefined) {
logger.error(`Enviroment variable MONERO_WALLET_FILEPATH is required but not set!`);
return false;
}
if (process.env.MONERO_ZMQ_ADDRESS === undefined) {
logger.error(`Enviroment variable MONERO_ZMQ_ADDRESS is required but not set!`);
return false;
}
this.rpcClient = rpc.Client.http({
path: '/json_rpc',
port: 38085
});
this.rpcClient.request('open_wallet', {
filename: process.env.MONERO_WALLET_NAME,
password: process.env.MONERO_WALLET_PASSWORD
}, (err, message) => {
if (err) {
console.log(err);
logger.error(`Failed to open Monero wallet: ${err}\nMaybe a wrong password or path?`);
return; return;
} }
if (process.env.MONERO_WALLET_NAME === undefined) {
reject('Enviroment variable MONERO_WALLET_FILEPATH is required but not set!');
return;
}
if (process.env.MONERO_ZMQ_ADDRESS === undefined) {
reject('Enviroment variable MONERO_ZMQ_ADDRESS is required but not set!');
return;
}
this.rpcClient = rpc.Client.http({
path: '/json_rpc',
port: 38085
});
this.rpcClient.request('open_wallet', {
filename: process.env.MONERO_WALLET_NAME,
password: process.env.MONERO_WALLET_PASSWORD
}, (err, message) => {
if (err) {
reject(`Failed to open Monero wallet: ${err}\nMaybe a wrong password or path?`)
return;
}
this.listener();
resolve();
});
}); });
this.listener();
return true;
} }
async listener() { async listener() {