Skip to content

Commit

Permalink
Improve listen count checking (#8784)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored Jun 11, 2024
1 parent dfa3b62 commit 7852ecc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/identity-service/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@
"solanaFeePayerWallets": "[]",
"sentryDSN": "",
"solanaEndpoint": "https://api.mainnet-beta.solana.com",
"solanaEndpointListensProgram": "https://api.mainnet-beta.solana.com",
"stripeSecretKey": ""
}
44 changes: 30 additions & 14 deletions packages/identity-service/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class App {
prefix: `listenCountLimiter:::${interval}-ip-track:::`,
expiry: timeInSeconds,
max: config.get(`rateLimitingListensPerIPTrackPer${interval}`), // max requests per interval
skip: function (req) {
const { ip } = getIP(req)
return isIPWhitelisted(ip, req)
},
keyGenerator: function (req) {
const trackId = req.params.id
const { ip } = getIP(req)
Expand All @@ -260,6 +264,10 @@ class App {
prefix: `listenCountLimiter:::${interval}-ip-exclusive:::`,
expiry: timeInSeconds,
max: config.get(`rateLimitingListensPerIPPer${interval}`), // max requests per interval
skip: function (req) {
const { ip } = getIP(req)
return isIPWhitelisted(ip, req)
},
keyGenerator: function (req) {
const { ip } = getIP(req)
return `${ip}`
Expand Down Expand Up @@ -296,29 +304,37 @@ class App {
this.express.use('/tiktok/', tikTokRequestRateLimiter)

const ONE_HOUR_IN_SECONDS = 60 * 60
const [listenCountHourlyLimiter, listenCountHourlyIPTrackLimiter] =
this._createRateLimitsForListenCounts('Hour', ONE_HOUR_IN_SECONDS)
const [
listenCountDailyLimiter,
listenCountHourlyTrackLimiter,
listenCountHourlyIPTrackLimiter,
listenCountHourlyIPLimiter
] = this._createRateLimitsForListenCounts('Hour', ONE_HOUR_IN_SECONDS)
const [
listenCountDailyTrackLimiter,
listenCountDailyIPTrackLimiter,
listenCountDailyIPLimiter
] = this._createRateLimitsForListenCounts('Day', ONE_HOUR_IN_SECONDS * 24)
const [listenCountWeeklyLimiter, listenCountWeeklyIPTrackLimiter] =
this._createRateLimitsForListenCounts(
'Week',
ONE_HOUR_IN_SECONDS * 24 * 7
)
const [
listenCountWeeklyTrackLimiter,
listenCountWeeklyIPTrackLimiter,
listenCountWeeklyIPLimiter
] = this._createRateLimitsForListenCounts(
'Week',
ONE_HOUR_IN_SECONDS * 24 * 7
)

// This limiter double dips with the reqLimiter. The 5 requests every hour are also counted here
this.express.use(
'/tracks/:id/listen',
listenCountWeeklyIPTrackLimiter,
listenCountWeeklyLimiter,
listenCountDailyIPTrackLimiter,
listenCountDailyLimiter,
listenCountHourlyTrackLimiter,
listenCountHourlyIPTrackLimiter,
listenCountHourlyLimiter,
listenCountDailyIPLimiter
listenCountHourlyIPLimiter,
listenCountDailyTrackLimiter,
listenCountDailyIPTrackLimiter,
listenCountDailyIPLimiter,
listenCountWeeklyTrackLimiter,
listenCountWeeklyIPTrackLimiter,
listenCountWeeklyIPLimiter
)

// Eth relay rate limits
Expand Down
6 changes: 6 additions & 0 deletions packages/identity-service/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@ const config = convict({
env: 'solanaEndpoint',
default: null
},
solanaEndpointListensProgram: {
doc: 'The Solana RPC endpoint to make requests against for the listens program',
format: String,
env: 'solanaEndpointListensProgram',
default: null
},
solanaTrackListenCountAddress: {
doc: 'solanaTrackListenCountAddress',
format: String,
Expand Down
37 changes: 25 additions & 12 deletions packages/identity-service/src/routes/trackListens.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
createTrackListenInstructions,
getFeePayerKeypair
} = require('../solana-client')
const { TransactionMessage, VersionedTransaction } = require('@solana/web3.js')
const config = require('../config.js')

function trimToHour(date) {
Expand Down Expand Up @@ -368,7 +369,9 @@ module.exports = function (app) {
'/tracks/:id/listen',
handleResponse(async (req, res) => {
const libs = req.app.get('audiusLibs')
const connection = libs.solanaWeb3Manager.getConnection()
const connection = new libs.solanaWeb3Manager.solanaWeb3.Connection(
config.get('solanaEndpointListensProgram')
)
const solanaWeb3 = libs.solanaWeb3Manager.solanaWeb3
const redis = req.app.get('redis')
const trackId = parseInt(req.params.id)
Expand Down Expand Up @@ -454,19 +457,29 @@ module.exports = function (app) {
req.logger.info(
`TrackListen tx submission, trackId=${trackId} userId=${userId} - sendRawTransaction`
)

const transactionHandler = libs.solanaWeb3Manager.transactionHandler
const { res: solTxSignature, error } =
await transactionHandler.handleTransaction({
instructions,
skipPreflight: false, // TODO
feePayerOverride: feePayerAccount,
retry: true
let solTxSignature
try {
const recentBlock = await connection.getLatestBlockhash('confirmed')

const message = new TransactionMessage({
payerKey: feePayerAccount.publicKey,
recentBlockhash: recentBlock.blockhash,
instructions
})

if (error) {
const versionedMessage = message.compileToV0Message()
const transaction = new VersionedTransaction(versionedMessage)
transaction.sign([feePayerAccount])
solTxSignature = await connection.sendRawTransaction(
transaction.serialize(),
{
skipPreflight: true,
preflightCommitment: 'processed',
maxRetries: 0
}
)
} catch (e) {
return errorResponseServerError(
`TrackListens tx error, trackId=${trackId} userId=${userId} : ${error}`
`TrackListens tx error, trackId=${trackId} userId=${userId} : ${e}`
)
}

Expand Down

0 comments on commit 7852ecc

Please sign in to comment.