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: persistent storage #462

Merged
merged 26 commits into from
Mar 17, 2023
Merged

Conversation

r4mmer
Copy link
Member

@r4mmer r4mmer commented Jan 24, 2023

Acceptance Criteria

  • Implement a store of data using LevelDB
  • Implement reverse search indexes to comply with the IStore interface
  • Deprecate node v8 and v10

Security Checklist

  • Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.

@r4mmer r4mmer self-assigned this Jan 24, 2023
@r4mmer r4mmer changed the base branch from dev to feat/new-storage-scheme January 24, 2023 01:12
@r4mmer r4mmer force-pushed the feat/persistent-storage branch from 2b0f792 to 378368e Compare March 3, 2023 16:56
@r4mmer r4mmer requested a review from andreabadesso March 3, 2023 16:56
@codecov
Copy link

codecov bot commented Mar 6, 2023

Codecov Report

Merging #462 (49f4f4a) into feat/new-storage-scheme (9a7ddd5) will increase coverage by 0.86%.
The diff coverage is 64.76%.

❗ Current head 49f4f4a differs from pull request most recent head 9e72320. Consider uploading reports for the commit 9e72320 to get more accurate results

@@                     Coverage Diff                     @@
##           feat/new-storage-scheme     #462      +/-   ##
===========================================================
+ Coverage                    52.04%   52.90%   +0.86%     
===========================================================
  Files                           57       64       +7     
  Lines                         4206     4682     +476     
  Branches                       868      934      +66     
===========================================================
+ Hits                          2189     2477     +288     
- Misses                        1923     2111     +188     
  Partials                        94       94              
Impacted Files Coverage Δ
src/api/metadataApi.ts 0.00% <0.00%> (ø)
src/new/wallet.js 27.32% <0.00%> (-0.05%) ⬇️
src/storage/index.ts 100.00% <ø> (ø)
src/types.ts 33.33% <ø> (ø)
src/utils/tokens.ts 26.36% <40.00%> (-2.47%) ⬇️
src/storage/leveldb/utxo_index.ts 48.14% <48.14%> (ø)
src/storage/leveldb/errors.ts 50.00% <50.00%> (ø)
src/storage/leveldb/history_index.ts 56.25% <56.25%> (ø)
src/storage/leveldb/token_index.ts 57.14% <57.14%> (ø)
src/storage/leveldb/address_index.ts 57.30% <57.30%> (ø)
... and 4 more

... and 2 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@@ -4,6 +4,7 @@ on:
branches:
- dev
- master
- feat/new-storage-scheme
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to make the CI run on the PR, it will be removed before being merged.

@@ -10,13 +10,14 @@ on:
branches:
- dev
- master
- feat/new-storage-scheme
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to make the CI run on the PR, it will be removed before being merged.

}
}

describe("LevelDB persistent store", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use single quotes

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}
// The order should follow the index ordering
expect(values).toStrictEqual(['b', 'a', 'c']);

Copy link
Contributor

Choose a reason for hiding this comment

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

Extra blankline

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed

index.js Outdated
@@ -9,7 +9,9 @@ const txMiningApi = require('./lib/api/txMining');
const versionApi = require('./lib/api/version');
const axios = require('./lib/api/axiosInstance');
const metadataApi = require('./lib/api/metadataApi');
const storage = require('./lib/storage');
const storage = require('./lib/storage/storage');
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const storage = require('./lib/storage/storage');
const { Storage } = require('./lib/storage/storage');

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

index.js Outdated
@@ -57,7 +59,9 @@ module.exports = {
ErrorMessages: ErrorMessages,
constants,
axios,
storage: storage.default,
Storage: storage.Storage,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Storage: storage.Storage,
Storage,

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

setupTests.js Outdated
@@ -72,4 +72,9 @@ expect.extend({
});


global.window = {};
// global.window = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

Copy link
Member Author

Choose a reason for hiding this comment

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

removed


try {
await this.tsHistoryDB.get(`${value.timestamp}:${value.tx_id}`);
} catch(err: unknown) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} catch(err: unknown) {
} catch (err: unknown) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Comment on lines 54 to 59
if (process.version < 'v14.14.0') {
// rmSync was introduced in v14.14.0, earlier versions should use rmdirSync
// https://nodejs.org/docs/latest-v14.x/api/fs.html#fs_fs_rmdirsync_path_options
fs.rmdirSync(this.dbpath, { recursive: true });
} else {
fs.rmSync(this.dbpath, { recursive: true, force: true });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This method already returns a promise, why do we need to use the sync methods?

This will only cause clients using our lib and calling destroy() to be slower as it will block the event loop

Copy link
Member Author

Choose a reason for hiding this comment

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

Started using async version or rm


async saveAddress(info: IAddressInfo): Promise<void> {
if (!info.base58) {
throw new Error('Invalid address');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have standard errors like the ones in errors.ts so they can be better handled?

e.g.

class InvalidAddressError extends Error

Copy link
Member Author

Choose a reason for hiding this comment

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

I also believe we should create a standard error, but since it would affect both the persistent storage files and the base storage scheme implementation I think we should make this on its own PR, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, can you create an issue so we don't forget to refactor this?

@r4mmer r4mmer force-pushed the feat/persistent-storage branch from 00a8d5d to 4fcf868 Compare March 10, 2023 14:31
@r4mmer r4mmer force-pushed the feat/persistent-storage branch from 920957d to 66121af Compare March 15, 2023 15:28
@r4mmer r4mmer requested a review from pedroferreira1 March 16, 2023 17:47
}

/**
* Run a full count of the txs in the database.
* Since we have made a full count we can update the tx count in the database.
Copy link
Member

Choose a reason for hiding this comment

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

You could also remove this line in the address index file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed from the other file

@r4mmer r4mmer merged commit b72e950 into feat/new-storage-scheme Mar 17, 2023
@r4mmer r4mmer deleted the feat/persistent-storage branch March 17, 2023 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants