radix_engine/blueprints/consensus_manager/
package.rs

1use crate::blueprints::consensus_manager::{ConsensusManagerBlueprint, ValidatorBlueprint};
2use crate::errors::{ApplicationError, RuntimeError};
3use crate::internal_prelude::*;
4use radix_engine_interface::api::SystemApi;
5use radix_engine_interface::blueprints::consensus_manager::*;
6use radix_engine_interface::blueprints::package::PackageDefinition;
7
8pub const VALIDATOR_ROLE: &str = "validator";
9
10pub struct ConsensusManagerNativePackage;
11
12impl ConsensusManagerNativePackage {
13    pub fn definition() -> PackageDefinition {
14        let blueprints = indexmap!(
15            CONSENSUS_MANAGER_BLUEPRINT.to_string() => ConsensusManagerBlueprint::definition(),
16            VALIDATOR_BLUEPRINT.to_string() => ValidatorBlueprint::definition(),
17        );
18
19        PackageDefinition { blueprints }
20    }
21
22    pub fn invoke_export<Y: SystemApi<RuntimeError>>(
23        export_name: &str,
24        input: &IndexedScryptoValue,
25        api: &mut Y,
26    ) -> Result<IndexedScryptoValue, RuntimeError> {
27        match export_name {
28            CONSENSUS_MANAGER_CREATE_IDENT => {
29                let input: ConsensusManagerCreateInput = input.as_typed().map_err(|e| {
30                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
31                })?;
32                let rtn = ConsensusManagerBlueprint::create(
33                    input.validator_owner_token_address,
34                    input.component_address,
35                    input.initial_epoch,
36                    input.initial_config,
37                    input.initial_time_ms,
38                    input.initial_current_leader,
39                    api,
40                )?;
41                Ok(IndexedScryptoValue::from_typed(&rtn))
42            }
43            CONSENSUS_MANAGER_GET_CURRENT_EPOCH_IDENT => {
44                let _input: ConsensusManagerGetCurrentEpochInput =
45                    input.as_typed().map_err(|e| {
46                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
47                    })?;
48
49                let rtn = ConsensusManagerBlueprint::get_current_epoch(api)?;
50                Ok(IndexedScryptoValue::from_typed(&rtn))
51            }
52            CONSENSUS_MANAGER_START_IDENT => {
53                let _input: ConsensusManagerStartInput = input.as_typed().map_err(|e| {
54                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
55                })?;
56                let rtn = ConsensusManagerBlueprint::start(api)?;
57
58                Ok(IndexedScryptoValue::from_typed(&rtn))
59            }
60            CONSENSUS_MANAGER_GET_CURRENT_TIME_IDENT => {
61                let input: ConsensusManagerGetCurrentTimeInputV1 =
62                    input.as_typed().map_err(|e| {
63                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
64                    })?;
65                let rtn = ConsensusManagerBlueprint::get_current_time_v1(input.precision, api)?;
66
67                Ok(IndexedScryptoValue::from_typed(&rtn))
68            }
69            CONSENSUS_MANAGER_COMPARE_CURRENT_TIME_IDENT => {
70                let input: ConsensusManagerCompareCurrentTimeInputV1 =
71                    input.as_typed().map_err(|e| {
72                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
73                    })?;
74                let rtn = ConsensusManagerBlueprint::compare_current_time_v1(
75                    input.instant,
76                    input.precision,
77                    input.operator,
78                    api,
79                )?;
80
81                Ok(IndexedScryptoValue::from_typed(&rtn))
82            }
83            CONSENSUS_MANAGER_NEXT_ROUND_IDENT => {
84                let input: ConsensusManagerNextRoundInput = input.as_typed().map_err(|e| {
85                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
86                })?;
87                let rtn = ConsensusManagerBlueprint::next_round(
88                    input.round,
89                    input.proposer_timestamp_ms,
90                    input.leader_proposal_history,
91                    api,
92                )?;
93
94                Ok(IndexedScryptoValue::from_typed(&rtn))
95            }
96            CONSENSUS_MANAGER_CREATE_VALIDATOR_IDENT => {
97                let input: ConsensusManagerCreateValidatorInput =
98                    input.as_typed().map_err(|e| {
99                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
100                    })?;
101                let rtn = ConsensusManagerBlueprint::create_validator(
102                    input.key,
103                    input.fee_factor,
104                    input.xrd_payment,
105                    api,
106                )?;
107
108                Ok(IndexedScryptoValue::from_typed(&rtn))
109            }
110            VALIDATOR_REGISTER_IDENT => {
111                let _input: ValidatorRegisterInput = input.as_typed().map_err(|e| {
112                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
113                })?;
114                let rtn = ValidatorBlueprint::register(api)?;
115                Ok(IndexedScryptoValue::from_typed(&rtn))
116            }
117            VALIDATOR_UNREGISTER_IDENT => {
118                let _input: ValidatorUnregisterInput = input.as_typed().map_err(|e| {
119                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
120                })?;
121                let rtn = ValidatorBlueprint::unregister(api)?;
122                Ok(IndexedScryptoValue::from_typed(&rtn))
123            }
124            VALIDATOR_STAKE_AS_OWNER_IDENT => {
125                let input: ValidatorStakeAsOwnerInput = input.as_typed().map_err(|e| {
126                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
127                })?;
128                let rtn = ValidatorBlueprint::stake_as_owner(input.stake, api)?;
129                Ok(IndexedScryptoValue::from_typed(&rtn))
130            }
131            VALIDATOR_STAKE_IDENT => {
132                let input: ValidatorStakeInput = input.as_typed().map_err(|e| {
133                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
134                })?;
135                let rtn = ValidatorBlueprint::stake(input.stake, api)?;
136                Ok(IndexedScryptoValue::from_typed(&rtn))
137            }
138            VALIDATOR_UNSTAKE_IDENT => {
139                let input: ValidatorUnstakeInput = input.as_typed().map_err(|e| {
140                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
141                })?;
142                let rtn = ValidatorBlueprint::unstake(input.stake_unit_bucket, api)?;
143                Ok(IndexedScryptoValue::from_typed(&rtn))
144            }
145            VALIDATOR_CLAIM_XRD_IDENT => {
146                let input: ValidatorClaimXrdInput = input.as_typed().map_err(|e| {
147                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
148                })?;
149                let rtn = ValidatorBlueprint::claim_xrd(input.bucket, api)?;
150                Ok(IndexedScryptoValue::from_typed(&rtn))
151            }
152            VALIDATOR_UPDATE_KEY_IDENT => {
153                let input: ValidatorUpdateKeyInput = input.as_typed().map_err(|e| {
154                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
155                })?;
156                let rtn = ValidatorBlueprint::update_key(input.key, api)?;
157                Ok(IndexedScryptoValue::from_typed(&rtn))
158            }
159            VALIDATOR_UPDATE_FEE_IDENT => {
160                let input: ValidatorUpdateFeeInput = input.as_typed().map_err(|e| {
161                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
162                })?;
163                let rtn = ValidatorBlueprint::update_fee(input.new_fee_factor, api)?;
164                Ok(IndexedScryptoValue::from_typed(&rtn))
165            }
166            VALIDATOR_UPDATE_ACCEPT_DELEGATED_STAKE_IDENT => {
167                let input: ValidatorUpdateAcceptDelegatedStakeInput =
168                    input.as_typed().map_err(|e| {
169                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
170                    })?;
171                let rtn = ValidatorBlueprint::update_accept_delegated_stake(
172                    input.accept_delegated_stake,
173                    api,
174                )?;
175                Ok(IndexedScryptoValue::from_typed(&rtn))
176            }
177            VALIDATOR_ACCEPTS_DELEGATED_STAKE_IDENT => {
178                let _: ValidatorAcceptsDelegatedStakeInput = input.as_typed().map_err(|e| {
179                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
180                })?;
181                let rtn = ValidatorBlueprint::accepts_delegated_stake(api)?;
182                Ok(IndexedScryptoValue::from_typed(&rtn))
183            }
184            VALIDATOR_TOTAL_STAKE_XRD_AMOUNT_IDENT => {
185                let _: ValidatorTotalStakeXrdAmountInput = input.as_typed().map_err(|e| {
186                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
187                })?;
188                let rtn = ValidatorBlueprint::total_stake_xrd_amount(api)?;
189                Ok(IndexedScryptoValue::from_typed(&rtn))
190            }
191            VALIDATOR_TOTAL_STAKE_UNIT_SUPPLY_IDENT => {
192                let _: ValidatorTotalStakeUnitSupplyInput = input.as_typed().map_err(|e| {
193                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
194                })?;
195                let rtn = ValidatorBlueprint::total_stake_unit_supply(api)?;
196                Ok(IndexedScryptoValue::from_typed(&rtn))
197            }
198            VALIDATOR_GET_REDEMPTION_VALUE_IDENT => {
199                let input: ValidatorGetRedemptionValueInput = input.as_typed().map_err(|e| {
200                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
201                })?;
202                let rtn =
203                    ValidatorBlueprint::get_redemption_value(input.amount_of_stake_units, api)?;
204                Ok(IndexedScryptoValue::from_typed(&rtn))
205            }
206            VALIDATOR_SIGNAL_PROTOCOL_UPDATE_READINESS_IDENT => {
207                let input: ValidatorSignalProtocolUpdateReadinessInput =
208                    input.as_typed().map_err(|e| {
209                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
210                    })?;
211                let rtn = ValidatorBlueprint::signal_protocol_update_readiness(input.vote, api)?;
212                Ok(IndexedScryptoValue::from_typed(&rtn))
213            }
214            VALIDATOR_GET_PROTOCOL_UPDATE_READINESS_IDENT => {
215                let _input: ValidatorGetProtocolUpdateReadinessInput =
216                    input.as_typed().map_err(|e| {
217                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
218                    })?;
219                let rtn = ValidatorBlueprint::get_protocol_update_readiness(api)?;
220                Ok(IndexedScryptoValue::from_typed(&rtn))
221            }
222            VALIDATOR_LOCK_OWNER_STAKE_UNITS_IDENT => {
223                let input: ValidatorLockOwnerStakeUnitsInput = input.as_typed().map_err(|e| {
224                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
225                })?;
226                let rtn = ValidatorBlueprint::lock_owner_stake_units(input.stake_unit_bucket, api)?;
227                Ok(IndexedScryptoValue::from_typed(&rtn))
228            }
229            VALIDATOR_START_UNLOCK_OWNER_STAKE_UNITS_IDENT => {
230                let input: ValidatorStartUnlockOwnerStakeUnitsInput =
231                    input.as_typed().map_err(|e| {
232                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
233                    })?;
234                let rtn = ValidatorBlueprint::start_unlock_owner_stake_units(
235                    input.requested_stake_unit_amount,
236                    api,
237                )?;
238                Ok(IndexedScryptoValue::from_typed(&rtn))
239            }
240            VALIDATOR_FINISH_UNLOCK_OWNER_STAKE_UNITS_IDENT => {
241                let _input: ValidatorFinishUnlockOwnerStakeUnitsInput =
242                    input.as_typed().map_err(|e| {
243                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
244                    })?;
245                let rtn = ValidatorBlueprint::finish_unlock_owner_stake_units(api)?;
246                Ok(IndexedScryptoValue::from_typed(&rtn))
247            }
248            VALIDATOR_APPLY_EMISSION_IDENT => {
249                let input: ValidatorApplyEmissionInput = input.as_typed().map_err(|e| {
250                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
251                })?;
252                let rtn = ValidatorBlueprint::apply_emission(
253                    input.xrd_bucket,
254                    input.epoch,
255                    input.proposals_made,
256                    input.proposals_missed,
257                    api,
258                )?;
259                Ok(IndexedScryptoValue::from_typed(&rtn))
260            }
261            VALIDATOR_APPLY_REWARD_IDENT => {
262                let input: ValidatorApplyRewardInput = input.as_typed().map_err(|e| {
263                    RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
264                })?;
265                let rtn = ValidatorBlueprint::apply_reward(input.xrd_bucket, input.epoch, api)?;
266                Ok(IndexedScryptoValue::from_typed(&rtn))
267            }
268            _ => Err(RuntimeError::ApplicationError(
269                ApplicationError::ExportDoesNotExist(export_name.to_string()),
270            )),
271        }
272    }
273}
274
275pub struct ConsensusManagerSecondsPrecisionNativeCode;
276
277impl ConsensusManagerSecondsPrecisionNativeCode {
278    pub fn invoke_export<Y: SystemApi<RuntimeError>>(
279        export_name: &str,
280        input: &IndexedScryptoValue,
281        api: &mut Y,
282    ) -> Result<IndexedScryptoValue, RuntimeError> {
283        match export_name {
284            CONSENSUS_MANAGER_GET_CURRENT_TIME_IDENT => {
285                let input: ConsensusManagerGetCurrentTimeInputV2 =
286                    input.as_typed().map_err(|e| {
287                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
288                    })?;
289                let rtn = ConsensusManagerBlueprint::get_current_time_v2(input.precision, api)?;
290
291                Ok(IndexedScryptoValue::from_typed(&rtn))
292            }
293            CONSENSUS_MANAGER_COMPARE_CURRENT_TIME_IDENT => {
294                let input: ConsensusManagerCompareCurrentTimeInputV2 =
295                    input.as_typed().map_err(|e| {
296                        RuntimeError::ApplicationError(ApplicationError::InputDecodeError(e))
297                    })?;
298                let rtn = ConsensusManagerBlueprint::compare_current_time_v2(
299                    input.instant,
300                    input.precision,
301                    input.operator,
302                    api,
303                )?;
304
305                Ok(IndexedScryptoValue::from_typed(&rtn))
306            }
307            _ => Err(RuntimeError::ApplicationError(
308                ApplicationError::ExportDoesNotExist(export_name.to_string()),
309            )),
310        }
311    }
312}