Skip to content

Commit 19d3fb0

Browse files
authored
Merge pull request CosmWasm#1220 from CosmWasm/fix-gasinfo-addassign
Fix `AddAssign` implementation of `GasInfo`
2 parents 3a62f2d + 6cc8091 commit 19d3fb0

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project adheres to
1818
[#1199]: https://github.com/CosmWasm/cosmwasm/issues/1199
1919
[#1214]: https://github.com/CosmWasm/cosmwasm/issues/1214
2020

21+
### Fixed
22+
23+
- cosmwasm-vm: Fix `AddAssign` implementation of `GasInfo`.
24+
2125
## [1.0.0-beta4] - 2021-12-23
2226

2327
### Changed

packages/vm/src/backend.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ use cosmwasm_std::{Binary, ContractResult, SystemResult};
77
#[cfg(feature = "iterator")]
88
use cosmwasm_std::{Order, Record};
99

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

245+
#[test]
246+
fn gas_info_implements_add_assign() {
247+
let mut a = GasInfo::new(0, 0);
248+
a += GasInfo::new(0, 0);
249+
assert_eq!(
250+
a,
251+
GasInfo {
252+
cost: 0,
253+
externally_used: 0
254+
}
255+
);
256+
257+
let mut a = GasInfo::new(0, 0);
258+
a += GasInfo::new(12, 0);
259+
assert_eq!(
260+
a,
261+
GasInfo {
262+
cost: 12,
263+
externally_used: 0
264+
}
265+
);
266+
267+
let mut a = GasInfo::new(10, 0);
268+
a += GasInfo::new(3, 0);
269+
assert_eq!(
270+
a,
271+
GasInfo {
272+
cost: 13,
273+
externally_used: 0
274+
}
275+
);
276+
277+
let mut a = GasInfo::new(0, 0);
278+
a += GasInfo::new(0, 7);
279+
assert_eq!(
280+
a,
281+
GasInfo {
282+
cost: 0,
283+
externally_used: 7
284+
}
285+
);
286+
287+
let mut a = GasInfo::new(0, 8);
288+
a += GasInfo::new(0, 9);
289+
assert_eq!(
290+
a,
291+
GasInfo {
292+
cost: 0,
293+
externally_used: 17
294+
}
295+
);
296+
297+
let mut a = GasInfo::new(100, 200);
298+
a += GasInfo::new(1, 2);
299+
assert_eq!(
300+
a,
301+
GasInfo {
302+
cost: 101,
303+
externally_used: 202
304+
}
305+
);
306+
}
307+
239308
// constructors
240309

241310
#[test]

0 commit comments

Comments
 (0)