|
| 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); |
0 commit comments