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: Update Relayer to use module level MockDb #576

Merged
merged 11 commits into from
Aug 24, 2022

Conversation

bvrooman
Copy link
Contributor

@bvrooman bvrooman commented Aug 23, 2022

Related issues:

This PR removes the Relayer tests' dependency on the existing DummyDb that comes pre-seeded, and uses a module level database, MockDb instead. MockDb does not come seeded with any data, and tests are expected to perform explicit setup. This means tests become more descriptive, self-documenting, and stable by removing sensitivity to data changes across shared test configurations.

The Relayer module is also the last area where the DummyDb is used. Once this PR is merged, we can also remove the DummyDb entirely, allowing us to move closer towards a convention of explicit test setup.

@bvrooman bvrooman marked this pull request as ready for review August 24, 2022 09:30
@bvrooman bvrooman changed the title feat: Update Relayer to use mock db feat: Update Relayer to use module level MockDb Aug 24, 2022
@bvrooman bvrooman requested review from Dentosal, a user and xgreenx August 24, 2022 09:38
blocks.handle_block_commit(b1, 2u64.into(), 2, IsReverted::False);
blocks.handle_block_commit(b2, 3u64.into(), 3, IsReverted::False);
blocks.handle_block_commit(b2, 3u64.into(), 3, IsReverted::True);

db.tap_sealed_blocks_mut(|sealed_blocks| {
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make sense to have a helper test function that takes N amount of blocks and creates them instead of repating this process in multiple tests?

Copy link
Contributor Author

@bvrooman bvrooman Aug 24, 2022

Choose a reason for hiding this comment

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

Personally, I prefer having the test setup localized to the test itself, even if it means repeated code. Extra helper functions make setup less localized, and may obfuscate test setup (even if it's just by a little bit). The spirit of this PR is to be explicit in the test setup and to increase code locality (by removing the DummyDb and its implicit setup) and I think that such an approach would be counter to that.

I understand the suggestion, but personally I would like to avoid it if I can!

Edit: That isn't to say that helper functions do not have a place. I like to use them in ways that simplify setup without reducing locality. For example, I may use helper functions to create instances of test data, but I prefer to mutate the database within the test itself.

where
F: Fn(&mut HashMap<BlockHeight, Arc<SealedFuelBlock>>) -> R,
{
func(&mut self.data.lock().unwrap().sealed_blocks)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd usually point too many unwraps but since this is a MockDb, and unwraps are only assuming the mutex is not poisoned it should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's not the most "proper" approach to data access, but we can make that assumption in tests. I do not think MockDb will be a production-ready replacement for RocksDB anytime soon, unfortunately (fortunately?).

Copy link
Member

Choose a reason for hiding this comment

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

Within the impl of the mock db itself, using lock()/unwrap() internally is fine. However, I think these tests will likely need to be re-written / redesigned since they are making assertions directly on the internals of the mockdb hashmaps instead of interacting with the relayerdb interface. I think it's fine to defer fixing these tests to a broader relayer cleanup initiative though.

@bvrooman bvrooman merged commit dc3451a into master Aug 24, 2022
@bvrooman bvrooman deleted the bvrooman/feat-relayer-mock-db branch August 24, 2022 10:51
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