Skip to content

Commit baf74e6

Browse files
authored
feat: add support for reward actor in state decode params API (#5888)
1 parent ca67546 commit baf74e6

File tree

8 files changed

+333
-8
lines changed

8 files changed

+333
-8
lines changed

src/lotus_json/actor_states/methods/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ mod init_exec_params;
1010
mod miner_change_worker_params;
1111
mod miner_constructor_params;
1212
mod power_actor;
13+
mod reward_methods;
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright 2019-2025 ChainSafe Systems
2+
// SPDX-License-Identifier: Apache-2.0, MIT
3+
4+
use super::*;
5+
use crate::shim::address::Address;
6+
use crate::shim::econ::TokenAmount;
7+
use num_bigint::BigInt;
8+
use paste::paste;
9+
use schemars::JsonSchema;
10+
use serde::{Deserialize, Serialize};
11+
12+
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
13+
#[serde(transparent)]
14+
pub struct RewardConstructorParamsLotusJson(
15+
#[schemars(with = "LotusJson<Option<BigInt>>")]
16+
#[serde(with = "crate::lotus_json")]
17+
Option<BigInt>,
18+
);
19+
20+
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
21+
#[serde(rename_all = "PascalCase")]
22+
pub struct AwardBlockRewardParamsLotusJson {
23+
#[schemars(with = "LotusJson<Address>")]
24+
#[serde(with = "crate::lotus_json")]
25+
pub miner: Address,
26+
#[schemars(with = "LotusJson<TokenAmount>")]
27+
#[serde(with = "crate::lotus_json")]
28+
pub penalty: TokenAmount,
29+
#[schemars(with = "LotusJson<TokenAmount>")]
30+
#[serde(with = "crate::lotus_json")]
31+
pub gas_reward: TokenAmount,
32+
pub win_count: i64,
33+
}
34+
35+
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
36+
#[serde(transparent)]
37+
pub struct UpdateNetworkKPIParamsLotusJson(
38+
#[schemars(with = "LotusJson<Option<BigInt>>")]
39+
#[serde(with = "crate::lotus_json")]
40+
Option<BigInt>,
41+
);
42+
43+
// Implementation for ConstructorParams
44+
macro_rules! impl_reward_constructor_params {
45+
($type_suffix:path: $($version:literal),+) => {
46+
$(
47+
paste! {
48+
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::ConstructorParams {
49+
type LotusJson = RewardConstructorParamsLotusJson;
50+
51+
#[cfg(test)]
52+
fn snapshots() -> Vec<(serde_json::Value, Self)> {
53+
vec![
54+
(
55+
json!(null),
56+
Self {
57+
power: None,
58+
},
59+
),
60+
(
61+
json!("1000"),
62+
Self {
63+
power: Some($type_suffix::bigint_ser::BigIntDe(BigInt::from(1000))),
64+
},
65+
),
66+
]
67+
}
68+
69+
fn into_lotus_json(self) -> Self::LotusJson {
70+
RewardConstructorParamsLotusJson(self.power.map(|p| p.0))
71+
}
72+
73+
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
74+
Self {
75+
power: lotus_json.0.map(|p| $type_suffix::bigint_ser::BigIntDe(p)),
76+
}
77+
}
78+
}
79+
}
80+
)+
81+
};
82+
}
83+
84+
// Implementation for AwardBlockRewardParams
85+
macro_rules! impl_award_block_reward_params {
86+
($($version:literal),+) => {
87+
$(
88+
paste! {
89+
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::AwardBlockRewardParams {
90+
type LotusJson = AwardBlockRewardParamsLotusJson;
91+
92+
#[cfg(test)]
93+
fn snapshots() -> Vec<(serde_json::Value, Self)> {
94+
vec![
95+
(
96+
json!({
97+
"Miner": "f01234",
98+
"Penalty": "0",
99+
"GasReward": "1000",
100+
"WinCount": 1
101+
}),
102+
Self {
103+
miner: Address::new_id(1234).into(),
104+
penalty: TokenAmount::from_atto(0).into(),
105+
gas_reward: TokenAmount::from_atto(1000).into(),
106+
win_count: 1,
107+
},
108+
),
109+
]
110+
}
111+
112+
fn into_lotus_json(self) -> Self::LotusJson {
113+
AwardBlockRewardParamsLotusJson {
114+
miner: self.miner.into(),
115+
penalty: self.penalty.into(),
116+
gas_reward: self.gas_reward.into(),
117+
win_count: self.win_count,
118+
}
119+
}
120+
121+
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
122+
Self {
123+
miner: lotus_json.miner.into(),
124+
penalty: TokenAmount::from(lotus_json.penalty).into(),
125+
gas_reward: TokenAmount::from(lotus_json.gas_reward).into(),
126+
win_count: lotus_json.win_count,
127+
}
128+
}
129+
}
130+
}
131+
)+
132+
};
133+
}
134+
135+
// Implementation for UpdateNetworkKPIParams
136+
macro_rules! impl_update_network_kpi_params {
137+
($type_suffix:path: $($version:literal),+) => {
138+
$(
139+
paste! {
140+
impl HasLotusJson for fil_actor_reward_state::[<v $version>]::UpdateNetworkKPIParams {
141+
type LotusJson = UpdateNetworkKPIParamsLotusJson;
142+
143+
#[cfg(test)]
144+
fn snapshots() -> Vec<(serde_json::Value, Self)> {
145+
vec![
146+
(
147+
json!(null),
148+
Self {
149+
curr_realized_power: None,
150+
},
151+
),
152+
(
153+
json!("2000"),
154+
Self {
155+
curr_realized_power: Some($type_suffix::bigint_ser::BigIntDe(BigInt::from(2000))),
156+
},
157+
),
158+
]
159+
}
160+
161+
fn into_lotus_json(self) -> Self::LotusJson {
162+
UpdateNetworkKPIParamsLotusJson(self.curr_realized_power.map(|p| p.0))
163+
}
164+
165+
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
166+
Self {
167+
curr_realized_power: lotus_json.0.map(|p| $type_suffix::bigint_ser::BigIntDe(p)),
168+
}
169+
}
170+
}
171+
}
172+
)+
173+
};
174+
}
175+
176+
impl_reward_constructor_params!(fvm_shared4::bigint: 16, 15, 14, 13, 12);
177+
impl_reward_constructor_params!(fvm_shared3::bigint: 11);
178+
impl_award_block_reward_params!(16, 15, 14, 13, 12, 11, 10, 9, 8);
179+
impl_update_network_kpi_params!(fvm_shared4::bigint: 16, 15, 14, 13, 12);
180+
impl_update_network_kpi_params!(fvm_shared3::bigint: 11);

