Skip to content

Commit

Permalink
feat(cu): add flag to disable/enable wasm metering and apply #1040
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Oct 15, 2024
1 parent d912fae commit c3f24c1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions servers/cu/src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export const createApis = async (ctx) => {
ctx.logger('Ignoring Arweave Checkpoints for processes [ %s ]', ctx.PROCESS_IGNORE_ARWEAVE_CHECKPOINTS.join(', '))
ctx.logger('Ignoring Arweave Checkpoints [ %s ]', ctx.IGNORE_ARWEAVE_CHECKPOINTS.join(', '))
ctx.logger('Trusting Arweave Checkpoint Owners [ %s ]', ctx.PROCESS_CHECKPOINT_TRUSTED_OWNERS.join(', '))
ctx.logger('Process metering applied set to "%s"', ctx.PROCESS_WASM_APPLY_METERING)
ctx.logger('Allowing only process owners [ %s ]', ctx.ALLOW_OWNERS.join(', '))
ctx.logger('Restricting processes [ %s ]', ctx.RESTRICT_PROCESSES.join(', '))
ctx.logger('Allowing only processes [ %s ]', ctx.ALLOW_PROCESSES.join(', '))
Expand Down Expand Up @@ -241,6 +242,7 @@ export const createApis = async (ctx) => {
fetch: ctx.fetch,
ARWEAVE_URL: ctx.ARWEAVE_URL,
WASM_BINARY_FILE_DIRECTORY: ctx.WASM_BINARY_FILE_DIRECTORY,
PROCESS_WASM_APPLY_METERING: ctx.PROCESS_WASM_APPLY_METERING,
logger: ctx.logger,
cache: WasmClient.createWasmModuleCache({ MAX_SIZE: ctx.WASM_MODULE_CACHE_MAX_SIZE })
})
Expand Down
2 changes: 2 additions & 0 deletions servers/cu/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const CONFIG_ENVS = {
PROCESS_WASM_COMPUTE_MAX_LIMIT: process.env.PROCESS_WASM_COMPUTE_MAX_LIMIT || 9_000_000_000_000, // 9t
PROCESS_WASM_SUPPORTED_FORMATS: process.env.PROCESS_WASM_SUPPORTED_FORMATS || DEFAULT_PROCESS_WASM_MODULE_FORMATS,
PROCESS_WASM_SUPPORTED_EXTENSIONS: process.env.PROCESS_WASM_SUPPORTED_EXTENSIONS || [],
PROCESS_WASM_APPLY_METERING: process.env.PROCESS_WASM_APPLY_METERING !== 'false',
WASM_EVALUATION_MAX_WORKERS: process.env.WASM_EVALUATION_MAX_WORKERS || Math.max(cpus().length - 1, 1),
WASM_EVALUATION_PRIMARY_WORKERS_PERCENTAGE: process.env.WASM_EVALUATION_PRIMARY_WORKERS_PERCENTAGE || 70, // 70% of worker threads allocated to primary workloads
WASM_EVALUATION_WORKERS_DRY_RUN_MAX_QUEUE: process.env.WASM_EVALUATION_WORKERS_DRY_RUN_MAX_QUEUE || 3000,
Expand Down Expand Up @@ -171,6 +172,7 @@ const CONFIG_ENVS = {
PROCESS_WASM_COMPUTE_MAX_LIMIT: process.env.PROCESS_WASM_COMPUTE_MAX_LIMIT || 9_000_000_000_000, // 9t
PROCESS_WASM_SUPPORTED_FORMATS: process.env.PROCESS_WASM_SUPPORTED_FORMATS || DEFAULT_PROCESS_WASM_MODULE_FORMATS,
PROCESS_WASM_SUPPORTED_EXTENSIONS: process.env.PROCESS_WASM_SUPPORTED_EXTENSIONS || [],
PROCESS_WASM_APPLY_METERING: process.env.PROCESS_WASM_APPLY_METERING !== 'false',
WASM_EVALUATION_MAX_WORKERS: process.env.WASM_EVALUATION_MAX_WORKERS || Math.max(cpus().length - 1, 1),
WASM_EVALUATION_PRIMARY_WORKERS_PERCENTAGE: process.env.WASM_EVALUATION_PRIMARY_WORKERS_PERCENTAGE || 70, // 70% of worker threads allocated to primary workloads
WASM_EVALUATION_WORKERS_DRY_RUN_MAX_QUEUE: process.env.WASM_EVALUATION_WORKERS_DRY_RUN_MAX_QUEUE || 3000,
Expand Down
4 changes: 4 additions & 0 deletions servers/cu/src/domain/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const domainConfigSchema = z.object({
* The wasm extensions that this CU supports
*/
PROCESS_WASM_SUPPORTED_EXTENSIONS: commaDelimitedArraySchema,
/**
* Whether or not to apply metering to the wasm execution
*/
PROCESS_WASM_APPLY_METERING: z.preprocess((val) => !!val, z.boolean()),
/**
* The url for the graphql server to be used by the CU
* to query for metadata from an Arweave Gateway
Expand Down
11 changes: 7 additions & 4 deletions servers/cu/src/effects/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ export function bootstrapWasmInstanceWith () {
}
}

export function loadWasmModuleWith ({ fetch, ARWEAVE_URL, WASM_BINARY_FILE_DIRECTORY, logger, cache }) {
export function loadWasmModuleWith ({ fetch, ARWEAVE_URL, WASM_BINARY_FILE_DIRECTORY, PROCESS_WASM_APPLY_METERING, logger, cache }) {
const streamTransactionData = fromPromise(streamTransactionDataWith({ fetch, ARWEAVE_URL, logger }))
const readWasmFile = fromPromise(readWasmFileWith({ DIR: WASM_BINARY_FILE_DIRECTORY }))
const writeWasmFile = writeWasmFileWith({ DIR: WASM_BINARY_FILE_DIRECTORY })

const toWasmResponse = (moduleOptions) => fromPromise((stream) => WebAssembly.compileStreaming(wasmResponse(Readable.toWeb(stream), moduleOptions)))
const compileStreaming = (stream, moduleOptions) => WebAssembly.compileStreaming(
wasmResponse(stream),
{ ...moduleOptions, applyMetering: PROCESS_WASM_APPLY_METERING }
)

function maybeCachedModule (args) {
const { moduleId } = args
Expand All @@ -152,7 +155,7 @@ export function loadWasmModuleWith ({ fetch, ARWEAVE_URL, WASM_BINARY_FILE_DIREC

return of(moduleId)
.chain(readWasmFile)
.chain(toWasmResponse(moduleOptions))
.chain(fromPromise((nStream) => compileStreaming(Readable.toWeb(nStream), moduleOptions)))
.bimap(always(args), identity)
}

Expand All @@ -169,7 +172,7 @@ export function loadWasmModuleWith ({ fetch, ARWEAVE_URL, WASM_BINARY_FILE_DIREC
.chain(fromPromise(([s1, s2]) =>
Promise.all([
writeWasmFile(moduleId, Readable.fromWeb(s1)),
WebAssembly.compileStreaming(wasmResponse(s2), moduleOptions)
compileStreaming(s2, moduleOptions)
])
))
.map(([, res]) => res)
Expand Down

0 comments on commit c3f24c1

Please sign in to comment.