From f3ce4ca4775bec53deba79b84bad241a4b5630b9 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 1 Aug 2019 11:21:45 -0300 Subject: [PATCH 1/4] Removes promise retrys on getSubscriptorRole Adds logs Adds try catch --- server/helpers/subscriberUtils.js | 78 ++++++++++++++++++------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/server/helpers/subscriberUtils.js b/server/helpers/subscriberUtils.js index b615dc3..a6cc2fe 100644 --- a/server/helpers/subscriberUtils.js +++ b/server/helpers/subscriberUtils.js @@ -154,32 +154,36 @@ const findTelegramSubscription = async (address, chatId) => { } const getSubscriptorRole = async subscriptor => { + console.log(`[Subscribers-utils] - getSubscriptorRole()`) const { getProtocolService } = require('./services/protocolService') const { getDelegatorService } = require('./services/delegatorService') const protocolService = getProtocolService() const delegatorService = getDelegatorService() - - let [constants, delegator] = await promiseRetry(retry => { - return Promise.all([ - protocolService.getLivepeerDefaultConstants(), - delegatorService.getDelegatorAccount(subscriptor.address) - ]).catch(err => retry()) - }) - - const { status, address, delegateAddress } = delegator - - // Detect role - const role = - delegator && - status === constants.DELEGATOR_STATUS.Bonded && - delegateAddress && - address.toLowerCase() === delegateAddress.toLowerCase() - ? constants.ROLE.TRANSCODER - : constants.ROLE.DELEGATOR - return { - role, - constants, - delegator + try { + const constants = await protocolService.getLivepeerDefaultConstants() + console.log( + `[Subscribers-utils] - getDelegatorAccount() for subscriptor: ${subscriptor.address}` + ) + const delegator = await delegatorService.getDelegatorAccount(subscriptor.address) + console.log(`[Subscribers-utils] - getDelegatorAccount() finished, detecting role`) + const { status, address, delegateAddress } = delegator + + // Detect role + const role = + delegator && + status === constants.DELEGATOR_STATUS.Bonded && + delegateAddress && + address.toLowerCase() === delegateAddress.toLowerCase() + ? constants.ROLE.TRANSCODER + : constants.ROLE.DELEGATOR + return { + role, + constants, + delegator + } + } catch (err) { + console.error(`[Subscribers-utils] - error on getSubscriptorRole(): ${err}`) + throw err } } @@ -287,12 +291,16 @@ const filterSubscribersByDelegatorRole = async allSubscribers => { throw new Error('No allSubscribersList received on filterSubscribersByDelegatorRole()') } for (let subscriber of allSubscribers) { - const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - if (role === constants.ROLE.DELEGATOR) { - subscribersList.push({ - subscriber, - delegator - }) + try { + const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + if (role === constants.ROLE.DELEGATOR) { + subscribersList.push({ + subscriber, + delegator + }) + } + } catch (err) { + continue } } return subscribersList @@ -304,11 +312,15 @@ const filterSubscribersByDelegateRole = async allSubscribers => { throw new Error('No allSubscribers list received on filterSubscribersByDelegateRole()') } for (let subscriber of allSubscribers) { - const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - if (role === constants.ROLE.TRANSCODER) { - subscribersList.push({ - subscriber - }) + try { + const { role, constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + if (role === constants.ROLE.TRANSCODER) { + subscribersList.push({ + subscriber + }) + } + } catch (e) { + continue } } return subscribersList From fc4b7e0b35a103d66467b3860de4d97428523bf7 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 1 Aug 2019 11:23:57 -0300 Subject: [PATCH 2/4] Replaces sdk calls of getLivepeerDefaultConstants with constants --- config/constants.js | 13 ++++++++++++- server/helpers/services/protocolService.js | 9 ++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/config/constants.js b/config/constants.js index c16af50..5cb0176 100644 --- a/config/constants.js +++ b/config/constants.js @@ -6,6 +6,16 @@ const DAILY_FREQUENCY = 'daily' const WEEKLY_FREQUENCY = 'weekly' const VALID_SUBSCRIPTION_FREQUENCIES = [WEEKLY_FREQUENCY, DAILY_FREQUENCY] const TO_FIXED_VALUES_DECIMALS = 4 +const LIVEPEER_DEFAULT_CONSTANTS = { + DELEGATOR_STATUS: { + Bonded: 'Bonded', + Pending: 'Pending', + Unbonded: 'Unbonded', + Unbonding: 'Unbonding' + }, + ROLE: { DELEGATOR: 'Delegator', TRANSCODER: 'Transcoder' } +} + module.exports = { PROTOCOL_DIVISION_BASE, TOKEN_DECIMALS_MULTIPLIER, @@ -14,5 +24,6 @@ module.exports = { VALID_SUBSCRIPTION_FREQUENCIES, DAILY_FREQUENCY, WEEKLY_FREQUENCY, - TO_FIXED_VALUES_DECIMALS + TO_FIXED_VALUES_DECIMALS, + LIVEPEER_DEFAULT_CONSTANTS } diff --git a/server/helpers/services/protocolService.js b/server/helpers/services/protocolService.js index 51a5611..9cdcf5a 100644 --- a/server/helpers/services/protocolService.js +++ b/server/helpers/services/protocolService.js @@ -2,7 +2,11 @@ const promiseRetry = require('promise-retry') const { calculateNextRoundInflationRatio, tokenAmountInUnits, MathBN } = require('../utils') -const { CACHE_UPDATE_INTERVAL, PROTOCOL_DIVISION_BASE } = require('../../../config/constants') +const { + CACHE_UPDATE_INTERVAL, + PROTOCOL_DIVISION_BASE, + LIVEPEER_DEFAULT_CONSTANTS +} = require('../../../config/constants') const defaultProtocolSource = require('../sdk/protocol') let protocolServiceInstance @@ -19,7 +23,7 @@ class ProtocolService { // The source of the functions that fetch data of the protocol, currently we are only supporting SDK this.source = source this.currentRound = null - this.defaultConstants = null + this.defaultConstants = LIVEPEER_DEFAULT_CONSTANTS this.totalBonded = null this.targetBondingRate = null this.inflationChange = null @@ -33,7 +37,6 @@ class ProtocolService { // Sets an interval that will reset the cached values periodically setInterval(() => { this.currentRound = null - this.defaultConstants = null this.totalBonded = null this.targetBondingRate = null this.inflationChange = null From 8e965dc07a284249a5d4a9b5461cc95721b01f05 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 1 Aug 2019 11:25:47 -0300 Subject: [PATCH 3/4] Fixs sendDelegatorNotificationBondingPeriodHasEnded import --- server/helpers/sendDelegatorEmail.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/helpers/sendDelegatorEmail.js b/server/helpers/sendDelegatorEmail.js index 94bb976..b75b6c6 100644 --- a/server/helpers/sendDelegatorEmail.js +++ b/server/helpers/sendDelegatorEmail.js @@ -319,7 +319,7 @@ const sendDelegatorNotificationBondingPeriodHasEnded = async ( } let body = { - transcoderAddress: truncateStringInTheMiddle(delegateAddress), + transcoderAddress: utils.truncateStringInTheMiddle(delegateAddress), delegatingStatusUrl: `https://explorer.livepeer.org/accounts/${subscriber.address}/delegating`, delegateAddress: delegateAddress, templateId: sendgridTemplateIdNotificationDelegatorBondingPeriodHasEnded, From db3364259efb61a94ad6747197120aaa401dec5b Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 1 Aug 2019 11:25:59 -0300 Subject: [PATCH 4/4] Adds try catch on sendEmailAfterBondingPeriodHAsEndedNotificationToDelegators --- .../notification/notificateDelegatorUtils.js | 140 ++++++++++-------- 1 file changed, 75 insertions(+), 65 deletions(-) diff --git a/server/helpers/notification/notificateDelegatorUtils.js b/server/helpers/notification/notificateDelegatorUtils.js index 066772b..3187cb8 100644 --- a/server/helpers/notification/notificateDelegatorUtils.js +++ b/server/helpers/notification/notificateDelegatorUtils.js @@ -116,46 +116,51 @@ const sendEmailAfterBondingPeriodHasEndedNotificationToDelegators = async curren const currentRoundId = currentRoundInfo.id for (const subscriberItem of subscribers) { - const { subscriber } = subscriberItem + try { + const { subscriber } = subscriberItem - if (!subscriber.lastPendingToBondingPeriodEmailSent) { - subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId - await subscriber.save() - continue - } + if (!subscriber.lastPendingToBondingPeriodEmailSent) { + subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId + await subscriber.save() + continue + } - // Check if notification was already sent, backward to 1 or 2 rounds - const differenceAlreadySended = - +currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0) - const isNotificationAlreadySended = - differenceAlreadySended === 0 || - differenceAlreadySended === 1 || - differenceAlreadySended === 2 + // Check if notification was already sent, backward to 1 or 2 rounds + const differenceAlreadySended = + +currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0) + const isNotificationAlreadySended = + differenceAlreadySended === 0 || + differenceAlreadySended === 1 || + differenceAlreadySended === 2 - if (isNotificationAlreadySended) { - console.log( - `[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round` - ) - continue - } + if (isNotificationAlreadySended) { + console.log( + `[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round` + ) + continue + } - const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status - const difference = +currentRoundId - +delegator.startRound - const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 + // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status + const difference = +currentRoundId - +delegator.startRound + const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 - if ( - isDifferenceBetweenRoundsEqualTo && - delegator.status === constants.DELEGATOR_STATUS.Bonded - ) { - subscribersToSendEmails.push( - delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded( - subscriber, - delegator.delegateAddress, - currentRoundId + if ( + isDifferenceBetweenRoundsEqualTo && + delegator.status === constants.DELEGATOR_STATUS.Bonded + ) { + subscribersToSendEmails.push( + delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded( + subscriber, + delegator.delegateAddress, + currentRoundId + ) ) - ) + } + } catch (err) { + console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`) + continue } } console.log( @@ -181,45 +186,50 @@ const sendTelegramAfterBondingPeriodHasEndedNotificationToDelegators = async cur const currentRoundId = currentRoundInfo.id for (const subscriberItem of subscribers) { - const { subscriber } = subscriberItem + try { + const { subscriber } = subscriberItem - if (!subscriber.lastPendingToBondingPeriodTelegramSent) { - subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId - await subscriber.save() - continue - } + if (!subscriber.lastPendingToBondingPeriodTelegramSent) { + subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId + await subscriber.save() + continue + } - // Check if notification was already sent, backward to 1 or 2 rounds - const differenceAlreadySended = - +currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0) - const isNotificationAlreadySended = - differenceAlreadySended === 0 || - differenceAlreadySended === 1 || - differenceAlreadySended === 2 + // Check if notification was already sent, backward to 1 or 2 rounds + const differenceAlreadySended = + +currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0) + const isNotificationAlreadySended = + differenceAlreadySended === 0 || + differenceAlreadySended === 1 || + differenceAlreadySended === 2 - if (isNotificationAlreadySended) { - console.log( - `[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round` - ) - continue - } + if (isNotificationAlreadySended) { + console.log( + `[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round` + ) + continue + } - const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status - const difference = +currentRoundId - +delegator.startRound - const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 + // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status + const difference = +currentRoundId - +delegator.startRound + const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 - if ( - isDifferenceBetweenRoundsEqualTo && - delegator.status === constants.DELEGATOR_STATUS.Bonded - ) { - subscribersToSendTelegrams.push( - delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded( - subscriber, - currentRoundId + if ( + isDifferenceBetweenRoundsEqualTo && + delegator.status === constants.DELEGATOR_STATUS.Bonded + ) { + subscribersToSendTelegrams.push( + delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded( + subscriber, + currentRoundId + ) ) - ) + } + } catch (err) { + console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`) + continue } } console.log(