src/lotus_json/big_int.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,33 @@ impl HasLotusJson for BigInt {
2929
big_int
3030
}
3131
}
32+
33+
macro_rules! impl_bigint_de {
34+
($($bigint_de_type:ty),+) => {
35+
$(
36+
impl HasLotusJson for $bigint_de_type {
37+
type LotusJson = BigIntLotusJson;
38+
39+
#[cfg(test)]
40+
fn snapshots() -> Vec<(serde_json::Value, Self)> {
41+
vec![(json!("1000"), Self(BigInt::from(1000)))]
42+
}
43+
44+
fn into_lotus_json(self) -> Self::LotusJson {
45+
BigIntLotusJson(self.0)
46+
}
47+
48+
fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
49+
let BigIntLotusJson(big_int) = lotus_json;
50+
Self(big_int)
51+
}
52+
}
53+
)+
54+
};
55+
}
56+
57+
impl_bigint_de!(
58+
fvm_shared2::bigint::bigint_ser::BigIntDe,
59+
fvm_shared3::bigint::bigint_ser::BigIntDe,
60+
fvm_shared4::bigint::bigint_ser::BigIntDe
61+
);

src/rpc/registry/actors/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pub(crate) mod evm;
66
pub(crate) mod init;
77
pub(crate) mod miner;
88
pub(crate) mod power;
9+
pub(crate) mod reward;
910
pub(crate) mod system;

