-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
There was a problem hiding this 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>>; |
There was a problem hiding this comment.
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
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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
pending @rakita |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
Description
This PR refactors the internals of
BlockBuffer
, improves documentation, and fixes the bug that can potentially cause unbounded memory growth.BlockBuffer
internals refactor:BTreeMap<BlockNumber, HashMap<BlockHash, SealedBlockWithSenders>>
toHashMap<BlockHash, SealedBlockWithSenders>
. this reduces the complexity of accessing blocks within a buffer as well as the need forhash_to_num
mappingearliest_blocks
collectionBug
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.