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

Sequencer builder #367

Merged
merged 16 commits into from
Jun 14, 2022
Merged

Sequencer builder #367

merged 16 commits into from
Jun 14, 2022

Conversation

Mirko-von-Leipzig
Copy link
Contributor

@Mirko-von-Leipzig Mirko-von-Leipzig commented Jun 10, 2022

PR with my attempt at making a request builder for the sequencer queries. It uses the typestate pattern.

Current usage:

    let block: Starknetlock = self
        .request()    
        .feeder_gateway()
        .get_block()
        .at_block(block_number)
        .with_retry(Retry::Enabled)
        .get()
        .await
        .unwrap();

Still todo:

  • docs
  • move parsing to builder (?)
  • move retry to builder (?)

Also somewhat strangely, the tests for the transaction write's fail when executed locally, but pass when using the sequencer. I suspect it has something to do with the test setup; but I haven't figured out what / how to fix it.

This is due to the warp test server requiring query params be set even when there aren't any. Our previous request implementation always set these params to [] whereas this new one never sets them if there aren't any (None).

I've also collapsed the block_by_hash and block_by_number into a single call: block(block_id) where BlockId can represent Hash, Number, Latest and Pending.

@Mirko-von-Leipzig Mirko-von-Leipzig force-pushed the sequencer_builder branch 2 times, most recently from aee9900 to 0546f88 Compare June 10, 2022 13:54
I've done my best to keep the tests aligned, but maybe they need another
pass.
Would not work with mockall. Somewhat worse API now.

Would like to revisit once we remove mockall.
@Mirko-von-Leipzig Mirko-von-Leipzig marked this pull request as ready for review June 13, 2022 09:13
Copy link
Contributor Author

@Mirko-von-Leipzig Mirko-von-Leipzig left a comment

Choose a reason for hiding this comment

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

I've left a lot of the one-liner functions undocumented as the function name should be enough in those cases.

It would also have been nicer to link to the actual Request<stage> impls directly, but unfortunately rustdoc does not support that.

&self,
block_hash: BlockHashOrTag,
) -> Result<reply::Block, SequencerError>;
async fn block(&self, block: BlockId) -> Result<reply::Block, SequencerError>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted the following:

Suggested change
async fn block(&self, block: BlockId) -> Result<reply::Block, SequencerError>;
async fn block<B: Into<BlockId>>(&self, block: B)-> Result<reply::Block, SequencerError>;

but unfortunately this does not work with the mock library. Mostly just means that callers must append .into() themselves on some calls. Might revisit this if / when we remove mocks for channels.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is better probably overall but this probably would had removed the object safety from the method, which is why it cannot be used. In general there has recent-ishly been discussion on the amount of bloat this pattern creates and what kind of countermeasures it needs, for example some stdlib functions are constructed with an generic layer on top of unsized reference accepting arg: https://doc.rust-lang.org/stable/src/std/fs.rs.html#228-236

@Mirko-von-Leipzig
Copy link
Contributor Author

Mirko-von-Leipzig commented Jun 13, 2022

Uncertain why that test times out. It happens locally as well, but only if using the local sequencer mock.

This was due to not disabling retries under test cfg. This has now been fixed.

query_url.query_pairs_mut().extend_pairs(params);
tracing::trace!(%query_url);
query_url
fn request(&self) -> builder::Request<builder::stage::Gateway> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't there be a lifetime on the return value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Appears to be ellided? Or rather, it hasn't forced me to add it 🤔

Copy link
Contributor

@koivunej koivunej Jun 14, 2022

Choose a reason for hiding this comment

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

Need to add the warning then on these. I guess too many write them out by default.

Or not ... There's 41 in this repo of #[warn(rust_2018_idioms)]. I would prefer not to add to that. As a deny this looks like:

error: hidden lifetime parameters in types are deprecated
 --> crates/pathfinder/src/storage/schema/revision_0011.rs:8:37
  |
8 | pub(crate) fn migrate(transaction: &Transaction) -> anyhow::Result<PostMigrationAction> {
  |                                     ^^^^^^^^^^^ expected named lifetime parameter
  |
help: consider using the `'_` lifetime
  |
8 | pub(crate) fn migrate(transaction: &Transaction<'_>) -> anyhow::Result<PostMigrationAction> {
  |                                     ~~~~~~~~~~~~~~~

It is most likely cargo-fixable. I don't know why this isn't enabled automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tracking issue for warning for rust_2018_idioms by default indicates they were being conservative.

Personally, I didn't even know this was a thing :).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll create a new PR which fixes the whole codebase and adds the check to ci.

Mirko-von-Leipzig and others added 2 commits June 13, 2022 14:51
This fixes the tests from infinitely retrying on failure.

Also combines the final dual stages into a single one.
Previously our only route configured on the mock server actually
required query parameters to be present.

This changes the setup so that the query string is optional now. Note
that we still "canonicalize" the full path and query and append a `?`
even if there are now query parameters. This is actually required
so that we don't have to modify tests that use these kind of URLs.
Copy link
Member

@CHr15F0x CHr15F0x left a comment

Choose a reason for hiding this comment

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

LGTM! I really like the way queries are built now 🎉 .
Would be nice to disable those retries in tests.

crates/pathfinder/src/sequencer/builder.rs Outdated Show resolved Hide resolved
crates/pathfinder/src/sequencer.rs Outdated Show resolved Hide resolved
crates/pathfinder/src/sequencer.rs Show resolved Hide resolved
`eth_contract_addresses` and `add_transaction` tests are now consistent
with the actual paths that are generated for the live api.
@Mirko-von-Leipzig Mirko-von-Leipzig merged commit 0b4d20b into main Jun 14, 2022
@Mirko-von-Leipzig Mirko-von-Leipzig deleted the sequencer_builder branch June 14, 2022 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants