-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sync): sync pending data (#1175)
* feat(sync): sync pending data * CR fix
- Loading branch information
1 parent
56f4eff
commit 0debad0
Showing
7 changed files
with
251 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod base_layer; | |
pub mod central; | ||
#[cfg(test)] | ||
mod central_sync_test; | ||
pub mod pending; |
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,74 @@ | ||
#[cfg(test)] | ||
#[path = "pending_test.rs"] | ||
mod pending_test; | ||
|
||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
#[cfg(test)] | ||
use mockall::automock; | ||
use starknet_client::reader::{ | ||
PendingData, | ||
ReaderClientError, | ||
StarknetFeederGatewayClient, | ||
StarknetReader, | ||
}; | ||
use starknet_client::ClientCreationError; | ||
use tracing::{debug, trace}; | ||
|
||
// TODO(dvir): add pending config. | ||
use super::central::CentralSourceConfig; | ||
|
||
pub struct GenericPendingSource<TStarknetClient: StarknetReader + Send + Sync> { | ||
pub starknet_client: Arc<TStarknetClient>, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum PendingError { | ||
#[error(transparent)] | ||
ClientCreation(#[from] ClientCreationError), | ||
#[error(transparent)] | ||
ClientError(#[from] Arc<ReaderClientError>), | ||
#[error("Pending block not found")] | ||
PendingBlockNotFound, | ||
} | ||
#[cfg_attr(test, automock)] | ||
#[async_trait] | ||
pub trait PendingSourceTrait { | ||
async fn get_pending_data(&self) -> Result<PendingData, PendingError>; | ||
} | ||
|
||
#[async_trait] | ||
impl<TStarknetClient: StarknetReader + Send + Sync + 'static> PendingSourceTrait | ||
for GenericPendingSource<TStarknetClient> | ||
{ | ||
async fn get_pending_data(&self) -> Result<PendingData, PendingError> { | ||
match self.starknet_client.pending_data().await { | ||
Ok(Some(pending_data)) => { | ||
debug!("Received new pending data."); | ||
trace!("Pending data: {pending_data:#?}."); | ||
Ok(pending_data) | ||
} | ||
Ok(None) => Err(PendingError::PendingBlockNotFound), | ||
Err(err) => Err(PendingError::ClientError(Arc::new(err))), | ||
} | ||
} | ||
} | ||
|
||
pub type PendingSource = GenericPendingSource<StarknetFeederGatewayClient>; | ||
|
||
impl PendingSource { | ||
pub fn new( | ||
config: CentralSourceConfig, | ||
node_version: &'static str, | ||
) -> Result<PendingSource, ClientCreationError> { | ||
let starknet_client = StarknetFeederGatewayClient::new( | ||
&config.url, | ||
config.http_headers, | ||
node_version, | ||
config.retry_config, | ||
)?; | ||
|
||
Ok(PendingSource { starknet_client: Arc::new(starknet_client) }) | ||
} | ||
} |
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,20 @@ | ||
use std::sync::Arc; | ||
|
||
use pretty_assertions::assert_eq; | ||
use starknet_client::reader::{MockStarknetReader, PendingData}; | ||
|
||
use crate::sources::pending::{GenericPendingSource, PendingSourceTrait}; | ||
|
||
#[tokio::test] | ||
async fn get_pending_data() { | ||
let mut client_mock = MockStarknetReader::new(); | ||
|
||
// We need to perform all the mocks before moving the mock into pending_source. | ||
// TODO(dvir): use pending_data which isn't the default. | ||
client_mock.expect_pending_data().times(1).returning(|| Ok(Some(PendingData::default()))); | ||
|
||
let pending_source = GenericPendingSource { starknet_client: Arc::new(client_mock) }; | ||
|
||
let pending_data = pending_source.get_pending_data().await.unwrap(); | ||
assert_eq!(pending_data, PendingData::default()); | ||
} |
Oops, something went wrong.