-
Notifications
You must be signed in to change notification settings - Fork 153
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
refactor: migrate node to trait RpcEndpoint #4204
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,90 +1,98 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
#![allow(clippy::unused_async)] | ||
|
||
use std::time::{Duration, SystemTime, UNIX_EPOCH}; | ||
|
||
use crate::rpc::error::ServerError; | ||
use crate::rpc::Ctx; | ||
use crate::rpc::{ApiVersion, Ctx, RpcMethod, ServerError}; | ||
use fvm_ipld_blockstore::Blockstore; | ||
|
||
pub const NODE_STATUS: &str = "Filecoin.NodeStatus"; | ||
pub type NodeStatusResult = NodeStatus; | ||
|
||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::lotus_json::lotus_json_with_self; | ||
macro_rules! for_each_method { | ||
($callback:ident) => { | ||
$callback!(crate::rpc::node::NodeStatus); | ||
}; | ||
} | ||
pub(crate) use for_each_method; | ||
|
||
pub enum NodeStatus {} | ||
impl RpcMethod<0> for NodeStatus { | ||
const NAME: &'static str = "Filecoin.NodeStatus"; | ||
const PARAM_NAMES: [&'static str; 0] = []; | ||
const API_VERSION: ApiVersion = ApiVersion::V0; | ||
|
||
type Params = (); | ||
type Ok = NodeStatusResult; | ||
|
||
async fn handle(ctx: Ctx<impl Blockstore>, (): Self::Params) -> Result<Self::Ok, ServerError> { | ||
let mut node_status = NodeStatusResult::default(); | ||
|
||
let head = ctx.state_manager.chain_store().heaviest_tipset(); | ||
let cur_duration: Duration = SystemTime::now().duration_since(UNIX_EPOCH)?; | ||
|
||
let ts = head.min_timestamp(); | ||
let cur_duration_secs = cur_duration.as_secs(); | ||
let behind = if ts <= cur_duration_secs + 1 { | ||
cur_duration_secs.saturating_sub(ts) | ||
} else { | ||
return Err(anyhow::anyhow!( | ||
"System time should not be behind tipset timestamp, please sync the system clock." | ||
) | ||
.into()); | ||
}; | ||
|
||
let chain_finality = ctx.state_manager.chain_config().policy.chain_finality; | ||
|
||
node_status.sync_status.epoch = head.epoch() as u64; | ||
node_status.sync_status.behind = behind; | ||
|
||
if head.epoch() > chain_finality { | ||
let mut block_count = 0; | ||
let mut ts = head; | ||
|
||
for _ in 0..100 { | ||
block_count += ts.block_headers().len(); | ||
let tsk = ts.parents(); | ||
ts = ctx.chain_store.chain_index.load_required_tipset(tsk)?; | ||
} | ||
|
||
node_status.chain_status.blocks_per_tipset_last_100 = block_count as f64 / 100.; | ||
|
||
for _ in 100..chain_finality { | ||
block_count += ts.block_headers().len(); | ||
let tsk = ts.parents(); | ||
ts = ctx.chain_store.chain_index.load_required_tipset(tsk)?; | ||
} | ||
|
||
node_status.chain_status.blocks_per_tipset_last_finality = | ||
block_count as f64 / chain_finality as f64; | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, Clone)] | ||
Ok(node_status) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, Clone, JsonSchema)] | ||
pub struct NodeSyncStatus { | ||
pub epoch: u64, | ||
pub behind: u64, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, Clone)] | ||
#[derive(Debug, Serialize, Deserialize, Default, Clone, JsonSchema)] | ||
pub struct NodePeerStatus { | ||
pub peers_to_publish_msgs: u32, | ||
pub peers_to_publish_blocks: u32, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default, Clone)] | ||
#[derive(Debug, Serialize, Deserialize, Default, Clone, JsonSchema)] | ||
pub struct NodeChainStatus { | ||
pub blocks_per_tipset_last_100: f64, | ||
pub blocks_per_tipset_last_finality: f64, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Default, Serialize, Clone)] | ||
pub struct NodeStatus { | ||
#[derive(Debug, Deserialize, Default, Serialize, Clone, JsonSchema)] | ||
pub struct NodeStatusResult { | ||
pub sync_status: NodeSyncStatus, | ||
pub peer_status: NodePeerStatus, | ||
pub chain_status: NodeChainStatus, | ||
} | ||
|
||
lotus_json_with_self!(NodeStatus); | ||
|
||
pub async fn node_status<DB: Blockstore>(data: Ctx<DB>) -> Result<NodeStatusResult, ServerError> { | ||
let mut node_status = NodeStatusResult::default(); | ||
|
||
let head = data.state_manager.chain_store().heaviest_tipset(); | ||
let cur_duration: Duration = SystemTime::now().duration_since(UNIX_EPOCH)?; | ||
|
||
let ts = head.min_timestamp(); | ||
let cur_duration_secs = cur_duration.as_secs(); | ||
let behind = if ts <= cur_duration_secs + 1 { | ||
cur_duration_secs.saturating_sub(ts) | ||
} else { | ||
return Err(anyhow::anyhow!( | ||
"System time should not be behind tipset timestamp, please sync the system clock." | ||
) | ||
.into()); | ||
}; | ||
|
||
let chain_finality = data.state_manager.chain_config().policy.chain_finality; | ||
|
||
node_status.sync_status.epoch = head.epoch() as u64; | ||
node_status.sync_status.behind = behind; | ||
|
||
if head.epoch() > chain_finality { | ||
let mut block_count = 0; | ||
let mut ts = head; | ||
|
||
for _ in 0..100 { | ||
block_count += ts.block_headers().len(); | ||
let tsk = ts.parents(); | ||
ts = data.chain_store.chain_index.load_required_tipset(tsk)?; | ||
} | ||
|
||
node_status.chain_status.blocks_per_tipset_last_100 = block_count as f64 / 100.; | ||
|
||
for _ in 100..chain_finality { | ||
block_count += ts.block_headers().len(); | ||
let tsk = ts.parents(); | ||
ts = data.chain_store.chain_index.load_required_tipset(tsk)?; | ||
} | ||
|
||
node_status.chain_status.blocks_per_tipset_last_finality = | ||
block_count as f64 / chain_finality as f64; | ||
} | ||
|
||
Ok(node_status) | ||
} |
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
59 changes: 59 additions & 0 deletions
59
src/rpc/snapshots/forest_filecoin__rpc__tests__openrpc.snap
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we get rid of this line?