Skip to content

Commit

Permalink
Change to remove temporary accounts immediately after use
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Jun 14, 2024
1 parent f33a041 commit 150c782
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/relay/src/routers/LedgerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class LedgerRouter {
try {
let account: string = String(req.params.account).trim();
if (ContractUtils.isTemporaryAccount(account)) {
const realAccount = await this.storage.getRealAccount(account);
const realAccount = await this.storage.getRealAccountOnTemporary(account);
if (realAccount === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2004"));
} else {
Expand Down
14 changes: 11 additions & 3 deletions packages/relay/src/routers/PaymentRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class PaymentRouter {
if (!ContractUtils.verifyMessage(account, message, signature)) {
return res.status(200).json(ResponseMessage.getErrorMessage("1501"));
}
const temporaryAccount = await this.storage.getTemporaryAccount(account);
const temporaryAccount = await this.storage.getAccountOnTemporary(account);
this.metrics.add("success", 1);
return res.status(200).json(
this.makeResponseData(0, {
Expand Down Expand Up @@ -242,7 +242,7 @@ export class PaymentRouter {
try {
let account: string = String(req.query.account).trim();
if (ContractUtils.isTemporaryAccount(account)) {
const realAccount = await this.storage.getRealAccount(account);
const realAccount = await this.storage.getRealAccountOnTemporary(account);
if (realAccount === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2004"));
} else {
Expand Down Expand Up @@ -320,11 +320,13 @@ export class PaymentRouter {
}

let account: string = String(req.body.account).trim();
let temporaryAccount: string = "";
if (ContractUtils.isTemporaryAccount(account)) {
const realAccount = await this.storage.getRealAccount(account);
const realAccount = await this.storage.getRealAccountOnTemporary(account);
if (realAccount === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2004"));
} else {
temporaryAccount = account;
account = realAccount;
}
}
Expand Down Expand Up @@ -432,6 +434,12 @@ export class PaymentRouter {
await this._sender.send(to, title, contents.join(", "), data);
}

try {
if (temporaryAccount !== "") await this.storage.removeAccountTemporary(temporaryAccount);
} catch (_) {
//
}

this.metrics.add("success", 1);
return res.status(200).json(
this.makeResponseData(0, {
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/src/scheduler/CloseScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,6 @@ export class CloseScheduler extends Scheduler {
}

private async onRemoveExpiredAccount() {
await this.storage.removeExpiredAccount();
await this.storage.removeExpiredAccountOnTemporary();
}
}
24 changes: 17 additions & 7 deletions packages/relay/src/storage/RelayStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,19 +840,17 @@ export class RelayStorage extends Storage {
}
/// endregion

public async getTemporaryAccount(account: string): Promise<string> {
public async getAccountOnTemporary(account: string): Promise<string> {
let temporary_account: string;
while (true) {
temporary_account = ContractUtils.getTemporaryAccount();
if ((await this.getRealAccount(temporary_account)) === undefined) break;
if ((await this.getRealAccountOnTemporary(temporary_account)) === undefined) break;
}

const timestamp = ContractUtils.getTimeStampBigInt();
return new Promise<string>(async (resolve, reject) => {
this.queryForMapper("temporary_accounts", "postAccount", {
account: account.toLowerCase(),
temporary_account: temporary_account.toLowerCase(),
timestamp: timestamp.toString(),
})
.then(() => {
return resolve(temporary_account);
Expand All @@ -864,11 +862,10 @@ export class RelayStorage extends Storage {
});
}

public getRealAccount(temporary_account: string): Promise<string | undefined> {
public getRealAccountOnTemporary(temporary_account: string): Promise<string | undefined> {
return new Promise<string | undefined>(async (resolve, reject) => {
this.queryForMapper("temporary_accounts", "getRealAccount", {
temporary_account,
timestamp: (ContractUtils.getTimeStampBigInt() - BigInt(60)).toString(),
})
.then((result) => {
if (result.rows.length > 0) {
Expand All @@ -884,7 +881,7 @@ export class RelayStorage extends Storage {
});
}

public removeExpiredAccount(): Promise<void> {
public removeExpiredAccountOnTemporary(): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
this.queryForMapper("temporary_accounts", "removeExpiredAccount", {})
.then((result) => {
Expand All @@ -897,6 +894,19 @@ export class RelayStorage extends Storage {
});
}

public removeAccountTemporary(temporary_account: string): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
this.queryForMapper("temporary_accounts", "removeAccount", { temporary_account })
.then((result) => {
return resolve();
})
.catch((reason) => {
if (reason instanceof Error) return reject(reason);
return reject(new Error(reason));
});
});
}

// region Delegator
public async createDelegator(account: string, key: string): Promise<string> {
const wallet = hre.ethers.Wallet.createRandom();
Expand Down
4 changes: 4 additions & 0 deletions packages/relay/src/storage/mapper/temporary_accounts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
<select id="removeExpiredAccount">
DELETE FROM temporary_accounts WHERE EXTRACT(EPOCH FROM (now() - "timestamp")) <![CDATA[>]]> 300;
</select>

<select id="removeAccount">
DELETE FROM temporary_accounts WHERE LOWER("temporary_account") = LOWER(#{temporary_account});
</select>
</mapper>

0 comments on commit 150c782

Please sign in to comment.