Skip to content
1 change: 1 addition & 0 deletions src/lotus_json/actor_states/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ mod init_exec_params;
mod miner_change_worker_params;
mod miner_constructor_params;
mod power_actor;
mod reward_methods;
180 changes: 180 additions & 0 deletions src/lotus_json/actor_states/methods/reward_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::*;
use crate::shim::address::Address;
use crate::shim::econ::TokenAmount;
use num_bigint::BigInt;
use paste::paste;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[serde(transparent)]
pub struct RewardConstructorParamsLotusJson(
#[schemars(with = "LotusJson<Option<BigInt>>")]
#[serde(with = "crate::lotus_json")]
Option<BigInt>,
);

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct AwardBlockRewardParamsLotusJson {
#[schemars(with = "LotusJson<Address>")]
#[serde(with = "crate::lotus_json")]
pub miner: Address,
#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub penalty: TokenAmount,
#[schemars(with = "LotusJson<TokenAmount>")]
#[serde(with = "crate::lotus_json")]
pub gas_reward: TokenAmount,
pub win_count: i64,
}

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[serde(transparent)]
pub struct UpdateNetworkKPIParamsLotusJson(
#[schemars(with = "LotusJson<Option<BigInt>>")]
#[serde(with = "crate::lotus_json")]
Option<BigInt>,
);

// Implementation for ConstructorParams
macro_rules! impl_reward_constructor_params {
($type_suffix:path: $($version:literal),+) => {
$(
paste! {
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::ConstructorParams {
type LotusJson = RewardConstructorParamsLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![
(
json!(null),
Self {
power: None,
},
),
(
json!("1000"),
Self {
power: Some($type_suffix::bigint_ser::BigIntDe(BigInt::from(1000))),
},
),
]
}

fn into_lotus_json(self) -> Self::LotusJson {
RewardConstructorParamsLotusJson(self.power.map(|p| p.0))
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Self {
power: lotus_json.0.map(|p| $type_suffix::bigint_ser::BigIntDe(p)),
}
}
}
}
)+
};
}

// Implementation for AwardBlockRewardParams
macro_rules! impl_award_block_reward_params {
($($version:literal),+) => {
$(
paste! {
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::AwardBlockRewardParams {
type LotusJson = AwardBlockRewardParamsLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![
(
json!({
"Miner": "f01234",
"Penalty": "0",
"GasReward": "1000",
"WinCount": 1
}),
Self {
miner: Address::new_id(1234).into(),
penalty: TokenAmount::from_atto(0).into(),
gas_reward: TokenAmount::from_atto(1000).into(),
win_count: 1,
},
),
]
}

fn into_lotus_json(self) -> Self::LotusJson {
AwardBlockRewardParamsLotusJson {
miner: self.miner.into(),
penalty: self.penalty.into(),
gas_reward: self.gas_reward.into(),
win_count: self.win_count,
}
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Self {
miner: lotus_json.miner.into(),
penalty: TokenAmount::from(lotus_json.penalty).into(),
gas_reward: TokenAmount::from(lotus_json.gas_reward).into(),
win_count: lotus_json.win_count,
}
}
}
}
)+
};
}

// Implementation for UpdateNetworkKPIParams
macro_rules! impl_update_network_kpi_params {
($type_suffix:path: $($version:literal),+) => {
$(
paste! {
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::UpdateNetworkKPIParams {
type LotusJson = UpdateNetworkKPIParamsLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![
(
json!(null),
Self {
curr_realized_power: None,
},
),
(
json!("2000"),
Self {
curr_realized_power: Some($type_suffix::bigint_ser::BigIntDe(BigInt::from(2000))),
},
),
]
}

fn into_lotus_json(self) -> Self::LotusJson {
UpdateNetworkKPIParamsLotusJson(self.curr_realized_power.map(|p| p.0))
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
Self {
curr_realized_power: lotus_json.0.map(|p| $type_suffix::bigint_ser::BigIntDe(p)),
}
}
}
}
)+
};
}

impl_reward_constructor_params!(fvm_shared4::bigint: 16, 15, 14, 13, 12);
impl_reward_constructor_params!(fvm_shared3::bigint: 11);
impl_award_block_reward_params!(16, 15, 14, 13, 12, 11, 10, 9, 8);
impl_update_network_kpi_params!(fvm_shared4::bigint: 16, 15, 14, 13, 12);
impl_update_network_kpi_params!(fvm_shared3::bigint: 11);
30 changes: 30 additions & 0 deletions src/lotus_json/big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ impl HasLotusJson for BigInt {
big_int
}
}

