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

node: main chain db txn management #2112

Merged
merged 16 commits into from
Jun 18, 2024
Merged

Conversation

JacekGlen
Copy link
Member

This PR prepares MainChain for silkworm-go integration with Erigon. Erigon requires transaction-per-request, contrary to Silkworm where we use a single transaction.

Another tiny change is that MainChain::close() commits the data rather than aborting it. The reason is that insert_blocks() in the long-standing transaction mode, only commits the data every 1k blocks. So if you call close() before, you could lose valid data.

@JacekGlen JacekGlen requested review from mriccobene and canepat June 17, 2024 21:25
@canepat canepat changed the title Main chain db txn management node: main chain db txn management Jun 18, 2024
@canepat canepat added the enhancement New feature or improvement label Jun 18, 2024
//! @brief Handles the transaction lifecycle for long-standing and per-request transactions
class TransactionHandler {
public:
TransactionHandler(silkworm::db::RWTxnManaged& txn, silkworm::db::RWAccess& db_access, bool keep_db_txn_open = true)
Copy link
Member

Choose a reason for hiding this comment

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

We typically use db::RWTxnManaged only when we need to ensure proper ownership of txn lifecycle (e.g. in stagedsync::MainChain which holds its own txn), otherwise it is sufficient to use just db::RWTxn, passed by reference.

This approach follows the same convention used by MDBX API, which contains different abstractions for managed classes (e.g. ::mdbx::env_managed, ::mdbx::txn_managed...) and unmanaged ones (e.g. ::mdbx::env, ::mdbx::txn...). The latter ones are handles wrapping up a raw pointer and have nice pass-by-value-and-move semantics.


private:
silkworm::db::RWTxnManaged& txn_;
silkworm::db::RWAccess& db_access_;
Copy link
Member

Choose a reason for hiding this comment

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

db::ROAccess and db::RWAccess are lightweight wrappers for ::mdbx::env used to enforce strict usage of read-only vs read-write transactions. ::mdbx::env is in turn just a handle wrapping a raw pointer, so all of them are a perfect fit for pass by-value and move.

@canepat canepat merged commit 6542d38 into master Jun 18, 2024
4 checks passed
@canepat canepat deleted the main-chain-db-txn-management branch June 18, 2024 08:15
@canepat canepat mentioned this pull request Jun 18, 2024
7 tasks
@canepat canepat added the execution at tip Block execution at the chain tip label Jun 18, 2024
lupin012 pushed a commit that referenced this pull request Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement execution at tip Block execution at the chain tip
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants