Skip to content

Commit

Permalink
Merge pull request #1220 from CosmWasm/fix-gasinfo-addassign
Browse files Browse the repository at this point in the history
Fix `AddAssign` implementation of `GasInfo`
  • Loading branch information
webmaster128 committed Feb 7, 2022
2 parents 3a62f2d + 6cc8091 commit 19d3fb0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,10 @@ and this project adheres to
[#1199]: https://github.com/CosmWasm/cosmwasm/issues/1199
[#1214]: https://github.com/CosmWasm/cosmwasm/issues/1214

### Fixed

- cosmwasm-vm: Fix `AddAssign` implementation of `GasInfo`.

## [1.0.0-beta4] - 2021-12-23

### Changed
Expand Down
75 changes: 72 additions & 3 deletions packages/vm/src/backend.rs
Expand Up @@ -7,9 +7,15 @@ use cosmwasm_std::{Binary, ContractResult, SystemResult};
#[cfg(feature = "iterator")]
use cosmwasm_std::{Order, Record};

#[derive(Copy, Clone, Debug)]
/// A structure that represents gas cost to be deducted from the remaining gas.
/// This is always needed when computations are performed outside of
/// Wasm execution, such as calling crypto APIs or calls into the blockchain.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct GasInfo {
/// The gas cost of a computation that was executed already but not yet charged
/// The gas cost of a computation that was executed already but not yet charged.
///
/// This could be renamed to `internally_used` for consistency because it is used inside
/// of the `cosmwasm_vm`.
pub cost: u64,
/// Gas that was used and charged externally. This is needed to
/// adjust the VM's gas limit but does not affect the gas usage.
Expand Down Expand Up @@ -53,7 +59,7 @@ impl AddAssign for GasInfo {
fn add_assign(&mut self, other: Self) {
*self = GasInfo {
cost: self.cost + other.cost,
externally_used: self.externally_used + other.cost,
externally_used: self.externally_used + other.externally_used,
};
}
}
Expand Down Expand Up @@ -236,6 +242,69 @@ mod tests {
assert_eq!(gas_info.externally_used, 0);
}

#[test]
fn gas_info_implements_add_assign() {
let mut a = GasInfo::new(0, 0);
a += GasInfo::new(0, 0);
assert_eq!(
a,
GasInfo {
cost: 0,
externally_used: 0
}
);

let mut a = GasInfo::new(0, 0);
a += GasInfo::new(12, 0);
assert_eq!(
a,
GasInfo {
cost: 12,
externally_used: 0
}
);

let mut a = GasInfo::new(10, 0);
a += GasInfo::new(3, 0);
assert_eq!(
a,
GasInfo {
cost: 13,
externally_used: 0
}
);

let mut a = GasInfo::new(0, 0);
a += GasInfo::new(0, 7);
assert_eq!(
a,
GasInfo {
cost: 0,
externally_used: 7
}
);

let mut a = GasInfo::new(0, 8);
a += GasInfo::new(0, 9);
assert_eq!(
a,
GasInfo {
cost: 0,
externally_used: 17
}
);

let mut a = GasInfo::new(100, 200);
a += GasInfo::new(1, 2);
assert_eq!(
a,
GasInfo {
cost: 101,
externally_used: 202
}
);
}

// constructors

#[test]
Expand Down

0 comments on commit 19d3fb0

Please sign in to comment.