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

Bugfix: Race conditions between read and write into database #819

Merged
merged 7 commits into from
Dec 8, 2022

Conversation

xgreenx
Copy link
Collaborator

@xgreenx xgreenx commented Dec 7, 2022

Close #791.

This change would be nice to do after #812 with Transaction<Database> structure from fuel-core-storage. But while it is not merged, it is a variant of implementation without a known type. A workaround is to define a DatabaseTransaction<Database> trait and use the Box<dyn DatabaseTransaction<Database>> type instead.

The main idea of the change is to return a UncommittedResult from Executor and commit it after all updates of the Database at the end of the block production loop of PoACoordinator.

Reused patter of adapter in the `fuel-core`.
# Conflicts:
#	fuel-block-producer/src/block_producer.rs
#	fuel-block-producer/src/block_producer/tests.rs
#	fuel-block-producer/src/mocks.rs
#	fuel-core/src/executor.rs
#	fuel-core/src/schema/block.rs
#	fuel-core/src/schema/tx.rs
#	fuel-core/src/service/modules.rs
#	fuel-poa-coordinator/src/service.rs
#	fuel-poa-coordinator/tests/test_trigger.rs
Implemented with dynamic resolution approach for `DatabaseTransaction`.
@xgreenx xgreenx self-assigned this Dec 7, 2022
@xgreenx xgreenx requested a review from a team December 7, 2022 03:47
@xgreenx xgreenx marked this pull request as ready for review December 7, 2022 03:55
@xgreenx xgreenx added the bug Something isn't working label Dec 7, 2022

pub trait Executor<Database: ?Sized>: Sync + Send {
/// Executes the block and commits the result of the execution into the inner `Database`.
fn execute_and_commit(
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we wouldn't have definitions like this in our ports that aren't used at all by the current crate. Although we can deal with this during the refactor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are right. Let's keep only the required methods(I removed it)

@@ -54,6 +52,8 @@ use std::{

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

these new imports should probably be sorted in with other fuel_* imports

Copy link
Member

@Voxelot Voxelot left a comment

Choose a reason for hiding this comment

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

This is a good incremental step towards a fuel-core-storage crate, while also addressing our immediate issues with the block fetching race condition 👍🏻

Even though the architecture doc laid out a case against having globally shared traits, I think it's justifiable in certain circumstances with traits like Transactional. This is because it's very narrow in scope and doesn't bleed any irrelevant concerns across domains (e.g. changes to the TxPool db trait are still isolated from other domain crates).

@Voxelot
Copy link
Member

Voxelot commented Dec 8, 2022

Would be nice if we had a way to test that this solves the race condition, but a clean solution for doing so eludes me 🤔

@xgreenx xgreenx merged commit 5cb5a69 into master Dec 8, 2022
@xgreenx xgreenx deleted the bugfix/race-condition-seal-block branch December 8, 2022 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race conditions between read and write into database
2 participants