Skip to content

Commit

Permalink
PROTO-1364: fix relay heavy load (#6878)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecsavvy authored Dec 7, 2023
1 parent 67554f1 commit 888789f
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logger } from '../logger'
import { NextFunction, Request, Response } from 'express'
import { config } from '..'
import { antiAbuseError, internalError } from '../error'
import { decodeAbi } from '../abi'

type AbuseRule = {
rule: number
Expand All @@ -20,24 +21,28 @@ type AbuseStatus = {
}

export const antiAbuseMiddleware = async (
request: Request,
_: Request,
response: Response,
next: NextFunction
) => {
const aaoConfig = config.aao
const { ip, recoveredSigner: user } = response.locals.ctx
await detectAbuse(aaoConfig, user, ip, false, next)
const decodedAbi = decodeAbi(response.locals.ctx.validatedRelayRequest.encodedABI)
const isUserCreate = (decodedAbi.action === "Create" && decodedAbi.entityType === "User")
await detectAbuse(aaoConfig, user, ip, isUserCreate, false, next)
}

export const detectAbuse = async (
aaoConfig: AntiAbuseConfig,
user: Users,
reqIp: string,
isUserCreate: boolean,
abbreviated: boolean,
next: NextFunction
) => {
// if aao turned off, never detect abuse
if (!aaoConfig.useAao || !user.handle) {
// on user create a user will not be in AAO yet
if (!aaoConfig.useAao || !user.handle || isUserCreate) {
next()
return
}
Expand Down

0 comments on commit 888789f

Please sign in to comment.