From 189594df793771caa50ec7fcec328f536b43bdfd Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Fri, 18 Aug 2023 19:35:11 +0200 Subject: [PATCH] Fixes parallel vps' gas accounting --- core/src/ledger/gas.rs | 21 +++++++++++++++------ shared/src/ledger/protocol/mod.rs | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/src/ledger/gas.rs b/core/src/ledger/gas.rs index fc0187331f3..84d1ebb2310 100644 --- a/core/src/ledger/gas.rs +++ b/core/src/ledger/gas.rs @@ -310,19 +310,27 @@ impl VpGasMeter { } impl VpsGas { - /// Set the gas cost from a single VP run. It consumes the [`VpGasMeter`] + /// Set the gas cost from a VP run. It consumes the [`VpGasMeter`] /// instance which shouldn't be accessed passed this point. pub fn set(&mut self, vp_gas_meter: VpGasMeter) -> Result<()> { - debug_assert_eq!(self.max, None); - debug_assert!(self.rest.is_empty()); - self.max = Some(vp_gas_meter.current_gas); + match self.max { + Some(previous_max) => { + if vp_gas_meter.current_gas > previous_max { + self.rest.push(previous_max); + self.max = Some(vp_gas_meter.current_gas); + } else { + self.rest.push(vp_gas_meter.current_gas); + } + } + None => self.max = Some(vp_gas_meter.current_gas), + } self.check_limit(&vp_gas_meter) } - /// Merge validity predicates gas meters from parallelized runs. + /// Merge validity predicates gas meters from parallelized runs. Consumes the other 'VpsGas' instance which shouldn't be used passed this point. pub fn merge( &mut self, - other: &mut VpsGas, + mut other: VpsGas, tx_gas_meter: &TxGasMeter, ) -> Result<()> { match (self.max, other.max) { @@ -344,6 +352,7 @@ impl VpsGas { self.check_limit(tx_gas_meter) } + /// Check if the vp went out of gas. Starts from the gas consumed by the transaction. fn check_limit(&self, gas_meter: &impl GasMetering) -> Result<()> { let total = gas_meter .get_tx_consumed_gas() diff --git a/shared/src/ledger/protocol/mod.rs b/shared/src/ledger/protocol/mod.rs index 1d205af2214..8b0b701f0fd 100644 --- a/shared/src/ledger/protocol/mod.rs +++ b/shared/src/ledger/protocol/mod.rs @@ -1085,7 +1085,7 @@ fn merge_vp_results( errors.append(&mut b.errors); let mut gas_used = a.gas_used; - gas_used.merge(&mut b.gas_used, tx_gas_meter)?; + gas_used.merge(b.gas_used, tx_gas_meter)?; Ok(VpsResult { accepted_vps,