Skip to main content

forest/rpc/methods/
common.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::lotus_json::lotus_json_with_self;
5use crate::rpc::error::ServerError;
6use crate::rpc::{ApiPaths, Ctx, Permission, RpcMethod};
7use enumflags2::BitFlags;
8use fvm_ipld_blockstore::Blockstore;
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use std::any::Any;
12use std::sync::LazyLock;
13use uuid::Uuid;
14
15static SESSION_UUID: LazyLock<Uuid> = LazyLock::new(crate::utils::rand::new_uuid_v4);
16
17/// The returned session UUID uniquely identifies the API node.
18pub enum Session {}
19impl RpcMethod<0> for Session {
20    const NAME: &'static str = "Filecoin.Session";
21    const PARAM_NAMES: [&'static str; 0] = [];
22    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
23    const PERMISSION: Permission = Permission::Read;
24
25    type Params = ();
26    type Ok = Uuid;
27
28    async fn handle(
29        _: Ctx<impl Any>,
30        (): Self::Params,
31        _: &http::Extensions,
32    ) -> Result<Uuid, ServerError> {
33        Ok(*SESSION_UUID)
34    }
35}
36
37pub enum Version {}
38impl RpcMethod<0> for Version {
39    const NAME: &'static str = "Filecoin.Version";
40    const PARAM_NAMES: [&'static str; 0] = [];
41    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
42    const PERMISSION: Permission = Permission::Read;
43
44    type Params = ();
45    type Ok = PublicVersion;
46
47    async fn handle(
48        ctx: Ctx<impl Blockstore>,
49        (): Self::Params,
50        _: &http::Extensions,
51    ) -> Result<Self::Ok, ServerError> {
52        Ok(PublicVersion {
53            version: crate::utils::version::FOREST_VERSION_STRING.clone(),
54            // This matches Lotus's versioning for the API v1.
55            // For the API v0, we don't support it but it should be `1.5.0`.
56            api_version: ShiftingVersion::new(2, 3, 0),
57            block_delay: ctx.chain_config().block_delay_secs,
58        })
59    }
60}
61
62pub enum Shutdown {}
63impl RpcMethod<0> for Shutdown {
64    const NAME: &'static str = "Filecoin.Shutdown";
65    const PARAM_NAMES: [&'static str; 0] = [];
66    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
67    const PERMISSION: Permission = Permission::Admin;
68
69    type Params = ();
70    type Ok = ();
71
72    async fn handle(
73        ctx: Ctx<impl Any>,
74        (): Self::Params,
75        _: &http::Extensions,
76    ) -> Result<Self::Ok, ServerError> {
77        ctx.shutdown.send(()).await?;
78        Ok(())
79    }
80}
81
82pub enum StartTime {}
83impl RpcMethod<0> for StartTime {
84    const NAME: &'static str = "Filecoin.StartTime";
85    const PARAM_NAMES: [&'static str; 0] = [];
86    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
87    const PERMISSION: Permission = Permission::Read;
88
89    type Params = ();
90    type Ok = chrono::DateTime<chrono::Utc>;
91
92    async fn handle(
93        ctx: Ctx<impl Blockstore>,
94        (): Self::Params,
95        _: &http::Extensions,
96    ) -> Result<Self::Ok, ServerError> {
97        Ok(ctx.start_time)
98    }
99}
100
101/// Represents the current version of the API.
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
103#[serde(rename_all = "PascalCase")]
104pub struct PublicVersion {
105    pub version: String,
106    #[serde(rename = "APIVersion")]
107    pub api_version: ShiftingVersion,
108    pub block_delay: u32,
109}
110lotus_json_with_self!(PublicVersion);
111
112/// Integer based value on version information. Highest order bits for Major,
113/// Mid order for Minor and lowest for Patch.
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
115pub struct ShiftingVersion(u32);
116
117impl ShiftingVersion {
118    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
119        Self(((major as u32) << 16) | ((minor as u32) << 8) | (patch as u32))
120    }
121}