Skip to content

Commit 6d91f86

Browse files
DentosalVoxelot
andauthored
LDC support in predicates (#612)
Closes #456 Requires #611 VM PR FuelLabs/fuel-vm#848 ### Before requesting review - [x] I have reviewed the code myself --------- Co-authored-by: Brandon Kite <[email protected]>
1 parent f0b29bc commit 6d91f86

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/fuel-vm/index.md

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
- [Instruction Set](#instruction-set)
88
- [VM Initialization](#vm-initialization)
99
- [Contexts](#contexts)
10-
- [Predicate Estimation](#predicate-estimation)
11-
- [Predicate Verification](#predicate-verification)
12-
- [Script Execution](#script-execution)
10+
- [Predicate Estimation and Verification](#predicate-estimation-and-verification)
11+
- [Script Execution](#script-execution)
12+
- [Call](#call)
1313
- [Call Frames](#call-frames)
1414
- [Ownership](#ownership)
1515

@@ -82,7 +82,7 @@ To initialize the VM, the following is pushed on the stack sequentially:
8282
1. Transaction hash (`byte[32]`, word-aligned), computed as defined [here](../identifiers/transaction-id.md).
8383
1. Base asset ID (`byte[32]`, word-aligned)
8484
1. [`MAX_INPUTS`](../tx-format/consensus_parameters.md) pairs of `(asset_id: byte[32], balance: uint64)`, of:
85-
1. For [predicate estimation](#predicate-estimation) and [predicate verification](#predicate-verification), zeroes.
85+
1. For [predicate estimation and verification](#predicate-estimation-and-verification), zeroes.
8686
1. For [script execution](#script-execution), the free balance for each asset ID seen in the transaction's inputs, ordered in ascending order. If there are fewer than `MAX_INPUTS` asset IDs, the pair has a value of zero.
8787
1. Transaction length, in bytes (`uint64`, word-aligned).
8888
1. The [transaction, serialized](../tx-format/index.md).
@@ -95,54 +95,39 @@ Then the following registers are initialized (without explicit initialization, a
9595

9696
## Contexts
9797

98-
There are 4 _contexts_ in the FuelVM: [predicate estimation](#predicate-estimation), [predicate verification](#predicate-verification), [scripts](#script-execution), and [calls](./instruction-set.md#call-call-contract). A context is an isolated execution environment with defined [memory ownership](#ownership) and can be _external_ or _internal_:
98+
There are 4 _contexts_ in the FuelVM: [predicate estimation and verification](#predicate-estimation-and-verification), [scripts](#script-execution), and [calls](#call). A context is an isolated execution environment with defined [memory ownership](#ownership) and can be _external_ or _internal_:
9999

100100
- External: predicate and script. `$fp` will be zero.
101101
- Internal: call. `$fp` will be non-zero.
102102

103103
[Returning](./instruction-set.md#return-return-from-call) from a context behaves differently depending on whether the context is external or internal.
104104

105-
## Predicate Estimation
105+
### Predicate Estimation and Verification
106106

107-
For any input of type [`InputType.Coin`](../tx-format/index.md) or [`InputType.Message`](../tx-format/index.md), a non-zero `predicateLength` field means the UTXO being spent is a [`P2SH`](https://en.bitcoin.it/wiki/P2SH) rather than a [`P2PKH`](https://en.bitcoin.it/P2PKH) output.
107+
There are two ways to run predicates on the VM:
108108

109-
For each such input in the transaction, the VM is [initialized](#vm-initialization), then:
110-
111-
1. `$pc` and `$is` are set to the start of the input's `predicate` field.
112-
1. `$ggas` and `$cgas` are set to `MAX_GAS_PER_PREDICATE`.
113-
114-
Predicate estimation will fail if gas is exhausted during execution.
115-
116-
During predicate mode, hitting any of the following instructions causes predicate estimation to halt, returning Boolean `false`:
117-
118-
1. Any [contract instruction](./instruction-set.md#contract-instructions).
119-
120-
In addition, during predicate mode if `$pc` is set to a value greater than the end of predicate bytecode (this would allow bytecode outside the actual predicate), predicate estimation halts returning Boolean `false`.
121-
122-
A predicate that halts without returning Boolean `true` would not pass verification, making the entire transaction invalid. Note that predicate validity is monotonic with respect to time (i.e. if a predicate evaluates to `true` then it will always evaluate to `true` in the future).
123-
124-
After successful execution, `predicateGasUsed` is set to `MAX_GAS_PER_PREDICATE - $ggas`.
125-
126-
## Predicate Verification
109+
1. Estimation: runs the predicate and updates the amount of gas used
110+
1. Verification: runs the predicate and verifies the amount of gas used matches the input
127111

128112
For any input of type [`InputType.Coin`](../tx-format/input.md#inputcoin) or [`InputType.Message`](../tx-format/input.md#inputmessage), a non-zero `predicateLength` field means the UTXO being spent is a [`P2SH`](https://en.bitcoin.it/P2SH) rather than a [`P2PKH`](https://en.bitcoin.it/P2PKH) output.
129113

130114
For each such input in the transaction, the VM is [initialized](#vm-initialization), then:
131115

132116
1. `$pc` and `$is` are set to the start of the input's `predicate` field.
133-
1. `$ggas` and `$cgas` are set to `predicateGasUsed`.
117+
1. `$ggas` and `$cgas` are set to `MAX_GAS_PER_PREDICATE` for estimation, and `predicateGasUsed` for verification.
134118

135-
Predicate verification will fail if gas is exhausted during execution.
119+
Predicate execution will fail if gas is exhausted during execution.
136120

137-
During predicate mode, hitting any [contract instruction](./instruction-set.md#contract-instructions) causes predicate verification to halt, returning Boolean `false`.
121+
During predicate mode, hitting any [contract instruction](./instruction-set.md#contract-instructions) (except `ldc` with non-contract target) causes predicate verification to halt, returning Boolean `false`.
138122

139-
In addition, during predicate mode if `$pc` is set to a value greater than the end of predicate bytecode (this would allow bytecode outside the actual predicate), predicate verification halts returning Boolean `false`.
123+
A predicate that halts without returning Boolean `true` would not pass verification, making the entire transaction invalid. Note that predicate return value is monotonic with respect to time (i.e. if a predicate evaluates to `true` then it will always evaluate to `true` in the future).
140124

141-
A predicate that halts without returning Boolean `true` does not pass verification, making the entire transaction invalid. Note that predicate validity is monotonic with respect to time (i.e. if a predicate evaluates to `true` then it will always evaluate to `true` in the future).
125+
After successful execution, the run mode is determines the final step:
142126

143-
After execution, if `$ggas` is non-zero, predicate verification fails.
127+
1. Estimation: `predicateGasUsed` is set to `MAX_GAS_PER_PREDICATE - $ggas`.
128+
1. Verification: if `$ggas` is non-zero, predicate verification fails.
144129

145-
## Script Execution
130+
### Script Execution
146131

147132
If script bytecode is present, transaction validation requires execution.
148133

@@ -157,6 +142,10 @@ For each instruction, its gas cost `gc` is first computed. If `gc > $cgas`, dedu
157142

158143
After the script has been executed, `tx.receiptsRoot` is updated to contain the Merkle root of the receipts, [as described in the `TransactionScript` spec](../tx-format/transaction.md#`TransactionScript`).
159144

145+
### Call
146+
147+
Call context is entered via [`CALL` instruction](./instruction-set.md#call-call-contract). It's also called _internal context_, or _contract context_. Call context is used to access state of a contract.
148+
160149
## Call Frames
161150

162151
Cross-contract calls push a _call frame_ onto the stack, similar to a stack frame used in regular languages for function calls (which may be used by a high-level language that targets the FuelVM). The distinction is as follows:

0 commit comments

Comments
 (0)