From 85ecf4bf35680a890c59e61c448d00b06331514b Mon Sep 17 00:00:00 2001 From: Teddy Ding Date: Thu, 14 Nov 2024 19:00:28 -0500 Subject: [PATCH] Use alphanumeric suffix for username (#2573) (cherry picked from commit 95a80efbda83a52290e75fb02cbd9f39bea64273) --- .../__tests__/helpers/usernames-helper.test.ts | 16 ++++++++-------- indexer/services/roundtable/src/config.ts | 4 ++-- .../roundtable/src/helpers/usernames-helper.ts | 9 ++++++--- .../src/tasks/subaccount-username-generator.ts | 10 ++++++++++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/indexer/services/roundtable/__tests__/helpers/usernames-helper.test.ts b/indexer/services/roundtable/__tests__/helpers/usernames-helper.test.ts index 49e5fdec1f..5ba926245a 100644 --- a/indexer/services/roundtable/__tests__/helpers/usernames-helper.test.ts +++ b/indexer/services/roundtable/__tests__/helpers/usernames-helper.test.ts @@ -16,14 +16,14 @@ describe('usernames-helper', () => { ]; const expectedUsernames = [ - 'CushyHand599', - 'AmpleCube324', - 'AwareFood215', - 'LoudLand654', - 'MossyStraw800', - 'BoldGap392', - 'ZoomEra454', - 'WiryFern332', + 'CushyHandVE6', + 'AmpleCubeLKI', + 'AwareFoodHGP', + 'LoudLandXWV', + 'MossyStraw2JJ', + 'BoldGapOGY', + 'ZoomEraQE0', + 'WiryFernLEC', ]; for (let i = 0; i < addresses.length; i++) { diff --git a/indexer/services/roundtable/src/config.ts b/indexer/services/roundtable/src/config.ts index 1666898a30..6d7efbaca6 100644 --- a/indexer/services/roundtable/src/config.ts +++ b/indexer/services/roundtable/src/config.ts @@ -211,8 +211,8 @@ export const configSchema = { STALE_ORDERBOOK_LEVEL_THRESHOLD_SECONDS: parseInteger({ default: 10 }), // Subaccount username generator - SUBACCOUNT_USERNAME_NUM_RANDOM_DIGITS: parseInteger({ default: 3 }), - SUBACCOUNT_USERNAME_BATCH_SIZE: parseInteger({ default: 1000 }), + SUBACCOUNT_USERNAME_SUFFIX_RANDOM_DIGITS: parseInteger({ default: 3 }), + SUBACCOUNT_USERNAME_BATCH_SIZE: parseInteger({ default: 2000 }), // number of attempts to generate username for a subaccount ATTEMPT_PER_SUBACCOUNT: parseInteger({ default: 3 }), }; diff --git a/indexer/services/roundtable/src/helpers/usernames-helper.ts b/indexer/services/roundtable/src/helpers/usernames-helper.ts index 798790733b..1c30e076b6 100644 --- a/indexer/services/roundtable/src/helpers/usernames-helper.ts +++ b/indexer/services/roundtable/src/helpers/usernames-helper.ts @@ -4,6 +4,8 @@ import config from '../config'; import adjectives from './adjectives.json'; import nouns from './nouns.json'; +const suffixCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + export function generateUsernameForSubaccount( subaccountId: string, subaccountNum: number, @@ -12,12 +14,13 @@ export function generateUsernameForSubaccount( const rng = seedrandom(`${subaccountId}/${subaccountNum}/${nounce}`); const randomAdjective: string = adjectives[Math.floor(rng() * adjectives.length)]; const randomNoun: string = nouns[Math.floor(rng() * nouns.length)]; - const randomNumber: string = Math.floor(rng() * 1000).toString().padStart( - config.SUBACCOUNT_USERNAME_NUM_RANDOM_DIGITS, '0'); + const randomSuffix: string = Array.from( + { length: config.SUBACCOUNT_USERNAME_SUFFIX_RANDOM_DIGITS }, + () => suffixCharacters.charAt(Math.floor(rng() * suffixCharacters.length))).join(''); const capitalizedAdjective: string = randomAdjective.charAt( 0).toUpperCase() + randomAdjective.slice(1); const capitalizedNoun: string = randomNoun.charAt(0).toUpperCase() + randomNoun.slice(1); - return `${capitalizedAdjective}${capitalizedNoun}${randomNumber}`; + return `${capitalizedAdjective}${capitalizedNoun}${randomSuffix}`; } diff --git a/indexer/services/roundtable/src/tasks/subaccount-username-generator.ts b/indexer/services/roundtable/src/tasks/subaccount-username-generator.ts index 9c3d7d46ef..d1a435cb64 100644 --- a/indexer/services/roundtable/src/tasks/subaccount-username-generator.ts +++ b/indexer/services/roundtable/src/tasks/subaccount-username-generator.ts @@ -9,6 +9,8 @@ import config from '../config'; import { generateUsernameForSubaccount } from '../helpers/usernames-helper'; export default async function runTask(): Promise { + const start: number = Date.now(); + const subaccountZerosWithoutUsername: SubaccountsWithoutUsernamesResult[] = await SubaccountUsernamesTable.getSubaccountZerosWithoutUsernames( @@ -66,11 +68,19 @@ export default async function runTask(): Promise { (subaccount) => subaccount.address, ); + const duration = Date.now() - start; + logger.info({ at: 'subaccount-username-generator#runTask', message: 'Generated usernames', batchSize: subaccountZerosWithoutUsername.length, successCount, addressSample: subaccountAddresses.slice(0, 10), + duration, }); + + stats.timing( + `${config.SERVICE_NAME}.subaccount_username_generator`, + duration, + ); }