Skip to content

Commit

Permalink
piecrust: passing economic mode downstream
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Apr 25, 2024
1 parent afe2b8d commit 18cdf90
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Session {
return Err(InitalizationError("init call not allowed".into()));
}

let (data, gas_spent, call_tree) =
let (data, gas_spent, call_tree, economic_mode) =
self.call_inner(contract, fn_name, fn_arg.into(), gas_limit)?;
let events = mem::take(&mut self.inner.events);

Expand All @@ -393,6 +393,7 @@ impl Session {
events,
call_tree,
data,
economic_mode,
})
}

Expand Down Expand Up @@ -750,7 +751,7 @@ impl Session {
fname: &str,
fdata: Vec<u8>,
limit: u64,
) -> Result<(Vec<u8>, u64, CallTree), Error> {
) -> Result<(Vec<u8>, u64, CallTree, EconomicMode), Error> {
let stack_element = self.push_callstack(contract, limit)?;
let instance = self
.instance(&stack_element.contract_id)
Expand Down Expand Up @@ -782,20 +783,8 @@ impl Session {
let ret = instance.read_bytes_from_arg_buffer(ret_len as u32);
let economic_mode = instance.read_eco_mode();

let mut spent = limit - instance.get_remaining_gas();
let spent = limit - instance.get_remaining_gas();

match economic_mode {
EconomicMode::Allowance(allowance) if allowance > 0 => {
// the call has been paid for by the contract so we can set
// the spent amount to zero,
spent = 0;
}
EconomicMode::Charge(charge) => {
// add possible contract's charge
spent += charge;
}
_ => (),
}
for elem in self.inner.call_tree.iter() {
let instance = self
.instance(&elem.contract_id)
Expand All @@ -813,7 +802,7 @@ impl Session {
mem::swap(&mut self.inner.call_tree, &mut call_tree);
call_tree.update_spent(spent);

Ok((ret, spent, call_tree))
Ok((ret, spent, call_tree, economic_mode))
}

pub fn contract_metadata(
Expand Down Expand Up @@ -843,6 +832,9 @@ pub struct CallReceipt<T> {

/// The data returned by the called contract.
pub data: T,

/// Economic mode applied during the execution of the call.
pub economic_mode: EconomicMode,
}

impl CallReceipt<Vec<u8>> {
Expand All @@ -863,6 +855,7 @@ impl CallReceipt<Vec<u8>> {
events: self.events,
call_tree: self.call_tree,
data,
economic_mode: self.economic_mode,
})
}
}
Expand Down

0 comments on commit 18cdf90

Please sign in to comment.