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

BlockInterval support for hash ranges #728

Merged
merged 27 commits into from
Oct 28, 2024

Conversation

sergerad
Copy link
Collaborator

@sergerad sergerad commented Oct 16, 2024

Closes #287.

Hash-based Block Intervals

Update scripts, CLI, and leader stack to allow block ranges to be specified by block hashes, not just block numbers.

Changes

Refactored BlockInterval::new() to allow for the simplest support for block hash ranges in the runtime and associated scripts.

Changed BlockInterval to only contain u64 ranges (invariant of valid numerical range).

Update CLI to provide flags: --start-block [num|hash] and --end-block [num|hash]

@github-actions github-actions bot added crate: zero_bin Anything related to the zero-bin subcrates. ci labels Oct 16, 2024
@sergerad
Copy link
Collaborator Author

@atanmarko I have reimplemented the existing unit tests with mocks and added new tests for hash-based inputs.

The functionality is all in place, just need to clean things up. But I'll hold off for now until you get a chance to do a review pass. Thanks!

.github/workflows/jerigon-zero.yml Show resolved Hide resolved
scripts/prove_rpc.sh Outdated Show resolved Hide resolved
scripts/prove_rpc.sh Outdated Show resolved Hide resolved
scripts/prove_rpc.sh Show resolved Hide resolved
scripts/prove_rpc.sh Show resolved Hide resolved
zero/src/block_interval.rs Outdated Show resolved Hide resolved
zero/src/block_interval.rs Outdated Show resolved Hide resolved
zero/src/provider.rs Outdated Show resolved Hide resolved
zero/src/provider.rs Outdated Show resolved Hide resolved
zero/src/provider.rs Show resolved Hide resolved
@sergerad sergerad marked this pull request as ready for review October 22, 2024 23:07
@sergerad sergerad changed the title WIP: BlockInterval support for hash ranges BlockInterval support for hash ranges Oct 23, 2024
scripts/prove_rpc.sh Outdated Show resolved Hide resolved
scripts/prove_rpc.sh Show resolved Hide resolved
scripts/prove_rpc.sh Show resolved Hide resolved
zero/src/provider.rs Outdated Show resolved Hide resolved
zero/src/provider.rs Outdated Show resolved Hide resolved
/// end_block is always treated as inclusive because it may have been
/// specified as a block hash.
pub async fn new(
provider: Arc<impl ZeroBlockProvider>,
Copy link
Contributor

Choose a reason for hiding this comment

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

smell: doesn't feel right to need an Arc here

Copy link
Member

@atanmarko atanmarko Oct 23, 2024

Choose a reason for hiding this comment

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

No smell here. We use one provider instance in the whole program, we pass it from the top level using Arc.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a case of needless pass by value

  • ZeroBlockProvider only has &self receivers
  • So the function only needs [&] impl ZeroBlockProvider (note that alloy_provider::Provider already does the impl<T: Provider> Provider for &T { .. }

The function should require as little from its callers as possible

Copy link
Member

Choose a reason for hiding this comment

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

@0xaatif In the leader we have couple of threads using provider. We only instantiate one instance at the start of the program and pass it from the top and multiple async tasks use this provider in parallel. I agree that in this particular case (in particular where the block interval is created in the leader) pass by value (or with Arc smart pointer) is not needed, but it was easier and cleaner to by convention maintain the same Provider argument through the whole application than to create Provider, pass it by reference and then wrap it in Arc to pass it further to other tasks. Cloning the Arc is just increasing the reference counter so it is not really an overhead.

Copy link
Member

@atanmarko atanmarko Oct 24, 2024

Choose a reason for hiding this comment

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

P.S. Earlier, Provider was indeed created and handled as an common reference. But during refactors it often happened that functions were called from other tasks, so the only way they could get the provider is through Arc pointer. Hence the Arc convention was born.

/// assert_eq!(BlockInterval::new("32141").unwrap(), BlockInterval::SingleBlockId(BlockId::Number(32141.into())));
/// assert_eq!(BlockInterval::new("100..").unwrap(), BlockInterval::FollowFrom{start_block: 100});
/// ```
pub fn new(s: &str) -> anyhow::Result<BlockInterval> {
Copy link
Contributor

Choose a reason for hiding this comment

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

we should close #284 if we're dropping all the parsing :)

@0xPolygonZero 0xPolygonZero deleted a comment from aatifsyed Oct 24, 2024
use crate::parsing;
use crate::provider::CachedProvider;
#[cfg_attr(test, automock)]
pub trait BlockIntervalProvider {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@0xaatif note that auto_impl(&, Box) caused conflicting implementations to impl<T: Provider> BlockIntervalProvider for T { so that its why its not here. My understanding is that the impl block below would cover refs and Boxes. LMK if I'm mistaken

@sergerad sergerad requested a review from 0xaatif October 24, 2024 20:05
Copy link
Contributor

@0xaatif 0xaatif left a comment

Choose a reason for hiding this comment

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

lgtm

fn latest_block_number(&self) -> impl Future<Output = anyhow::Result<u64>> + Send;
}

impl<T: Transport + Clone, P: Provider<T>> BlockIntervalProvider<T> for P {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@0xaatif apologies the approach was actually more involved than I had realized - the previous commit was not compiling, my IDE must have been seriously lagging for me to not notice before pushing up.

This does appear to work for our purposes. The problem is that it causes a conflict with the mocked type. I don't know how to get around that conflict yet unfortunately. LMK if you have any ideas.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pushed another commit that manages the conflict

zero/src/block_interval.rs Show resolved Hide resolved
zero/src/rpc/mod.rs Outdated Show resolved Hide resolved
@atanmarko atanmarko merged commit 1e1e762 into 0xPolygonZero:develop Oct 28, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci crate: zero_bin Anything related to the zero-bin subcrates.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add more BlockInterval support for hashes
4 participants