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

fix: use in memory state for state_by_hash #10105

Merged
merged 3 commits into from
Aug 5, 2024
Merged

Conversation

Rjected
Copy link
Member

@Rjected Rjected commented Aug 5, 2024

Previously we would just go to the database for state, when the state for that block may only exist in memory. This caused payload building to fail in hive.

Now, we check for state in the following order:

  1. Database
  2. Canonical in-memory state
  3. Pending block

Otherwise, an error will be returned.

Previously we would just go to the database for state, when the state
for that block may only exist in memory. This caused payload building to
fail in hive.

Now, we check for state in the following order:

 1. Database
 2. Canonical in-memory state
 3. Pending block

Otherwise, an error will be returned.
@Rjected Rjected added C-bug An unexpected or incorrect behavior A-consensus Related to the consensus engine A-db Related to the database A-blockchain-tree Related to sidechains, reorgs and pending blocks labels Aug 5, 2024
Comment on lines 903 to 908
// ... or this could be tracked by the in memory state
let last_persisted_block_number = self.database.last_block_number()?;
let latest_historical =
self.database.history_by_block_number(last_persisted_block_number)?;
let state_provider =
self.canonical_in_memory_state.state_provider(hash, latest_historical);
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is susceptible to a race condition,

The BlockState has a list of all in memory parents and the on disk anchor can be obtained via:

pub fn anchor(&self) -> BlockNumHash {

so we need to use this here to find the proper disk number

Copy link
Member Author

Choose a reason for hiding this comment

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

ah yeah, the anchor makes sense here

@Rjected Rjected force-pushed the dan/add-proper-state-by-hash branch from 65c3747 to ec7639c Compare August 5, 2024 22:07
Comment on lines +902 to +908
} else if let Some(state) = self.canonical_in_memory_state.state_by_hash(hash) {
// ... or this could be tracked by the in memory state
let anchor_hash = state.anchor().hash;
let latest_historical = self.database.history_by_block_hash(anchor_hash)?;
let state_provider =
self.canonical_in_memory_state.state_provider(hash, latest_historical);
Ok(Box::new(state_provider))
Copy link
Collaborator

@mattsse mattsse Aug 5, 2024

Choose a reason for hiding this comment

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

I'm pretty sure this can be improved with a helper closure that loads the anchor state and avoid the second lookup, but we can track that separately

@Rjected Rjected added this pull request to the merge queue Aug 5, 2024
Merged via the queue into main with commit 40f44b7 Aug 5, 2024
32 checks passed
@Rjected Rjected deleted the dan/add-proper-state-by-hash branch August 5, 2024 22:33
martinezjorge pushed a commit to martinezjorge/reth that referenced this pull request Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-blockchain-tree Related to sidechains, reorgs and pending blocks A-consensus Related to the consensus engine A-db Related to the database C-bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants