lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

stock_replacement.rs (8341B)


      1 use radroots_domain::{AgreementLineId, PoolId, Sequence, listing::MeasurementUnit, stock::*};
      2 
      3 fn ticks(value: u64) -> StockTicks {
      4     StockTicks::new(value).unwrap()
      5 }
      6 fn allocation(line: u8, pool: u8, quantity: u64) -> StockAllocation {
      7     StockAllocation::new(
      8         AgreementLineId::from_bytes([line; 32]),
      9         PoolId::from_bytes([pool; 32]),
     10         ticks(quantity),
     11     )
     12     .unwrap()
     13 }
     14 fn pool(id: u8, available: u64, committed: u64) -> PoolSnapshot {
     15     PoolSnapshot::new(
     16         PoolId::from_bytes([id; 32]),
     17         PoolUnit::new(MeasurementUnit::Each, 0).unwrap(),
     18         PoolBalances::new(
     19             ticks(available + committed),
     20             PhysicalBalances {
     21                 available: ticks(available),
     22                 committed_hold: ticks(committed),
     23                 ..PhysicalBalances::ZERO
     24             },
     25         )
     26         .unwrap(),
     27         Sequence::new(7).unwrap(),
     28     )
     29 }
     30 
     31 #[test]
     32 fn positive_differences_hold_before_issuance_and_leave_original_commitments_untouched() {
     33     let before = pool(1, 10, 3);
     34     let plan =
     35         plan_replacement_allocations(&[allocation(1, 1, 3)], &[allocation(1, 1, 5)], &[before])
     36             .unwrap();
     37     assert_eq!(plan.additional_allocations(), &[allocation(1, 1, 2)]);
     38     assert!(plan.pending_releases().is_empty());
     39     let hold = plan.preparatory_holds()[0];
     40     assert_eq!(hold.before(), before);
     41     assert_eq!(hold.after().balances().physical().available, ticks(8));
     42     assert_eq!(
     43         hold.after().balances().physical().preparatory_hold,
     44         ticks(2)
     45     );
     46     assert_eq!(hold.after().balances().physical().committed_hold, ticks(3));
     47     assert_eq!(
     48         hold.after().balances().introduced(),
     49         before.balances().introduced()
     50     );
     51     assert_eq!(before.balances().physical().available, ticks(10));
     52 }
     53 
     54 #[test]
     55 fn negative_changes_are_only_pending_original_draws_not_available_stock_or_version_steps() {
     56     let before = pool(1, 2, 5);
     57     let plan =
     58         plan_replacement_allocations(&[allocation(1, 1, 5)], &[allocation(1, 1, 2)], &[before])
     59             .unwrap();
     60     assert_eq!(plan.pending_releases(), &[allocation(1, 1, 3)]);
     61     assert!(plan.additional_allocations().is_empty());
     62     assert!(plan.preparatory_holds().is_empty());
     63     assert_eq!(before.version().get(), 7);
     64     assert_eq!(before.balances().physical().available, ticks(2));
     65     assert_eq!(before.balances().physical().committed_hold, ticks(5));
     66 }
     67 
     68 #[test]
     69 fn neither_same_pool_line_swaps_nor_cross_pool_releases_can_finance_positive_holds() {
     70     for (current, desired, pools) in [
     71         (
     72             vec![allocation(1, 1, 3)],
     73             vec![allocation(2, 1, 3)],
     74             vec![pool(1, 0, 3)],
     75         ),
     76         (
     77             vec![allocation(1, 1, 3)],
     78             vec![allocation(1, 2, 3)],
     79             vec![pool(1, 0, 3), pool(2, 2, 0)],
     80         ),
     81     ] {
     82         assert_eq!(
     83             plan_replacement_allocations(&current, &desired, &pools).unwrap_err(),
     84             AllocationPlanError::StaleState
     85         );
     86     }
     87     let current = [allocation(1, 1, 3)];
     88     let desired = [allocation(2, 1, 3)];
     89     let plan = plan_replacement_allocations(&current, &desired, &[pool(1, 3, 3)]).unwrap();
     90     assert_eq!(
     91         plan.preparatory_holds()[0]
     92             .after()
     93             .balances()
     94             .physical()
     95             .available,
     96         ticks(0)
     97     );
     98     assert_eq!(
     99         plan.preparatory_holds()[0]
    100             .after()
    101             .balances()
    102             .physical()
    103             .committed_hold,
    104         ticks(3)
    105     );
    106     assert_eq!(plan.pending_releases(), &current);
    107 }
    108 
    109 #[test]
    110 fn unchanged_obligations_do_not_cure_deficits_or_replace_original_pool_identity() {
    111     let outstanding = [allocation(1, 1, 5)];
    112     let original = pool(1, 10, 3); // The other two obligation ticks have no backing.
    113     let plan = plan_replacement_allocations(&outstanding, &outstanding, &[original]).unwrap();
    114     assert!(plan.additional_allocations().is_empty());
    115     assert!(plan.pending_releases().is_empty());
    116     assert!(plan.preparatory_holds().is_empty());
    117     assert_eq!(original.balances().physical().committed_hold, ticks(3));
    118     let changed = [allocation(1, 2, 5)];
    119     let plan =
    120         plan_replacement_allocations(&outstanding, &changed, &[original, pool(2, 5, 0)]).unwrap();
    121     assert_eq!(plan.pending_releases(), &outstanding);
    122     assert_eq!(plan.additional_allocations(), &changed);
    123     assert_eq!(
    124         plan.preparatory_holds()[0].before().id(),
    125         changed[0].pool_id()
    126     );
    127 }
    128 
    129 #[test]
    130 fn complete_multi_pool_plan_is_stable_sorted_and_aggregates_only_additional_ticks() {
    131     let current = [allocation(2, 2, 3), allocation(1, 1, 2)];
    132     let desired = [
    133         allocation(3, 2, 1),
    134         allocation(2, 2, 5),
    135         allocation(1, 1, 1),
    136     ];
    137     let pools = [pool(2, 10, 3), pool(1, 4, 2)];
    138     let plan = plan_replacement_allocations(&current, &desired, &pools).unwrap();
    139     assert_eq!(
    140         plan.additional_allocations(),
    141         &[allocation(2, 2, 2), allocation(3, 2, 1)]
    142     );
    143     assert_eq!(plan.pending_releases(), &[allocation(1, 1, 1)]);
    144     assert_eq!(plan.preparatory_holds().len(), 1);
    145     assert_eq!(plan.preparatory_holds()[0].quantity(), ticks(3));
    146     let reverse = plan_replacement_allocations(
    147         &current.into_iter().rev().collect::<Vec<_>>(),
    148         &desired.into_iter().rev().collect::<Vec<_>>(),
    149         &pools.into_iter().rev().collect::<Vec<_>>(),
    150     )
    151     .unwrap();
    152     assert_eq!(
    153         plan.additional_allocations(),
    154         reverse.additional_allocations()
    155     );
    156     assert_eq!(plan.pending_releases(), reverse.pending_releases());
    157     assert_eq!(plan.preparatory_holds(), reverse.preparatory_holds());
    158 }
    159 
    160 #[test]
    161 fn duplicate_unknown_stale_and_over_budget_data_never_make_partial_plans() {
    162     let value = allocation(1, 1, 1);
    163     for (current, desired, pools) in [
    164         (vec![value, value], vec![], vec![pool(1, 4, 2)]),
    165         (vec![], vec![value, value], vec![pool(1, 4, 2)]),
    166         (vec![value], vec![], vec![]),
    167         (vec![], vec![value], vec![]),
    168         (vec![], vec![value], vec![pool(1, 4, 2), pool(1, 4, 2)]),
    169     ] {
    170         assert!(plan_replacement_allocations(&current, &desired, &pools).is_err());
    171     }
    172     assert_eq!(
    173         plan_replacement_allocations(&vec![value; 65], &[], &[pool(1, 4, 2)]).unwrap_err(),
    174         AllocationPlanError::Capacity
    175     );
    176     assert_eq!(
    177         plan_replacement_allocations(&[], &vec![value; 65], &[pool(1, 4, 2)]).unwrap_err(),
    178         AllocationPlanError::Capacity
    179     );
    180     assert_eq!(
    181         plan_replacement_allocations(&[], &[], &vec![pool(1, 4, 2); 97]).unwrap_err(),
    182         AllocationPlanError::Capacity
    183     );
    184     let old = pool(1, 4, 2);
    185     let absent = PoolSnapshot::new(
    186         old.id(),
    187         old.unit(),
    188         old.balances(),
    189         Sequence::new(0).unwrap(),
    190     );
    191     assert!(plan_replacement_allocations(&[], &[value], &[absent]).is_err());
    192 }
    193 
    194 #[test]
    195 fn bounded_small_state_grid_conserves_each_positive_hold_without_crediting_any_release() {
    196     for old in 0..=5 {
    197         for new in 0..=5 {
    198             let current = if old == 0 {
    199                 vec![]
    200             } else {
    201                 vec![allocation(1, 1, old)]
    202             };
    203             let desired = if new == 0 {
    204                 vec![]
    205             } else {
    206                 vec![allocation(1, 1, new)]
    207             };
    208             let before = pool(1, 5, old);
    209             let plan = plan_replacement_allocations(&current, &desired, &[before]).unwrap();
    210             if new > old {
    211                 let effect = plan.preparatory_holds()[0];
    212                 assert_eq!(effect.quantity(), ticks(new - old));
    213                 assert_eq!(
    214                     effect.after().balances().physical().available,
    215                     ticks(5 - (new - old))
    216                 );
    217                 assert_eq!(
    218                     effect.after().balances().physical().committed_hold,
    219                     ticks(old)
    220                 );
    221                 assert_eq!(effect.after().balances().introduced(), ticks(5 + old));
    222             } else {
    223                 assert!(plan.preparatory_holds().is_empty());
    224             }
    225             if old > new {
    226                 assert_eq!(plan.pending_releases(), &[allocation(1, 1, old - new)]);
    227             } else {
    228                 assert!(plan.pending_releases().is_empty());
    229             }
    230         }
    231     }
    232 }