Skip to content

Commit

Permalink
Fast Sync Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Nov 28, 2024
1 parent 421d118 commit 0fba5b0
Show file tree
Hide file tree
Showing 51 changed files with 5,011 additions and 3,225 deletions.
1,128 changes: 691 additions & 437 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions api/src/handlers/peers_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::router::{Handler, ResponseFuture};
use crate::web::*;
use grin_p2p::types::Direction;
use grin_p2p::types::PeerInfoDisplayLegacy;
use grin_p2p::Capabilities;
use hyper::{Body, Request, StatusCode};
use std::net::SocketAddr;
use std::sync::Weak;
Expand All @@ -30,7 +31,7 @@ pub struct PeersAllHandler {

impl Handler for PeersAllHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture {
let peers = &w_fut!(&self.peers).all_peer_data();
let peers = &w_fut!(&self.peers).all_peer_data(Capabilities::UNKNOWN);
json_response_pretty(&peers)
}
}
Expand Down Expand Up @@ -134,22 +135,22 @@ impl PeerHandler {
pub fn get_peers(&self, addr: Option<SocketAddr>) -> Result<Vec<PeerData>, Error> {
if let Some(addr) = addr {
let peer_addr = PeerAddr::Ip(addr);
let peer_data: PeerData = w(&self.peers)?.get_peer(peer_addr.clone()).map_err(|e| {
let peer_data: PeerData = w(&self.peers)?.get_peer(&peer_addr).map_err(|e| {
Error::Internal(format!(
"Unable to get peer for address {}, {}",
peer_addr, e
))
})?;
return Ok(vec![peer_data]);
}
let peers = w(&self.peers)?.all_peer_data();
let peers = w(&self.peers)?.all_peer_data(Capabilities::UNKNOWN);
Ok(peers)
}

pub fn ban_peer(&self, addr: SocketAddr) -> Result<(), Error> {
let peer_addr = PeerAddr::Ip(addr);
w(&self.peers)?
.ban_peer(peer_addr.clone(), ReasonForBan::ManualBan)
.ban_peer(&peer_addr, ReasonForBan::ManualBan, "banned from api")
.map_err(|e| {
Error::Internal(format!(
"Unable to ban peer for address {}, {}",
Expand All @@ -160,7 +161,7 @@ impl PeerHandler {

pub fn unban_peer(&self, addr: SocketAddr) -> Result<(), Error> {
let peer_addr = PeerAddr::Ip(addr);
w(&self.peers)?.unban_peer(peer_addr.clone()).map_err(|e| {
w(&self.peers)?.unban_peer(&peer_addr).map_err(|e| {
Error::Internal(format!(
"Unable to unban peer for address {}, {}",
peer_addr, e
Expand Down Expand Up @@ -190,7 +191,7 @@ impl Handler for PeerHandler {
);
}

match w_fut!(&self.peers).get_peer(peer_addr.clone()) {
match w_fut!(&self.peers).get_peer(&peer_addr) {
Ok(peer) => json_response(&peer),
Err(_) => response(
StatusCode::NOT_FOUND,
Expand Down Expand Up @@ -223,14 +224,18 @@ impl Handler for PeerHandler {
};

match command {
"ban" => match w_fut!(&self.peers).ban_peer(addr.clone(), ReasonForBan::ManualBan) {
"ban" => match w_fut!(&self.peers).ban_peer(
&addr,
ReasonForBan::ManualBan,
"banned from CLI",
) {
Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("ban for peer {} failed, {:?}", addr, e),
),
},
"unban" => match w_fut!(&self.peers).unban_peer(addr.clone()) {
"unban" => match w_fut!(&self.peers).unban_peer(&addr) {
Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
19 changes: 8 additions & 11 deletions api/src/handlers/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,14 @@ impl Handler for StatusHandler {
fn sync_status_to_api(sync_status: SyncStatus) -> (String, Option<serde_json::Value>) {
match sync_status {
SyncStatus::NoSync => ("no_sync".to_string(), None),
SyncStatus::AwaitingPeers(_) => ("awaiting_peers".to_string(), None),
SyncStatus::AwaitingPeers => ("awaiting_peers".to_string(), None),
SyncStatus::HeaderSync {
sync_head,
highest_height,
current_height,
archive_height,
..
} => (
"header_sync".to_string(),
Some(json!({ "current_height": sync_head.height, "highest_height": highest_height })),
),
SyncStatus::TxHashsetDownload(stats) => (
"txhashset_download".to_string(),
Some(
json!({ "downloaded_size": stats.downloaded_size, "total_size": stats.total_size }),
),
Some(json!({ "current_height": current_height, "highest_height": archive_height })),
),
SyncStatus::TxHashsetRangeProofsValidation {
rproofs,
Expand All @@ -148,11 +142,14 @@ fn sync_status_to_api(sync_status: SyncStatus) -> (String, Option<serde_json::Va
Some(json!({ "kernels": kernels, "kernels_total": kernels_total })),
),
SyncStatus::BodySync {
archive_height,
current_height,
highest_height,
} => (
"body_sync".to_string(),
Some(json!({ "current_height": current_height, "highest_height": highest_height })),
Some(
json!({ "archive_height":archive_height, "current_height": current_height, "highest_height": highest_height }),
),
),
SyncStatus::Shutdown => ("shutdown".to_string(), None),
// any other status is considered syncing (should be unreachable)
Expand Down
2 changes: 1 addition & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde = "1"
serde_derive = "1"
thiserror = "1"
chrono = "0.4.11"
lru-cache = "0.1"
lru = "0.12"
lazy_static = "1"
tokio = {version = "0.2", features = ["full"] }
num_cpus = "1"
Expand Down
Loading

0 comments on commit 0fba5b0

Please sign in to comment.