fix(hydrateUtxosWL): Adding usrObj for rate limit control

This commit is contained in:
Chris Troutner
2021-03-08 17:51:04 -08:00
parent f7125be3c8
commit 154229d5f5
2 changed files with 43 additions and 6 deletions
+8
View File
@@ -141,6 +141,14 @@ class RateLimits {
} }
} }
} else { } else {
// This should be a corner case. Calls should not be going into this
// code path, so the system should throw up big warning signs when they
// do.
// This code path happens when an internal call is made but does not
// pass the usrObj. Legacy code needs to be refactored to use the usrObj
// and avoid this code path. This code path is 'pooled': all users
// share the same rate limits. Even at 1000 RPM, this pool will get
// exhausted easily.
console.log( console.log(
'Internal call. req.body.usrObj does not exist. Applying high-speed internal rate limits.' 'Internal call. req.body.usrObj does not exist. Applying high-speed internal rate limits.'
) )
+35 -6
View File
@@ -36,9 +36,13 @@ util.inspect.defaultOptions = { depth: 5 }
// Determine the Access password for a private instance of SLPDB. // Determine the Access password for a private instance of SLPDB.
// https://gist.github.com/christroutner/fc717ca704dec3dded8b52fae387eab2 // https://gist.github.com/christroutner/fc717ca704dec3dded8b52fae387eab2
// Password for General Purpose (GP) SLPDB. // Password for General Purpose (GP) SLPDB.
const SLPDB_PASS_GP = process.env.SLPDB_PASS_GP ? process.env.SLPDB_PASS_GP : 'BITBOX' const SLPDB_PASS_GP = process.env.SLPDB_PASS_GP
? process.env.SLPDB_PASS_GP
: 'BITBOX'
// Password for Whitelist (WL) SLPDB. // Password for Whitelist (WL) SLPDB.
const SLPDB_PASS_WL = process.env.SLPDB_PASS_WL ? process.env.SLPDB_PASS_WL : 'BITBOX' const SLPDB_PASS_WL = process.env.SLPDB_PASS_WL
? process.env.SLPDB_PASS_WL
: 'BITBOX'
// const rawtransactions = require('./full-node/rawtransactions') // const rawtransactions = require('./full-node/rawtransactions')
const RawTransactions = require('./full-node/rawtransactions') const RawTransactions = require('./full-node/rawtransactions')
@@ -46,7 +50,7 @@ const rawTransactions = new RawTransactions()
// Setup REST and TREST URLs used by slpjs // Setup REST and TREST URLs used by slpjs
// Dev note: this allows for unit tests to mock the URL. // Dev note: this allows for unit tests to mock the URL.
if (!process.env.REST_URL) process.env.REST_URL = 'https://bchn.fullstack.cash/v4/' if (!process.env.REST_URL) { process.env.REST_URL = 'https://bchn.fullstack.cash/v4/' }
if (!process.env.TREST_URL) { if (!process.env.TREST_URL) {
process.env.TREST_URL = 'https://testnet.fullstack.cash/v4/' process.env.TREST_URL = 'https://testnet.fullstack.cash/v4/'
} }
@@ -1982,7 +1986,9 @@ class Slp {
// Extract a delay value if the user passed it in. // Extract a delay value if the user passed it in.
const usrObjIn = req.body.usrObj const usrObjIn = req.body.usrObj
let utxoDelay = 0 let utxoDelay = 0
if (usrObjIn && usrObjIn.utxoDelay) { utxoDelay = usrObjIn.utxoDelay } if (usrObjIn && usrObjIn.utxoDelay) {
utxoDelay = usrObjIn.utxoDelay
}
// console.log('req: ', req) // console.log('req: ', req)
// console.log(`req._remoteAddress: ${req._remoteAddress}`) // console.log(`req._remoteAddress: ${req._remoteAddress}`)
@@ -2031,7 +2037,10 @@ class Slp {
const theseUtxos = utxos[i].utxos const theseUtxos = utxos[i].utxos
// Get SLP token details. // Get SLP token details.
const details = await _this.bchjs.SLP.Utils.tokenUtxoDetails(theseUtxos, usrObj) const details = await _this.bchjs.SLP.Utils.tokenUtxoDetails(
theseUtxos,
usrObj
)
// console.log('details: ', details) // console.log('details: ', details)
// Replace the original UTXO data with the hydrated data. // Replace the original UTXO data with the hydrated data.
@@ -2082,6 +2091,23 @@ class Slp {
try { try {
const utxos = req.body.utxos const utxos = req.body.utxos
// Extract a delay value if the user passed it in.
const usrObjIn = req.body.usrObj
let utxoDelay = 0
if (usrObjIn && usrObjIn.utxoDelay) {
utxoDelay = usrObjIn.utxoDelay
}
// Generate a user object that can be passed along with internal calls
// from bch-js.
const usrObj = {
ip: req._remoteAddress,
jwtToken: req.locals.jwtToken,
proLimit: req.locals.proLimit,
apiLevel: req.locals.apiLevel,
utxoDelay
}
// Validate inputs // Validate inputs
if (!Array.isArray(utxos)) { if (!Array.isArray(utxos)) {
res.status(422) res.status(422)
@@ -2117,7 +2143,10 @@ class Slp {
// console.log(`theseUtxos: ${JSON.stringify(theseUtxos, null, 2)}`) // console.log(`theseUtxos: ${JSON.stringify(theseUtxos, null, 2)}`)
// Get SLP token details. // Get SLP token details.
const details = await _this.bchjs.SLP.Utils.tokenUtxoDetailsWL(theseUtxos) const details = await _this.bchjs.SLP.Utils.tokenUtxoDetailsWL(
theseUtxos,
usrObj
)
// console.log('details : ', details) // console.log('details : ', details)
// Replace the original UTXO data with the hydrated data. // Replace the original UTXO data with the hydrated data.