Skip to content

Commit

Permalink
Makes max field of VpsGas non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Sep 7, 2023
1 parent 10751b8 commit e146907
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions core/src/ledger/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub struct VpGasMeter {
Clone, Debug, Default, BorshSerialize, BorshDeserialize, BorshSchema,
)]
pub struct VpsGas {
max: Option<Gas>,
max: Gas,
rest: Vec<Gas>,
}

Expand Down Expand Up @@ -313,40 +313,28 @@ impl VpsGas {
/// 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<()> {
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),
if vp_gas_meter.current_gas > self.max {
self.rest.push(self.max);
self.max = vp_gas_meter.current_gas;
} else {
self.rest.push(vp_gas_meter.current_gas);
}

self.check_limit(&vp_gas_meter)
}

/// Merge validity predicates gas meters from parallelized runs. Consumes
/// the other 'VpsGas' instance which shouldn't be used passed this point.
/// the other `VpsGas` instance which shouldn't be used passed this point.
pub fn merge(
&mut self,
mut other: VpsGas,
tx_gas_meter: &TxGasMeter,
) -> Result<()> {
match (self.max, other.max) {
(None, Some(_)) => {
self.max = other.max;
}
(Some(this_max), Some(other_max)) => {
if this_max < other_max {
self.rest.push(this_max);
self.max = other.max;
} else {
self.rest.push(other_max);
}
}
_ => {}
if self.max < other.max {
self.rest.push(self.max);
self.max = other.max;
} else {
self.rest.push(other.max);
}
self.rest.append(&mut other.rest);

Expand All @@ -372,10 +360,7 @@ impl VpsGas {
self.rest.iter().try_fold(Gas::default(), |acc, gas| {
acc.checked_add(*gas).ok_or(Error::GasOverflow)
})? / PARALLEL_GAS_DIVIDER;
self.max
.unwrap_or_default()
.checked_add(parallel_gas)
.ok_or(Error::GasOverflow)
self.max.checked_add(parallel_gas).ok_or(Error::GasOverflow)
}
}

Expand Down

0 comments on commit e146907

Please sign in to comment.