From e596e31841d44f4580f179688081208de6b6c736 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:17:10 -0700 Subject: [PATCH 1/6] Add BAL balance opcode --- specs/vm/opcodes.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index 3a5a602a..45428a0d 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -73,6 +73,7 @@ - [SWWQ: State write 32 bytes](#swwq-state-write-32-bytes) - [TR: Transfer coins to contract](#tr-transfer-coins-to-contract) - [TRO: Transfer coins to output](#tro-transfer-coins-to-output) + - [BAL: Balance of contract ID](#bal-balance) - [Cryptographic Opcodes](#cryptographic-opcodes) - [ECR: Signature recovery](#ecr-signature-recovery) - [K256: keccak-256](#k256-keccak-256) @@ -1519,6 +1520,25 @@ In an external context, decrease `MEM[balanceOfStart(MEM[$rD, 32]), 8]` by `$rC` This modifies the `balanceRoot` field of the appropriate output(s). +### BAL: Balance + +| | | +|-------------|---------------------------------------------------------------------------| +| Description | Set `$rA` to the balance of color at `$rB` for contract with ID at `$rC`. | +| Operation | ```$rA = balanceOf(MEM[$rB, 32], MEM[$rC, 32]);``` | +| Syntax | `bal $rA, $rB, $rC` | +| Encoding | `0x00 rA rB rC -` | +| Notes | | + +Panic if: + +- `$rA` is a [reserved register](./main.md#semantics) +- `$rB + 32` overflows +- `$rC + 32` overflows +- `$rB + 32 > VM_MAX_RAM` +- `$rC + 32 > VM_MAX_RAM` +- Contract with ID `MEM[$rC, 32]` is not in `tx.inputs` + ## Cryptographic Opcodes All these opcodes advance the program counter `$pc` by `4` after performing their operation. From 047fb61c50164a6d1b6549aa6c9fb65a50467bf6 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Wed, 24 Nov 2021 10:54:25 -0700 Subject: [PATCH 2/6] Fix BAL opcode formatting --- specs/vm/opcodes.md | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index 45428a0d..8da8b760 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -52,6 +52,7 @@ - [SB: Store byte](#sb-store-byte) - [SW: Store word](#sw-store-word) - [Contract Opcodes](#contract-opcodes) + - [BAL: Balance of contract ID](#bal-balance-of-contract-id) - [BHEI: Block height](#bhei-block-height) - [BHSH: Block hash](#bhsh-block-hash) - [BURN: Burn existing coins](#burn-burn-existing-coins) @@ -73,7 +74,6 @@ - [SWWQ: State write 32 bytes](#swwq-state-write-32-bytes) - [TR: Transfer coins to contract](#tr-transfer-coins-to-contract) - [TRO: Transfer coins to output](#tro-transfer-coins-to-output) - - [BAL: Balance of contract ID](#bal-balance) - [Cryptographic Opcodes](#cryptographic-opcodes) - [ECR: Signature recovery](#ecr-signature-recovery) - [K256: keccak-256](#k256-keccak-256) @@ -973,6 +973,25 @@ Panic if: All these opcodes advance the program counter `$pc` by `4` after performing their operation, except for [CALL](#call-call-contract), [RETD](#retd-return-from-context-with-data) and [RVRT](#rvrt-revert). +### BAL: Balance of contract ID + +| | | +|-------------|---------------------------------------------------------------------------| +| Description | Set `$rA` to the balance of color at `$rB` for contract with ID at `$rC`. | +| Operation | ```$rA = balanceOf(MEM[$rB, 32], MEM[$rC, 32]);``` | +| Syntax | `bal $rA, $rB, $rC` | +| Encoding | `0x00 rA rB rC -` | +| Notes | | + +Panic if: + +- `$rA` is a [reserved register](./main.md#semantics) +- `$rB + 32` overflows +- `$rC + 32` overflows +- `$rB + 32 > VM_MAX_RAM` +- `$rC + 32 > VM_MAX_RAM` +- Contract with ID `MEM[$rC, 32]` is not in `tx.inputs` + ### BHEI: Block height | | | @@ -1520,25 +1539,6 @@ In an external context, decrease `MEM[balanceOfStart(MEM[$rD, 32]), 8]` by `$rC` This modifies the `balanceRoot` field of the appropriate output(s). -### BAL: Balance - -| | | -|-------------|---------------------------------------------------------------------------| -| Description | Set `$rA` to the balance of color at `$rB` for contract with ID at `$rC`. | -| Operation | ```$rA = balanceOf(MEM[$rB, 32], MEM[$rC, 32]);``` | -| Syntax | `bal $rA, $rB, $rC` | -| Encoding | `0x00 rA rB rC -` | -| Notes | | - -Panic if: - -- `$rA` is a [reserved register](./main.md#semantics) -- `$rB + 32` overflows -- `$rC + 32` overflows -- `$rB + 32 > VM_MAX_RAM` -- `$rC + 32 > VM_MAX_RAM` -- Contract with ID `MEM[$rC, 32]` is not in `tx.inputs` - ## Cryptographic Opcodes All these opcodes advance the program counter `$pc` by `4` after performing their operation. From cf2167f10c695d7c17d4d5ec9277fecf10dac8d9 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Thu, 25 Nov 2021 11:23:15 -0700 Subject: [PATCH 3/6] Clarify balance() helper for BAL opcode --- specs/vm/opcodes.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index 8da8b760..de0197a9 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -978,11 +978,13 @@ All these opcodes advance the program counter `$pc` by `4` after performing thei | | | |-------------|---------------------------------------------------------------------------| | Description | Set `$rA` to the balance of color at `$rB` for contract with ID at `$rC`. | -| Operation | ```$rA = balanceOf(MEM[$rB, 32], MEM[$rC, 32]);``` | +| Operation | ```$rA = balance(MEM[$rB, 32], MEM[$rC, 32]);``` | | Syntax | `bal $rA, $rB, $rC` | | Encoding | `0x00 rA rB rC -` | | Notes | | +Where helper `balance(color: byte[32], contract_id: byte[32]) -> uint32` returns the current balance of `color` of contract with ID `contract_id`. + Panic if: - `$rA` is a [reserved register](./main.md#semantics) From 89318fea08287d46fd1df18f46fed9750571d716 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Fri, 26 Nov 2021 08:35:22 -0700 Subject: [PATCH 4/6] Fix balance() helper fn return value for BAL opcode --- specs/vm/opcodes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index de0197a9..f0f97bda 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -983,7 +983,7 @@ All these opcodes advance the program counter `$pc` by `4` after performing thei | Encoding | `0x00 rA rB rC -` | | Notes | | -Where helper `balance(color: byte[32], contract_id: byte[32]) -> uint32` returns the current balance of `color` of contract with ID `contract_id`. +Where helper `balance(color: byte[32], contract_id: byte[32]) -> uint64` returns the current balance of `color` of contract with ID `contract_id`. Panic if: From d350c07d5f4c3c50a0ffbbd89c3202f5d53402e3 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Fri, 26 Nov 2021 10:31:45 -0700 Subject: [PATCH 5/6] Specify panic reasons for BAL opcode --- specs/vm/opcodes.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index f0f97bda..a5de6344 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -989,11 +989,37 @@ Panic if: - `$rA` is a [reserved register](./main.md#semantics) - `$rB + 32` overflows -- `$rC + 32` overflows - `$rB + 32 > VM_MAX_RAM` + +In a script transaction, append an additional receipt to the list of receipts, modifying `tx.receiptsRoot`: + +| name | type | description | +|------------|---------------|-------------------------------------------------------------------------| +| `type` | `ReceiptType` | `ReceiptType.ScriptResult` | +| `result` | `uint64` | `PanicReason.MemoryOverflow \| rB >> 8` | +| `gas_used` | `uint64` | Gas consumed by the script. | + +- `$rC + 32` overflows - `$rC + 32 > VM_MAX_RAM` + +In a script transaction, append an additional receipt to the list of receipts, modifying `tx.receiptsRoot`: + +| name | type | description | +|------------|---------------|-------------------------------------------------------------------------| +| `type` | `ReceiptType` | `ReceiptType.ScriptResult` | +| `result` | `uint64` | `PanicReason.MemoryOverflow \| rC >> 8` | +| `gas_used` | `uint64` | Gas consumed by the script. | + - Contract with ID `MEM[$rC, 32]` is not in `tx.inputs` +In a script transaction, append an additional receipt to the list of receipts, modifying `tx.receiptsRoot`: + +| name | type | description | +|------------|---------------|-------------------------------------------------------------------------| +| `type` | `ReceiptType` | `ReceiptType.ScriptResult` | +| `result` | `uint64` | `PanicReason.ContractNotInInputs \| rC >> 8` | +| `gas_used` | `uint64` | Gas consumed by the script. | + ### BHEI: Block height | | | From b23f90da51cdf3acbfb554bf858e7d6148004f30 Mon Sep 17 00:00:00 2001 From: wolflo <33909953+wolflo@users.noreply.github.com> Date: Fri, 26 Nov 2021 11:54:40 -0700 Subject: [PATCH 6/6] Formatting: panic reason for BAL opcode --- specs/vm/opcodes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/vm/opcodes.md b/specs/vm/opcodes.md index a5de6344..5206e2aa 100644 --- a/specs/vm/opcodes.md +++ b/specs/vm/opcodes.md @@ -996,7 +996,7 @@ In a script transaction, append an additional receipt to the list of receipts, m | name | type | description | |------------|---------------|-------------------------------------------------------------------------| | `type` | `ReceiptType` | `ReceiptType.ScriptResult` | -| `result` | `uint64` | `PanicReason.MemoryOverflow \| rB >> 8` | +| `result` | `uint64` | ```PanicReason.MemoryOverflow \| rB >> 8``` | | `gas_used` | `uint64` | Gas consumed by the script. | - `$rC + 32` overflows