forest/rpc/methods/
common.rs

1// Copyright 2019-2025 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(_: Ctx<impl Any>, (): Self::Params) -> Result<Uuid, ServerError> {
29        Ok(*SESSION_UUID)
30    }
31}
32
33pub enum Version {}
34impl RpcMethod<0> for Version {
35    const NAME: &'static str = "Filecoin.Version";
36    const PARAM_NAMES: [&'static str; 0] = [];
37    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
38    const PERMISSION: Permission = Permission::Read;
39
40    type Params = ();
41    type Ok = PublicVersion;
42
43    async fn handle(ctx: Ctx<impl Blockstore>, (): Self::Params) -> Result<Self::Ok, ServerError> {
44        Ok(PublicVersion {
45            version: crate::utils::version::FOREST_VERSION_STRING.clone(),
46            // This matches Lotus's versioning for the API v1.
47            // For the API v0, we don't support it but it should be `1.5.0`.
48            api_version: ShiftingVersion::new(2, 3, 0),
49            block_delay: ctx.chain_config().block_delay_secs,
50        })
51    }
52}
53
54pub enum Shutdown {}
55impl RpcMethod<0> for Shutdown {
56    const NAME: &'static str = "Filecoin.Shutdown";
57    const PARAM_NAMES: [&'static str; 0] = [];
58    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
59    const PERMISSION: Permission = Permission::Admin;
60
61    type Params = ();
62    type Ok = ();
63
64    async fn handle(ctx: Ctx<impl Any>, (): Self::Params) -> Result<Self::Ok, ServerError> {
65        ctx.shutdown.send(()).await?;
66        Ok(())
67    }
68}
69
70pub enum StartTime {}
71impl RpcMethod<0> for StartTime {
72    const NAME: &'static str = "Filecoin.StartTime";
73    const PARAM_NAMES: [&'static str; 0] = [];
74    const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
75    const PERMISSION: Permission = Permission::Read;
76
77    type Params = ();
78    type Ok = chrono::DateTime<chrono::Utc>;
79
80    async fn handle(ctx: Ctx<impl Blockstore>, (): Self::Params) -> Result<Self::Ok, ServerError> {
81        Ok(ctx.start_time)
82    }
83}
84
85/// Represents the current version of the API.
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
87#[serde(rename_all = "PascalCase")]
88pub struct PublicVersion {
89    pub version: String,
90    #[serde(rename = "APIVersion")]
91    pub api_version: ShiftingVersion,
92    pub block_delay: u32,
93}
94lotus_json_with_self!(PublicVersion);
95
96/// Integer based value on version information. Highest order bits for Major,
97/// Mid order for Minor and lowest for Patch.
98#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
99pub struct ShiftingVersion(u32);
100
101impl ShiftingVersion {
102    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
103        Self(((major as u32) << 16) | ((minor as u32) << 8) | (patch as u32))
104    }
105}