-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from worldcoin/0xkitsune/sync-to-head
Fix: `WorldTree::sync_to_head`
- Loading branch information
Showing
10 changed files
with
234 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "ethers-throttle" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
async-trait = "0.1.74" | ||
ethers = "2.0.10" | ||
eyre = "0.6.8" | ||
governor = "0.6.0" | ||
serde = { version = "1.0.189", features = ["derive"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::num::NonZeroU32; | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use ethers::providers::{JsonRpcClient, ProviderError}; | ||
use governor::clock::{QuantaClock, QuantaInstant}; | ||
use governor::middleware::NoOpMiddleware; | ||
use governor::state::{InMemoryState, NotKeyed}; | ||
use governor::{Jitter, Quota, RateLimiter}; | ||
use serde::de::DeserializeOwned; | ||
use serde::Serialize; | ||
|
||
pub type Throttle = RateLimiter< | ||
NotKeyed, | ||
InMemoryState, | ||
QuantaClock, | ||
NoOpMiddleware<QuantaInstant>, | ||
>; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ThrottledProvider<P: JsonRpcClient> { | ||
throttle: Arc<Throttle>, | ||
jitter: Option<Jitter>, | ||
inner: P, | ||
} | ||
|
||
impl<P: JsonRpcClient> ThrottledProvider<P> { | ||
pub fn new( | ||
provider: P, | ||
requests_per_second: u32, | ||
jitter: Option<Jitter>, | ||
) -> Self { | ||
let throttle = Arc::new(RateLimiter::direct(Quota::per_second( | ||
NonZeroU32::new(requests_per_second) | ||
.expect("Could not initialize NonZeroU32"), | ||
))); | ||
|
||
ThrottledProvider { | ||
throttle, | ||
jitter, | ||
inner: provider, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<P: JsonRpcClient> JsonRpcClient for ThrottledProvider<P> { | ||
type Error = P::Error; | ||
|
||
/// Sends a request with the provided JSON-RPC and parameters serialized as JSON | ||
async fn request<T, R>( | ||
&self, | ||
method: &str, | ||
params: T, | ||
) -> Result<R, Self::Error> | ||
where | ||
T: std::fmt::Debug + Serialize + Send + Sync, | ||
R: DeserializeOwned + Send, | ||
{ | ||
if let Some(jitter) = self.jitter { | ||
self.throttle.until_ready_with_jitter(jitter).await; | ||
} else { | ||
self.throttle.until_ready().await; | ||
} | ||
|
||
self.inner.request(method, params).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.