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

feat: add quorum provider #409

Merged
merged 7 commits into from Aug 29, 2021
Merged

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented Aug 27, 2021

Motivation

Support multiple provider endpoints

Solution

Adds the QuorumProvider type that can run internal multiple providers internally and only returns a response if a quorum was reached. The internal providers can be weighted.
This required two Wrapper traits for JsonRpcClient PubsubClient to get rid of the generic types.
How a quorum is reached can be configured with Quorum, default is Quorum::Majority which reaches a quorum if >50% of the weighted providers returned the same value

@prestwich
Copy link
Collaborator

for RPC calls that take a block height we need to ensure that all providers request info at the same height, preferably the minimum height across all providers

this is like, eth_call etc

@mattsse
Copy link
Collaborator Author

mattsse commented Aug 27, 2021

Good point,
I had a quick look at ethers.js FallbackProvider, from what I can tell, they're using the median value of all block numbers if none was explicitly set.
So I guess, we need to match the method name and check if we need to normalize something for that call. For eth_call this would mean that we need to check if the second entry in the param json array is "latest" and then replacing this with the median? ofeth_blockNumber response of all providers?

Copy link
Owner

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

In general LGTM. I think it'd be useful if we also had an integration test in the middleware crate showing that Streams / Subscriptions work when paired with 1 Infura and 1 Alchemy node

total / 2 + rem
}
Quorum::Percentage(p) => {
providers.iter().map(|p| p.weight).sum::<u64>() * (p as u64) / 100
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe add a if p > 100 { panic!() } here?


fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
for n in (0..this.requests.len()).rev() {
Copy link
Owner

Choose a reason for hiding this comment

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

Why are we iterating in reverse here?

Copy link
Collaborator Author

@mattsse mattsse Aug 29, 2021

Choose a reason for hiding this comment

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

because how swap_remove works, which replaces the removed element with the last element. but this also means that if we'd start at 0 we would miss the value at the last index, therefor we need to start at the last index.

use crate::{Middleware, MockProvider, Provider};
use ethers_core::types::U64;

async fn test_quorum(q: Quorum) {
Copy link
Owner

Choose a reason for hiding this comment

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

Nice usage of the mock provider

@gakonst gakonst merged commit 824bedb into gakonst:master Aug 29, 2021
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.

None yet

3 participants