Skip to content

Commit

Permalink
Merge pull request #25 from Adamant-im/feat/check-health-callback
Browse files Browse the repository at this point in the history
feat: add callback for check health at startup
  • Loading branch information
martiliones authored Dec 22, 2022
2 parents 8bbc988 + db64d24 commit 0137d77
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
"license": "GPL-3.0",
"dependencies": {
"@liskhq/lisk-cryptography": "3.2.1",
"axios": "^1.1.3",
"bignumber.js": "^9.1.0",
"bitcoinjs-lib": "^5.2.0",
"axios": "^1.2.1",
"bignumber.js": "^9.1.1",
"bitcoinjs-lib": "^6.1.0",
"bitcore-mnemonic": "^8.25.40",
"bytebuffer": "^5.0.1",
"coininfo": "^5.2.1",
"ecpair": "^2.1.0",
"ed2curve": "^0.3.0",
"ethereumjs-util": "^7.1.5",
"hdkey": "^2.0.1",
"pbkdf2": "^3.1.2",
"socket.io-client": "^4.5.3",
"sodium-browserify-tweetnacl": "^0.2.6"
"socket.io-client": "^4.5.4",
"sodium-browserify-tweetnacl": "^0.2.6",
"tiny-secp256k1": "^2.2.1"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -63,11 +65,11 @@
},
"homepage": "https://github.com/Adamant-im/adamant-api-jsclient#readme",
"devDependencies": {
"@commitlint/cli": "^17.2.0",
"@commitlint/config-conventional": "^17.2.0",
"eslint": "^8.27.0",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
"eslint": "^8.30.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jest": "^27.1.7",
"husky": "^8.0.2",
"jest": "^29.3.1"
}
Expand Down
12 changes: 8 additions & 4 deletions src/groups/btc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const bitcoin = require('bitcoinjs-lib');
const {ECPairFactory} = require('ecpair');
const tinysecp = require('tiny-secp256k1');

const coinNetworks = require('./coinNetworks');
const btc = { };
const btc = {};

/**
* Generates a BTC account from the passphrase specified.
* @param {string} passphrase ADAMANT account passphrase
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
* @return {object} network info, keyPair, privateKey, privateKeyWIF
*/

btc.keys = (passphrase) => {
const network = coinNetworks.BTC;
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});

const ECPairAPI = new ECPairFactory(tinysecp);
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});

return {
network,
Expand Down
10 changes: 7 additions & 3 deletions src/groups/dash.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const bitcoin = require('bitcoinjs-lib');
const {ECPairFactory} = require('ecpair');
const tinysecp = require('tiny-secp256k1');

const coinNetworks = require('./coinNetworks');
const dash = { };

/**
* Generates a DASH account from the passphrase specified.
* @param {string} passphrase ADAMANT account passphrase
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
* @return {object} network info, keyPair, privateKey, privateKeyWIF
*/

dash.keys = (passphrase) => {
const network = coinNetworks.DASH;
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});

const ECPairAPI = new ECPairFactory(tinysecp);
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});

return {
network,
Expand Down
12 changes: 8 additions & 4 deletions src/groups/doge.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const bitcoin = require('bitcoinjs-lib');
const {ECPairFactory} = require('ecpair');
const tinysecp = require('tiny-secp256k1');

const coinNetworks = require('./coinNetworks');
const doge = { };
const doge = {};

/**
* Generates a DOGE account from the passphrase specified.
* @param {string} passphrase ADAMANT account passphrase
* @returns {object} network info, keyPair, privateKey, privateKeyWIF
* @return {object} network info, keyPair, privateKey, privateKeyWIF
*/

doge.keys = (passphrase) => {
const network = coinNetworks.DOGE;
const pwHash = bitcoin.crypto.sha256(Buffer.from(passphrase));
const keyPair = bitcoin.ECPair.fromPrivateKey(pwHash, {network});

const ECPairAPI = new ECPairFactory(tinysecp);
const keyPair = ECPairAPI.fromPrivateKey(pwHash, {network});

return {
network,
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/healthCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {RE_IP, RE_HTTP_URL} = require('./constants');
const CHECK_NODES_INTERVAL = 60 * 5 * 1000; // Update active nodes every 5 minutes
const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced

module.exports = (nodes, checkHealthAtStartup = true) => {
module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallback) => {
const nodesList = nodes;
let isCheckingNodes = false;

Expand All @@ -37,8 +37,9 @@ module.exports = (nodes, checkHealthAtStartup = true) => {
/**
* Requests every ADAMANT node for its status, makes a list of live nodes, and chooses one active
* @param {boolean} forceChangeActiveNode
* @param {function?} checkNodesCallback
*/
async function checkNodes(forceChangeActiveNode) {
async function checkNodes(forceChangeActiveNode, checkNodesCallback) {
isCheckingNodes = true;

const liveNodes = [];
Expand Down Expand Up @@ -163,15 +164,18 @@ module.exports = (nodes, checkHealthAtStartup = true) => {
}

isCheckingNodes = false;
checkNodesCallback?.();
}

if (checkHealthAtStartup) {
changeNodes(true);
changeNodes(true, checkHealthAtStartupCallback);

setInterval(
() => changeNodes(true),
CHECK_NODES_INTERVAL,
);
} else {
checkHealthAtStartupCallback?.();
}

return {
Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ const constants = require('./helpers/constants.js');
const transactionFormer = require('./helpers/transactionFormer');
const keys = require('./helpers/keys');

module.exports = (params, customLogger) => {
/**
* Create new API instance
* @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string}} params
* @param {{ error: function, warn: function, info: function, log: function }?} customLogger
* @param {function?} checkHealthAtStartupCallback callback which is called after the first health check or
* just at startup when params.checkHealthAtStartup is passed
* @return {object} API methods and helpers
*/
module.exports = (params, customLogger, checkHealthAtStartupCallback) => {
const log = customLogger || console;

logger.initLogger(params.logLevel, log);

const nodeManager = healthCheck(params.node, params.checkHealthAtStartup);
const nodeManager = healthCheck(
params.node,
params.checkHealthAtStartup,
checkHealthAtStartupCallback,
);

return {
get: get(nodeManager),
Expand Down

0 comments on commit 0137d77

Please sign in to comment.