Skip to content

Commit

Permalink
🐛 Add Lock to prevent block frontier conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
anarkrypto committed Sep 20, 2023
1 parent 266e2bb commit 06d13bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/wallet/Locker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default class Locker {
isLocked = false;
private queue: ((value: unknown) => void)[] = [];

async acquire() {
if (!this.isLocked) {
this.isLocked = true;
return;
}

return new Promise(resolve => {
this.queue.push(resolve);
});
}

release() {
if (this.queue.length > 0) {
const nextResolve = this.queue.shift();
nextResolve && nextResolve(true);
} else {
this.isLocked = false;
}
}
}
15 changes: 15 additions & 0 deletions src/wallet/WalletController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import BaseController from '@/BaseController';
import { TunedBigNumber } from '@/utils';
import Logger from '@/logger/Logger';
import Locker from './Locker';

export interface NanoWalletConfig extends NanoRpcConfig {
privateKey: string;
Expand Down Expand Up @@ -77,6 +78,8 @@ export default class NanoWallet extends BaseController<

logger: Logger;

locker = new Locker();

constructor(config: NanoWalletConfig, state?: NanoWalletState | null) {
super(config, state || undefined);
this.publicKey = derivePublicKey(config.privateKey);
Expand Down Expand Up @@ -225,6 +228,7 @@ export default class NanoWallet extends BaseController<
}

async receive(link: string) {
await this.locker.acquire();
try {
link = link.toUpperCase();

Expand Down Expand Up @@ -289,10 +293,13 @@ export default class NanoWallet extends BaseController<
error instanceof Error ? error.message : error,
);
throw error;
} finally {
await this.locker.release();
}
}

async send(to: string, amount: string) {
await this.locker.acquire();
try {
if (this.state.frontier === null) {
throw new Error('No frontier');
Expand Down Expand Up @@ -340,10 +347,13 @@ export default class NanoWallet extends BaseController<
error instanceof Error ? error.message : error,
);
throw error;
} finally {
this.locker.release();
}
}

async sweep(to: string) {
await this.locker.acquire();
try {
if (this.state.frontier === null) {
throw new Error('No frontier');
Expand Down Expand Up @@ -382,10 +392,13 @@ export default class NanoWallet extends BaseController<
error instanceof Error ? error.message : error,
);
throw error;
} finally {
this.locker.release();
}
}

async setRepresentative(account?: string) {
await this.locker.acquire();
try {
if (this.state.frontier === null) {
throw new Error('No frontier');
Expand Down Expand Up @@ -431,6 +444,8 @@ export default class NanoWallet extends BaseController<
error instanceof Error ? error.message : error,
);
throw error;
} finally {
this.locker.release();
}
}

Expand Down

0 comments on commit 06d13bd

Please sign in to comment.