use crate::{
amount::{Amount, NegativeAllowed, NonNegative, MAX_MONEY},
value_balance::{ValueBalance, ValueBalanceError},
};
#[test]
fn ironwood_pool_enforces_non_negative_balance() {
let _init_guard = zebra_test::init();
let chain = ValueBalance::<NonNegative>::zero()
.add_chain_value_pool_change(ValueBalance::from_ironwood_amount(
Amount::<NegativeAllowed>::try_from(100).expect("valid amount"),
))
.expect("adding positive ironwood value to an empty pool is valid");
assert_eq!(
chain.ironwood_amount(),
Amount::<NonNegative>::try_from(100).expect("valid amount"),
);
let error = chain
.add_chain_value_pool_change(ValueBalance::from_ironwood_amount(
Amount::<NegativeAllowed>::try_from(-101).expect("valid amount"),
))
.expect_err("draining the ironwood pool below zero must be rejected");
assert!(matches!(error, ValueBalanceError::Ironwood(_)));
}
#[test]
fn total_over_max_money_is_rejected() {
let _init_guard = zebra_test::init();
let mut chain = ValueBalance::<NonNegative>::zero();
chain.set_transparent_value_balance(ValueBalance::from_transparent_amount(
Amount::try_from(MAX_MONEY).expect("MAX_MONEY is a valid amount"),
));
let error = chain
.add_chain_value_pool_change(ValueBalance::from_sprout_amount(
Amount::<NegativeAllowed>::try_from(MAX_MONEY).expect("MAX_MONEY is a valid amount"),
))
.expect_err("a total exceeding MAX_MONEY must be rejected");
assert!(matches!(error, ValueBalanceError::Total(_)));
}
#[test]
fn ironwood_included_in_remaining_transaction_value() {
let _init_guard = zebra_test::init();
let value_balance =
ValueBalance::<NegativeAllowed>::from_ironwood_amount(Amount::try_from(50).expect("valid"));
assert_eq!(
value_balance
.remaining_transaction_value()
.expect("positive ironwood value is valid remaining transaction value"),
Amount::<NonNegative>::try_from(50).expect("valid amount"),
);
}
#[test]
fn nsm_value_balance_is_not_part_of_the_issued_supply() {
let _init_guard = zebra_test::init();
let max_money = Amount::<NonNegative>::try_from(MAX_MONEY).expect("valid amount");
let mut pools = ValueBalance::<NonNegative>::zero();
pools.set_transparent_value_balance(ValueBalance::from_transparent_amount(max_money));
pools.set_nsm_amount(Amount::<NonNegative>::try_from(100).expect("valid amount"));
assert_eq!(pools.total().expect("total excludes nsm"), max_money);
let mut change = ValueBalance::<NegativeAllowed>::zero();
change.set_nsm_amount(Amount::<NegativeAllowed>::try_from(1).expect("valid amount"));
let pools = pools
.add_chain_value_pool_change(change)
.expect("the NSM value balance is not capped by MAX_MONEY");
assert_eq!(
pools.nsm_amount(),
Amount::<NonNegative>::try_from(101).expect("valid amount")
);
}