@@ -7,9 +7,15 @@ use cosmwasm_std::{Binary, ContractResult, SystemResult};
7
7
#[ cfg( feature = "iterator" ) ]
8
8
use cosmwasm_std:: { Order , Record } ;
9
9
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 ) ]
11
14
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`.
13
19
pub cost : u64 ,
14
20
/// Gas that was used and charged externally. This is needed to
15
21
/// adjust the VM's gas limit but does not affect the gas usage.
@@ -53,7 +59,7 @@ impl AddAssign for GasInfo {
53
59
fn add_assign ( & mut self , other : Self ) {
54
60
* self = GasInfo {
55
61
cost : self . cost + other. cost ,
56
- externally_used : self . externally_used + other. cost ,
62
+ externally_used : self . externally_used + other. externally_used ,
57
63
} ;
58
64
}
59
65
}
@@ -236,6 +242,69 @@ mod tests {
236
242
assert_eq ! ( gas_info. externally_used, 0 ) ;
237
243
}
238
244
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
+
239
308
// constructors
240
309
241
310
#[ test]
0 commit comments