Skip to content

Commit 6a62407

Browse files
Service Protocol V6 + random seed (#593)
1 parent 14cf983 commit 6a62407

File tree

14 files changed

+53
-100
lines changed

14 files changed

+53
-100
lines changed

packages/restate-sdk-cloudflare-workers/patches/vm/sdk_shared_core_wasm_bindings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class WasmInput {
8686
readonly key: string;
8787
readonly headers: WasmHeader[];
8888
readonly input: Uint8Array;
89+
readonly random_seed: bigint;
8990
}
9091
export class WasmResponseHead {
9192
private constructor();

packages/restate-sdk-cloudflare-workers/patches/vm/sdk_shared_core_wasm_bindings_bg.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ export class WasmInput {
484484
const ret = wasm.__wbg_get_wasminput_input(this.__wbg_ptr);
485485
return ret;
486486
}
487+
/**
488+
* @returns {bigint}
489+
*/
490+
get random_seed() {
491+
const ret = wasm.__wbg_get_wasminput_random_seed(this.__wbg_ptr);
492+
return BigInt.asUintN(64, ret);
493+
}
487494
}
488495

489496
const WasmResponseHeadFinalization = (typeof FinalizationRegistry === 'undefined')
-2.48 KB
Binary file not shown.

packages/restate-sdk-cloudflare-workers/patches/vm/sdk_shared_core_wasm_bindings_bg.wasm.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export const __wbg_wasmresponsehead_free: (a: number, b: number) => void;
1111
export const __wbg_get_wasmresponsehead_status_code: (a: number) => number;
1212
export const __wbg_get_wasmresponsehead_headers: (a: number) => [number, number];
1313
export const __wbg_wasminput_free: (a: number, b: number) => void;
14+
export const __wbg_get_wasminput_key: (a: number) => [number, number];
1415
export const __wbg_get_wasminput_headers: (a: number) => [number, number];
1516
export const __wbg_get_wasminput_input: (a: number) => any;
17+
export const __wbg_get_wasminput_random_seed: (a: number) => bigint;
1618
export const __wbg_wasmvm_free: (a: number, b: number) => void;
1719
export const wasmvm_new: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
1820
export const wasmvm_get_response_head: (a: number) => number;
@@ -61,7 +63,6 @@ export const wasmidentityverifier_new: (a: number, b: number) => [number, number
6163
export const wasmidentityverifier_verify_identity: (a: number, b: number, c: number, d: number, e: number) => [number, number];
6264
export const cancel_handle: () => number;
6365
export const __wbg_get_wasminput_invocation_id: (a: number) => [number, number];
64-
export const __wbg_get_wasminput_key: (a: number) => [number, number];
6566
export const ring_core_0_17_11__bn_mul_mont: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
6667
export const __wbindgen_malloc: (a: number, b: number) => number;
6768
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;

packages/restate-sdk/src/context_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
110110
readonly journalValueCodec: JournalValueCodec,
111111
private readonly asTerminalError?: (error: any) => TerminalError | undefined
112112
) {
113-
this.rand = new RandImpl(input.invocation_id, () => {
113+
this.rand = new RandImpl(input.random_seed, () => {
114114
// TODO reimplement this check with async context
115115
// if (coreVm.is_inside_run()) {
116116
// throw new Error(

packages/restate-sdk/src/endpoint/endpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function computeDiscovery(
210210
): discovery.Endpoint {
211211
return {
212212
minProtocolVersion: 5,
213-
maxProtocolVersion: 5,
213+
maxProtocolVersion: 6,
214214
services: [...components.values()].map((c) => c.discovery()),
215215
};
216216
}

packages/restate-sdk/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class WasmInput {
9797
readonly key: string;
9898
readonly headers: WasmHeader[];
9999
readonly input: Uint8Array;
100+
readonly random_seed: bigint;
100101
}
101102
export class WasmResponseHead {
102103
private constructor();

packages/restate-sdk/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/restate-sdk/src/utils/buffer.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

packages/restate-sdk/src/utils/rand.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,29 @@
1313
//! License MIT
1414

1515
import type { Rand } from "../context.js";
16-
import { createHash } from "node:crypto";
1716
import { Buffer } from "node:buffer";
18-
import { readBigUInt64LE } from "./buffer.js";
1917

2018
export class RandImpl implements Rand {
2119
private randstate256: [bigint, bigint, bigint, bigint];
2220

2321
constructor(
24-
id: string | [bigint, bigint, bigint, bigint],
22+
id: bigint | [bigint, bigint, bigint, bigint],
2523
private readonly checkState: (state: string) => void = () => undefined
2624
) {
27-
if (typeof id === "string") {
28-
// hash the invocation ID, which is known to contain 74 bits of entropy
29-
const hash = createHash("sha256").update(id).digest();
30-
31-
this.randstate256 = [
32-
readBigUInt64LE(hash, 0),
33-
readBigUInt64LE(hash, 8),
34-
readBigUInt64LE(hash, 16),
35-
readBigUInt64LE(hash, 24),
36-
];
25+
if (typeof id === "bigint") {
26+
// Seed SplitMix64 with the provided 64-bit seed and expand to 256 bits
27+
let x = id & RandImpl.U64_MASK;
28+
const next = (): bigint => {
29+
x = (x + 0x9e3779b97f4a7c15n) & RandImpl.U64_MASK;
30+
let z = x;
31+
z ^= z >> 30n;
32+
z = (z * 0xbf58476d1ce4e5b9n) & RandImpl.U64_MASK;
33+
z ^= z >> 27n;
34+
z = (z * 0x94d049bb133111ebn) & RandImpl.U64_MASK;
35+
z ^= z >> 31n;
36+
return z & RandImpl.U64_MASK;
37+
};
38+
this.randstate256 = [next(), next(), next(), next()];
3739
} else {
3840
this.randstate256 = id;
3941
}

0 commit comments

Comments
 (0)