diff --git a/frame/staking/src/darwinia_tests.rs b/frame/staking/src/darwinia_tests.rs index a100f2624d..8ba47743c9 100644 --- a/frame/staking/src/darwinia_tests.rs +++ b/frame/staking/src/darwinia_tests.rs @@ -550,6 +550,72 @@ fn punished_claim_should_work() { assert_eq!(Staking::ledger(controller).unwrap(), ledger); assert_eq!(Kton::free_balance(&stash), COIN - 3); }); + + // slash value for unbond deposit claim after a duration should correct + ExtBuilder::default().build().execute_with(|| { + let (stash, controller) = (1001, 1000); + let promise_month = 36; + let bond_value = 10 * COIN; + let deposit_item_expire_time = INIT_TIMESTAMP + promise_month * MONTH_IN_MILLISECONDS; + let _ = Ring::deposit_creating(&stash, 1000 * COIN); + + let mut ledger = StakingLedger { + stash, + active_ring: bond_value, + active_deposit_ring: bond_value, + deposit_items: vec![TimeDepositItem { + value: bond_value, + start_time: INIT_TIMESTAMP, + expire_time: deposit_item_expire_time, + }], + ring_staking_lock: StakingLock { + staking_amount: bond_value, + unbondings: vec![], + }, + ..Default::default() + }; + + // will emit RawEvent::BondRing + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::RingBalance(bond_value), + RewardDestination::Stash, + promise_month as u8, + )); + assert_eq!(Staking::ledger(controller).unwrap(), ledger); + + // set a fake blockchain time to simulate elapsed time + Timestamp::set_timestamp(Timestamp::now() + 14 * MONTH_IN_MILLISECONDS); + assert_ok!(Staking::try_claim_deposits_with_punish( + Origin::signed(controller), + deposit_item_expire_time, + )); + // ledger no change cause no kton for punishment + assert_eq!(Staking::ledger(controller).unwrap(), ledger); + + // Set more kton balance to make it work. + let _ = Kton::deposit_creating(&stash, COIN); + let free_kton = Kton::free_balance(&stash); + assert_ok!(Staking::try_claim_deposits_with_punish( + Origin::signed(controller), + deposit_item_expire_time, + )); + + // should claim success + let slashed: KtonBalance = inflation::compute_kton_reward::(bond_value, 36) + - inflation::compute_kton_reward::(bond_value, 14); + assert_eq!( + staking_events()[1], + RawEvent::DepositsClaimedWithPunish(ledger.stash.clone(), slashed * 3) + ); + // assert leger + ledger.active_deposit_ring -= bond_value; + ledger.deposit_items.clear(); + + assert_eq!(Staking::ledger(controller).unwrap(), ledger); + assert_eq!(Kton::free_balance(&stash), free_kton - slashed * 3); + }); } #[test] @@ -582,7 +648,7 @@ fn deposit_zero_should_do_nothing() { // NO-OP assert_ok!(Staking::deposit_extra(Origin::signed(stash), COIN, 1)); assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 1); - }) + }); } #[test] diff --git a/frame/staking/src/inflation_tests.rs b/frame/staking/src/inflation_tests.rs index 42f4a7fccd..50e49b695c 100644 --- a/frame/staking/src/inflation_tests.rs +++ b/frame/staking/src/inflation_tests.rs @@ -213,3 +213,165 @@ fn print_total_payout_error_rate() { println!("total inflation: {}", total_inflation); println!("total left: {}", total_left); } + +#[test] +fn kton_slash_should_work() { + let exp_slashes: [&[Balance]; 36] = [ + &[76142], + &[152284, 76142], + &[233502, 157360, 81218], + &[309644, 233502, 157360, 76142], + &[395939, 319797, 243655, 162437, 86295], + &[477157, 401015, 324873, 243655, 167513, 81218], + &[563451, 487309, 411167, 329949, 253807, 167512, 86294], + &[ + 644670, 568528, 492386, 411168, 335026, 248731, 167513, 81219, + ], + &[ + 730964, 654822, 578680, 497462, 421320, 335025, 253807, 167513, 86294, + ], + &[ + 822335, 746193, 670051, 588833, 512691, 426396, 345178, 258884, 177665, 91371, + ], + &[ + 908629, 832487, 756345, 675127, 598985, 512690, 431472, 345178, 263959, 177665, 86294, + ], + &[ + 1000000, 923858, 847716, 766498, 690356, 604061, 522843, 436549, 355330, 269036, + 177665, 91371, + ], + &[ + 1091370, 1015228, 939086, 857868, 781726, 695431, 614213, 527919, 446700, 360406, + 269035, 182741, 91370, + ], + &[ + 1187817, 1111675, 1035533, 954315, 878173, 791878, 710660, 624366, 543147, 456853, + 365482, 279188, 187817, 96447, + ], + &[ + 1284263, 1208121, 1131979, 1050761, 974619, 888324, 807106, 720812, 639593, 553299, + 461928, 375634, 284263, 192893, 96446, + ], + &[ + 1380710, 1304568, 1228426, 1147208, 1071066, 984771, 903553, 817259, 736040, 649746, + 558375, 472081, 380710, 289340, 192893, 96447, + ], + &[ + 1477157, 1401015, 1324873, 1243655, 1167513, 1081218, 1000000, 913706, 832487, 746193, + 654822, 568528, 477157, 385787, 289340, 192894, 96447, + ], + &[ + 1573604, 1497462, 1421320, 1340102, 1263960, 1177665, 1096447, 1010153, 928934, 842640, + 751269, 664975, 573604, 482234, 385787, 289341, 192894, 96447, + ], + &[ + 1675126, 1598984, 1522842, 1441624, 1365482, 1279187, 1197969, 1111675, 1030456, + 944162, 852791, 766497, 675126, 583756, 487309, 390863, 294416, 197969, 101522, + ], + &[ + 1776649, 1700507, 1624365, 1543147, 1467005, 1380710, 1299492, 1213198, 1131979, + 1045685, 954314, 868020, 776649, 685279, 588832, 492386, 395939, 299492, 203045, + 101523, + ], + &[ + 1883248, 1807106, 1730964, 1649746, 1573604, 1487309, 1406091, 1319797, 1238578, + 1152284, 1060913, 974619, 883248, 791878, 695431, 598985, 502538, 406091, 309644, + 208122, 106599, + ], + &[ + 1989847, 1913705, 1837563, 1756345, 1680203, 1593908, 1512690, 1426396, 1345177, + 1258883, 1167512, 1081218, 989847, 898477, 802030, 705584, 609137, 512690, 416243, + 314721, 213198, 106599, + ], + &[ + 2096446, 2020304, 1944162, 1862944, 1786802, 1700507, 1619289, 1532995, 1451776, + 1365482, 1274111, 1187817, 1096446, 1005076, 908629, 812183, 715736, 619289, 522842, + 421320, 319797, 213198, 106599, + ], + &[ + 2203045, 2126903, 2050761, 1969543, 1893401, 1807106, 1725888, 1639594, 1558375, + 1472081, 1380710, 1294416, 1203045, 1111675, 1015228, 918782, 822335, 725888, 629441, + 527919, 426396, 319797, 213198, 106599, + ], + &[ + 2314720, 2238578, 2162436, 2081218, 2005076, 1918781, 1837563, 1751269, 1670050, + 1583756, 1492385, 1406091, 1314720, 1223350, 1126903, 1030457, 934010, 837563, 741116, + 639594, 538071, 431472, 324873, 218274, 111675, + ], + &[ + 2426395, 2350253, 2274111, 2192893, 2116751, 2030456, 1949238, 1862944, 1781725, + 1695431, 1604060, 1517766, 1426395, 1335025, 1238578, 1142132, 1045685, 949238, 852791, + 751269, 649746, 543147, 436548, 329949, 223350, 111675, + ], + &[ + 2538071, 2461929, 2385787, 2304569, 2228427, 2142132, 2060914, 1974620, 1893401, + 1807107, 1715736, 1629442, 1538071, 1446701, 1350254, 1253808, 1157361, 1060914, + 964467, 862945, 761422, 654823, 548224, 441625, 335026, 223351, 111676, + ], + &[ + 2654822, 2578680, 2502538, 2421320, 2345178, 2258883, 2177665, 2091371, 2010152, + 1923858, 1832487, 1746193, 1654822, 1563452, 1467005, 1370559, 1274112, 1177665, + 1081218, 979696, 878173, 771574, 664975, 558376, 451777, 340102, 228427, 116751, + ], + &[ + 2771573, 2695431, 2619289, 2538071, 2461929, 2375634, 2294416, 2208122, 2126903, + 2040609, 1949238, 1862944, 1771573, 1680203, 1583756, 1487310, 1390863, 1294416, + 1197969, 1096447, 994924, 888325, 781726, 675127, 568528, 456853, 345178, 233502, + 116751, + ], + &[ + 2893401, 2817259, 2741117, 2659899, 2583757, 2497462, 2416244, 2329950, 2248731, + 2162437, 2071066, 1984772, 1893401, 1802031, 1705584, 1609138, 1512691, 1416244, + 1319797, 1218275, 1116752, 1010153, 903554, 796955, 690356, 578681, 467006, 355330, + 238579, 121828, + ], + &[ + 3010152, 2934010, 2857868, 2776650, 2700508, 2614213, 2532995, 2446701, 2365482, + 2279188, 2187817, 2101523, 2010152, 1918782, 1822335, 1725889, 1629442, 1532995, + 1436548, 1335026, 1233503, 1126904, 1020305, 913706, 807107, 695432, 583757, 472081, + 355330, 238579, 116751, + ], + &[ + 3137055, 3060913, 2984771, 2903553, 2827411, 2741116, 2659898, 2573604, 2492385, + 2406091, 2314720, 2228426, 2137055, 2045685, 1949238, 1852792, 1756345, 1659898, + 1563451, 1461929, 1360406, 1253807, 1147208, 1040609, 934010, 822335, 710660, 598984, + 482233, 365482, 243654, 126903, + ], + &[ + 3258883, 3182741, 3106599, 3025381, 2949239, 2862944, 2781726, 2695432, 2614213, + 2527919, 2436548, 2350254, 2258883, 2167513, 2071066, 1974620, 1878173, 1781726, + 1685279, 1583757, 1482234, 1375635, 1269036, 1162437, 1055838, 944163, 832488, 720812, + 604061, 487310, 365482, 248731, 121828, + ], + &[ + 3385786, 3309644, 3233502, 3152284, 3076142, 2989847, 2908629, 2822335, 2741116, + 2654822, 2563451, 2477157, 2385786, 2294416, 2197969, 2101523, 2005076, 1908629, + 1812182, 1710660, 1609137, 1502538, 1395939, 1289340, 1182741, 1071066, 959391, 847715, + 730964, 614213, 492385, 375634, 248731, 126903, + ], + &[ + 3512690, 3436548, 3360406, 3279188, 3203046, 3116751, 3035533, 2949239, 2868020, + 2781726, 2690355, 2604061, 2512690, 2421320, 2324873, 2228427, 2131980, 2035533, + 1939086, 1837564, 1736041, 1629442, 1522843, 1416244, 1309645, 1197970, 1086295, + 974619, 857868, 741117, 619289, 502538, 375635, 253807, 126904, + ], + &[ + 3644670, 3568528, 3492386, 3411168, 3335026, 3248731, 3167513, 3081219, 3000000, + 2913706, 2822335, 2736041, 2644670, 2553300, 2456853, 2360407, 2263960, 2167513, + 2071066, 1969544, 1868021, 1761422, 1654823, 1548224, 1441625, 1329950, 1218275, + 1106599, 989848, 873097, 751269, 634518, 507615, 385787, 258884, 131980, + ], + ]; + + for promise_month in 1..=36 { + for passed_month in 0..=promise_month - 1 { + let kton_reward = compute_kton_reward::(10 * COIN, promise_month); + let slashes = kton_reward - compute_kton_reward::(10 * COIN, passed_month); + + assert_eq!( + slashes, + exp_slashes[promise_month as usize - 1][passed_month as usize] + ); + } + } +}