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

feat: add cleanTokens to new/wallet #648

Merged
merged 3 commits into from
May 16, 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
15 changes: 13 additions & 2 deletions __tests__/storage/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ describe('handleStop', () => {
it('should work with memory store', async () => {
const store = new MemoryStore();
await handleStopTest(store);
}, 10000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have had to increase the timeout to make the test pass on the CI process.

}, 20000);

it('should work with leveldb store', async () => {
const walletId = walletUtils.getWalletIdFromXPub(accessData.xpubkey);
const store = new LevelDBStore(walletId, DATA_DIR);
await handleStopTest(store);
}, 10000);
}, 20000);

/**
* @param {IStore} store
Expand Down Expand Up @@ -81,6 +81,7 @@ describe('handleStop', () => {
// Nothing changed in the store
await expect(store.historyCount()).resolves.toEqual(1);
await expect(store.addressCount()).resolves.toEqual(20);
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
tokens = await toArray(storage.getRegisteredTokens());
expect(tokens).toHaveLength(1);
expect(tokens[0]).toEqual(testToken);
Expand All @@ -90,6 +91,7 @@ describe('handleStop', () => {
// Will clean the history bit not addresses or registered tokens
await expect(store.historyCount()).resolves.toEqual(0);
await expect(store.addressCount()).resolves.toEqual(20);
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
tokens = await toArray(storage.getRegisteredTokens());
expect(tokens).toHaveLength(1);
expect(tokens[0]).toEqual(testToken);
Expand All @@ -106,10 +108,19 @@ describe('handleStop', () => {
// Will clean the history bit not addresses
await expect(store.historyCount()).resolves.toEqual(1);
await expect(store.addressCount()).resolves.toEqual(0);
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeTruthy();
tokens = await toArray(storage.getRegisteredTokens());
expect(tokens).toHaveLength(1);
expect(tokens[0]).toEqual(testToken);

// handleStop with cleanAddresses = true
await loadAddresses(0, 20, storage);
await storage.handleStop({ cleanTokens: true });
// Will clean the history bit not addresses
await expect(store.historyCount()).resolves.toEqual(1);
await expect(store.addressCount()).resolves.toEqual(20);
await expect(store.isTokenRegistered(testToken.uid)).resolves.toBeFalsy();

// Access data is untouched when stopping the wallet
// XXX: since we stringify to save on store, the optional undefined properties are removed
// Since they are optional and unset, we can safely remove them from the expected value
Expand Down
4 changes: 2 additions & 2 deletions src/new/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,11 +1450,11 @@
/**
* Close the connections and stop emitting events.
*/
async stop({ cleanStorage = true, cleanAddresses = false } = {}) {
async stop({ cleanStorage = true, cleanAddresses = false, cleanTokens = false } = {}) {
this.setState(HathorWallet.CLOSED);
this.removeAllListeners();

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

Check warning on line 1457 in src/new/wallet.js

View check run for this annotation

Codecov / codecov/patch

src/new/wallet.js#L1457

Added line #L1457 was not covered by tests

this.firstConnection = true;
this.walletStopped = true;
Expand Down
13 changes: 9 additions & 4 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,19 +814,24 @@ export class Storage implements IStorage {

/**
* Handle storage operations for a wallet being stopped.
* @param {{connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean}} Options to handle stop
* @param {{
* connection?: FullNodeConnection;
* cleanStorage?: boolean;
* cleanAddresses?: boolean;
* cleanTokens?: boolean;
* }} Options to handle stop
* @returns {Promise<void>}
*/
async handleStop({connection, cleanStorage = false, cleanAddresses = false}: {connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean} = {}): Promise<void> {
async handleStop({connection, cleanStorage = false, cleanAddresses = false, cleanTokens = false}: {connection?: FullNodeConnection, cleanStorage?: boolean, cleanAddresses?: boolean, cleanTokens?: boolean} = {}): Promise<void> {
if (connection) {
for await (const addressInfo of this.getAllAddresses()) {
connection.unsubscribeAddress(addressInfo.base58);
}
connection.removeMetricsHandlers();
}
this.version = null;
if (cleanStorage || cleanAddresses) {
await this.cleanStorage(cleanStorage, cleanAddresses);
if (cleanStorage || cleanAddresses || cleanTokens) {
await this.cleanStorage(cleanStorage, cleanAddresses, cleanTokens);
}
}

Expand Down
Loading