Skip to content

Commit

Permalink
Change gas
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Aug 5, 2024
1 parent 7b3a911 commit 5bef1c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ fn execute_allocate_large_memory() {
// Gas consumption is relatively small
// Note: the exact gas usage depends on the Rust version used to compile Wasm,
// which we only fix when using rust-optimizer, not integration tests.
assert_approx_eq!(gas_used, 4413600, "0.2");
assert_approx_eq!(gas_used, 9470400, "0.2");
let used = deps.memory_pages();
assert_eq!(used, pages_before + 48, "Memory used: {used} pages");
pages_before += 48;
Expand All @@ -431,7 +431,7 @@ fn execute_allocate_large_memory() {
// Gas consumption is relatively small
// Note: the exact gas usage depends on the Rust version used to compile Wasm,
// which we only fix when using rust-optimizer, not integration tests.
let expected = 4859700; // +/- 20%
let expected = 9553320; // +/- 20%
assert!(gas_used > expected * 80 / 100, "Gas used: {gas_used}");
assert!(gas_used < expected * 120 / 100, "Gas used: {gas_used}");
let used = deps.memory_pages();
Expand Down
8 changes: 4 additions & 4 deletions packages/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ mod tests {

let report2 = instance.create_gas_report();
assert_eq!(report2.used_externally, 251);
assert_eq!(report2.used_internally, 8461548);
assert_eq!(report2.used_internally, 15479658);
assert_eq!(report2.limit, LIMIT);
assert_eq!(
report2.remaining,
Expand Down Expand Up @@ -1049,7 +1049,7 @@ mod tests {
.unwrap();

let init_used = orig_gas - instance.get_gas_left();
assert_eq!(init_used, 8461799);
assert_eq!(init_used, 15479909);
}

#[test]
Expand All @@ -1074,7 +1074,7 @@ mod tests {
.unwrap();

let execute_used = gas_before_execute - instance.get_gas_left();
assert_eq!(execute_used, 11181706);
assert_eq!(execute_used, 20477581);
}

#[test]
Expand Down Expand Up @@ -1117,6 +1117,6 @@ mod tests {
);

let query_used = gas_before_query - instance.get_gas_left();
assert_eq!(query_used, 7142556);
assert_eq!(query_used, 12737176);
}
}
17 changes: 15 additions & 2 deletions packages/vm/src/wasm_backend/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ use super::limiting_tunables::LimitingTunables;
/// https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md
const MAX_WASM_PAGES: u32 = 65536;

fn cost(_operator: &Operator) -> u64 {
fn cost(operator: &Operator) -> u64 {
// A flat fee for each operation
// The target is 1 Teragas per second (see GAS.md).
//
// In https://github.com/CosmWasm/cosmwasm/pull/1042 a profiler is developed to
// identify runtime differences between different Wasm operation, but this is not yet
// precise enough to derive insights from it.
150
const GAS_PER_OPERATION: u64 = 115;

match operator {
Operator::Loop { .. }
| Operator::End
| Operator::Else
| Operator::Br { .. }
| Operator::BrTable { .. }
| Operator::BrIf { .. }
| Operator::Call { .. }
| Operator::CallIndirect { .. }
| Operator::Return => GAS_PER_OPERATION * 14,
_ => GAS_PER_OPERATION,
}
}

/// Creates an engine without a compiler.
Expand Down

0 comments on commit 5bef1c5

Please sign in to comment.