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

fix(rpc): starknet_syncing returns wrong value #164

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/pathfinder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pathfinder"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
62 changes: 55 additions & 7 deletions crates/pathfinder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn run_server(addr: SocketAddr, api: RpcApi) -> Result<(HttpServerHandle, So
// context.protocol_version().await
// })?;
module.register_async_method("starknet_syncing", |_, context| async move {
context.chain_id().await
context.syncing().await
})?;
let module = module.into_inner();
server.start(module).map(|handle| (handle, local_addr))
Expand Down Expand Up @@ -1727,11 +1727,59 @@ mod tests {
.unwrap();
}

#[tokio::test]
#[should_panic]
async fn syncing() {
client_request::<types::reply::Syncing>("starknet_syncing", rpc_params!())
.await
.unwrap();
mod syncing {
use crate::{
rpc::types::reply::{syncing, Syncing},
state::SYNC_STATUS,
};
use pretty_assertions::assert_eq;

use super::*;

#[tokio::test]
async fn not_syncing() {
// Set the syncing status and drop the guard.
{
let mut status = SYNC_STATUS.lock().await;
*status = Syncing::False(false);
}

let storage = setup_storage();
let sequencer = SeqClient::new(Chain::Goerli).unwrap();
let api = RpcApi::new(storage, sequencer, Chain::Goerli);
let (__handle, addr) = run_server(*LOCALHOST, api).unwrap();
let syncing = client(addr)
.request::<Syncing>("starknet_syncing", rpc_params!())
.await
.unwrap();

assert_eq!(syncing, Syncing::False(false));
}

#[tokio::test]
async fn syncing() {
let expected = Syncing::Status(syncing::Status {
starting_block: StarknetBlockHash(StarkHash::from_be_slice(b"starting").unwrap()),
current_block: StarknetBlockHash(StarkHash::from_be_slice(b"current").unwrap()),
highest_block: StarknetBlockHash(StarkHash::from_be_slice(b"highest").unwrap()),
});

// Set the syncing status and drop the guard.
{
let mut status = SYNC_STATUS.lock().await;
*status = expected.clone();
}

let storage = setup_storage();
let sequencer = SeqClient::new(Chain::Goerli).unwrap();
let api = RpcApi::new(storage, sequencer, Chain::Goerli);
let (__handle, addr) = run_server(*LOCALHOST, api).unwrap();
let syncing = client(addr)
.request::<Syncing>("starknet_syncing", rpc_params!())
.await
.unwrap();

assert_eq!(syncing, expected);
}
}
}
4 changes: 2 additions & 2 deletions crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub async fn sync(
// Update sync status
match &mut *SYNC_STATUS.lock().await {
SyncStatus::False(_) => {}
SyncStatus::Status(mut status) => {
SyncStatus::Status(status) => {
status.current_block = block_hash;
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ async fn update_sync_status_latest(
highest_block: latest,
});
}
SyncStatus::Status(mut status) => status.highest_block = latest,
SyncStatus::Status(status) => status.highest_block = latest,
}

tracing::debug!(
Expand Down