From 4bf6eafbf4d361f75a23f9567d40fb353f98b557 Mon Sep 17 00:00:00 2001 From: Noa Date: Wed, 11 Mar 2026 13:46:55 -0500 Subject: [PATCH] Tidy up old code from the benchmark --- .../module_bindings/create_account_reducer.ts | 16 --- templates/keynote-2/module_bindings/index.ts | 9 +- .../module_bindings/transfer_reducer.ts | 1 - .../module_bindings/types/reducers.ts | 2 - templates/keynote-2/rust_module/src/lib.rs | 17 +-- templates/keynote-2/spacetimedb/src/index.ts | 31 +++-- templates/keynote-2/src/cli.ts | 2 +- .../keynote-2/src/connectors/spacetimedb.ts | 110 +----------------- 8 files changed, 34 insertions(+), 154 deletions(-) delete mode 100644 templates/keynote-2/module_bindings/create_account_reducer.ts diff --git a/templates/keynote-2/module_bindings/create_account_reducer.ts b/templates/keynote-2/module_bindings/create_account_reducer.ts deleted file mode 100644 index ba21ef86e29..00000000000 --- a/templates/keynote-2/module_bindings/create_account_reducer.ts +++ /dev/null @@ -1,16 +0,0 @@ -// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE -// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. - -/* eslint-disable */ -/* tslint:disable */ -import { - TypeBuilder as __TypeBuilder, - t as __t, - type AlgebraicTypeType as __AlgebraicTypeType, - type Infer as __Infer, -} from "spacetimedb"; - -export default { - id: __t.u32(), - balance: __t.i64(), -}; diff --git a/templates/keynote-2/module_bindings/index.ts b/templates/keynote-2/module_bindings/index.ts index 956920fbe6d..7b8bb865725 100644 --- a/templates/keynote-2/module_bindings/index.ts +++ b/templates/keynote-2/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.0.1 (commit 7923d6f64b8c0f60fe2eb636054494d7d2136076). +// This was generated using spacetimedb cli version 2.0.3 (commit c5743cfc8d2fe70b31f43f275313332524a56476). /* eslint-disable */ /* tslint:disable */ @@ -34,7 +34,6 @@ import { } from "spacetimedb"; // Import all reducer arg schemas -import CreateAccountReducer from "./create_account_reducer"; import SeedReducer from "./seed_reducer"; import TransferReducer from "./transfer_reducer"; @@ -50,6 +49,9 @@ const tablesSchema = __schema({ accounts: __table({ name: 'accounts', indexes: [ + { accessor: 'id', name: 'accounts_id_idx_hash', algorithm: 'btree', columns: [ + 'id', + ] }, ], constraints: [ { name: 'accounts_id_key', constraint: 'unique', columns: ['id'] }, @@ -59,7 +61,6 @@ const tablesSchema = __schema({ /** The schema information for all reducers in this module. This is defined the same way as the reducers would have been defined in the server, except the body of the reducer is omitted in code generation. */ const reducersSchema = __reducers( - __reducerSchema("create_account", CreateAccountReducer), __reducerSchema("seed", SeedReducer), __reducerSchema("transfer", TransferReducer), ); @@ -71,7 +72,7 @@ const proceduresSchema = __procedures( /** The remote SpacetimeDB module schema, both runtime and type information. */ const REMOTE_MODULE = { versionInfo: { - cliVersion: "2.0.1" as const, + cliVersion: "2.0.3" as const, }, tables: tablesSchema.schemaType.tables, reducers: reducersSchema.reducersType.reducers, diff --git a/templates/keynote-2/module_bindings/transfer_reducer.ts b/templates/keynote-2/module_bindings/transfer_reducer.ts index d8db8d6e508..2af9928066c 100644 --- a/templates/keynote-2/module_bindings/transfer_reducer.ts +++ b/templates/keynote-2/module_bindings/transfer_reducer.ts @@ -14,5 +14,4 @@ export default { from: __t.u32(), to: __t.u32(), amount: __t.i64(), - clientTxnId: __t.u64(), }; diff --git a/templates/keynote-2/module_bindings/types/reducers.ts b/templates/keynote-2/module_bindings/types/reducers.ts index a7faba02d4b..af3cd4fda0d 100644 --- a/templates/keynote-2/module_bindings/types/reducers.ts +++ b/templates/keynote-2/module_bindings/types/reducers.ts @@ -6,11 +6,9 @@ import { type Infer as __Infer } from "spacetimedb"; // Import all reducer arg schemas -import CreateAccountReducer from "../create_account_reducer"; import SeedReducer from "../seed_reducer"; import TransferReducer from "../transfer_reducer"; -export type CreateAccountParams = __Infer; export type SeedParams = __Infer; export type TransferParams = __Infer; diff --git a/templates/keynote-2/rust_module/src/lib.rs b/templates/keynote-2/rust_module/src/lib.rs index 19c6ab70d28..a69ca97bbb5 100644 --- a/templates/keynote-2/rust_module/src/lib.rs +++ b/templates/keynote-2/rust_module/src/lib.rs @@ -5,6 +5,7 @@ use spacetimedb::{reducer, ReducerContext, Table}; #[derive(Debug, Clone)] pub struct Accounts { #[primary_key] + #[index(hash)] pub id: u32, pub balance: i64, } @@ -31,21 +32,7 @@ pub fn seed(ctx: &ReducerContext, n: u32, initial_balance: i64) -> Result<(), St } #[reducer] -pub fn create_account(ctx: &ReducerContext, id: u32, balance: i64) -> Result<(), String> { - let accounts = ctx.db.accounts(); - let by_id = accounts.id(); - - if let Some(mut row) = by_id.find(&id) { - row.balance = balance; - by_id.update(row); - } else { - accounts.insert(Accounts { id, balance }); - } - Ok(()) -} - -#[reducer] -pub fn transfer(ctx: &ReducerContext, from: u32, to: u32, amount: i64, _client_txn_id: u64) -> Result<(), String> { +pub fn transfer(ctx: &ReducerContext, from: u32, to: u32, amount: i64) -> Result<(), String> { if from == to { return Err("same_account".into()); } diff --git a/templates/keynote-2/spacetimedb/src/index.ts b/templates/keynote-2/spacetimedb/src/index.ts index 023dfadcd24..a8e0c064999 100644 --- a/templates/keynote-2/spacetimedb/src/index.ts +++ b/templates/keynote-2/spacetimedb/src/index.ts @@ -1,8 +1,8 @@ import { schema, table, t, SenderError } from 'spacetimedb/server'; const spacetimedb = schema({ - account: table( - { name: 'account' }, + accounts: table( + { name: 'accounts', public: true }, { id: t.u32().primaryKey().index('hash'), balance: t.i64(), @@ -12,9 +12,9 @@ const spacetimedb = schema({ export default spacetimedb; export const seed = spacetimedb.reducer( - { n: t.u32(), balance: t.i64() }, - (ctx, { n, balance }) => { - const accounts = ctx.db.account; + { n: t.u32(), initialBalance: t.i64() }, + (ctx, { n, initialBalance: balance }) => { + const accounts = ctx.db.accounts; for (const row of accounts) { accounts.delete(row); @@ -27,15 +27,24 @@ export const seed = spacetimedb.reducer( ); export const transfer = spacetimedb.reducer( - { from: t.u32(), to: t.u32(), amount: t.u32() }, - (ctx, { from, to, amount: amt }) => { - const accounts = ctx.db.account; + { from: t.u32(), to: t.u32(), amount: t.i64() }, + (ctx, { from, to, amount }) => { + if (from === to) { + throw new SenderError('same_account'); + } + if (amount <= 0) { + throw new SenderError('non_positive_amount'); + } + + const accounts = ctx.db.accounts; const byId = accounts.id; - const fromRow = byId.find(from)!; - const toRow = byId.find(to)!; + const fromRow = byId.find(from); + const toRow = byId.find(to); + if (fromRow === null || toRow === null) { + throw new SenderError('account_missing'); + } - const amount = BigInt(amt); if (fromRow.balance < amount) { throw new SenderError('insufficient_funds'); } diff --git a/templates/keynote-2/src/cli.ts b/templates/keynote-2/src/cli.ts index 0ce7ea5721b..a046d23648a 100644 --- a/templates/keynote-2/src/cli.ts +++ b/templates/keynote-2/src/cli.ts @@ -226,7 +226,7 @@ const testDirPath = fileURLToPath(testDirUrl); if (connectors && !connectors.includes(tc.system)) continue; - const makeConnector = (CONNECTORS as any)[tc.system]; + const makeConnector = CONNECTORS[tc.system]; if (!makeConnector) throw new Error(`Unknown connector ${tc.system}`); const connector = makeConnector(); diff --git a/templates/keynote-2/src/connectors/spacetimedb.ts b/templates/keynote-2/src/connectors/spacetimedb.ts index 6837a6d098d..f38ed51288b 100644 --- a/templates/keynote-2/src/connectors/spacetimedb.ts +++ b/templates/keynote-2/src/connectors/spacetimedb.ts @@ -17,15 +17,6 @@ export function spacetimedb( }); } - // --- reducer completion tracking ------------------------ - const transferWaiters = new Map< - bigint, - { resolve: () => void; reject: (e: unknown) => void } - >(); - - let nextTransferId = 1n; - let transferHooked = false; - async function connectWithBindings() { if (!url) throw new Error('STDB_URL not set'); if (!moduleName) throw new Error('STDB_MODULE not set'); @@ -49,53 +40,6 @@ export function spacetimedb( console.log('[stdb] connected'); const conn = ctx; - const reducers = conn.reducers; - - if ( - process.env.USE_SPACETIME_METRICS_ENDPOINT === '0' && - !transferHooked - ) { - transferHooked = true; - console.log('[stdb] hooking onTransfer'); - (reducers as any).onTransfer( - ( - eventCtx: any, - args: { - from: number; - to: number; - amount: bigint; - clientTxnId: bigint; - }, - ) => { - const clientTxnId = args.clientTxnId; - // console.log('[stdb] onTransfer fired', { ...args, status: eventCtx?.event?.status }); - - const waiter = transferWaiters.get(clientTxnId); - if (!waiter) { - console.warn( - '[stdb] no waiter for clientTxnId', - clientTxnId.toString(), - ); - return; - } - - transferWaiters.delete(clientTxnId); - - const status = eventCtx?.event?.status; - - if (status?.tag === 'Committed') { - waiter.resolve(); - } else if (status?.tag === 'Failed') { - waiter.reject(new Error(status?.value ?? 'transfer failed')); - } else if (status?.tag === 'OutOfEnergy') { - waiter.reject(new Error('transfer out of energy')); - } else { - waiter.reject(new Error('unknown transfer status')); - } - }, - ); - } - resolveReady(); if (subscriptions.length > 0) { @@ -147,19 +91,6 @@ export function spacetimedb( }, async close() { - const err = new Error('SpacetimeDB connection closed'); - - // Fail any in-flight transfers - for (const waiter of transferWaiters.values()) { - try { - waiter.reject(err); - } catch { - /* ignore */ - } - } - transferWaiters.clear(); - transferHooked = false; - try { conn.disconnect(); } catch (e) { @@ -184,47 +115,18 @@ export function spacetimedb( switch (fn) { case 'seed': { - conn.reducers.seed({ + return conn.reducers.seed({ n: args.accounts, initialBalance: args.initialBalance, }); - return; - } - - case 'createAccount': { - conn.reducers.createAccount({ id: args.id, balance: args.balance }); - return; } case 'transfer': { - const clientTxnId = nextTransferId++; - - if (process.env.USE_SPACETIME_METRICS_ENDPOINT === '0') { - return new Promise((resolve, reject) => { - const waiter = { resolve, reject }; - transferWaiters.set(clientTxnId, waiter); - - try { - conn.reducers.transfer({ - from: args.from, - to: args.to, - amount: args.amount, - clientTxnId, - }); - } catch (err) { - console.log(`ERROR ${err}`); - transferWaiters.delete(clientTxnId); - reject(err); - } - }); - } else { - return conn.reducers.transfer({ - from: args.from, - to: args.to, - amount: args.amount, - clientTxnId, - }); - } + return conn.reducers.transfer({ + from: args.from, + to: args.to, + amount: args.amount, + }); } default: