From 3a8e2c7f73668350521837d51568d77db3beb5a1 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 15:05:13 +0300 Subject: [PATCH 01/12] Add Blob tx and related instructions --- src/fuel-vm/instruction-set.md | 57 ++++++++++++++++++++++++++++++++++ src/identifiers/blob-id.md | 10 ++++++ src/identifiers/index.md | 1 + src/tx-format/index.md | 3 ++ src/tx-format/transaction.md | 28 +++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 src/identifiers/blob-id.md diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index b301bb4f..43988d04 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -106,6 +106,10 @@ - [`TIME`: Timestamp at height](#time-timestamp-at-height) - [`TR`: Transfer coins to contract](#tr-transfer-coins-to-contract) - [`TRO`: Transfer coins to output](#tro-transfer-coins-to-output) +- [Blob Instructions](#blob-instructions) + - [`BSIZ`: Blob size](#bsiz-blob-size) + - [`BLDC`: Load code from a blob](#blcd-load-code-from-a-blob) + - [`BLDD`: Load data from a blob](#blcd-load-data-from-a-blob) - [Cryptographic Instructions](#cryptographic-instructions) - [`ECK1`: Secp251k1 signature recovery](#eck1-secp256k1-signature-recovery) - [`ECR1`: Secp256r1 signature recovery](#ecr1-secp256r1-signature-recovery) @@ -2255,6 +2259,59 @@ In an external context, decrease `MEM[balanceOfStart(MEM[$rD, 32]), 8]` by `$rC` This modifies the `balanceRoot` field of the appropriate output(s). +## Blob Instructions + +All these instructions advance the program counter `$pc` by `4` after performing their operation. + +### `BSIZ`: Blob size + +| | | +|-------------|-----------------------------------------------------------------------------------------------------------| +| Description | Set `$rA` to the size of the blob with ID equal to the 32 bytes in memory starting at `$rB`. | +| Operation | ```$rA = blobsize(MEM[$rB, 32]);``` | +| Syntax | `bsiz $rA, $rB` | +| Encoding | `0x00 rA rB - -` | +| Notes | | + +Panic if: + +- `$rA` is a [reserved register](./index.md#semantics) +- `$rB + 32` overflows or `> VM_MAX_RAM` +- Blob ID `MEM[$rB, 32]` is not found + +### `BLDC`: Load code from a blob + +|-------------|-------------------------------------------------------------------------------------------------------------| +| Description | Load 32-byte blob id at `$rB`, and copy `$rD` bytes starting from `$rC` into `$sp`. Set `$ssp=$sp=$sp+$rC`. | +| Operation | `assert($rA == 0); MEM[$sp, $rD] = blob($rB)[$rC, $rD];` | +| Syntax | `bldc $rA, $rB, rC, $rD` | +| Encoding | `0x00 rA rB rC rD` | +| Notes | If `$rC >` blob size, zero bytes are filled in. `$rA` is reserved for future use, and must be zero. | + +Panic if: + +- `$ra != 0` +- `$sp + $rD` overflows or `> VM_MAX_RAM` or `> $hp` +- `$rB + 32` overflows or `> VM_MAX_RAM` + +Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$sp` to `$ssp`. + +This instruction can be used to extend current script or contract from a blob. + +### `BLDD`: Load data from a blob + +|-------------|-------------------------------------------------------------------------------------------------------------| +| Description | Load 32-byte blob id at `$rB`, and copy `$rD` bytes starting from `$rC` into `$sA`. | +| Operation | `0; MEM[$rA, $rD] = blob($rB)[$rC, $rD];` | +| Syntax | `bldd $rA, $rB, rC, $rD` | +| Encoding | `0x00 rA rB rC rD` | +| Notes | If `$rC >` blob size, zero bytes are filled in. | + +Panic if: + +- `$rA + $rD` overflows or `> VM_MAX_RAM` or `> $hp` +- `$rB + 32` overflows or `> VM_MAX_RAM` + ## Cryptographic Instructions All these instructions advance the program counter `$pc` by `4` after performing their operation. diff --git a/src/identifiers/blob-id.md b/src/identifiers/blob-id.md new file mode 100644 index 00000000..8c7e062e --- /dev/null +++ b/src/identifiers/blob-id.md @@ -0,0 +1,10 @@ +# Blob ID + +The _blob ID_ (also called _blob hash_) of a transaction is computed as +the [hash](../protocol/cryptographic-primitives.md#hashing) of the blob data. + +Blob ID calculation doesn't vary between chains. + +```python +sha256(blob_data) +``` diff --git a/src/identifiers/index.md b/src/identifiers/index.md index 0cff02bb..52bf2c84 100644 --- a/src/identifiers/index.md +++ b/src/identifiers/index.md @@ -3,6 +3,7 @@ This chapter defines how to compute unique identifiers. - [Asset ID](./asset.md) +- [Blob ID](./blob-id.md) - [Contract ID](./contract-id.md) - [Predicate ID](./predicate-id.md) - [Transaction ID](./transaction-id.md) diff --git a/src/tx-format/index.md b/src/tx-format/index.md index bfc91a1f..ade5b18e 100644 --- a/src/tx-format/index.md +++ b/src/tx-format/index.md @@ -7,6 +7,9 @@ The Fuel Transaction Format. - [`TransactionScript`](./transaction.md#transactionscript) - [`TransactionCreate`](./transaction.md#transactioncreate) - [`TransactionMint`](./transaction.md#transactionmint) + - [`TransactionUpgrade`](./transaction.md#transactionupgrade) + - [`TransactionUpload`](./transaction.md#transactionupload) + - [`TransactionBlob`](./transaction.md#transactionblob) - [Input](./input.md) - [`InputCoin`](./input.md#inputcoin) - [`InputContract`](./input.md#inputcontract) diff --git a/src/tx-format/transaction.md b/src/tx-format/transaction.md index 06bfc67a..28dc8fcf 100644 --- a/src/tx-format/transaction.md +++ b/src/tx-format/transaction.md @@ -7,6 +7,7 @@ enum TransactionType : uint8 { Mint = 2, Upgrade = 3, Upload = 4, + Blob = 5, } ``` @@ -232,3 +233,30 @@ Transaction is invalid if: - `subsectionIndex` >= `subsectionsNumber` - `subsectionsNumber > MAX_BYTECODE_SUBSECTIONS` - The [Binary Merkle tree](../protocol/cryptographic-primitives.md#binary-merkle-tree) root calculated from `(witnesses[witnessIndex], subsectionIndex, subsectionsNumber, proofSet)` is not equal to the `root`. Root calculation is affected by all fields, so modification of one of them invalidates the proof. + + +## `TransactionBlob` + +The `Blob` inserts a simple binary blob in the chain. It's raw immutable data that can be cheaply loaded by the VM and used as instructions or just data. Unlike `Create`, it doesn't hold any state or balances. + +`Blob`s are content-addressed, i.e. the they are uniquely identified by hash of the data field. Programs running on the VM can load an already-posted blob just by the hash, without having to specify it in contract inputs. + +| name | type | description | +|---------------------|-----------------------------|----------------------------------| +| `dataLength` | `uint64` | Size of the blob, in bytes. | +| `policyTypes` | `uint32` | Bitfield of used policy types. | +| `inputsCount` | `uint16` | Number of inputs. | +| `outputsCount` | `uint16` | Number of outputs. | +| `witnessesCount` | `uint16` | Number of witnesses. | +| `data` | `byte[]` | The data to post. | +| `policies` | [Policy](./policy.md)`[]` | List of policies. | +| `inputs` | [Input](./input.md)`[]` | List of inputs. | +| `outputs` | [Output](./output.md)`[]` | List of outputs. | +| `witnesses` | [Witness](./witness.md)`[]` | List of witnesses. | + +Transaction is invalid if: + +- Any input is of type `InputType.Contract` or `InputType.Message` where `input.dataLength > 0` +- Any input uses non-base asset. +- Any output is of type `OutputType.Contract` or `OutputType.Variable` or `OutputType.Message` or `OutputType.ContractCreated` +- Any output is of type `OutputType.Change` with non-base `asset_id` From f735803fbd87a50527c97a10237f2e5291adb4f5 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 15:26:42 +0300 Subject: [PATCH 02/12] Fix lint issues --- src/fuel-vm/instruction-set.md | 6 ++++-- src/tx-format/transaction.md | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 43988d04..5398627f 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -108,8 +108,8 @@ - [`TRO`: Transfer coins to output](#tro-transfer-coins-to-output) - [Blob Instructions](#blob-instructions) - [`BSIZ`: Blob size](#bsiz-blob-size) - - [`BLDC`: Load code from a blob](#blcd-load-code-from-a-blob) - - [`BLDD`: Load data from a blob](#blcd-load-data-from-a-blob) + - [`BLDC`: Load code from a blob](#bldc-load-code-from-a-blob) + - [`BLDD`: Load data from a blob](#bldd-load-data-from-a-blob) - [Cryptographic Instructions](#cryptographic-instructions) - [`ECK1`: Secp251k1 signature recovery](#eck1-secp256k1-signature-recovery) - [`ECR1`: Secp256r1 signature recovery](#ecr1-secp256r1-signature-recovery) @@ -2293,6 +2293,7 @@ Panic if: - `$ra != 0` - `$sp + $rD` overflows or `> VM_MAX_RAM` or `> $hp` - `$rB + 32` overflows or `> VM_MAX_RAM` +- Blob ID `MEM[$rB, 32]` is not found Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$sp` to `$ssp`. @@ -2311,6 +2312,7 @@ Panic if: - `$rA + $rD` overflows or `> VM_MAX_RAM` or `> $hp` - `$rB + 32` overflows or `> VM_MAX_RAM` +- Blob ID `MEM[$rB, 32]` is not found ## Cryptographic Instructions diff --git a/src/tx-format/transaction.md b/src/tx-format/transaction.md index 28dc8fcf..dd745c8a 100644 --- a/src/tx-format/transaction.md +++ b/src/tx-format/transaction.md @@ -234,7 +234,6 @@ Transaction is invalid if: - `subsectionsNumber > MAX_BYTECODE_SUBSECTIONS` - The [Binary Merkle tree](../protocol/cryptographic-primitives.md#binary-merkle-tree) root calculated from `(witnesses[witnessIndex], subsectionIndex, subsectionsNumber, proofSet)` is not equal to the `root`. Root calculation is affected by all fields, so modification of one of them invalidates the proof. - ## `TransactionBlob` The `Blob` inserts a simple binary blob in the chain. It's raw immutable data that can be cheaply loaded by the VM and used as instructions or just data. Unlike `Create`, it doesn't hold any state or balances. From 2042cf83c4a7dd472c898f0d37b3d58dea9fe0ad Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 15:28:38 +0300 Subject: [PATCH 03/12] More lint fixes --- src/SUMMARY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 61266ca4..4b341134 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -13,6 +13,7 @@ - [Transaction Pointer](./tx-format/tx-pointer.md) - [Computing Identifiers](./identifiers/index.md) - [Asset ID](./identifiers/asset.md) + - [Blob ID](./identifiers/blob-id.md) - [Contract ID](./identifiers/contract-id.md) - [Predicate ID](./identifiers/predicate-id.md) - [Transaction ID](./identifiers/transaction-id.md) From d572ede8ceb54477ee864e7fde5afad9df40ab35 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 15:52:58 +0300 Subject: [PATCH 04/12] Clarify wordings of BLDC --- src/fuel-vm/instruction-set.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 5398627f..386bba5a 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -2282,8 +2282,8 @@ Panic if: ### `BLDC`: Load code from a blob |-------------|-------------------------------------------------------------------------------------------------------------| -| Description | Load 32-byte blob id at `$rB`, and copy `$rD` bytes starting from `$rC` into `$sp`. Set `$ssp=$sp=$sp+$rC`. | -| Operation | `assert($rA == 0); MEM[$sp, $rD] = blob($rB)[$rC, $rD];` | +| Description | Copy `$rD` bytes at offset `$rC` from blob with 32-byte id at `$rB` into memory just after the stack. | +| Operation | `assert($rA == 0); MEM[$sp, $rD] = blob(MEM[$rB, 32])[$rC, $rD]; $sp=$sp+$rC; $ssp=$sp;` | | Syntax | `bldc $rA, $rB, rC, $rD` | | Encoding | `0x00 rA rB rC rD` | | Notes | If `$rC >` blob size, zero bytes are filled in. `$rA` is reserved for future use, and must be zero. | @@ -2297,13 +2297,13 @@ Panic if: Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$sp` to `$ssp`. -This instruction can be used to extend current script or contract from a blob. +This instruction can be used to extend current script or contract from a blob. Previous stack contents are frozen, and will be treated as code by the VM. ### `BLDD`: Load data from a blob |-------------|-------------------------------------------------------------------------------------------------------------| | Description | Load 32-byte blob id at `$rB`, and copy `$rD` bytes starting from `$rC` into `$sA`. | -| Operation | `0; MEM[$rA, $rD] = blob($rB)[$rC, $rD];` | +| Operation | `MEM[$rA, $rD] = blob(MEM[$rB, 32])[$rC, $rD];` | | Syntax | `bldd $rA, $rB, rC, $rD` | | Encoding | `0x00 rA rB rC rD` | | Notes | If `$rC >` blob size, zero bytes are filled in. | From da2989087508b3dd55b41748700dd6072ef19c15 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 15:55:14 +0300 Subject: [PATCH 05/12] Fix incorrect order of $sp and $ssp --- src/fuel-vm/instruction-set.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 386bba5a..87cbd8ce 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -2295,7 +2295,7 @@ Panic if: - `$rB + 32` overflows or `> VM_MAX_RAM` - Blob ID `MEM[$rB, 32]` is not found -Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$sp` to `$ssp`. +Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$ssp` to `$sp`. This instruction can be used to extend current script or contract from a blob. Previous stack contents are frozen, and will be treated as code by the VM. From ffd8572235d31063f7cef4fb673d0a8b3a7470ac Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 17:39:22 +0300 Subject: [PATCH 06/12] Remove BLDC, make stack executable --- src/fuel-vm/index.md | 4 ---- src/fuel-vm/instruction-set.md | 23 +---------------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/src/fuel-vm/index.md b/src/fuel-vm/index.md index 1a17be1a..d94184df 100644 --- a/src/fuel-vm/index.md +++ b/src/fuel-vm/index.md @@ -198,7 +198,3 @@ If the context is internal, the owned memory range for a call frame is: 1. `[$ssp, $sp)`: the writable stack area of the call frame. 1. `[$hp, $fp->$hp)`: the heap area allocated by this call frame. - -### Executablity - -Memory is only executable in range `[$is, $ssp)`. Attempting to execute instructions outside these boundaries will cause a panic. This area never overlaps with writable memory, essentially providing [W^X](https://en.wikipedia.org/wiki/W%5EX) protection. diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 87cbd8ce..3a71ef4b 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -108,7 +108,6 @@ - [`TRO`: Transfer coins to output](#tro-transfer-coins-to-output) - [Blob Instructions](#blob-instructions) - [`BSIZ`: Blob size](#bsiz-blob-size) - - [`BLDC`: Load code from a blob](#bldc-load-code-from-a-blob) - [`BLDD`: Load data from a blob](#bldd-load-data-from-a-blob) - [Cryptographic Instructions](#cryptographic-instructions) - [`ECK1`: Secp251k1 signature recovery](#eck1-secp256k1-signature-recovery) @@ -2268,7 +2267,7 @@ All these instructions advance the program counter `$pc` by `4` after performing | | | |-------------|-----------------------------------------------------------------------------------------------------------| | Description | Set `$rA` to the size of the blob with ID equal to the 32 bytes in memory starting at `$rB`. | -| Operation | ```$rA = blobsize(MEM[$rB, 32]);``` | +| Operation | `$rA = len(blob(MEM[$rB, 32]));` | | Syntax | `bsiz $rA, $rB` | | Encoding | `0x00 rA rB - -` | | Notes | | @@ -2279,26 +2278,6 @@ Panic if: - `$rB + 32` overflows or `> VM_MAX_RAM` - Blob ID `MEM[$rB, 32]` is not found -### `BLDC`: Load code from a blob - -|-------------|-------------------------------------------------------------------------------------------------------------| -| Description | Copy `$rD` bytes at offset `$rC` from blob with 32-byte id at `$rB` into memory just after the stack. | -| Operation | `assert($rA == 0); MEM[$sp, $rD] = blob(MEM[$rB, 32])[$rC, $rD]; $sp=$sp+$rC; $ssp=$sp;` | -| Syntax | `bldc $rA, $rB, rC, $rD` | -| Encoding | `0x00 rA rB rC rD` | -| Notes | If `$rC >` blob size, zero bytes are filled in. `$rA` is reserved for future use, and must be zero. | - -Panic if: - -- `$ra != 0` -- `$sp + $rD` overflows or `> VM_MAX_RAM` or `> $hp` -- `$rB + 32` overflows or `> VM_MAX_RAM` -- Blob ID `MEM[$rB, 32]` is not found - -Increment `$fp->codesize` and `$sp` by `$rD` padded to word alignment. Then set `$ssp` to `$sp`. - -This instruction can be used to extend current script or contract from a blob. Previous stack contents are frozen, and will be treated as code by the VM. - ### `BLDD`: Load data from a blob |-------------|-------------------------------------------------------------------------------------------------------------| From 9defdb520169749d8ddd77151acd12ed46f0da5f Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Mon, 24 Jun 2024 18:00:13 +0300 Subject: [PATCH 07/12] Restore some nonexecutability rules --- src/fuel-vm/index.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fuel-vm/index.md b/src/fuel-vm/index.md index d94184df..8ee258f1 100644 --- a/src/fuel-vm/index.md +++ b/src/fuel-vm/index.md @@ -198,3 +198,7 @@ If the context is internal, the owned memory range for a call frame is: 1. `[$ssp, $sp)`: the writable stack area of the call frame. 1. `[$hp, $fp->$hp)`: the heap area allocated by this call frame. + +### Executablity + +Memory is only executable in range `[$is, $sp)`. Attempting to execute instructions outside these boundaries will cause a panic. From 3699961efa2cecca3b621e3f7fd3e8a4de7be88b Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Wed, 26 Jun 2024 08:11:48 +0300 Subject: [PATCH 08/12] Move blob tx data to a witness, like other txs do --- src/tx-format/transaction.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tx-format/transaction.md b/src/tx-format/transaction.md index dd745c8a..1d223abe 100644 --- a/src/tx-format/transaction.md +++ b/src/tx-format/transaction.md @@ -242,12 +242,12 @@ The `Blob` inserts a simple binary blob in the chain. It's raw immutable data th | name | type | description | |---------------------|-----------------------------|----------------------------------| -| `dataLength` | `uint64` | Size of the blob, in bytes. | +| `id` | `byte[32]` | Blob id, i.e. hash of the data. | +| `witnessIndex` | `uint16` | The witness index of the data. | | `policyTypes` | `uint32` | Bitfield of used policy types. | | `inputsCount` | `uint16` | Number of inputs. | | `outputsCount` | `uint16` | Number of outputs. | | `witnessesCount` | `uint16` | Number of witnesses. | -| `data` | `byte[]` | The data to post. | | `policies` | [Policy](./policy.md)`[]` | List of policies. | | `inputs` | [Input](./input.md)`[]` | List of inputs. | | `outputs` | [Output](./output.md)`[]` | List of outputs. | @@ -259,3 +259,5 @@ Transaction is invalid if: - Any input uses non-base asset. - Any output is of type `OutputType.Contract` or `OutputType.Variable` or `OutputType.Message` or `OutputType.ContractCreated` - Any output is of type `OutputType.Change` with non-base `asset_id` +- `witnessIndex >= tx.witnessesCount` +- `sha256(witnesses[witnessIndex]) != id` From 5a2c3a018e743d031e6d13a352da7a6b8b852680 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Fri, 26 Jul 2024 16:48:24 +0300 Subject: [PATCH 09/12] Revert executability section --- src/fuel-vm/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/index.md b/src/fuel-vm/index.md index 5d7be7ce..7ae5eb88 100644 --- a/src/fuel-vm/index.md +++ b/src/fuel-vm/index.md @@ -209,4 +209,4 @@ If the context is internal, the owned memory range for a call frame is: ### Executability -Memory is only executable in range `[$is, $sp)`. Attempting to execute instructions outside these boundaries will cause a panic. +Memory is only executable in range `[$is, $ssp)`. Attempting to execute instructions outside these boundaries will cause a panic. This area never overlaps with writable memory, essentially providing [W^X](https://en.wikipedia.org/wiki/W%5EX) protection. \ No newline at end of file From 79f0a549e819344e0574cf198d443b11a44c24da Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Fri, 26 Jul 2024 16:50:00 +0300 Subject: [PATCH 10/12] Newline --- src/fuel-vm/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fuel-vm/index.md b/src/fuel-vm/index.md index 7ae5eb88..fb443a20 100644 --- a/src/fuel-vm/index.md +++ b/src/fuel-vm/index.md @@ -209,4 +209,4 @@ If the context is internal, the owned memory range for a call frame is: ### Executability -Memory is only executable in range `[$is, $ssp)`. Attempting to execute instructions outside these boundaries will cause a panic. This area never overlaps with writable memory, essentially providing [W^X](https://en.wikipedia.org/wiki/W%5EX) protection. \ No newline at end of file +Memory is only executable in range `[$is, $ssp)`. Attempting to execute instructions outside these boundaries will cause a panic. This area never overlaps with writable memory, essentially providing [W^X](https://en.wikipedia.org/wiki/W%5EX) protection. From 71f74625f47c8ad86a86a2a21a06639c784bd1e3 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Wed, 31 Jul 2024 18:57:06 +0300 Subject: [PATCH 11/12] Add LDC changes --- src/fuel-vm/instruction-set.md | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 8f3368af..61d99be8 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -91,7 +91,7 @@ - [`CCP`: Code copy](#ccp-code-copy) - [`CROO`: Code Merkle root](#croo-code-merkle-root) - [`CSIZ`: Code size](#csiz-code-size) - - [`LDC`: Load code from an external contract](#ldc-load-code-from-an-external-contract) + - [`LDC`: Load code from an external contract](#ldc-load-code-from-an-external-contract-or-blob) - [`LOG`: Log event](#log-log-event) - [`LOGD`: Log data event](#logd-log-data-event) - [`MINT`: Mint new coins](#mint-mint-new-coins) @@ -1796,27 +1796,37 @@ Panic if: - `$rB + 32` overflows or `> VM_MAX_RAM` - Contract with ID `MEM[$rB, 32]` is not in `tx.inputs` -### `LDC`: Load code from an external contract +### `LDC`: Load code from an external contract or blob | | | |-------------|---------------------------------------------------------------------------------------------------------------------------------------------------| -| Description | Copy `$rC` bytes of code starting at `$rB` for contract with ID equal to the 32 bytes in memory starting at `$rA` into memory starting at `$ssp`. | -| Operation | ```MEM[$ssp, $rC] = code($rA, $rB, $rC);``` | -| Syntax | `ldc $rA, $rB, $rC` | -| Encoding | `0x00 rA rB rC -` | +| Description | Copy `$rC` bytes of code at offset `$rB` from object with 32 byte id starting at `$rA` into memory starting at `$ssp`. Object type is in `imm`. | +| Operation | `id = mem[$rA,32]; code = match imm { 0 => contract_code($id), 1 => blob_payload($id) }; MEM[$ssp, $rC] = code[$rB, $rC];` | +| Syntax | `ldc $rA, $rB, $rC, imm` | +| Encoding | `0x00 rA rB rC imm` | | Notes | If `$rC` is greater than the code size, zero bytes are filled in. | +Object type from `imm` determined the source for loading as follows: + +| `imm` | Object type | +|-------|---------------| +| `0` | Contract code | +| `1` | Blob payload | +| other | *reserved* | + Panic if: - `$ssp + $rC` overflows or `> VM_MAX_RAM` - `$rA + 32` overflows or `> VM_MAX_RAM` - `$ssp + $rC >= $hp` -- `$rC > CONTRACT_MAX_SIZE` -- Contract with ID `MEM[$rA, 32]` is not in `tx.inputs` +- `imm == 0` and `$rC > CONTRACT_MAX_SIZE` +- `imm == 0` and contract with ID `MEM[$rA, 32]` is not in `tx.inputs` +- `imm == 1` and contract with ID `MEM[$rA, 32]` is not found in the chain state +- `imm >= 2` (reserved value) Increment `$fp->codesize`, `$ssp` by `$rC` padded to word alignment. Then set `$sp` to `$ssp`. -This instruction can be used to concatenate the code of multiple contracts together. It can only be used when the stack area of the call frame is zero-sized. +This instruction can be used to concatenate the code of multiple contracts or blobs together. It can only be used when the stack area of the call frame is zero-sized. ### `LOG`: Log event From 88a3aae3d38ff350806a9f81af9fc75a266bb998 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Wed, 31 Jul 2024 18:58:29 +0300 Subject: [PATCH 12/12] lint --- src/fuel-vm/instruction-set.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fuel-vm/instruction-set.md b/src/fuel-vm/instruction-set.md index 61d99be8..811c12b7 100644 --- a/src/fuel-vm/instruction-set.md +++ b/src/fuel-vm/instruction-set.md @@ -1752,7 +1752,7 @@ Panic if: | Notes | If `$rD` is greater than the code size, zero bytes are filled in. | This is used only for reading and inspecting code of other contracts. -Use [`LDC`](#ldc-load-code-from-an-external-contract) to load code for executing. +Use [`LDC`](#ldc-load-code-from-an-external-contract-or-blob) to load code for executing. Panic if: @@ -1812,7 +1812,7 @@ Object type from `imm` determined the source for loading as follows: |-------|---------------| | `0` | Contract code | | `1` | Blob payload | -| other | *reserved* | +| other | _reserved_ | Panic if: