|
1 |
| -use std::collections::{btree_map::Entry, BTreeMap}; |
| 1 | +use std::{ |
| 2 | + collections::{btree_map::Entry, BTreeMap}, |
| 3 | + ops::{Add, Div, Mul, Sub}, |
| 4 | +}; |
2 | 5 |
|
3 | 6 | use itertools::Itertools;
|
4 | 7 | use serde::{Deserialize, Serialize};
|
5 | 8 |
|
| 9 | +use crate::{chain::PublicKeyHash, transaction::StakeTransaction, wit::Wit}; |
| 10 | + |
6 | 11 | use super::prelude::*;
|
7 | 12 |
|
8 | 13 | /// The main data structure that provides the "stakes tracker" functionality.
|
@@ -34,20 +39,16 @@ where
|
34 | 39 | + Default
|
35 | 40 | + Ord
|
36 | 41 | + From<u64>
|
| 42 | + + Into<u64> |
37 | 43 | + num_traits::Zero
|
38 |
| - + std::ops::Add<Output = Coins> |
39 |
| - + std::ops::Sub<Output = Coins> |
40 |
| - + std::ops::Mul |
41 |
| - + std::ops::Mul<Epoch, Output = Power>, |
| 44 | + + Add<Output = Coins> |
| 45 | + + Sub<Output = Coins> |
| 46 | + + Mul |
| 47 | + + Mul<Epoch, Output = Power>, |
42 | 48 | Address: Clone + Ord + 'static,
|
43 |
| - Epoch: Copy + Default + num_traits::Saturating + std::ops::Sub<Output = Epoch>, |
44 |
| - Power: Copy |
45 |
| - + Default |
46 |
| - + Ord |
47 |
| - + std::ops::Add<Output = Power> |
48 |
| - + std::ops::Div<Output = Power> |
49 |
| - + std::ops::Div<Coins, Output = Epoch> |
50 |
| - + 'static, |
| 49 | + Epoch: Copy + Default + num_traits::Saturating + Sub<Output = Epoch> + From<u32>, |
| 50 | + Power: Copy + Default + Ord + Add<Output = Power> + Div<Output = Power>, |
| 51 | + u64: From<Coins> + From<Power>, |
51 | 52 | {
|
52 | 53 | /// Register a certain amount of additional stake for a certain address and epoch.
|
53 | 54 | pub fn add_stake<ISK>(
|
@@ -224,6 +225,59 @@ where
|
224 | 225 | }
|
225 | 226 | }
|
226 | 227 |
|
| 228 | +/// Adds stake, based on the data from a stake transaction. |
| 229 | +/// |
| 230 | +/// This function was made static instead of adding it to `impl Stakes` because it is not generic over `Address` and |
| 231 | +/// `Coins`. |
| 232 | +pub fn process_stake_transaction<Epoch, Power>( |
| 233 | + stakes: &mut Stakes<PublicKeyHash, Wit, Epoch, Power>, |
| 234 | + transaction: &StakeTransaction, |
| 235 | + epoch: Epoch, |
| 236 | +) -> StakingResult<(), PublicKeyHash, Wit, Epoch> |
| 237 | +where |
| 238 | + Epoch: Copy + Default + Sub<Output = Epoch> + num_traits::Saturating + From<u32>, |
| 239 | + Power: |
| 240 | + Add<Output = Power> + Copy + Default + Div<Output = Power> + Ord, |
| 241 | + Wit: Mul<Epoch, Output = Power>, |
| 242 | + u64: From<Wit> + From<Power>, |
| 243 | +{ |
| 244 | + // This line would check that the authorization message is valid for the provided validator and withdrawer |
| 245 | + // address. But it is commented out here because stake transactions should be validated upfront (when |
| 246 | + // considering block candidates). The line is reproduced here for later reference when implementing those |
| 247 | + // validations. Once those are in place, we're ok to remove this comment. |
| 248 | + //transaction.body.authorization_is_valid().map_err(|_| StakesError::InvalidAuthentication)?; |
| 249 | + |
| 250 | + let key = transaction.body.output.key.clone(); |
| 251 | + let coins = Wit::from_nanowits(transaction.body.output.value); |
| 252 | + |
| 253 | + stakes.add_stake(key, coins, epoch)?; |
| 254 | + |
| 255 | + Ok(()) |
| 256 | +} |
| 257 | + |
| 258 | +/// Adds stakes, based on the data from multiple stake transactions. |
| 259 | +/// |
| 260 | +/// This function was made static instead of adding it to `impl Stakes` because it is not generic over `Address` and |
| 261 | +/// `Coins`. |
| 262 | +pub fn process_stake_transactions<'a, Epoch, Power>( |
| 263 | + stakes: &mut Stakes<PublicKeyHash, Wit, Epoch, Power>, |
| 264 | + transactions: impl Iterator<Item = &'a StakeTransaction>, |
| 265 | + epoch: Epoch, |
| 266 | +) -> Result<(), StakesError<PublicKeyHash, Wit, Epoch>> |
| 267 | +where |
| 268 | + Epoch: Copy + Default + Sub<Output = Epoch> + num_traits::Saturating + From<u32>, |
| 269 | + Power: |
| 270 | + Add<Output = Power> + Copy + Default + Div<Output = Power> + Ord, |
| 271 | + Wit: Mul<Epoch, Output = Power>, |
| 272 | + u64: From<Wit> + From<Power>, |
| 273 | +{ |
| 274 | + for transaction in transactions { |
| 275 | + process_stake_transaction(stakes, transaction, epoch)?; |
| 276 | + } |
| 277 | + |
| 278 | + Ok(()) |
| 279 | +} |
| 280 | + |
227 | 281 | #[cfg(test)]
|
228 | 282 | mod tests {
|
229 | 283 | use super::*;
|
|
0 commit comments