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

refactor(tree): block buffer #5879

Merged
merged 5 commits into from
Dec 29, 2023
Merged

refactor(tree): block buffer #5879

merged 5 commits into from
Dec 29, 2023

Conversation

rkrasiuk
Copy link
Member

@rkrasiuk rkrasiuk commented Dec 28, 2023

Description

This PR refactors the internals of BlockBuffer, improves documentation, and fixes the bug that can potentially cause unbounded memory growth.

BlockBuffer internals refactor:

  • buffered blocks collection representation changed from BTreeMap<BlockNumber, HashMap<BlockHash, SealedBlockWithSenders>> to HashMap<BlockHash, SealedBlockWithSenders>. this reduces the complexity of accessing blocks within a buffer as well as the need for hash_to_num mapping
  • the change of representation also alleviates the need for accessing everything by block hash and block number. now any block can be accessed by its hash
  • the previous requirement of sorted blocks by number is now handled by a separate earliest_blocks collection

Bug

Unbounded memory growth

Upon removing blocks from the buffer, it did not clean up the parent hash relationship properly.
The bug can be reproduced by running cargo test -p reth-blockchain-tree --lib -- block_buffer::tests --nocapture on test/block-buffer-lens branch.

Testing

This PR also improves test coverage of the buffer by having more explicit assertions for its inner collections.

@rkrasiuk rkrasiuk added C-bug An unexpected or incorrect behavior C-debt Refactor of code section that is hard to understand or maintain A-blockchain-tree Related to sidechains, reorgs and pending blocks labels Dec 28, 2023
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

nice find,

a few questions

num::NonZeroUsize,
};

use crate::metrics::BlockBufferMetrics;
/// Type that contains blocks by number and hash.
pub type BufferedBlocks = BTreeMap<BlockNumber, HashMap<BlockHash, SealedBlockWithSenders>>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

god I hate type aliases for maps like this

crates/blockchain-tree/src/block_buffer.rs Outdated Show resolved Hide resolved
pub(crate) hash_to_num: HashMap<BlockHash, BlockNumber>,
/// All blocks in the buffer stored by their block hash.
pub(crate) blocks: HashMap<BlockHash, SealedBlockWithSenders>,
/// Map of any parent block hash (even the ones not currently in the buffer)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why would we track block hashes of blocks that are not in the buffer?

Copy link
Member Author

@rkrasiuk rkrasiuk Dec 28, 2023

Choose a reason for hiding this comment

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

because we want the ability to look up buffered blocks by their parent. e.g. block comes in, we can insert it into tree and we want to check buffer for any children blocks that can now be added to the tree as well

crates/blockchain-tree/src/block_buffer.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

imo this is now more readable and has better test coverage

last doc error

@rkrasiuk
Copy link
Member Author

pending @rakita

Copy link
Collaborator

@rakita rakita left a comment

Choose a reason for hiding this comment

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

lgtm!

@rkrasiuk rkrasiuk added this pull request to the merge queue Dec 29, 2023
Merged via the queue into main with commit 21c7e77 Dec 29, 2023
27 checks passed
@rkrasiuk rkrasiuk deleted the rkrasiuk/refactor-block-buffer branch December 29, 2023 14:23
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 C-bug An unexpected or incorrect behavior C-debt Refactor of code section that is hard to understand or maintain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants