Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rpc server cleanup & misc fixes #11145

Merged
merged 7 commits into from
Jan 11, 2025
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 @@ -13,13 +13,6 @@ To write a contract:

```rust
#include_code declaration /noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr raw

// Imports

// Storage

// Functions
}
```

2. Define imports in your contract block
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/cli/aztec_start_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function aztecStart(options: any, userLog: LogFn, debugLogger: Logg
installSignalHandlers(debugLogger.info, signalHandlers);

if (Object.entries(services).length > 0) {
const rpcServer = createNamespacedSafeJsonRpcServer(services, false, debugLogger);
const rpcServer = createNamespacedSafeJsonRpcServer(services, { http200OnError: false, log: debugLogger });
const { port } = await startHttpRpcServer(rpcServer, { port: options.port });
debugLogger.info(`Aztec Server listening on port ${port}`);
}
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/bot/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { type BotRunner } from './runner.js';
* @returns An JSON-RPC HTTP server
*/
export function createBotRunnerRpcServer(botRunner: BotRunner) {
createSafeJsonRpcServer(botRunner, BotRunnerApiSchema, false, botRunner.isHealthy.bind(botRunner));
createSafeJsonRpcServer(botRunner, BotRunnerApiSchema, {
http200OnError: false,
healthCheck: botRunner.isHealthy.bind(botRunner),
});
}

export function getBotRunnerApiHandler(botRunner: BotRunner): ApiHandler {
Expand Down
37 changes: 22 additions & 15 deletions yarn-project/foundation/src/json-rpc/server/safe_json_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class SafeJsonRpcServer {
const message = err.issues.map(e => `${e.message} (${e.path.join('.')})`).join('. ') || 'Validation error';
ctx.status = 400;
ctx.body = { jsonrpc: '2.0', id: null, error: { code: -32701, message } };
} else if (this.http200OnError) {
ctx.body = {
jsonrpc: '2.0',
id: null,
error: { code: err.code || -32600, data: err.data, message: err.message },
};
} else {
ctx.status = 500;
ctx.body = { jsonrpc: '2.0', id: null, error: { code: -32600, message: err.message ?? 'Internal error' } };
Expand Down Expand Up @@ -111,16 +117,8 @@ export class SafeJsonRpcServer {
ctx.body = { jsonrpc, id, error: { code: -32601, message: `Method not found: ${method}` } };
} else {
ctx.status = 200;
try {
const result = await this.proxy.call(method, params);
ctx.body = { jsonrpc, id, result };
} catch (err: any) {
if (this.http200OnError) {
ctx.body = { jsonrpc, id, error: { code: err.code || -32600, data: err.data, message: err.message } };
} else {
throw err;
}
}
const result = await this.proxy.call(method, params);
ctx.body = { jsonrpc, id, result };
}
});

Expand Down Expand Up @@ -265,16 +263,25 @@ function makeAggregateHealthcheck(namedHandlers: NamespacedApiHandlers, log?: Lo
};
}

type SafeJsonRpcServerOptions = {
http200OnError: boolean;
healthCheck?: StatusCheckFn;
log?: Logger;
};

/**
* Creates a single SafeJsonRpcServer from multiple handlers.
* @param servers - List of handlers to be combined.
* @returns A single JsonRpcServer with namespaced methods.
*/
export function createNamespacedSafeJsonRpcServer(
handlers: NamespacedApiHandlers,
http200OnError = false,
log = createLogger('json-rpc:server'),
options: Omit<SafeJsonRpcServerOptions, 'healthcheck'> = {
http200OnError: false,
log: createLogger('json-rpc:server'),
},
): SafeJsonRpcServer {
const { http200OnError, log } = options;
const proxy = new NamespacedSafeJsonProxy(handlers);
const healthCheck = makeAggregateHealthcheck(handlers, log);
return new SafeJsonRpcServer(proxy, http200OnError, healthCheck, log);
Expand All @@ -283,11 +290,11 @@ export function createNamespacedSafeJsonRpcServer(
export function createSafeJsonRpcServer<T extends object = any>(
handler: T,
schema: ApiSchemaFor<T>,
http200OnError = false,
healthCheck?: StatusCheckFn,
options: SafeJsonRpcServerOptions = { http200OnError: false },
) {
const { http200OnError, log, healthCheck } = options;
const proxy = new SafeJsonProxy(handler, schema);
return new SafeJsonRpcServer(proxy, http200OnError, healthCheck);
return new SafeJsonRpcServer(proxy, http200OnError, healthCheck, log);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/txe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ const TXEDispatcherApiSchema: ApiSchemaFor<TXEDispatcher> = {
* @returns A TXE RPC server.
*/
export function createTXERpcServer(logger: Logger) {
return createSafeJsonRpcServer(new TXEDispatcher(logger), TXEDispatcherApiSchema, true);
return createSafeJsonRpcServer(new TXEDispatcher(logger), TXEDispatcherApiSchema, { http200OnError: true });
}
Loading