|
7 | 7 | The cost of executing a transaction is measured in gas, and counted by updating the $gc$ state variable in the runtime. |
8 | 8 |
|
9 | 9 | We charge gas for 5 categories of operations: |
10 | | -- [WASM opcode execution](#wasm-opcode-execution-inside-a-contract-call) inside contract calls. |
11 | | -- [Reading and writing WASM memory from host functions](#host-function-execution-inside-a-contract-call) inside contract calls. |
| 10 | +- [WASM opcode execution](#wasm-opcode-execution) inside contract calls. |
| 11 | +- [Reading and writing WASM memory from host functions](#accessing-wasm-memory-from-host-functions) inside contract calls. |
12 | 12 | - [Transaction-related data storage](#transaction-related-data-storage). |
13 | 13 | - [World state storage and access](#world-state-storage-and-access). |
14 | 14 | - [Cryptographic operations](#cryptographic-operations) inside contract calls. |
@@ -54,16 +54,31 @@ Transaction-related data includes every byte in the Borsh-serialization of trans |
54 | 54 | |---|---|---|---| |
55 | 55 | |$G_{txdata}$|30|Cost of including 1 byte of data in a Block as part of a transaction or a receipt.|$G_{txdatanonzero} = 16$| |
56 | 56 |
|
57 | | -The cost of storing a transaction and the fixed-sized component of its receipt ($G_{minrcpsize}) is known before the transaction is executed and charged in the transaction's [inclusion cost](#transaction-inclusion-cost). |
| 57 | +The cost of storing a transaction and the fixed-sized component of its receipt is known before the transaction is charged in the transaction's [inclusion cost](#transaction-inclusion-cost). On the other hand, the size of the dynamic-sized contents of command receipts, namely return values and logs, are dynamically charged during execution. |
| 58 | + |
| 59 | +## Transaction inclusion cost |
| 60 | + |
| 61 | +An amount of gas called the "transaction inclusion cost" is charged before every other step in a transaction's execution by initializing the [$gc$ Runtime state variable](Runtime.md#state-variables) to it: |
58 | 62 |
|
59 | 63 | |Formula|Value|Description| |
60 | 64 | |---|---|---| |
61 | | -|$G_{minrcpsize}(cmds)$|$4 + cmds \times G_{cmdrcpminsize}$|Serialized size of a receipt containing $cmds$ minimum-sized command receipts.| |
62 | | -|$G_{mincmdrcpsize}$|$17$|Serialized size of a minimum-size command receipt.| |
| 65 | +|$G_{txincl}(txn)$|$[serialize(txn).len() + G_{minrcpsize}(txn.commands)] \times G_{txdata} + 5 \times [G_{sget}(G_{acckeylen}, 8) + G_{sset}(G_{acckeylen}, 8, 8)]$|The transaction inclusion cost of a transaction $txn$.| |
63 | 66 |
|
64 | | -On the other hand, the size of the dynamic-sized contents of command receipts, namely return values and logs, can only be determined during execution: |
| 67 | +The above formula consists of two terms that are added together: |
| 68 | +1. The first term accounts for storing the transaction and its "minimal-size" receipt. |
| 69 | +2. The second term accounts for getting and then setting 5 pair of keys in the Accounts Trie in the pre-charge and charge execution phases: |
| 70 | + 1. The signer's nonce. |
| 71 | + 2. The signer's balance, in the pre-charge phase. |
| 72 | + 3. The signer's balance again, in the charge phase. |
| 73 | + 4. The proposer's balance. |
| 74 | + 5. The treasury's balance. |
65 | 75 |
|
66 | | -A nuance with return values is that the cost of writing the length of the return value--return values are `Vec<u8>`--is already charged in writing a minimum-size command receipt, and so only the cost of writing the contents of the vector is charged when a command returns a value. |
| 76 | +The minimal-size receipt of any transaction with $n$ commands is a `Vec<CommandReceipt>` containing $n$ identical command receipts, each with an empty return value and log: |
| 77 | + |
| 78 | +|Formula|Value|Description| |
| 79 | +|---|---|---| |
| 80 | +|$G_{minrcpsize}(n)$|$4 + G_{cmdrcpminsize}$|Size of a receipt containing $n$ minimal-sized command receipt.| |
| 81 | +|$G_{mincmdrcpsize}$|$17$|Size of a single minimal-sized command receipt.| |
67 | 82 |
|
68 | 83 | ## World state storage and access |
69 | 84 |
|
@@ -183,23 +198,7 @@ The second part: |
183 | 198 | 5. Compile the Rust binary to a wasm32-unknown-unknown target with `--release` optimizations. |
184 | 199 | 6. Execute the WASM module with varying message lengths, gas metering opcodes according to our schedule. |
185 | 200 |
|
186 | | -We found that the base cost of verifying an Ed25519 signature in a contract is 7,000,000. Applying the 5x faster estimation from before, this led us to setting a base cost of 1,400,000 in $G_{wvrfy25519}$. The $len * 16$ formula comes from the fact that Ed25519 verification involves a SHA512 as a preparation step. |
187 | | - |
188 | | -## Transaction inclusion cost |
189 | | - |
190 | | -To be included in the blockchain, a transaction must, at the minimum, be able to pay for the following operations: |
191 | | -- $G_{txdata}$ for storing the transaction in a block. |
192 | | -- $G_{txdata}$ for storing a minimum-sized Receipt of length `transaction.commands.len()` in a block. |
193 | | -- $G_{sget}$ and $G_{sset}$ for getting, and setting these 5 keys, each with values of length 8: |
194 | | - - The signer's nonce. |
195 | | - - The signer's balance, once in the [pre-charge](Runtime.md#pre-charge) phase. |
196 | | - - The signer's balance again, in the [charge](Runtime.md#charge) phase. |
197 | | - - The proposer's balance. |
198 | | - - The treasury's balance. |
199 | | - |
200 | | -|Formula|Value|Description| |
201 | | -|---|---|---| |
202 | | -|$G_{txincl}(txn)$|$[serialize(txn).len() + G_{minrcpsize}(txn.commands)] \times G_{txdata} + 5 \times [G_{sget}(8) + G_{sset}(8)]$|Cost of including a transaction $txn$ in the blockchain that is borne at the beginning of its execution.| |
| 201 | +We found that the base cost of verifying an Ed25519 signature in a contract is 7,000,000. Applying the 5x faster estimation from before, this led us to setting a base cost of 1,400,000 in $G_{wvrfy25519}$. The $len * 16$ formula comes from the fact that Ed25519 verification involves a SHA512 as a preparation step. |
203 | 202 |
|
204 | 203 | ## Appendix: WASM opcode gas schedule |
205 | 204 |
|
|
0 commit comments