Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use alphanumeric suffix for username #2573

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
}
Loading