Skip to content

Commit

Permalink
New ControlMessage format
Browse files Browse the repository at this point in the history
  • Loading branch information
xash committed Nov 14, 2024
1 parent 94eed96 commit 1be94ee
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 205 deletions.
2 changes: 2 additions & 0 deletions src/wgps/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const msgLogicalChannels: Record<MsgKind, LogicalChannel | null> = {
[MsgKind.ControlApologise]: null,
[MsgKind.ControlFree]: null,
[MsgKind.ControlPlead]: null,
[MsgKind.ControlLimitSending]: null,
[MsgKind.ControlLimitReceiving]: null,
[MsgKind.ControlIssueGuarantee]: null,
[MsgKind.PaiRequestSubspaceCapability]: null,
[MsgKind.PaiReplySubspaceCapability]: null,
Expand Down
68 changes: 48 additions & 20 deletions src/wgps/decoding/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
type MsgControlFree,
type MsgControlIssueGuarantee,
type MsgControlPlead,
type MsgControlLimitSending,
type MsgControlLimitReceiving,
MsgKind,
} from "../types.ts";
import { compactWidthFromEndOfByte } from "./util.ts";
Expand Down Expand Up @@ -65,15 +67,9 @@ export function decodeHandleTypeFromBeginningOfByte(byte: number): HandleType {
export async function decodeControlIssueGuarantee(
bytes: GrowingBytes,
): Promise<MsgControlIssueGuarantee> {
await bytes.nextAbsolute(1);

// We know we have a byte.
const compactWidth = compactWidthFromEndOfByte(bytes.array[0]);

// Wait for another byte and decode the channel from it
await bytes.nextAbsolute(2);

const channel = decodeChannelFromBeginningOfByte(bytes.array[1]);
const compactWidth = compactWidthFromEndOfByte(bytes.array[1]);
const channel = decodeChannelFromEndOfByte(bytes.array[1] >> 3);

// Wait for the number of bytes compact width told us to expect.

Expand All @@ -93,13 +89,9 @@ export async function decodeControlIssueGuarantee(
export async function decodeControlAbsolve(
bytes: GrowingBytes,
): Promise<MsgControlAbsolve> {
await bytes.nextAbsolute(1);

const compactWidth = compactWidthFromEndOfByte(bytes.array[0]);

await bytes.nextAbsolute(2);

const channel = decodeChannelFromBeginningOfByte(bytes.array[1]);
const compactWidth = compactWidthFromEndOfByte(bytes.array[1]);
const channel = decodeChannelFromEndOfByte(bytes.array[1] >> 3);

await bytes.nextAbsolute(2 + compactWidth);

Expand All @@ -117,13 +109,9 @@ export async function decodeControlAbsolve(
export async function decodeControlPlead(
bytes: GrowingBytes,
): Promise<MsgControlPlead> {
await bytes.nextAbsolute(1);

const compactWidth = compactWidthFromEndOfByte(bytes.array[0]);

await bytes.nextAbsolute(2);

const channel = decodeChannelFromBeginningOfByte(bytes.array[1]);
const compactWidth = compactWidthFromEndOfByte(bytes.array[1]);
const channel = decodeChannelFromEndOfByte(bytes.array[1] >> 3);

await bytes.nextAbsolute(2 + compactWidth);

Expand All @@ -138,6 +126,46 @@ export async function decodeControlPlead(
};
}

export async function decodeControlLimitSending(
bytes: GrowingBytes,
): Promise<MsgControlLimitSending> {
await bytes.nextAbsolute(2);
const compactWidth = compactWidthFromEndOfByte(bytes.array[1]);
const channel = decodeChannelFromEndOfByte(bytes.array[1] >> 3);

await bytes.nextAbsolute(2 + compactWidth);

const bound = decodeCompactWidth(bytes.array.subarray(2, 2 + compactWidth));

bytes.prune(2 + compactWidth);

return {
kind: MsgKind.ControlLimitSending,
channel,
bound: BigInt(bound),
};
}

export async function decodeControlLimitReceiving(
bytes: GrowingBytes,
): Promise<MsgControlLimitReceiving> {
await bytes.nextAbsolute(2);
const compactWidth = compactWidthFromEndOfByte(bytes.array[1]);
const channel = decodeChannelFromEndOfByte(bytes.array[1] >> 3);

await bytes.nextAbsolute(2 + compactWidth);

const bound = decodeCompactWidth(bytes.array.subarray(2, 2 + compactWidth));

bytes.prune(2 + compactWidth);

return {
kind: MsgKind.ControlLimitReceiving,
channel,
bound: BigInt(bound),
};
}

export async function decodeControlAnnounceDropping(
bytes: GrowingBytes,
): Promise<MsgControlAnnounceDropping> {
Expand Down
Loading

0 comments on commit 1be94ee

Please sign in to comment.