Skip to content

Commit 4af7d5a

Browse files
authored
Merge pull request CosmWasm#1222 from CosmWasm/improve-gas-docs
Improve readability of Gas docs and add section on overflow potential
2 parents be4d94a + 513fc29 commit 4af7d5a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

docs/GAS.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ including CPU time and storage cost. It's unit is 1, i.e. you can think of it as
55
countable points. Gas consumption is deterministic, so executing the same thing
66
costs the same amount of gas across all hardware and operating systems.
77

8+
## CosmWasm gas vs. Cosmos SDK gas
9+
810
CosmWasm charges gas for Wasm operations, calls to host functions and calls to
9-
the Cosmos SDK. CosmWasm gas is different from Cosmos SDK gas as the numbers
11+
the Cosmos SDK. _CosmWasm gas_ is different from _Cosmos SDK gas_ as the numbers
1012
here are much larger. Since we charge gas for arbitrary user defined operations,
1113
we need to charge each Wasm operation individually and cannot group larger tasks
1214
together. As a result, the gas values become much larger than in Cosmos SDK even
1315
for very fast executions. There is a [multiplier][defaultgasmultiplier] to
1416
translate between CosmWasm gas and Cosmos SDK. It was measured and set to 100 a
1517
while ago and can be adjusted when necessary.
1618

19+
## CosmWasm gas pricing
20+
1721
For CosmWasm gas, the target gas consumption is 1 Teragas (10^12 gas) per
1822
millisecond. This idea is [inspired by NEAR][neargas] and we encourage you to
1923
read their excellent docs on that topic.
@@ -38,3 +42,26 @@ multiple ways:
3842
https://github.com/CosmWasm/wasmd/blob/v0.19.0/x/wasm/keeper/gas_register.go#L18
3943
[neargas]: https://docs.near.org/docs/concepts/gas
4044
[#1120]: https://github.com/CosmWasm/cosmwasm/pull/1120
45+
46+
## Gas overflow potential
47+
48+
CosmWasm gas aims for 1 Teragas/millisecond, i.e. the uint64 range exceeds after
49+
18 million seconds (5 hours)<sup>1</sup>. Assuming a max supported block
50+
execution time of 30 seconds, the gas price has to be over-priced by a factor of
51+
614 (614 Teragas/millisecond) in order to exceed the uint64 range<sup>2</sup>.
52+
Since serious over or underpricing is considered a bug, using uint64 for gas
53+
measurements is considered safe.
54+
55+
Cosmos SDK gas uses values that are smaller by a factor of 150_000, so those
56+
don't overflow as well. Since no Cosmos SDK gas values are processed inside of
57+
this repository, this is not our main concern. However, it's good to know that
58+
we can safely pass them in uint64 fields, as long as the full range is
59+
supported. This is the case for the C API as well as
60+
[JSON numbers](https://www.json.org/) as long as both sides support integers in
61+
their JSON implementation. Go and Rust do that while many other implementations
62+
don't support integers, and convert them to IEEE-754 doubles, which has a safe
63+
integer range up to about 53 bit (e.g. JavaScript and jq).
64+
65+
<sup>1</sup> Python3: `(2**64-1)/1000 / 10**12`
66+
67+
<sup>2</sup> Python3: `((2**64-1)/1000/30) / 10**122`

0 commit comments

Comments
 (0)