Skip to content

Commit

Permalink
Use alphanumeric suffix for username (backport #2573) (#2574)
Browse files Browse the repository at this point in the history
Co-authored-by: Teddy Ding <[email protected]>
  • Loading branch information
mergify[bot] and teddyding authored Nov 15, 2024
1 parent 033d5c2 commit 5308f8d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
4 changes: 2 additions & 2 deletions indexer/services/roundtable/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
};
Expand Down
9 changes: 6 additions & 3 deletions indexer/services/roundtable/src/helpers/usernames-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import config from '../config';
import { generateUsernameForSubaccount } from '../helpers/usernames-helper';

export default async function runTask(): Promise<void> {
const start: number = Date.now();

const subaccountZerosWithoutUsername:
SubaccountsWithoutUsernamesResult[] = await
SubaccountUsernamesTable.getSubaccountZerosWithoutUsernames(
Expand Down Expand Up @@ -66,11 +68,19 @@ export default async function runTask(): Promise<void> {
(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,
);
}

0 comments on commit 5308f8d

Please sign in to comment.