src/rpc/registry/actors/reward.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2019-2025 ChainSafe Systems
2+
// SPDX-License-Identifier: Apache-2.0, MIT
3+
4+
use crate::rpc::registry::methods_reg::{MethodRegistry, register_actor_methods};
5+
use crate::shim::message::MethodNum;
6+
use anyhow::Result;
7+
use cid::Cid;
8+
9+
macro_rules! register_reward_version_11_to_16 {
10+
($registry:expr, $code_cid:expr, $state_version:path) => {{
11+
use $state_version::{
12+
AwardBlockRewardParams, ConstructorParams, Method, UpdateNetworkKPIParams,
13+
};
14+
15+
register_actor_methods!(
16+
$registry,
17+
$code_cid,
18+
[
19+
(Method::Constructor, ConstructorParams),
20+
(Method::AwardBlockReward, AwardBlockRewardParams),
21+
(Method::UpdateNetworkKPI, UpdateNetworkKPIParams),
22+
]
23+
);
24+
25+
// Register methods without parameters
26+
register_actor_methods!($registry, $code_cid, [(Method::ThisEpochReward, empty)]);
27+
}};
28+
}
29+
30+
macro_rules! register_reward_version_8_to_10 {
31+
($registry:expr, $code_cid:expr, $state_version:path, $fvm_shared_version:path) => {{
32+
use $state_version::{AwardBlockRewardParams, Method};
33+
use $fvm_shared_version::{bigint::bigint_ser::BigIntDe};
34+
35+
register_actor_methods!(
36+
$registry,
37+
$code_cid,
38+
[
39+
(Method::Constructor, Option<BigIntDe>),
40+
(Method::AwardBlockReward, AwardBlockRewardParams),
41+
(Method::UpdateNetworkKPI, Option<BigIntDe>),
42+
]
43+
);
44+
45+
// Register methods without parameters
46+
register_actor_methods!($registry, $code_cid, [(Method::ThisEpochReward, empty)]);
47+
}};
48+
}
49+
50+
pub(crate) fn register_actor_methods(registry: &mut MethodRegistry, cid: Cid, version: u64) {
51+
match version {
52+
8 => {
53+
register_reward_version_8_to_10!(registry, cid, fil_actor_reward_state::v8, fvm_shared2)
54+
}
55+
9 => {
56+
register_reward_version_8_to_10!(registry, cid, fil_actor_reward_state::v9, fvm_shared2)
57+
}
58+
10 => {
59+
register_reward_version_8_to_10!(
60+
registry,
61+
cid,
62+
fil_actor_reward_state::v10,
63+
fvm_shared3
64+
)
65+
}
66+
11 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v11),
67+
12 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v12),
68+
13 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v13),
69+
14 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v14),
70+
15 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v15),
71+
16 => register_reward_version_11_to_16!(registry, cid, fil_actor_reward_state::v16),
72+
_ => {}
73+
}
74+
}

src/rpc/registry/methods_reg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl MethodRegistry {
7373
}
7474

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

7878
for (&cid, &(actor_type, version)) in ACTOR_REGISTRY.iter() {
7979
match actor_type {
@@ -85,6 +85,7 @@ impl MethodRegistry {
8585
BuiltinActor::Init => init::register_actor_methods(self, cid, version),
8686
BuiltinActor::System => system::register_actor_methods(self, cid, version),
8787
BuiltinActor::Power => power::register_actor_methods(self, cid, version),
88+
BuiltinActor::Reward => reward::register_actor_methods(self, cid, version),
8889
_ => {}
8990
}
9091
}

0 commit comments

Comments
 (0)