diff --git a/actors/market/src/lib.rs b/actors/market/src/lib.rs index 14ed2d975..69104c244 100644 --- a/actors/market/src/lib.rs +++ b/actors/market/src/lib.rs @@ -26,6 +26,7 @@ use log::info; use num_derive::FromPrimitive; use num_traits::{FromPrimitive, Zero}; +use crate::balance_table::BalanceTable; use fil_actors_runtime::cbor::{deserialize, serialize, serialize_vec}; use fil_actors_runtime::runtime::builtins::Type; use fil_actors_runtime::runtime::{ActorCode, Policy, Runtime}; @@ -72,9 +73,10 @@ pub enum Method { OnMinerSectorsTerminate = 7, ComputeDataCommitment = 8, CronTick = 9, - // Method numbers derived from FRC-XXXX standards + // Method numbers derived from FRC-0042 standards AddBalanceExported = frc42_dispatch::method_hash!("AddBalance"), WithdrawBalanceExported = frc42_dispatch::method_hash!("WithdrawBalance"), + GetBalanceExported = frc42_dispatch::method_hash!("GetBalance"), } /// Market Actor @@ -185,6 +187,33 @@ impl Actor { Ok(WithdrawBalanceReturn { amount_withdrawn: amount_extracted }) } + /// Returns the escrow balance and locked amount for an address. + fn get_balance( + rt: &mut impl Runtime, + account: Address, + ) -> Result { + rt.validate_immediate_caller_accept_any()?; + let nominal = rt.resolve_address(&account).ok_or_else(|| { + actor_error!(illegal_argument, "failed to resolve address {}", account) + })?; + let account = Address::new_id(nominal); + + let store = rt.store(); + let st: State = rt.state()?; + let balances = BalanceTable::from_root(store, &st.escrow_table) + .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load escrow table")?; + let locks = BalanceTable::from_root(store, &st.locked_table) + .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to load locked table")?; + let balance = balances + .get(&account) + .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get escrow balance")?; + let locked = locks + .get(&account) + .context_code(ExitCode::USR_ILLEGAL_STATE, "failed to get locked balance")?; + + Ok(GetBalanceReturn { balance, locked }) + } + /// Publish a new set of storage deals (not yet included in a sector). fn publish_storage_deals( rt: &mut impl Runtime, @@ -1431,6 +1460,10 @@ impl ActorCode for Actor { Self::cron_tick(rt)?; Ok(RawBytes::default()) } + Some(Method::GetBalanceExported) => { + let res = Self::get_balance(rt, cbor::deserialize_params(params)?)?; + Ok(RawBytes::serialize(res)?) + } None => Err(actor_error!(unhandled_message, "Invalid method")), } } diff --git a/actors/market/src/types.rs b/actors/market/src/types.rs index 39ee64e13..bac78d0df 100644 --- a/actors/market/src/types.rs +++ b/actors/market/src/types.rs @@ -22,7 +22,7 @@ use super::deal::{ClientDealProposal, DealProposal, DealState}; pub const PROPOSALS_AMT_BITWIDTH: u32 = 5; pub const STATES_AMT_BITWIDTH: u32 = 6; -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct WithdrawBalanceParams { pub provider_or_client: Address, pub amount: TokenAmount, @@ -30,33 +30,32 @@ pub struct WithdrawBalanceParams { impl Cbor for WithdrawBalanceParams {} -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] #[serde(transparent)] pub struct WithdrawBalanceReturn { pub amount_withdrawn: TokenAmount, } -#[derive(Serialize_tuple, Deserialize_tuple)] -pub struct OnMinerSectorsTerminateParams { - pub epoch: ChainEpoch, - pub deal_ids: Vec, +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] +pub struct GetBalanceReturn { + pub balance: TokenAmount, + pub locked: TokenAmount, } -#[derive(Serialize_tuple)] - -pub struct OnMinerSectorsTerminateParamsRef<'a> { +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] +pub struct OnMinerSectorsTerminateParams { pub epoch: ChainEpoch, - pub deal_ids: &'a [DealID], + pub deal_ids: Vec, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct PublishStorageDealsParams { pub deals: Vec, } impl Cbor for PublishStorageDealsParams {} -#[derive(Serialize_tuple, Deserialize_tuple, Debug)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, PartialEq)] // Add Eq when BitField does pub struct PublishStorageDealsReturn { pub ids: Vec, pub valid_deals: BitField, @@ -65,41 +64,36 @@ pub struct PublishStorageDealsReturn { // Changed since V2: // - Array of Sectors rather than just one // - Removed SectorStart -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct VerifyDealsForActivationParams { pub sectors: Vec, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct SectorDeals { pub sector_type: RegisteredSealProof, pub sector_expiry: ChainEpoch, pub deal_ids: Vec, } -#[derive(Serialize_tuple)] -pub struct VerifyDealsForActivationParamsRef<'a> { - pub sectors: &'a [SectorDeals], -} - -#[derive(Serialize_tuple, Deserialize_tuple, Default)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct VerifyDealsForActivationReturn { pub sectors: Vec, } -#[derive(Serialize_tuple, Deserialize_tuple, Default, Clone)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq, Default)] pub struct SectorDealData { /// Option::None signifies commitment to empty sector, meaning no deals. pub commd: Option, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct ActivateDealsParams { pub deal_ids: Vec, pub sector_expiry: ChainEpoch, } -#[derive(Serialize_tuple, Deserialize_tuple, Clone)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct VerifiedDealInfo { pub client: ActorID, pub allocation_id: AllocationID, @@ -107,13 +101,13 @@ pub struct VerifiedDealInfo { pub size: PaddedPieceSize, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct ActivateDealsResult { #[serde(with = "bigint_ser")] pub nonverified_deal_space: BigInt, pub verified_infos: Vec, } -#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Default)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct DealSpaces { #[serde(with = "bigint_ser")] pub deal_space: BigInt, @@ -121,17 +115,12 @@ pub struct DealSpaces { pub verified_deal_space: BigInt, } -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct ComputeDataCommitmentParams { pub inputs: Vec, } -#[derive(Serialize_tuple)] -pub struct ComputeDataCommitmentParamsRef<'a> { - pub inputs: &'a [SectorDataSpec], -} - -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct ComputeDataCommitmentReturn { pub commds: Vec, } @@ -142,7 +131,7 @@ pub type DealArray<'bs, BS> = Array<'bs, DealProposal, BS>; /// A specialization of a array to deals. pub type DealMetaArray<'bs, BS> = Array<'bs, DealState, BS>; -#[derive(Serialize_tuple, Deserialize_tuple)] +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] pub struct SectorDataSpec { pub deal_ids: Vec, pub sector_type: RegisteredSealProof, diff --git a/actors/market/tests/cron_tick_deal_expiry.rs b/actors/market/tests/cron_tick_deal_expiry.rs index 38b7bce7c..6118cd3fe 100644 --- a/actors/market/tests/cron_tick_deal_expiry.rs +++ b/actors/market/tests/cron_tick_deal_expiry.rs @@ -163,8 +163,8 @@ fn expired_deal_should_unlock_the_remaining_client_and_provider_locked_balance_a ); let deal_proposal = get_deal_proposal(&mut rt, deal_id); - let c_escrow = get_escrow_balance(&rt, &CLIENT_ADDR).unwrap(); - let p_escrow = get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(); + let c_escrow = get_balance(&mut rt, &CLIENT_ADDR).balance; + let p_escrow = get_balance(&mut rt, &PROVIDER_ADDR).balance; // move the current epoch so that deal is expired rt.set_epoch(END_EPOCH + 1000); @@ -173,11 +173,13 @@ fn expired_deal_should_unlock_the_remaining_client_and_provider_locked_balance_a // assert balances let payment = deal_proposal.total_storage_fee(); - assert_eq!(c_escrow - &payment, get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); - assert!(get_locked_balance(&mut rt, CLIENT_ADDR).is_zero()); + let client_acct = get_balance(&mut rt, &CLIENT_ADDR); + assert_eq!(c_escrow - &payment, client_acct.balance); + assert!(client_acct.locked.is_zero()); - assert_eq!(p_escrow + &payment, get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); - assert!(get_locked_balance(&mut rt, PROVIDER_ADDR).is_zero()); + let provider_acct = get_balance(&mut rt, &PROVIDER_ADDR); + assert_eq!(p_escrow + &payment, provider_acct.balance); + assert!(provider_acct.locked.is_zero()); // deal should be deleted assert_deal_deleted(&mut rt, deal_id, deal_proposal); @@ -201,7 +203,7 @@ fn all_payments_are_made_for_a_deal_client_withdraws_collateral_and_client_accou // move the current epoch so that deal is expired rt.set_epoch(END_EPOCH + 100); cron_tick(&mut rt); - assert_eq!(deal_proposal.client_collateral, get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); + assert_eq!(deal_proposal.client_collateral, get_balance(&mut rt, &CLIENT_ADDR).balance); // client withdraws collateral -> account should be removed as it now has zero balance withdraw_client_balance( diff --git a/actors/market/tests/cron_tick_timedout_deals.rs b/actors/market/tests/cron_tick_timedout_deals.rs index 55714aab5..dfdff2e45 100644 --- a/actors/market/tests/cron_tick_timedout_deals.rs +++ b/actors/market/tests/cron_tick_timedout_deals.rs @@ -36,7 +36,7 @@ fn timed_out_deal_is_slashed_and_deleted() { ); let deal_proposal = get_deal_proposal(&mut rt, deal_id); - let c_escrow = get_escrow_balance(&rt, &CLIENT_ADDR).unwrap(); + let c_escrow = get_balance(&mut rt, &CLIENT_ADDR).balance; // do a cron tick for it -> should time out and get slashed rt.set_epoch(process_epoch(START_EPOCH, deal_id)); @@ -50,8 +50,9 @@ fn timed_out_deal_is_slashed_and_deleted() { ); cron_tick(&mut rt); - assert_eq!(c_escrow, get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); - assert!(get_locked_balance(&mut rt, CLIENT_ADDR).is_zero()); + let client_acct = get_balance(&mut rt, &CLIENT_ADDR); + assert_eq!(c_escrow, client_acct.balance); + assert!(client_acct.locked.is_zero()); assert_account_zero(&mut rt, PROVIDER_ADDR); assert_deal_deleted(&mut rt, deal_id, deal_proposal); check_state(&rt); diff --git a/actors/market/tests/harness.rs b/actors/market/tests/harness.rs index 8ab5de6b4..7bccd1506 100644 --- a/actors/market/tests/harness.rs +++ b/actors/market/tests/harness.rs @@ -10,10 +10,10 @@ use std::{cell::RefCell, collections::HashMap}; use fil_actor_market::ext::account::{AuthenticateMessageParams, AUTHENTICATE_MESSAGE_METHOD}; use fil_actor_market::ext::verifreg::{AllocationID, AllocationRequest, AllocationsResponse}; use fil_actor_market::{ - balance_table::BalanceTable, deal_id_key, ext, ext::miner::GetControlAddressesReturnParams, - gen_rand_next_epoch, testing::check_state_invariants, ActivateDealsParams, ActivateDealsResult, + deal_id_key, ext, ext::miner::GetControlAddressesReturnParams, gen_rand_next_epoch, + testing::check_state_invariants, ActivateDealsParams, ActivateDealsResult, Actor as MarketActor, ClientDealProposal, DealArray, DealMetaArray, DealProposal, DealState, - Label, Method, OnMinerSectorsTerminateParams, PublishStorageDealsParams, + GetBalanceReturn, Label, Method, OnMinerSectorsTerminateParams, PublishStorageDealsParams, PublishStorageDealsReturn, SectorDeals, State, VerifyDealsForActivationParams, VerifyDealsForActivationReturn, WithdrawBalanceParams, WithdrawBalanceReturn, NO_ALLOCATION_ID, PROPOSALS_AMT_BITWIDTH, @@ -123,13 +123,16 @@ pub fn construct_and_verify(rt: &mut MockRuntime) { rt.verify(); } -pub fn get_escrow_balance(rt: &MockRuntime, addr: &Address) -> Result { - let st: State = rt.get_state(); - - let et = BalanceTable::from_root(rt.store(), &st.escrow_table) - .expect("failed to construct balance table from blockstore"); - - Ok(et.get(addr).expect("address does not exist in escrow balance table")) +pub fn get_balance(rt: &mut MockRuntime, addr: &Address) -> GetBalanceReturn { + rt.set_caller(make_identity_cid(b"1234"), Address::new_id(1234)); + rt.expect_validate_caller_any(); + let ret: GetBalanceReturn = rt + .call::(Method::GetBalanceExported as u64, &RawBytes::serialize(addr).unwrap()) + .unwrap() + .deserialize() + .unwrap(); + rt.verify(); + ret } pub fn expect_get_control_addresses( @@ -316,12 +319,6 @@ pub fn get_pending_deal_allocation(rt: &mut MockRuntime, deal_id: DealID) -> All *pending_allocations.get(&deal_id_key(deal_id)).unwrap().unwrap_or(&NO_ALLOCATION_ID) } -pub fn get_locked_balance(rt: &mut MockRuntime, addr: Address) -> TokenAmount { - let st: State = rt.get_state(); - let lt = BalanceTable::from_root(&rt.store, &st.locked_table).unwrap(); - lt.get(&addr).unwrap() -} - pub fn get_deal_state(rt: &mut MockRuntime, deal_id: DealID) -> DealState { let st: State = rt.get_state(); let states = DealMetaArray::load(&st.states, &rt.store).unwrap(); @@ -359,10 +356,8 @@ pub fn cron_tick_and_assert_balances( deal_id: DealID, ) -> (TokenAmount, TokenAmount) { // fetch current client and provider escrow balances - let c_locked = get_locked_balance(rt, client_addr); - let c_escrow = get_escrow_balance(rt, &client_addr).unwrap(); - let p_locked = get_locked_balance(rt, provider_addr); - let p_escrow = get_escrow_balance(rt, &provider_addr).unwrap(); + let c_acct = get_balance(rt, &client_addr); + let p_acct = get_balance(rt, &provider_addr); let mut amount_slashed = TokenAmount::zero(); let s = get_deal_state(rt, deal_id); @@ -399,10 +394,10 @@ pub fn cron_tick_and_assert_balances( let payment = duration * d.storage_price_per_epoch; // expected updated amounts - let updated_client_escrow = c_escrow - &payment; - let updated_provider_escrow = (p_escrow + &payment) - &amount_slashed; - let mut updated_client_locked = c_locked - &payment; - let mut updated_provider_locked = p_locked; + let updated_client_escrow = c_acct.balance - &payment; + let updated_provider_escrow = (p_acct.balance + &payment) - &amount_slashed; + let mut updated_client_locked = c_acct.locked - &payment; + let mut updated_provider_locked = p_acct.locked; // if the deal has expired or been slashed, locked amount will be zero for provider and client. let is_deal_expired = payment_end == d.end_epoch; if is_deal_expired || s.slash_epoch != EPOCH_UNDEFINED { @@ -412,10 +407,12 @@ pub fn cron_tick_and_assert_balances( cron_tick(rt); - assert_eq!(updated_client_escrow, get_escrow_balance(rt, &client_addr).unwrap()); - assert_eq!(updated_client_locked, get_locked_balance(rt, client_addr)); - assert_eq!(updated_provider_escrow, get_escrow_balance(rt, &provider_addr).unwrap()); - assert_eq!(updated_provider_locked, get_locked_balance(rt, provider_addr)); + let client_acct = get_balance(rt, &client_addr); + let provider_acct = get_balance(rt, &provider_addr); + assert_eq!(updated_client_escrow, client_acct.balance); + assert_eq!(updated_client_locked, client_acct.locked); + assert_eq!(updated_provider_escrow, provider_acct.balance); + assert_eq!(updated_provider_locked, provider_acct.locked); (payment, amount_slashed) } @@ -424,19 +421,17 @@ pub fn cron_tick_no_change(rt: &mut MockRuntime, client_addr: Address, provider_ let epoch_cid = st.deal_ops_by_epoch; // fetch current client and provider escrow balances - let c_locked = get_locked_balance(rt, client_addr); - let c_escrow = get_escrow_balance(rt, &client_addr).unwrap(); - let p_locked = get_locked_balance(rt, provider_addr); - let p_escrow = get_escrow_balance(rt, &provider_addr).unwrap(); + let client_acct = get_balance(rt, &client_addr); + let provider_acct = get_balance(rt, &provider_addr); cron_tick(rt); let st: State = rt.get_state(); + let new_client_acct = get_balance(rt, &client_addr); + let new_provider_acct = get_balance(rt, &provider_addr); assert_eq!(epoch_cid, st.deal_ops_by_epoch); - assert_eq!(c_locked, get_locked_balance(rt, client_addr)); - assert_eq!(c_escrow, get_escrow_balance(rt, &client_addr).unwrap()); - assert_eq!(p_locked, get_locked_balance(rt, provider_addr)); - assert_eq!(p_escrow, get_escrow_balance(rt, &provider_addr).unwrap()); + assert_eq!(client_acct, new_client_acct); + assert_eq!(provider_acct, new_provider_acct); } pub fn publish_deals( @@ -983,8 +978,9 @@ pub fn terminate_deals_raw( } pub fn assert_account_zero(rt: &mut MockRuntime, addr: Address) { - assert!(get_escrow_balance(rt, &addr).unwrap().is_zero()); - assert!(get_locked_balance(rt, addr).is_zero()); + let account = get_balance(rt, &addr); + assert!(account.balance.is_zero()); + assert!(account.locked.is_zero()); } pub fn verify_deals_for_activation( diff --git a/actors/market/tests/market_actor_test.rs b/actors/market/tests/market_actor_test.rs index 3a78313f0..b4e0c63e0 100644 --- a/actors/market/tests/market_actor_test.rs +++ b/actors/market/tests/market_actor_test.rs @@ -209,10 +209,9 @@ fn adds_to_provider_escrow_funds() { rt.verify(); - assert_eq!( - get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(), - TokenAmount::from_atto(tc.total) - ); + let acct = get_balance(&mut rt, &PROVIDER_ADDR); + assert_eq!(acct.balance, TokenAmount::from_atto(tc.total)); + assert_eq!(acct.locked, TokenAmount::zero()); check_state(&rt); } } @@ -224,7 +223,7 @@ fn fails_if_withdraw_from_non_provider_funds_is_not_initiated_by_the_recipient() add_participant_funds(&mut rt, CLIENT_ADDR, TokenAmount::from_atto(20u8)); - assert_eq!(TokenAmount::from_atto(20u8), get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); + assert_eq!(TokenAmount::from_atto(20u8), get_balance(&mut rt, &CLIENT_ADDR).balance); rt.expect_validate_caller_addr(vec![CLIENT_ADDR]); @@ -245,7 +244,7 @@ fn fails_if_withdraw_from_non_provider_funds_is_not_initiated_by_the_recipient() rt.verify(); // verify there was no withdrawal - assert_eq!(TokenAmount::from_atto(20u8), get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); + assert_eq!(TokenAmount::from_atto(20u8), get_balance(&mut rt, &CLIENT_ADDR).balance); check_state(&rt); } @@ -268,8 +267,12 @@ fn balance_after_withdrawal_must_always_be_greater_than_or_equal_to_locked_amoun end_epoch, ); let deal = get_deal_proposal(&mut rt, deal_id); - assert_eq!(deal.provider_collateral, get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); - assert_eq!(deal.client_balance_requirement(), get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); + let provider_acct = get_balance(&mut rt, &PROVIDER_ADDR); + assert_eq!(deal.provider_collateral, provider_acct.balance); + assert_eq!(deal.provider_collateral, provider_acct.locked); + let client_acct = get_balance(&mut rt, &CLIENT_ADDR); + assert_eq!(deal.client_balance_requirement(), client_acct.balance); + assert_eq!(deal.client_balance_requirement(), client_acct.locked); let withdraw_amount = TokenAmount::from_atto(1u8); let withdrawable_amount = TokenAmount::zero(); @@ -295,6 +298,10 @@ fn balance_after_withdrawal_must_always_be_greater_than_or_equal_to_locked_amoun let withdrawable_amount = TokenAmount::from_atto(25u8); add_provider_funds(&mut rt, withdrawable_amount.clone(), &MinerAddresses::default()); + let provider_acct = get_balance(&mut rt, &PROVIDER_ADDR); + assert_eq!(&deal.provider_collateral + &withdrawable_amount, provider_acct.balance); + assert_eq!(deal.provider_collateral, provider_acct.locked); + withdraw_provider_balance( &mut rt, withdraw_amount.clone(), @@ -306,6 +313,10 @@ fn balance_after_withdrawal_must_always_be_greater_than_or_equal_to_locked_amoun // add some more funds to the client & ensure withdrawal is limited by the locked funds add_participant_funds(&mut rt, CLIENT_ADDR, withdrawable_amount.clone()); + let client_acct = get_balance(&mut rt, &CLIENT_ADDR); + assert_eq!(deal.client_balance_requirement() + &withdrawable_amount, client_acct.balance); + assert_eq!(deal.client_balance_requirement(), client_acct.locked); + withdraw_client_balance(&mut rt, withdraw_amount, withdrawable_amount, CLIENT_ADDR); check_state(&rt); } @@ -420,10 +431,7 @@ fn adds_to_non_provider_funds() { rt.verify(); - assert_eq!( - get_escrow_balance(&rt, caller_addr).unwrap(), - TokenAmount::from_atto(tc.total) - ); + assert_eq!(get_balance(&mut rt, caller_addr).balance, TokenAmount::from_atto(tc.total)); check_state(&rt); } } @@ -436,7 +444,7 @@ fn withdraws_from_provider_escrow_funds_and_sends_to_owner() { let amount = TokenAmount::from_atto(20); add_provider_funds(&mut rt, amount.clone(), &MinerAddresses::default()); - assert_eq!(amount, get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); + assert_eq!(amount, get_balance(&mut rt, &PROVIDER_ADDR).balance); // worker calls WithdrawBalance, balance is transferred to owner let withdraw_amount = TokenAmount::from_atto(1); @@ -449,7 +457,7 @@ fn withdraws_from_provider_escrow_funds_and_sends_to_owner() { WORKER_ADDR, ); - assert_eq!(TokenAmount::from_atto(19), get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); + assert_eq!(TokenAmount::from_atto(19), get_balance(&mut rt, &PROVIDER_ADDR).balance); check_state(&rt); } @@ -460,12 +468,12 @@ fn withdraws_from_non_provider_escrow_funds() { let amount = TokenAmount::from_atto(20); add_participant_funds(&mut rt, CLIENT_ADDR, amount.clone()); - assert_eq!(get_escrow_balance(&rt, &CLIENT_ADDR).unwrap(), amount); + assert_eq!(get_balance(&mut rt, &CLIENT_ADDR).balance, amount); let withdraw_amount = TokenAmount::from_atto(1); withdraw_client_balance(&mut rt, withdraw_amount.clone(), withdraw_amount, CLIENT_ADDR); - assert_eq!(get_escrow_balance(&rt, &CLIENT_ADDR).unwrap(), TokenAmount::from_atto(19)); + assert_eq!(get_balance(&mut rt, &CLIENT_ADDR).balance, TokenAmount::from_atto(19)); check_state(&rt); } @@ -480,7 +488,7 @@ fn client_withdrawing_more_than_escrow_balance_limits_to_available_funds() { let withdraw_amount = TokenAmount::from_atto(25); withdraw_client_balance(&mut rt, withdraw_amount, amount, CLIENT_ADDR); - assert_eq!(get_escrow_balance(&rt, &CLIENT_ADDR).unwrap(), TokenAmount::zero()); + assert_eq!(get_balance(&mut rt, &CLIENT_ADDR).balance, TokenAmount::zero()); check_state(&rt); } @@ -491,7 +499,7 @@ fn worker_withdrawing_more_than_escrow_balance_limits_to_available_funds() { let amount = TokenAmount::from_atto(20); add_provider_funds(&mut rt, amount.clone(), &MinerAddresses::default()); - assert_eq!(get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(), amount); + assert_eq!(get_balance(&mut rt, &PROVIDER_ADDR).balance, amount); let withdraw_amount = TokenAmount::from_atto(25); withdraw_provider_balance( @@ -503,7 +511,7 @@ fn worker_withdrawing_more_than_escrow_balance_limits_to_available_funds() { WORKER_ADDR, ); - assert_eq!(get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(), TokenAmount::zero()); + assert_eq!(get_balance(&mut rt, &PROVIDER_ADDR).balance, TokenAmount::zero()); check_state(&rt); } @@ -554,7 +562,7 @@ fn fails_if_withdraw_from_provider_funds_is_not_initiated_by_the_owner_or_worker let amount = TokenAmount::from_atto(20u8); add_provider_funds(&mut rt, amount.clone(), &MinerAddresses::default()); - assert_eq!(get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(), amount); + assert_eq!(get_balance(&mut rt, &PROVIDER_ADDR).balance, amount); // only signing parties can add balance for client AND provider. rt.expect_validate_caller_addr(vec![OWNER_ADDR, WORKER_ADDR]); @@ -577,7 +585,7 @@ fn fails_if_withdraw_from_provider_funds_is_not_initiated_by_the_owner_or_worker rt.verify(); // verify there was no withdrawal - assert_eq!(get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap(), amount); + assert_eq!(get_balance(&mut rt, &PROVIDER_ADDR).balance, amount); check_state(&rt); } @@ -820,10 +828,7 @@ fn provider_and_client_addresses_are_resolved_before_persisting_state_and_sent_t rt.verify(); rt.add_balance(amount); - assert_eq!( - deal.client_balance_requirement(), - get_escrow_balance(&rt, &client_resolved).unwrap() - ); + assert_eq!(deal.client_balance_requirement(), get_balance(&mut rt, &client_resolved).balance); // add funds for provider using it's BLS address -> will be resolved and persisted rt.value_received = deal.provider_collateral.clone(); @@ -841,7 +846,7 @@ fn provider_and_client_addresses_are_resolved_before_persisting_state_and_sent_t ); rt.verify(); rt.add_balance(deal.provider_collateral.clone()); - assert_eq!(deal.provider_collateral, get_escrow_balance(&rt, &provider_resolved).unwrap()); + assert_eq!(deal.provider_collateral, get_balance(&mut rt, &provider_resolved).balance); // publish deal using the BLS addresses rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR); @@ -1056,13 +1061,13 @@ fn publish_multiple_deals_for_different_clients_and_ensure_balances_are_correct( // assert locked balance for all clients and provider let provider_locked_expected = &deal1.provider_collateral + &deal2.provider_collateral + &deal3.provider_collateral; - let client1_locked = get_locked_balance(&mut rt, client1_addr); - let client2_locked = get_locked_balance(&mut rt, client2_addr); - let client3_locked = get_locked_balance(&mut rt, client3_addr); + let client1_locked = get_balance(&mut rt, &client1_addr).locked; + let client2_locked = get_balance(&mut rt, &client2_addr).locked; + let client3_locked = get_balance(&mut rt, &client3_addr).locked; assert_eq!(deal1.client_balance_requirement(), client1_locked); assert_eq!(deal2.client_balance_requirement(), client2_locked); assert_eq!(deal3.client_balance_requirement(), client3_locked); - assert_eq!(provider_locked_expected, get_locked_balance(&mut rt, PROVIDER_ADDR)); + assert_eq!(provider_locked_expected, get_balance(&mut rt, &PROVIDER_ADDR).locked); // assert locked funds dealStates let st: State = rt.get_state(); @@ -1095,16 +1100,16 @@ fn publish_multiple_deals_for_different_clients_and_ensure_balances_are_correct( // assert locked balances for clients and provider let provider_locked_expected = &provider_locked_expected + &deal4.provider_collateral + &deal5.provider_collateral; - assert_eq!(provider_locked_expected, get_locked_balance(&mut rt, PROVIDER_ADDR)); + assert_eq!(provider_locked_expected, get_balance(&mut rt, &PROVIDER_ADDR).locked); - let client3_locked_updated = get_locked_balance(&mut rt, client3_addr); + let client3_locked_updated = get_balance(&mut rt, &client3_addr).locked; assert_eq!( &client3_locked + &deal4.client_balance_requirement() + &deal5.client_balance_requirement(), client3_locked_updated ); - let client1_locked = get_locked_balance(&mut rt, client1_addr); - let client2_locked = get_locked_balance(&mut rt, client2_addr); + let client1_locked = get_balance(&mut rt, &client1_addr).locked; + let client2_locked = get_balance(&mut rt, &client2_addr).locked; assert_eq!(deal1.client_balance_requirement(), client1_locked); assert_eq!(deal2.client_balance_requirement(), client2_locked); @@ -1138,15 +1143,15 @@ fn publish_multiple_deals_for_different_clients_and_ensure_balances_are_correct( // assertions let st: State = rt.get_state(); let provider2_locked = &deal6.provider_collateral + &deal7.provider_collateral; - assert_eq!(provider2_locked, get_locked_balance(&mut rt, provider2_addr)); - let client1_locked_updated = get_locked_balance(&mut rt, client1_addr); + assert_eq!(provider2_locked, get_balance(&mut rt, &provider2_addr).locked); + let client1_locked_updated = get_balance(&mut rt, &client1_addr).locked; assert_eq!( &deal7.client_balance_requirement() + &client1_locked + &deal6.client_balance_requirement(), client1_locked_updated ); // assert first provider's balance as well - assert_eq!(provider_locked_expected, get_locked_balance(&mut rt, PROVIDER_ADDR)); + assert_eq!(provider_locked_expected, get_balance(&mut rt, &PROVIDER_ADDR).locked); let total_client_collateral_locked = &total_client_collateral_locked + &deal6.client_collateral + &deal7.client_collateral; @@ -1676,7 +1681,7 @@ fn market_actor_deals() { // test adding provider funds let funds = TokenAmount::from_atto(20_000_000); add_provider_funds(&mut rt, funds.clone(), &MinerAddresses::default()); - assert_eq!(funds, get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); + assert_eq!(funds, get_balance(&mut rt, &PROVIDER_ADDR).balance); add_participant_funds(&mut rt, CLIENT_ADDR, funds); let mut deal_proposal = @@ -1714,7 +1719,7 @@ fn max_deal_label_size() { // Test adding provider funds from both worker and owner address let funds = TokenAmount::from_atto(20_000_000); add_provider_funds(&mut rt, funds.clone(), &MinerAddresses::default()); - assert_eq!(funds, get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap()); + assert_eq!(funds, get_balance(&mut rt, &PROVIDER_ADDR).balance); add_participant_funds(&mut rt, CLIENT_ADDR, funds); let mut deal_proposal = @@ -1779,10 +1784,10 @@ fn insufficient_client_balance_in_a_batch() { rt.verify(); - assert_eq!(deal2.client_balance_requirement(), get_escrow_balance(&rt, &CLIENT_ADDR).unwrap()); + assert_eq!(deal2.client_balance_requirement(), get_balance(&mut rt, &CLIENT_ADDR).balance); assert_eq!( deal1.provider_balance_requirement().add(deal2.provider_balance_requirement()), - get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap() + get_balance(&mut rt, &PROVIDER_ADDR).balance ); let buf1 = RawBytes::serialize(&deal1).expect("failed to marshal deal proposal"); @@ -1894,11 +1899,11 @@ fn insufficient_provider_balance_in_a_batch() { assert_eq!( deal1.client_balance_requirement().add(deal2.client_balance_requirement()), - get_escrow_balance(&rt, &CLIENT_ADDR).unwrap() + get_balance(&mut rt, &CLIENT_ADDR).balance ); assert_eq!( deal2.provider_balance_requirement().clone(), - get_escrow_balance(&rt, &PROVIDER_ADDR).unwrap() + get_balance(&mut rt, &PROVIDER_ADDR).balance ); let buf1 = RawBytes::serialize(&deal1).expect("failed to marshal deal proposal");