Skip to main content

drive/state_transition_action/address_funds/
mod.rs

1/// Address credit withdrawal transition action
2pub mod address_credit_withdrawal;
3/// Address funding from asset lock transition action
4pub mod address_funding_from_asset_lock;
5/// Address funds transfer transition action
6pub mod address_funds_transfer;
7
8use crate::error::drive::DriveError;
9use crate::error::Error;
10use dpp::address_funds::PlatformAddress;
11use dpp::fee::Credits;
12use dpp::prelude::AddressNonce;
13use std::collections::BTreeMap;
14
15/// Gives each input of a failed transition back the amount it would have spent.
16///
17/// `inputs_with_remaining_balance` holds each input's balance after its spend (the stored balance
18/// minus the requested amount). A failed transition spends nothing, so each input keeps its whole
19/// balance. Both maps come from the same inputs, so an input missing from either, or a sum above
20/// the stored balance's range, is a code error, not a transition error.
21pub fn restore_input_spends_for_failed_transition(
22    inputs_with_remaining_balance: &mut BTreeMap<PlatformAddress, (AddressNonce, Credits)>,
23    requested_inputs: &BTreeMap<PlatformAddress, (AddressNonce, Credits)>,
24) -> Result<(), Error> {
25    if inputs_with_remaining_balance.len() != requested_inputs.len() {
26        return Err(Error::Drive(DriveError::CorruptedCodeExecution(
27            "a failed transition's remaining balances must cover exactly its inputs",
28        )));
29    }
30    for (address, (_nonce, balance)) in inputs_with_remaining_balance.iter_mut() {
31        let (_, requested_spend) = requested_inputs.get(address).ok_or(Error::Drive(
32            DriveError::CorruptedCodeExecution(
33                "a failed transition's remaining balances must cover exactly its inputs",
34            ),
35        ))?;
36        *balance = balance.checked_add(*requested_spend).ok_or(Error::Drive(
37            DriveError::CorruptedCodeExecution(
38                "an input's remaining balance plus its spend exceeds the credit range",
39            ),
40        ))?;
41    }
42    Ok(())
43}