Repository: MystenLabs/sui
File: crates/sui-types/src/inner_temporary_store.rs (Rust)

Code before:
    /// - Split(100)
    ///
    /// Then the accumulator_running_max_withdraws for this account will be 100,
    /// because at any given moment, the net withdraws is at most 100.
    pub accumulator_running_max_withdraws: BTreeMap<AccumulatorObjId, u128>,
}

impl InnerTemporaryStore {
    /// Checks if object funds withdrawals are sufficient for simulate/dry-run/test contexts
    /// where ObjectFundsChecker is unavailable. Filters out address_funds (handled by the
    /// scheduler in production).
    ///
    /// WARNING: This always loads the latest object balance from the store, is not properly
    /// ordered with respect to concurrent transactions, and hence cannot be safely used in
    /// deterministic execution paths (e.g. validators). Only use in non-deterministic contexts
    /// like simulate and dry-run.
    pub fn has_sufficient_object_funds_for_simulate(
        &self,
        address_funds: &BTreeSet<AccumulatorObjId>,
        resolver: &dyn ChildObjectResolver,
    ) -> bool {
        for (account_id, max_withdraw) in &self.accumulator_running_max_withdraws {
            if address_funds.contains(account_id) {
                continue;
            }
            let balance = AccumulatorValue::load_by_id::<U128>(resolver, None, *account_id)
                .ok()
                .flatten()
                .map_or(0, |v| v.value);
            if balance < *max_withdraw {
                return false;
            }
        }
        true
    }
}

pub struct TemporaryModuleResolver<'a, R> {
    temp_store: &'a InnerTemporaryStore,
    fallback: R,
}

impl<'a, R> TemporaryModuleResolver<'a, R> {
    pub fn new(temp_store: &'a InnerTemporaryStore, fallback: R) -> Self {
        Self {
            temp_store,
            fallback,
        }
    }
}


Review comment (suggestion):
use `AccountFundsRead::get_latest_account_amount` or `AccountFundsRead::check_amounts_available` please

Code after:
    /// Then the accumulator_running_max_withdraws for this account will be 100,
    /// because at any given moment, the net withdraws is at most 100.
    pub accumulator_running_max_withdraws: BTreeMap<AccumulatorObjId, u128>,
}

pub struct TemporaryModuleResolver<'a, R> {
    temp_store: &'a InnerTemporaryStore,
    fallback: R,
}

impl<'a, R> TemporaryModuleResolver<'a, R> {
    pub fn new(temp_store: &'a InnerTemporaryStore, fallback: R) -> Self {
        Self {
            temp_store,
            fallback,
        }
    }
}

impl<R> GetModule for TemporaryModuleResolver<'_, R>
where
    R: GetModule<Item = Arc<CompiledModule>, Error = anyhow::Error>,
{
    type Error = anyhow::Error;
    type Item = Arc<CompiledModule>;

    fn get_module_by_id(&self, id: &ModuleId) -> anyhow::Result<Option<Self::Item>, Self::Error> {
        let obj = self.temp_store.written.get(&ObjectID::from(*id.address()));
        if let Some(o) = obj
            && let Some(p) = o.data.try_as_package()
        {
            return Ok(Some(Arc::new(p.deserialize_module(
                &id.name().into(),
                &self.temp_store.binary_config,
            )?)));
        }
        self.fallback.get_module_by_id(id)
    }
}

impl BackingPackageStore for InnerTemporaryStore {
    fn get_package_object(&self, package_id: &ObjectID) -> SuiResult<Option<PackageObject>> {
        Ok(self
            .written
            .get(package_id)
            .cloned()
            .map(PackageObject::new))
    }
}

pub struct PackageStoreWithFallback<P, F> {