Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class WasmInput {
readonly key: string;
readonly headers: WasmHeader[];
readonly input: Uint8Array;
readonly random_seed: bigint;
}
export class WasmResponseHead {
private constructor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ export class WasmInput {
const ret = wasm.__wbg_get_wasminput_input(this.__wbg_ptr);
return ret;
}
/**
* @returns {bigint}
*/
get random_seed() {
const ret = wasm.__wbg_get_wasminput_random_seed(this.__wbg_ptr);
return BigInt.asUintN(64, ret);
}
}

const WasmResponseHeadFinalization = (typeof FinalizationRegistry === 'undefined')
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const __wbg_wasmresponsehead_free: (a: number, b: number) => void;
export const __wbg_get_wasmresponsehead_status_code: (a: number) => number;
export const __wbg_get_wasmresponsehead_headers: (a: number) => [number, number];
export const __wbg_wasminput_free: (a: number, b: number) => void;
export const __wbg_get_wasminput_key: (a: number) => [number, number];
export const __wbg_get_wasminput_headers: (a: number) => [number, number];
export const __wbg_get_wasminput_input: (a: number) => any;
export const __wbg_get_wasminput_random_seed: (a: number) => bigint;
export const __wbg_wasmvm_free: (a: number, b: number) => void;
export const wasmvm_new: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
export const wasmvm_get_response_head: (a: number) => number;
Expand Down Expand Up @@ -61,7 +63,6 @@ export const wasmidentityverifier_new: (a: number, b: number) => [number, number
export const wasmidentityverifier_verify_identity: (a: number, b: number, c: number, d: number, e: number) => [number, number];
export const cancel_handle: () => number;
export const __wbg_get_wasminput_invocation_id: (a: number) => [number, number];
export const __wbg_get_wasminput_key: (a: number) => [number, number];
export const ring_core_0_17_11__bn_mul_mont: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
Expand Down
2 changes: 1 addition & 1 deletion packages/restate-sdk/src/context_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
readonly journalValueCodec: JournalValueCodec,
private readonly asTerminalError?: (error: any) => TerminalError | undefined
) {
this.rand = new RandImpl(input.invocation_id, () => {
this.rand = new RandImpl(input.random_seed, () => {
// TODO reimplement this check with async context
// if (coreVm.is_inside_run()) {
// throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion packages/restate-sdk/src/endpoint/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function computeDiscovery(
): discovery.Endpoint {
return {
minProtocolVersion: 5,
maxProtocolVersion: 5,
maxProtocolVersion: 6,
services: [...components.values()].map((c) => c.discovery()),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class WasmInput {
readonly key: string;
readonly headers: WasmHeader[];
readonly input: Uint8Array;
readonly random_seed: bigint;
}
export class WasmResponseHead {
private constructor();
Expand Down

Large diffs are not rendered by default.

66 changes: 0 additions & 66 deletions packages/restate-sdk/src/utils/buffer.ts

This file was deleted.

28 changes: 15 additions & 13 deletions packages/restate-sdk/src/utils/rand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@
//! License MIT

import type { Rand } from "../context.js";
import { createHash } from "node:crypto";
import { Buffer } from "node:buffer";
import { readBigUInt64LE } from "./buffer.js";

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

constructor(
id: string | [bigint, bigint, bigint, bigint],
id: bigint | [bigint, bigint, bigint, bigint],
private readonly checkState: (state: string) => void = () => undefined
) {
if (typeof id === "string") {
// hash the invocation ID, which is known to contain 74 bits of entropy
const hash = createHash("sha256").update(id).digest();

this.randstate256 = [
readBigUInt64LE(hash, 0),
readBigUInt64LE(hash, 8),
readBigUInt64LE(hash, 16),
readBigUInt64LE(hash, 24),
];
if (typeof id === "bigint") {
// Seed SplitMix64 with the provided 64-bit seed and expand to 256 bits
let x = id & RandImpl.U64_MASK;
const next = (): bigint => {
x = (x + 0x9e3779b97f4a7c15n) & RandImpl.U64_MASK;
let z = x;
z ^= z >> 30n;
z = (z * 0xbf58476d1ce4e5b9n) & RandImpl.U64_MASK;
z ^= z >> 27n;
z = (z * 0x94d049bb133111ebn) & RandImpl.U64_MASK;
z ^= z >> 31n;
return z & RandImpl.U64_MASK;
};
this.randstate256 = [next(), next(), next(), next()];
} else {
this.randstate256 = id;
}
Expand Down
26 changes: 12 additions & 14 deletions packages/restate-sdk/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ import { RandImpl } from "../src/utils/rand.js";
import { describe, expect, it } from "vitest";

describe("rand", () => {
it("correctly hashes invocation ids", () => {
const rand = new RandImpl(
"f311f1fdcb9863f0018bd3400ecd7d69b547204e776218b2"
);
it("accepts seed", () => {
const rand = new RandImpl(11111111111111111n);

const actual: bigint[] = Array.from(Array(10)).map(() => rand.u64());

// These values were produced with the reference implementation:
// http://xoshiro.di.unimi.it/xoshiro256plusplus.c
const expected = [
6221017497105640564n,
6390535423083911304n,
6450107027926477268n,
6892944322147831477n,
11902315545316364308n,
11803614765068293030n,
3688900223715244673n,
16180860676245615018n,
14289837324736329951n,
5956009879523072622n,
14993938765686767826n,
8187951856952496553n,
10221716037851679518n,
5009267449921186858n,
2939623319151797546n,
6400458020329377757n,
10216112085426431330n,
17690642664638642265n,
13201164171824029981n,
18315010504635656422n,
];

expect(actual).toStrictEqual(expected);
Expand Down
3 changes: 1 addition & 2 deletions sdk-shared-core-wasm-bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk-shared-core-wasm-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.100"
restate-sdk-shared-core = { version = "0.5.1", features = ["request_identity"] }
restate-sdk-shared-core = { git = "https://github.com/restatedev/sdk-shared-core.git", rev = "c09e27010c251fa7447049668128934184f42803", features = ["request_identity"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
serde = { version = "1.0.210", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
Expand Down
3 changes: 3 additions & 0 deletions sdk-shared-core-wasm-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ pub struct WasmInput {
pub headers: Vec<WasmHeader>,
#[wasm_bindgen(readonly)]
pub input: Uint8Array,
#[wasm_bindgen(readonly)]
pub random_seed: u64,
}

impl From<Input> for WasmInput {
Expand All @@ -337,6 +339,7 @@ impl From<Input> for WasmInput {
key: value.key,
headers: value.headers.into_iter().map(Into::into).collect(),
input: (&*value.input).into(),
random_seed: value.random_seed,
}
}
}
Expand Down