macro_rules! impl_bigint_de {
($($bigint_de_type:ty),+) => {
$(
impl HasLotusJson for $bigint_de_type {
type LotusJson = BigIntLotusJson;

#[cfg(test)]
fn snapshots() -> Vec<(serde_json::Value, Self)> {
vec![(json!("1000"), Self(BigInt::from(1000)))]
}

fn into_lotus_json(self) -> Self::LotusJson {
BigIntLotusJson(self.0)
}

fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
let BigIntLotusJson(big_int) = lotus_json;
Self(big_int)
}
}
)+
};
}

impl_bigint_de!(
fvm_shared2::bigint::bigint_ser::BigIntDe,
fvm_shared3::bigint::bigint_ser::BigIntDe,
fvm_shared4::bigint::bigint_ser::BigIntDe
);
1 change: 1 addition & 0 deletions src/rpc/registry/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub(crate) mod evm;
pub(crate) mod init;
pub(crate) mod miner;
pub(crate) mod power;
pub(crate) mod reward;
pub(crate) mod system;
74 changes: 74 additions & 0 deletions src/rpc/registry/actors/reward.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2019-2025 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::rpc::registry::methods_reg::{MethodRegistry, register_actor_methods};
use crate::shim::message::MethodNum;
use anyhow::Result;
use cid::Cid;

macro_rules! register_reward_version_11_to_16 {
($registry:expr, $code_cid:expr, $state_version:path) => {{
use $state_version::{
AwardBlockRewardParams, ConstructorParams, Method, UpdateNetworkKPIParams,
};

register_actor_methods!(
$registry,
$code_cid,
[
(Method::Constructor, ConstructorParams),
(Method::AwardBlockReward, AwardBlockRewardParams),
(Method::UpdateNetworkKPI, UpdateNetworkKPIParams),
]
);

// Register methods without parameters
register_actor_methods!($registry, $code_cid, [(Method::ThisEpochReward, empty)]);
}};
}

macro_rules! register_reward_version_8_to_10 {
($registry:expr, $code_cid:expr, $state_version:path, $fvm_shared_version:path) => {{
use $state_version::{AwardBlockRewardParams, Method};
use $fvm_shared_version::{bigint::bigint_ser::BigIntDe};

register_actor_methods!(
$registry,
$code_cid,
[
(Method::Constructor, Option<BigIntDe>),
(Method::AwardBlockReward, AwardBlockRewardParams),
(Method::UpdateNetworkKPI, Option<BigIntDe>),
]
);

// Register methods without parameters
register_actor_methods!($registry, $code_cid, [(Method::ThisEpochReward, empty)]);
}};
}

pub(crate) fn register_actor_methods(registry: &mut MethodRegistry, cid: Cid, version: u64) {
match version {
8 => {
register_reward_version_8_to_10!(registry, cid, fil_actor_reward_state::v8, fvm_shared2)
}
9 => {
register_reward_version_8_to_10!(registry, cid, fil_actor_reward_state::v9, fvm_shared2)
}
10 => {
register_reward_version_8_to_10!(
registry,
cid,
fil_actor_reward_state::v10,
fvm_shared3
)
}
11 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v11),
12 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v12),
13 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v13),
14 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v14),
15 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v15),
16 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v16),
_ => {}
}
}
3 changes: 2 additions & 1 deletion src/rpc/registry/methods_reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl MethodRegistry {
}

fn register_known_methods(&mut self) {
use crate::rpc::registry::actors::{account, evm, init, miner, power, system};
use crate::rpc::registry::actors::{account, evm, init, miner, power, reward, system};

for (&cid, &(actor_type, version)) in ACTOR_REGISTRY.iter() {
match actor_type {
Expand All @@ -85,6 +85,7 @@ impl MethodRegistry {
BuiltinActor::Init => init::register_actor_methods(self, cid, version),
BuiltinActor::System => system::register_actor_methods(self, cid, version),
BuiltinActor::Power => power::register_actor_methods(self, cid, version),
BuiltinActor::Reward => reward::register_actor_methods(self, cid, version),
_ => {}
}
}
Expand Down
Loading
Loading