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

feat(cu): check that Module-Format is emscripten as current only supported format #213 #221

Merged
merged 1 commit into from
Dec 12, 2023
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
36 changes: 27 additions & 9 deletions servers/cu/src/domain/lib/loadModule.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fromPromise, of } from 'hyper-async'
import { fromPromise, of, Resolved, Rejected } from 'hyper-async'
import { mergeRight, prop } from 'ramda'
import { z } from 'zod'

import { loadTransactionDataSchema } from '../dal.js'
import { parseTags } from '../utils.js'
import { loadTransactionDataSchema, loadTransactionMetaSchema } from '../dal.js'
import { eqOrIncludes, parseTags } from '../utils.js'

/**
* The result that is produced from this step
Expand All @@ -21,16 +21,34 @@ const ctxSchema = z.object({
})
}).passthrough()

function getModuleBufferWith ({ loadTransactionData }) {
loadTransactionData = fromPromise(loadTransactionDataSchema.implement(loadTransactionData))
function getModuleWith ({ loadTransactionData, loadTransactionMeta }) {
loadTransactionData = loadTransactionDataSchema.implement(loadTransactionData)
loadTransactionMeta = loadTransactionMetaSchema.implement(loadTransactionMeta)

const checkTag = (name, pred, err) => tags => pred(tags[name])
? Resolved(tags)
: Rejected(`Tag '${name}': ${err}`)

return (tags) => {
return of(tags)
.map(parseTags)
.map(prop('Module'))
.chain(moduleId =>
.chain((moduleId) =>
of(moduleId)
.chain(loadTransactionData)
.chain(fromPromise((moduleId) => Promise.all([
loadTransactionData(moduleId),
loadTransactionMeta(moduleId)
])))
/**
* This CU currently only implements evaluation for emscripten Module-Format,
* so we check that the Module is the correct Module-Format, and reject if not
*/
.chain(([data, meta]) =>
of(meta.tags)
.map(parseTags)
.chain(checkTag('Module-Format', eqOrIncludes('emscripten'), 'only \'emscripten\' module format is supported by this CU'))
.map(() => data)
)
.chain(fromPromise((res) => res.arrayBuffer()))
.map(module => ({ module, moduleId }))
)
Expand All @@ -56,11 +74,11 @@ export function loadModuleWith (env) {
const logger = env.logger.child('loadModule')
env = { ...env, logger }

const getModuleBuffer = getModuleBufferWith(env)
const getModule = getModuleWith(env)

return (ctx) => {
return of(ctx.tags)
.chain(getModuleBuffer)
.chain(getModule)
.map(mergeRight(ctx))
.map(ctxSchema.parse)
.map(ctx => {
Expand Down
32 changes: 32 additions & 0 deletions servers/cu/src/domain/lib/loadModule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ describe('loadModule', () => {
const loadModule = loadModuleWith({
loadTransactionData: async (_id) =>
new Response(JSON.stringify({ hello: 'world' })),
loadTransactionMeta: async () => ({
owner: {
address: 'owner-123'
},
tags: [
{ name: 'Module-Format', value: 'emscripten' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Type', value: 'Module' }
]
}),
logger
})

Expand All @@ -20,4 +30,26 @@ describe('loadModule', () => {
assert.equal(result.moduleId, 'foobar')
assert.equal(result.id, PROCESS)
})

test('throw if "Module-Format" is not emscripten', async () => {
const loadModule = loadModuleWith({
loadTransactionData: async (_id) =>
new Response(JSON.stringify({ hello: 'world' })),
loadTransactionMeta: async () => ({
owner: {
address: 'owner-123'
},
tags: [
{ name: 'Module-Format', value: 'wasi32' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Type', value: 'Module' }
]
}),
logger
})

await loadModule({ id: PROCESS, tags: [{ name: 'Module', value: 'foobar' }] }).toPromise()
.then(() => assert.fail('unreachable. Should have thrown'))
.catch(err => assert.equal(err, "Tag 'Module-Format': only 'emscripten' module format is supported by this CU"))
})
})