Skip to content

Commit be9f170

Browse files
feat: add cleanTokens to new/wallet (#648)
* feat: add cleanTokens to new/wallet * test: add cleanTokens test to handleStop * chore: increase timeout
1 parent 07e7cbe commit be9f170

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

__tests__/storage/storage.test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ describe('handleStop', () => {
3636
it('should work with memory store', async () => {
3737
const store = new MemoryStore();
3838
await handleStopTest(store);
39-
}, 10000);
39+
}, 20000);
4040

4141
it('should work with leveldb store', async () => {
4242
const walletId = walletUtils.getWalletIdFromXPub(accessData.xpubkey);
4343
const store = new LevelDBStore(walletId, DATA_DIR);
4444
await handleStopTest(store);
45-
}, 10000);
45+
}, 20000);
4646

4747
/**
4848
* @param {IStore} store
@@ -81,6 +81,7 @@ describe('handleStop', () => {
8181
// Nothing changed in the store
8282
await expect(store.historyCount()).resolves.toEqual(1);
8383
await expect(store.addressCount()).resolves.toEqual(20);
84+
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
8485
tokens = await toArray(storage.getRegisteredTokens());
8586
expect(tokens).toHaveLength(1);
8687
expect(tokens[0]).toEqual(testToken);
@@ -90,6 +91,7 @@ describe('handleStop', () => {
9091
// Will clean the history bit not addresses or registered tokens
9192
await expect(store.historyCount()).resolves.toEqual(0);
9293
await expect(store.addressCount()).resolves.toEqual(20);
94+
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
9395
tokens = await toArray(storage.getRegisteredTokens());
9496
expect(tokens).toHaveLength(1);
9597
expect(tokens[0]).toEqual(testToken);
@@ -106,10 +108,19 @@ describe('handleStop', () => {
106108
// Will clean the history bit not addresses
107109
await expect(store.historyCount()).resolves.toEqual(1);
108110
await expect(store.addressCount()).resolves.toEqual(0);
111+
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
109112
tokens = await toArray(storage.getRegisteredTokens());
110113
expect(tokens).toHaveLength(1);
111114
expect(tokens[0]).toEqual(testToken);
112115

116+
// handleStop with cleanAddresses = true
117+
await loadAddresses(0, 20, storage);
118+
await storage.handleStop({ cleanTokens: true });
119+
// Will clean the history bit not addresses
120+
await expect(store.historyCount()).resolves.toEqual(1);
121+
await expect(store.addressCount()).resolves.toEqual(20);
122+
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeFalsy();
123+
113124
// Access data is untouched when stopping the wallet
114125
// XXX: since we stringify to save on store, the optional undefined properties are removed
115126
// Since they are optional and unset, we can safely remove them from the expected value

src/new/wallet.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1450,11 +1450,11 @@ class HathorWallet extends EventEmitter {
14501450
/**
14511451
* Close the connections and stop emitting events.
14521452
*/
1453-
async stop({ cleanStorage = true, cleanAddresses = false } = {}) {
1453+
async stop({ cleanStorage = true, cleanAddresses = false, cleanTokens = false } = {}) {
14541454
this.setState(HathorWallet.CLOSED);
14551455
this.removeAllListeners();
14561456

1457-
await this.storage.handleStop({connection: this.conn, cleanStorage, cleanAddresses});
1457+
await this.storage.handleStop({connection: this.conn, cleanStorage, cleanAddresses, cleanTokens});
14581458

14591459
this.firstConnection = true;
14601460
this.walletStopped = true;

src/storage/storage.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -814,19 +814,24 @@ export class Storage implements IStorage {
814814

815815
/**
816816
* Handle storage operations for a wallet being stopped.
817-
* @param {{connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean}} Options to handle stop
817+
* @param {{
818+
* connection?: FullNodeConnection;
819+
* cleanStorage?: boolean;
820+
* cleanAddresses?: boolean;
821+
* cleanTokens?: boolean;
822+
* }} Options to handle stop
818823
* @returns {Promise<void>}
819824
*/
820-
async handleStop({connection, cleanStorage = false, cleanAddresses = false}: {connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean} = {}): Promise<void> {
825+
async handleStop({connection, cleanStorage = false, cleanAddresses = false, cleanTokens = false}: {connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean, cleanTokens?: boolean} = {}): Promise<void> {
821826
if (connection) {
822827
for await (const addressInfo of this.getAllAddresses()) {
823828
connection.unsubscribeAddress(addressInfo.base58);
824829
}
825830
connection.removeMetricsHandlers();
826831
}
827832
this.version = null;
828-
if (cleanStorage || cleanAddresses) {
829-
await this.cleanStorage(cleanStorage, cleanAddresses);
833+
if (cleanStorage || cleanAddresses || cleanTokens) {
834+
await this.cleanStorage(cleanStorage, cleanAddresses, cleanTokens);
830835
}
831836
}
832837

0 commit comments

Comments
 (0)