From b71ee348a1b13a6eabb6d85eea8745b0c906a609 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 7 Feb 2022 22:56:59 +0100 Subject: [PATCH 1/3] Improve readability of Gas docs and add section on overflow potential --- docs/GAS.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/GAS.md b/docs/GAS.md index 4456a89aff..8c9b82bc61 100644 --- a/docs/GAS.md +++ b/docs/GAS.md @@ -5,8 +5,10 @@ including CPU time and storage cost. It's unit is 1, i.e. you can think of it as countable points. Gas consumption is deterministic, so executing the same thing costs the same amount of gas across all hardware and operating systems. +## CosmWasm gas vs. Cosmos SDK gas + CosmWasm charges gas for Wasm operations, calls to host functions and calls to -the Cosmos SDK. CosmWasm gas is different from Cosmos SDK gas as the numbers +the Cosmos SDK. _CosmWasm gas_ is different from _Cosmos SDK gas_ as the numbers here are much larger. Since we charge gas for arbitrary user defined operations, we need to charge each Wasm operation individually and cannot group larger tasks together. As a result, the gas values become much larger than in Cosmos SDK even @@ -14,6 +16,8 @@ for very fast executions. There is a [multiplier][defaultgasmultiplier] to translate between CosmWasm gas and Cosmos SDK. It was measured and set to 100 a while ago and can be adjusted when necessary. +## CosmWasm gas pricing + For CosmWasm gas, the target gas consumption is 1 Teragas (10^12 gas) per millisecond. This idea is [inspired by NEAR][neargas] and we encourage you to read their excellent docs on that topic. @@ -38,3 +42,26 @@ multiple ways: https://github.com/CosmWasm/wasmd/blob/v0.19.0/x/wasm/keeper/gas_register.go#L18 [neargas]: https://docs.near.org/docs/concepts/gas [#1120]: https://github.com/CosmWasm/cosmwasm/pull/1120 + +## Gas overflow potential + +CosmWasm gas aims for 1 Teragas/second, i.e. the uint64 range exceeds after 18 +billion seconds (213 days)1. Assuming a max supported block execution +time of 100 seconds, the gas price has to be over-priced by a factor of 184 +million (184 Petagas/second) in order to exceed the uint64 range2. +Since serious over or underpricing is considered a bug, using uint64 for gas +mesurments is considered safe. + +Cosmos SDK gas uses values that are smaller by a factor of 150_000, so those +don't overflow as well. Since no Cosmos SDK gas values are processed inside of +this repository, this is not our main concern. However, it's good to know that +we can safely pass them in uint64 fields, as long as the full range is +supported. This is the case for the C API as well as +[JSON numbers](https://www.json.org/) as long as both sides support integers in +their JSON implementation. Go and Rust do that while many other implementations +don't support integers numbers and convert to them to IEEE-754 doubles, which +has a safe integer range up about 53 bit (e.g. JavaScript and jq). + +1 Python3: `(2**64-1) / 10**12` + +2 Python3: `((2**64-1)/100) / 10**12` From 71dfc7078d1bd0f62f640bad7bd909c888c3c1d0 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 7 Feb 2022 23:03:10 +0100 Subject: [PATCH 2/3] Fix 1 Teragas/second -> 1 Teragas/millisecond --- docs/GAS.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/GAS.md b/docs/GAS.md index 8c9b82bc61..fc2961365a 100644 --- a/docs/GAS.md +++ b/docs/GAS.md @@ -45,12 +45,12 @@ multiple ways: ## Gas overflow potential -CosmWasm gas aims for 1 Teragas/second, i.e. the uint64 range exceeds after 18 -billion seconds (213 days)1. Assuming a max supported block execution -time of 100 seconds, the gas price has to be over-priced by a factor of 184 -million (184 Petagas/second) in order to exceed the uint64 range2. +CosmWasm gas aims for 1 Teragas/millisecond, i.e. the uint64 range exceeds after +18 million seconds (5 hours)1. Assuming a max supported block +execution time of 30 seconds, the gas price has to be over-priced by a factor of +614 (614 Teragas/millisecond) in order to exceed the uint64 range2. Since serious over or underpricing is considered a bug, using uint64 for gas -mesurments is considered safe. +measurements is considered safe. Cosmos SDK gas uses values that are smaller by a factor of 150_000, so those don't overflow as well. Since no Cosmos SDK gas values are processed inside of @@ -62,6 +62,6 @@ their JSON implementation. Go and Rust do that while many other implementations don't support integers numbers and convert to them to IEEE-754 doubles, which has a safe integer range up about 53 bit (e.g. JavaScript and jq). -1 Python3: `(2**64-1) / 10**12` +1 Python3: `(2**64-1)/1000 / 10**12` -2 Python3: `((2**64-1)/100) / 10**12` +2 Python3: `((2**64-1)/1000/30) / 10**122` From 513fc290ad8b8d141a988ad0fde8f62d9f1b7e91 Mon Sep 17 00:00:00 2001 From: Simon Warta <2603011+webmaster128@users.noreply.github.com> Date: Thu, 17 Feb 2022 23:09:21 +0100 Subject: [PATCH 3/3] Update docs/GAS.md Co-authored-by: Mauro Lacy --- docs/GAS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/GAS.md b/docs/GAS.md index fc2961365a..73eb260345 100644 --- a/docs/GAS.md +++ b/docs/GAS.md @@ -59,8 +59,8 @@ we can safely pass them in uint64 fields, as long as the full range is supported. This is the case for the C API as well as [JSON numbers](https://www.json.org/) as long as both sides support integers in their JSON implementation. Go and Rust do that while many other implementations -don't support integers numbers and convert to them to IEEE-754 doubles, which -has a safe integer range up about 53 bit (e.g. JavaScript and jq). +don't support integers, and convert them to IEEE-754 doubles, which has a safe +integer range up to about 53 bit (e.g. JavaScript and jq). 1 Python3: `(2**64-1)/1000 / 10**12`