Skip to content

Commit 3d8d024

Browse files
committed
TLV encoding definition in extension section
1 parent a040c07 commit 3d8d024

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

03-Protocol-Overview.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,53 @@ This prevents the needlessly wasted bandwidth and potentially serious performanc
131131

132132
See `ChannelEndpointChanged` message in Common Protocol Messages for details about how extensions interact with dynamic channel reconfiguration in proxies.
133133

134+
## 3.4.1 Stratum V2 TLV Encoding Model
135+
136+
To ensure a consistent and extensible way of adding optional fields to existing messages, **Stratum V2 supports Type-Length-Value (TLV) encoding** for protocol extensions. This model allows for structured, backward-compatible extensions while ensuring that unknown fields can be safely ignored.
137+
138+
### TLV Structure
139+
140+
Each TLV-encoded field follows this format:
141+
142+
| **Field** | **Size** | **Description** |
143+
|------------|---------|----------------|
144+
| **Type** | 3 bytes (U16 + U8) | Identifies the TLV field. The first 2 bytes represent the `extension_type`, and the third byte represents the `field_type` within the extension context. |
145+
| **Length** | 2 bytes (U16) | Indicates the size (in bytes) of the Value field. |
146+
| **Value** | `N` bytes | The actual data of the extension field, of variable length. |
147+
148+
- The **Type** field consists of **3 bytes**, where:
149+
- The **first 2 bytes** (`U16`) correspond to the negotiated `extension_type`, ensuring modular and self-contained extensions.
150+
- The **third byte** (`U8`) specifies the `field_type` defined in the extension, allowing multiple fields to be added within the same message.
151+
- The **Length** field defines the exact size of the **Value**, allowing efficient message parsing.
152+
- If the **Length** is `0x0000`, the **Value** field is omitted.
153+
- When multiple fields extend the same message type, their order **MUST** match the order defined in the extension.
154+
155+
### Usage Guidelines
156+
157+
- **TLV fields MUST be placed at the end of the message payload.** This ensures compatibility with existing Stratum V2 messages.
158+
- **TLV fields MUST be ordered by `extension_type`.** Since all extensions are negotiated beforehand, the recipient MUST process TLV fields in order of `extension_type` and use their `Type` identifiers to correctly interpret them.
159+
- **Order of TLV fields within the same extension MUST be respected.** If an extension defines multiple TLV fields to extend a single message, they **MUST** appear in the exact order specified by the extension’s documentation.
160+
- **Length constraints MUST be respected.** Each extension must specify the valid length range for its TLV fields. If a TLV field exceeds the maximum length allowed by its specification, the recipient MUST reject the message.
161+
162+
### Example: Extending `SubmitSharesExtended`
163+
164+
If extension **0x0002** (Worker-Specific Hashrate Tracking) is negotiated, clients must append the following TLV field to `SubmitSharesExtended`:
165+
```
166+
[TYPE: 0x0002] [LENGTH: 0x000A] [VALUE: "Worker_001"]
167+
```
168+
Encoded as:
169+
```
170+
00 02 00 0A 57 6F 72 6B 65 72 5F 30 30 31
171+
```
172+
173+
Where:
174+
- `00 02` → TLV Type (Worker Identity)
175+
- `00 0A` → Length = 10 bytes
176+
- `57 6F 72 6B 65 72 5F 30 30 31``"Worker_001"` (UTF-8 encoded)
177+
178+
A device processing `SubmitSharesExtended` **MUST scan for TLV fields** matching any negotiated extensions, allowing for future extensibility without breaking compatibility.
179+
180+
134181
## 3.5 Error Codes
135182

136183
The protocol uses string error codes.

0 commit comments

Comments
 (0)