Skip to content

Commit

Permalink
reproduce starge gas metering behavior on exhaustion:
Browse files Browse the repository at this point in the history
```
$ cd runtime/near-vm-runner
$ cargo test --release --lib -- tests::global_gas_test --exact --nocapture
   Compiling near-vm-runner v3.0.0 (/home/matklad/projects/nearcore/runtime/near-vm-runner)
    Finished release [optimized] target(s) in 4.11s
     Running /home/matklad/projects/nearcore/target/release/deps/near_vm_runner-f0eca327e6895b3d

running 1 test
cpu_ram_soak_test1: 125.684196ms FunctionCallError(HostError(GasLimitExceeded))
cpu_ram_soak_test2: 125.702125ms FunctionCallError(HostError(GasLimitExceeded))
cpu_ram_soak_test3: 125.205206ms FunctionCallError(HostError(GasLimitExceeded))
test tests::global_gas_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 61 filtered out; finished in 0.

$ cargo test --features protocol_feature_wasm_global_gas_counter --release --lib -- tests::global_gas_test --exact --nocapture
    Finished release [optimized] target(s) in 0.11s
     Running /home/matklad/projects/nearcore/target/release/deps/near_vm_runner-149fc9ad00e9397e

running 1 test
cpu_ram_soak_test1: 43.372139ms FunctionCallError(HostError(GasLimitExceeded))
cpu_ram_soak_test2: 431.044328ms FunctionCallError(HostError(GasLimitExceeded))
cpu_ram_soak_test3: 4.217799331s FunctionCallError(HostError(GasLimitExceeded))
test tests::global_gas_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 61 filtered out; finished in 4.76s
```

notice how in the second case the runtime increases. That's probably a
bug in the new gas metering. I suspect some integer overflow -- it only
manifests with 10^18 gas.
  • Loading branch information
matklad committed Sep 3, 2021
1 parent 0fa4b6e commit eb594a9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
39 changes: 39 additions & 0 deletions runtime/near-test-contracts/test-contract-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,42 @@ fn call_promise() {
}
}
}

#[no_mangle]
pub unsafe fn cpu_ram_soak_test1() {
let mut buf = [0u8; 100 * 1024];
let len = buf.len();
for i in 0..10_000_000 {
let j = (i * 7 + len / 2) % len;
let k = (i * 3) % len;
let tmp = buf[k];
buf[k] = buf[j];
buf[j] = tmp;
}
}

#[no_mangle]
pub unsafe fn cpu_ram_soak_test2() {
let mut buf = [0u8; 100 * 1024];
let len = buf.len();
for i in 0..100_000_000 {
let j = (i * 7 + len / 2) % len;
let k = (i * 3) % len;
let tmp = buf[k];
buf[k] = buf[j];
buf[j] = tmp;
}
}

#[no_mangle]
pub unsafe fn cpu_ram_soak_test3() {
let mut buf = [0u8; 100 * 1024];
let len = buf.len();
for i in 0..1_000_000_000 {
let j = (i * 7 + len / 2) % len;
let k = (i * 3) % len;
let tmp = buf[k];
buf[k] = buf[j];
buf[j] = tmp;
}
}
17 changes: 16 additions & 1 deletion runtime/near-vm-runner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn make_simple_contract_call_vm(
method_name: &str,
vm_kind: VMKind,
) -> (Option<VMOutcome>, Option<VMError>) {
make_simple_contract_call_with_gas_vm(code, method_name, 10u64.pow(14), vm_kind)
make_simple_contract_call_with_gas_vm(code, method_name, 10u64.pow(18), vm_kind)
}

fn make_cached_contract_call_vm(
Expand Down Expand Up @@ -118,3 +118,18 @@ fn make_cached_contract_call_vm(
Some(cache),
)
}

#[test]
fn global_gas_test() {
let code = near_test_contracts::rs_contract();

// warmup.
make_simple_contract_call_with_gas_vm(code, "cpu_ram_soak_test1", 10_u64.pow(18), VMKind::Wasmer0);

let methods = ["cpu_ram_soak_test1", "cpu_ram_soak_test2", "cpu_ram_soak_test3"];
for &m in methods.iter() {
let t = std::time::Instant::now();
let (_out, err) = make_simple_contract_call_vm(code, m, VMKind::Wasmer0);
eprintln!("{}: {:?} {:?}", m, t.elapsed(), err.unwrap())
}
}

0 comments on commit eb594a9

Please sign in to comment.