Skip to content

Commit

Permalink
Change gas
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Aug 8, 2024
1 parent 4171cd7 commit 9b4d6d0
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 @@ -405,7 +405,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, 4413600000, "0.2");
assert_approx_eq!(gas_used, 9470400000, "0.2");
let used = deps.memory_pages();
assert_eq!(used, pages_before + 48, "Memory used: {used} pages");
pages_before += 48;
Expand All @@ -424,7 +424,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 = 4859700000; // +/- 20%
let expected = 9553320000; // +/- 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 @@ -886,7 +886,7 @@ mod tests {

let report2 = instance.create_gas_report();
assert_eq!(report2.used_externally, 73);
assert_eq!(report2.used_internally, 5764950198);
assert_eq!(report2.used_internally, 10795970198);
assert_eq!(report2.limit, LIMIT);
assert_eq!(
report2.remaining,
Expand Down Expand Up @@ -1075,7 +1075,7 @@ mod tests {
.unwrap();

let init_used = orig_gas - instance.get_gas_left();
assert_eq!(init_used, 5764950271);
assert_eq!(init_used, 10795970271);
}

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

let execute_used = gas_before_execute - instance.get_gas_left();
assert_eq!(execute_used, 8548903606);
assert_eq!(execute_used, 16039958606);
}

#[test]
Expand Down Expand Up @@ -1132,6 +1132,6 @@ mod tests {
assert_eq!(answer.as_slice(), b"{\"verifier\":\"verifies\"}");

let query_used = gas_before_query - instance.get_gas_left();
assert_eq!(query_used, 4493700006);
assert_eq!(query_used, 8306910006);
}
}
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 millisecond (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_000
const GAS_PER_OPERATION: u64 = 115_000;

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 9b4d6d0

Please sign in to comment.